-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Color swatches ( 🟩 green 🟥 #ffaaaa ) #12308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
the-mikedavis
merged 15 commits into
helix-editor:master
from
nik-contrib:textDocument/documentColor
Mar 23, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
53c941d
feat: document color swatches
nik-rev 367770f
chore: remove inner option
nik-rev 0b05f61
chore: do not track views in document_colors handler
nik-rev 2ff65d5
feat: add support for multile language servers
nik-rev 9674041
chore: remove controllers
nik-rev a36cd24
minor: Match spec name for the request
the-mikedavis a28e248
Eagerly decode color position
the-mikedavis a6b59bd
Cancel ongoing requests when the document is changed
the-mikedavis 7ae1682
Simplify document colors handler
the-mikedavis 797c9b9
Increase debounce
the-mikedavis fae1969
Inline Style::rgb
the-mikedavis e6c5dd2
Avoid bytes in RGB highlight roundtrip test
the-mikedavis 7cc5dcf
minor: wording
the-mikedavis 72ed48a
Avoid duplicate requests when a language server is duplicated in config
the-mikedavis a1939f4
minor: spurious whitespace change
the-mikedavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
use std::{collections::HashSet, time::Duration}; | ||
|
||
use futures_util::{stream::FuturesOrdered, StreamExt}; | ||
use helix_core::{syntax::LanguageServerFeature, text_annotations::InlineAnnotation}; | ||
use helix_event::{cancelable_future, register_hook}; | ||
use helix_lsp::lsp; | ||
use helix_view::{ | ||
document::DocumentColorSwatches, | ||
events::{DocumentDidChange, DocumentDidOpen, LanguageServerExited, LanguageServerInitialized}, | ||
handlers::{lsp::DocumentColorsEvent, Handlers}, | ||
DocumentId, Editor, Theme, | ||
}; | ||
use tokio::time::Instant; | ||
|
||
use crate::job; | ||
|
||
#[derive(Default)] | ||
pub(super) struct DocumentColorsHandler { | ||
docs: HashSet<DocumentId>, | ||
} | ||
|
||
const DOCUMENT_CHANGE_DEBOUNCE: Duration = Duration::from_millis(250); | ||
|
||
impl helix_event::AsyncHook for DocumentColorsHandler { | ||
type Event = DocumentColorsEvent; | ||
|
||
fn handle_event(&mut self, event: Self::Event, _timeout: Option<Instant>) -> Option<Instant> { | ||
let DocumentColorsEvent(doc_id) = event; | ||
self.docs.insert(doc_id); | ||
Some(Instant::now() + DOCUMENT_CHANGE_DEBOUNCE) | ||
} | ||
|
||
fn finish_debounce(&mut self) { | ||
let docs = std::mem::take(&mut self.docs); | ||
|
||
job::dispatch_blocking(move |editor, _compositor| { | ||
for doc in docs { | ||
request_document_colors(editor, doc); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn request_document_colors(editor: &mut Editor, doc_id: DocumentId) { | ||
if !editor.config().lsp.display_color_swatches { | ||
return; | ||
} | ||
|
||
let Some(doc) = editor.document_mut(doc_id) else { | ||
return; | ||
}; | ||
|
||
let cancel = doc.color_swatch_controller.restart(); | ||
|
||
let mut seen_language_servers = HashSet::new(); | ||
let mut futures: FuturesOrdered<_> = doc | ||
.language_servers_with_feature(LanguageServerFeature::DocumentColors) | ||
.filter(|ls| seen_language_servers.insert(ls.id())) | ||
.map(|language_server| { | ||
let text = doc.text().clone(); | ||
let offset_encoding = language_server.offset_encoding(); | ||
let future = language_server | ||
.text_document_document_color(doc.identifier(), None) | ||
.unwrap(); | ||
|
||
async move { | ||
let colors: Vec<_> = future | ||
.await? | ||
.into_iter() | ||
.filter_map(|color_info| { | ||
let pos = helix_lsp::util::lsp_pos_to_pos( | ||
&text, | ||
color_info.range.start, | ||
offset_encoding, | ||
)?; | ||
Some((pos, color_info.color)) | ||
}) | ||
.collect(); | ||
anyhow::Ok(colors) | ||
} | ||
}) | ||
.collect(); | ||
|
||
tokio::spawn(async move { | ||
let mut all_colors = Vec::new(); | ||
loop { | ||
match cancelable_future(futures.next(), &cancel).await { | ||
Some(Some(Ok(items))) => all_colors.extend(items), | ||
Some(Some(Err(err))) => log::error!("document color request failed: {err}"), | ||
Some(None) => break, | ||
// The request was cancelled. | ||
None => return, | ||
} | ||
} | ||
job::dispatch(move |editor, _| attach_document_colors(editor, doc_id, all_colors)).await; | ||
}); | ||
} | ||
|
||
fn attach_document_colors( | ||
editor: &mut Editor, | ||
doc_id: DocumentId, | ||
mut doc_colors: Vec<(usize, lsp::Color)>, | ||
) { | ||
if !editor.config().lsp.display_color_swatches { | ||
return; | ||
} | ||
|
||
let Some(doc) = editor.documents.get_mut(&doc_id) else { | ||
return; | ||
}; | ||
|
||
if doc_colors.is_empty() { | ||
doc.color_swatches.take(); | ||
return; | ||
} | ||
|
||
doc_colors.sort_by_key(|(pos, _)| *pos); | ||
|
||
let mut color_swatches = Vec::with_capacity(doc_colors.len()); | ||
let mut color_swatches_padding = Vec::with_capacity(doc_colors.len()); | ||
let mut colors = Vec::with_capacity(doc_colors.len()); | ||
|
||
for (pos, color) in doc_colors { | ||
color_swatches_padding.push(InlineAnnotation::new(pos, " ")); | ||
color_swatches.push(InlineAnnotation::new(pos, "■")); | ||
colors.push(Theme::rgb_highlight( | ||
(color.red * 255.) as u8, | ||
(color.green * 255.) as u8, | ||
(color.blue * 255.) as u8, | ||
)); | ||
} | ||
|
||
doc.color_swatches = Some(DocumentColorSwatches { | ||
color_swatches, | ||
colors, | ||
color_swatches_padding, | ||
}); | ||
} | ||
|
||
pub(super) fn register_hooks(handlers: &Handlers) { | ||
register_hook!(move |event: &mut DocumentDidOpen<'_>| { | ||
// when a document is initially opened, request colors for it | ||
request_document_colors(event.editor, event.doc); | ||
|
||
Ok(()) | ||
}); | ||
|
||
let tx = handlers.document_colors.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)), | ||
); | ||
}; | ||
|
||
if let Some(DocumentColorSwatches { | ||
color_swatches, | ||
colors: _colors, | ||
color_swatches_padding, | ||
}) = &mut event.doc.color_swatches | ||
{ | ||
apply_color_swatch_changes(color_swatches); | ||
apply_color_swatch_changes(color_swatches_padding); | ||
} | ||
|
||
// 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(()) | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.