From a78febec6c9d62afd0bb86c34ff5465c91bd299c Mon Sep 17 00:00:00 2001 From: xenia Date: Sun, 12 Nov 2023 17:26:31 +0100 Subject: [PATCH] Highlight current node --- rc/tree.kak | 31 ++++++++++++++++++++++++++++++- src/kakoune.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 8 ++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/rc/tree.kak b/rc/tree.kak index 2c11a10..5cfcb69 100644 --- a/rc/tree.kak +++ b/rc/tree.kak @@ -9,6 +9,11 @@ declare-option str tree_log "/tmp/kak-tree.log" # Option to store draft of the current buffer before passing to shell. declare-option -hidden str tree_draft +declare-option str tree_highlight_style "black,blue" + +declare-option -hidden range-specs tree_highlight +add-highlighter global/tree_highlight ranges tree_highlight + define-command -hidden tree-command -params 1..2 -docstring %{ tree-command [] Send request to kak-tree and evaluate response. @@ -74,6 +79,30 @@ define-command tree-select-first-child -params ..1 -docstring %{ Select the first immediate visible children or the first descendant matching KIND when provided. } %{ tree-command-with-optional-kind SelectChildren %arg{1} - execute-keys + execute-keys , +} + +define-command tree-node-highlight -docstring %{ + tree-node-highlight + Highlight the current node using the tree_highlight_style face +} %{ tree-command HighlightCurrentNode } + +define-command tree-node-clear-highlight -docstring %{ + tree-node-clear-highlight + Removes any highlight +} %{ set-option buffer tree_highlight %val{timestamp} } + +define-command tree-always-highlight -docstring %{ + Highlight the current node at all times, as the cursor moves +} %{ + hook -group tree-always-highlight global RawKey .* tree-node-highlight + tree-node-highlight +} + +define-command tree-always-highlight-disable -docstring %{ + Stop highlighting the cursor +} %{ + remove-hooks global tree-always-highlight + tree-node-clear-highlight } diff --git a/src/kakoune.rs b/src/kakoune.rs index 1bc881b..7a2b03c 100644 --- a/src/kakoune.rs +++ b/src/kakoune.rs @@ -9,6 +9,32 @@ pub fn select_ranges(buffer: &[String], ranges: &[Range]) -> String { } } +pub fn highlight_ranges(buffer: &[String], ranges: &[Range]) -> String { + format!("set-option buffer tree_highlight %val{{timestamp}} {}", ranges_to_range_desc(&buffer, &ranges, "%opt{tree_highlight_style}".to_string())) +} + +pub fn ranges_to_range_desc(buffer: &[String], ranges: &[Range], string: String) -> String { + ranges + .iter() + .map(|range| { + let mut end_row = range.end_point.row; + let mut end_column = range.end_point.column; + if end_column > 0 { + end_column -= 1; + } else { + end_row -= 1; + end_column = 1_000_000; + } + format!( + "\"{},{}|{}\"", + point_to_kak_coords(buffer, range.start_point), + point_to_kak_coords(buffer, Point::new(end_row, end_column)), + string, + ) + }) + .join(" ") +} + pub fn ranges_to_selections_desc(buffer: &[String], ranges: &[Range]) -> String { ranges .iter() diff --git a/src/main.rs b/src/main.rs index 6fdec86..7cf98f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ struct Request { #[serde(tag = "type")] enum Op { NodeSExp, + HighlightCurrentNode, SelectChildren { kind: Option }, SelectNextNode { kind: Option }, SelectParentNode { kind: Option }, @@ -181,6 +182,13 @@ fn handle_request(config: &Config, request: &Request) -> String { let node = tree::shrink_to_range(tree.root_node(), &ranges[0]); format!("info '{}'", node.to_sexp()) } + Op::HighlightCurrentNode => { + for range in &ranges { + let node = tree::shrink_to_range(tree.root_node(), range); + new_ranges.push(node.range()); + } + kakoune::highlight_ranges(&buffer, &new_ranges) + } } }