Repository: helix-editor/helix
File: helix-term/src/handlers/document_colors.rs (Rust)

Code before:
            (color.blue * 255.) as u8,
        ));
    }

    let swatches = DocumentColorSwatches {
        color_swatches,
        colors,
        color_swatches_padding,
    };

    doc.set_color_swatches(view_id, swatches)
}

pub(super) fn register_hooks(handlers: &Handlers) {
    register_hook!(move |event: &mut DocumentDidOpen<'_>| {
        // when a document is initially opened, request colors for it
        let views: Vec<_> = event
            .editor
            .tree
            .views()
            .filter_map(|(view, _)| (view.doc == event.doc).then_some(view.id))
            .collect();

        for view in views {
            request_document_colors_for_view(event.editor, view, event.doc);
        }

        Ok(())
    });

    // Once these events carry a reference to the Editor then this `tx` method can be dropped
    // and we can use `DocumentColorHandler::event` instead.
    let tx = handlers.document_colors.tx().clone();
    register_hook!(move |event: &mut DocumentDidChange<'_>| {
        // Update the color swatch' positions, helping ensure they are displayed in the
        // proper place.
        let apply_color_swatch_changes = |annotations: &mut Vec<InlineAnnotation>| {
            event.changes.update_positions(
                annotations
                    .iter_mut()
                    .map(|annotation| (&mut annotation.char_idx, helix_core::Assoc::After)),
            );
        };

        for (_view_id, color_swatch) in event.doc.color_swatches_mut() {
            let DocumentColorSwatches {
                color_swatches,
                colors: _colors,
                color_swatches_padding,
            } = color_swatch;


Review comment (refactor):
Inlay hints need to track the view and store the hints on a per-view basis because the inlay hint request gives hints just for the requested range. The document colors request covers the whole document though so there's no need to track the view: the colors will apply to any view that shows the document. So the hook doesn't need to track view information and the helper can change from `request_document_colors_for_view(&mut Editor, ViewId, DocumentId)` to `request_document_colors(&mut Editor, DocumentId)`

Code after:

        // Cancel the ongoing request, if present.
        event.doc.color_swatch_controller.cancel();

        helix_event::send_blocking(&tx, DocumentColorsEvent(event.doc.id()));

        Ok(())
    });

    register_hook!(move |event: &mut LanguageServerInitialized<'_>| {
        let doc_ids: Vec<_> = event.editor.documents().map(|doc| doc.id()).collect();

        for doc_id in doc_ids {
            request_document_colors(event.editor, doc_id);
        }

        Ok(())
    });

    register_hook!(move |event: &mut LanguageServerExited<'_>| {
        // Clear and re-request all color swatches when a server exits.
        for doc in event.editor.documents_mut() {
            if doc.supports_language_server(event.server_id) {
                doc.color_swatches.take();
            }
        }

        let doc_ids: Vec<_> = event.editor.documents().map(|doc| doc.id()).collect();

        for doc_id in doc_ids {
            request_document_colors(event.editor, doc_id);
        }

        Ok(())
    });
}