Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-term/src/commands/typed.rs')
| -rw-r--r-- | helix-term/src/commands/typed.rs | 43 |
1 files changed, 19 insertions, 24 deletions
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index db2942ea..200323e1 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1672,16 +1672,14 @@ fn tree_sitter_highlight_name( _args: Args, event: PromptEvent, ) -> anyhow::Result<()> { - fn find_highlight_at_cursor( - cx: &mut compositor::Context<'_>, - ) -> Option<helix_core::syntax::Highlight> { - use helix_core::syntax::HighlightEvent; + use helix_core::syntax::Highlight; - let (view, doc) = current!(cx.editor); + fn find_highlight_at_cursor(editor: &Editor) -> Option<Highlight> { + let (view, doc) = current_ref!(editor); let syntax = doc.syntax()?; let text = doc.text().slice(..); let cursor = doc.selection(view.id).primary().cursor(text); - let byte = text.char_to_byte(cursor); + 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 = { @@ -1691,25 +1689,22 @@ fn tree_sitter_highlight_name( let last_line = text.len_lines().saturating_sub(1); let height = view.inner_area(doc).height; let last_visible_line = (row + height as usize).saturating_sub(1).min(last_line); - let start = text.line_to_byte(row.min(last_line)); - let end = text.line_to_byte(last_visible_line + 1); + let start = text.line_to_byte(row.min(last_line)) as u32; + let end = text.line_to_byte(last_visible_line + 1) as u32; start..end }; - let mut highlight = None; + let loader = editor.syn_loader.load(); + let mut highlighter = syntax.highlighter(text, &loader, range); - for event in syntax.highlight_iter(text, Some(range), None) { - match event.unwrap() { - HighlightEvent::Source { start, end } - if start == node.start_byte() && end == node.end_byte() => - { - return highlight; - } - HighlightEvent::HighlightStart(hl) => { - highlight = Some(hl); - } - _ => (), + 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(); } } @@ -1720,11 +1715,11 @@ fn tree_sitter_highlight_name( return Ok(()); } - let Some(highlight) = find_highlight_at_cursor(cx) else { + let Some(highlight) = find_highlight_at_cursor(cx.editor) else { return Ok(()); }; - let content = cx.editor.theme.scope(highlight.0).to_string(); + let content = cx.editor.theme.scope(highlight).to_string(); let callback = async move { let call: job::Callback = Callback::EditorCompositor(Box::new( @@ -2200,8 +2195,8 @@ fn tree_sitter_subtree( if let Some(syntax) = doc.syntax() { let primary_selection = doc.selection(view.id).primary(); let text = doc.text(); - let from = text.char_to_byte(primary_selection.from()); - let to = text.char_to_byte(primary_selection.to()); + let from = text.char_to_byte(primary_selection.from()) as u32; + let to = text.char_to_byte(primary_selection.to()) as u32; if let Some(selected_node) = syntax.descendant_for_byte_range(from, to) { let mut contents = String::from("```tsq\n"); helix_core::syntax::pretty_print_tree(&mut contents, selected_node)?; |