Compare commits

..

3 Commits

Author SHA1 Message Date
1314dce07d Ignore unnamed nodes 2023-11-12 17:26:53 +01:00
fc7288a27f tree-select-node to select current node 2023-11-12 17:26:44 +01:00
a78febec6c Highlight current node 2023-11-12 17:26:31 +01:00
5 changed files with 81 additions and 1 deletions

View File

@ -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. # Option to store draft of the current buffer before passing to shell.
declare-option -hidden str tree_draft 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 %{ define-command -hidden tree-command -params 1..2 -docstring %{
tree-command <OP_TYPE> [<OP_PARAMS>] tree-command <OP_TYPE> [<OP_PARAMS>]
Send request to kak-tree and evaluate response. Send request to kak-tree and evaluate response.
@ -49,6 +54,11 @@ define-command tree-select-parent-node -params ..1 -docstring %{
Select the closest visible ancestor or ancestor of KIND when provided. Select the closest visible ancestor or ancestor of KIND when provided.
} %{ tree-command-with-optional-kind SelectParentNode %arg{1} } } %{ tree-command-with-optional-kind SelectParentNode %arg{1} }
define-command tree-select-node -docstring %{
tree-select-node
Select the closest visible node
} %{ tree-command-with-optional-kind SelectNode }
define-command tree-select-next-node -params ..1 -docstring %{ define-command tree-select-next-node -params ..1 -docstring %{
tree-select-next-node [<KIND>] tree-select-next-node [<KIND>]
Select the closest visible next sibling or next sibling of KIND when provided. Select the closest visible next sibling or next sibling of KIND when provided.
@ -74,6 +84,30 @@ define-command tree-select-first-child -params ..1 -docstring %{
Select the first immediate visible children or the first descendant matching KIND when provided. Select the first immediate visible children or the first descendant matching KIND when provided.
} %{ } %{
tree-command-with-optional-kind SelectChildren %arg{1} tree-command-with-optional-kind SelectChildren %arg{1}
execute-keys <space> 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
} }

View File

@ -52,6 +52,9 @@ impl Config {
impl FiletypeConfig { impl FiletypeConfig {
pub fn is_node_visible(&self, node: Node) -> bool { pub fn is_node_visible(&self, node: Node) -> bool {
let kind = node.kind(); let kind = node.kind();
if !node.is_named() {
return false;
}
match &self.whitelist { match &self.whitelist {
Some(whitelist) => whitelist.iter().any(|x| x == kind), Some(whitelist) => whitelist.iter().any(|x| x == kind),
None => match &self.blacklist { None => match &self.blacklist {

View File

@ -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 { pub fn ranges_to_selections_desc(buffer: &[String], ranges: &[Range]) -> String {
ranges ranges
.iter() .iter()

View File

@ -23,10 +23,12 @@ struct Request {
#[serde(tag = "type")] #[serde(tag = "type")]
enum Op { enum Op {
NodeSExp, NodeSExp,
HighlightCurrentNode,
SelectChildren { kind: Option<String> }, SelectChildren { kind: Option<String> },
SelectNextNode { kind: Option<String> }, SelectNextNode { kind: Option<String> },
SelectParentNode { kind: Option<String> }, SelectParentNode { kind: Option<String> },
SelectPreviousNode { kind: Option<String> }, SelectPreviousNode { kind: Option<String> },
SelectNode,
} }
fn main() { fn main() {
@ -105,6 +107,13 @@ fn handle_request(config: &Config, request: &Request) -> String {
let mut new_ranges = Vec::new(); let mut new_ranges = Vec::new();
let filetype_config = config.get_filetype_config(&request.filetype); let filetype_config = config.get_filetype_config(&request.filetype);
match &request.op { match &request.op {
Op::SelectNode => {
for range in &ranges {
let node = tree::shrink_to_range(tree.root_node(), range);
new_ranges.push(node.range());
}
kakoune::select_ranges(&buffer, &new_ranges)
}
Op::SelectParentNode { kind } => { Op::SelectParentNode { kind } => {
let kinds = kind let kinds = kind
.as_ref() .as_ref()
@ -181,6 +190,13 @@ fn handle_request(config: &Config, request: &Request) -> String {
let node = tree::shrink_to_range(tree.root_node(), &ranges[0]); let node = tree::shrink_to_range(tree.root_node(), &ranges[0]);
format!("info '{}'", node.to_sexp()) 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)
}
} }
} }

View File

@ -12,6 +12,7 @@ pub fn shrink_to_range<'a>(root_node: Node<'a>, range: &Range) -> Node<'a> {
for child in parent.children(&mut cursor) { for child in parent.children(&mut cursor) {
if child.range().start_byte <= range.start_byte if child.range().start_byte <= range.start_byte
&& range.end_byte <= child.range().end_byte && range.end_byte <= child.range().end_byte
&& child.is_named()
{ {
node = child; node = child;
continue 'outer; continue 'outer;