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,
event: PromptEvent,
) -> 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!(editor);
let syntax = doc.syntax()?;
let (view, doc) = current_ref!(cx.editor);
let Some(syntax) = doc.syntax() else {
return Ok(());
};
let text = doc.text().slice(..);
let cursor = doc.selection(view.id).primary().cursor(text);
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.
let range = {
// Calculate viewport byte ranges:
@ -1695,31 +1697,27 @@ fn tree_sitter_highlight_name(
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 highlights = Vec::new();
while highlighter.next_event_offset() != u32::MAX {
let start = highlighter.next_event_offset();
highlighter.advance();
let end = highlighter.next_event_offset();
if start <= node.start_byte() && end >= node.end_byte() {
return highlighter.active_highlights().next_back();
while highlighter.next_event_offset() <= byte {
let (event, new_highlights) = highlighter.advance();
if event == helix_core::syntax::HighlightEvent::Refresh {
highlights.clear();
}
highlights.extend(new_highlights);
}
None
let content = highlights
.into_iter()
.fold(String::new(), |mut acc, highlight| {
if !acc.is_empty() {
acc.push_str(", ");
}
if event != PromptEvent::Validate {
return Ok(());
}
let Some(highlight) = find_highlight_at_cursor(cx.editor) else {
return Ok(());
};
let content = cx.editor.theme.scope(highlight).to_string();
acc.push_str(cx.editor.theme.scope(highlight));
acc
});
let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new(