Show all active scopes in :tree-sitter-highlight-name

Previously the command only showed the top of the stack of highlights.
Now it shows all active highlights under the cursor, comma separated.
pull/13708/head^2
Michael Davis 2025-06-21 12:29:13 -04:00
parent 472a27e4f2
commit 4a85171907
No known key found for this signature in database
1 changed files with 39 additions and 41 deletions

View File

@ -1672,15 +1672,17 @@ fn tree_sitter_highlight_name(
_args: Args, _args: Args,
event: PromptEvent, event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
use helix_core::syntax::Highlight; if event != PromptEvent::Validate {
return Ok(());
}
fn find_highlight_at_cursor(editor: &Editor) -> Option<Highlight> { let (view, doc) = current_ref!(cx.editor);
let (view, doc) = current_ref!(editor); let Some(syntax) = doc.syntax() else {
let syntax = doc.syntax()?; return Ok(());
};
let text = doc.text().slice(..); let text = doc.text().slice(..);
let cursor = doc.selection(view.id).primary().cursor(text); let cursor = doc.selection(view.id).primary().cursor(text);
let byte = text.char_to_byte(cursor) as u32; let byte = text.char_to_byte(cursor) as u32;
let node = syntax.descendant_for_byte_range(byte, byte)?;
// Query the same range as the one used in syntax highlighting. // Query the same range as the one used in syntax highlighting.
let range = { let range = {
// Calculate viewport byte ranges: // Calculate viewport byte ranges:
@ -1695,31 +1697,27 @@ fn tree_sitter_highlight_name(
start..end start..end
}; };
let loader = editor.syn_loader.load(); let loader = cx.editor.syn_loader.load();
let mut highlighter = syntax.highlighter(text, &loader, range); let mut highlighter = syntax.highlighter(text, &loader, range);
let mut highlights = Vec::new();
while highlighter.next_event_offset() != u32::MAX { while highlighter.next_event_offset() <= byte {
let start = highlighter.next_event_offset(); let (event, new_highlights) = highlighter.advance();
highlighter.advance(); if event == helix_core::syntax::HighlightEvent::Refresh {
let end = highlighter.next_event_offset(); highlights.clear();
if start <= node.start_byte() && end >= node.end_byte() {
return highlighter.active_highlights().next_back();
} }
highlights.extend(new_highlights);
} }
None let content = highlights
.into_iter()
.fold(String::new(), |mut acc, highlight| {
if !acc.is_empty() {
acc.push_str(", ");
} }
acc.push_str(cx.editor.theme.scope(highlight));
if event != PromptEvent::Validate { acc
return Ok(()); });
}
let Some(highlight) = find_highlight_at_cursor(cx.editor) else {
return Ok(());
};
let content = cx.editor.theme.scope(highlight).to_string();
let callback = async move { let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new( let call: job::Callback = Callback::EditorCompositor(Box::new(