Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
| `:lsp-restart` | Restarts the language servers used by the current doc |
| `:lsp-stop` | Stops the language servers that are used by the current doc |
| `:tree-sitter-scopes` | Display tree sitter scopes, primarily for theming and development. |
| `:tree-sitter-highlight-name` | Display tree-sitter highlight name. |
| `:debug-start`, `:dbg` | Start a debug session from a given template with given parameters. |
| `:debug-remote`, `:dbg-tcp` | Connect to a debug adapter by TCP address and start a debugging session from a given template with given parameters. |
| `:debug-eval` | Evaluate expression in current debug context. |
Expand Down
38 changes: 38 additions & 0 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,44 @@ pub fn pretty_print_tree<W: fmt::Write>(fmt: &mut W, node: Node) -> fmt::Result
}
}

pub fn get_highlight_for_node_at_position(
syntax: Option<&Syntax>,
text: RopeSlice,
pos: usize,
) -> Option<Highlight> {
let mut highlight: Option<Highlight> = None;
if let Some(syntax) = syntax {
let pos = text.char_to_byte(pos);

let node: Node = match syntax
.tree()
.root_node()
.descendant_for_byte_range(pos, pos)
{
Some(node) => node,
None => return None,
};

for event in syntax
.highlight_iter(text.slice(..), Some(node.byte_range()), None)
Comment thread
LukeHalasy marked this conversation as resolved.
Outdated
.map(|event| event.unwrap())
{
match event {
HighlightEvent::Source { start, end } => {
if start == node.start_byte() && end == node.end_byte() {
break;
}
}
HighlightEvent::HighlightStart(hl) => {
highlight = Some(hl);
}
HighlightEvent::HighlightEnd => {}
}
}
}
highlight
Comment thread
LukeHalasy marked this conversation as resolved.
Outdated
}

fn pretty_print_tree_impl<W: fmt::Write>(
fmt: &mut W,
cursor: &mut TreeCursor,
Expand Down
47 changes: 47 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,46 @@ fn tree_sitter_scopes(
Ok(())
}

fn tree_sitter_highlight_name(
cx: &mut compositor::Context,
_args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);

let pos = doc.selection(view.id).primary().cursor(text);

let mut highlight_name: &str = "";
if let Some(highlight) =
helix_core::syntax::get_highlight_for_node_at_position(doc.syntax(), text, pos)
Comment thread
LukeHalasy marked this conversation as resolved.
Outdated
{
let theme = &cx.editor.theme;
highlight_name = theme.scope(highlight.0);
}

let content = format!("``json\n{:?}\n````", highlight_name);
Comment thread
LukeHalasy marked this conversation as resolved.
Outdated

let callback = async move {
let call: job::Callback = Callback::EditorCompositor(Box::new(
move |editor: &mut Editor, compositor: &mut Compositor| {
let content = ui::Markdown::new(content, editor.syn_loader.clone());
let popup = Popup::new("hover", content).auto_close(true);
compositor.replace_or_push("hover", popup);
},
));
Ok(call)
};

cx.jobs.callback(callback);

Ok(())
}

fn vsplit(
cx: &mut compositor::Context,
args: &[Cow<str>],
Expand Down Expand Up @@ -2684,6 +2724,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: tree_sitter_scopes,
signature: CommandSignature::none(),
},
TypableCommand {
name: "tree-sitter-highlight-name",
aliases: &[],
doc: "Display tree-sitter highlight name.",
Comment thread
LukeHalasy marked this conversation as resolved.
Outdated
fun: tree_sitter_highlight_name,
signature: CommandSignature::none(),
},
TypableCommand {
name: "debug-start",
aliases: &["dbg"],
Expand Down
5 changes: 5 additions & 0 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ impl Theme {
self.highlights[index]
}

#[inline]
pub fn scope(&self, index: usize) -> &str {
&self.scopes[index]
}

pub fn name(&self) -> &str {
&self.name
}
Expand Down