From 09e4d9236d9252c988a251b219ebd74b042b7475 Mon Sep 17 00:00:00 2001 From: Ruslan Prokopchuk Date: Sat, 29 Dec 2018 09:30:44 +1100 Subject: [PATCH] tree-node-sexp --- README.asciidoc | 21 ++++++++++++++++++--- rc/tree.kak | 1 + src/main.rs | 17 ++++++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 8382a36..fda0336 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -21,10 +21,25 @@ Look at `Cargo.toml` for a full list of supported languages. == Usage +NOTE: "The deepest enclosing node" means the one which matters. For now it's so-called named +node (it's named in tree-sitter grammar definition for the given language), and in future custom +white/balcklisting will be supported on top of that. + +[cols=2*] |=== -| tree-select-node | Extend selections to the deepest enclosing nodes. If any selection is equal to node range, then extend to one's parent range. -| tree-select-next-node | Extend selections to the deepest enclosing nodes' next siblings. -| tree-select-prev-node | Extend selections to the deepest enclosing nodes' previous siblings. +| tree-select-node +| Extend selections to the deepest enclosing nodes. If any selection is equal to node range, then +extend to one's parent range. + +| tree-select-next-node +| Extend selections to the deepest enclosing nodes' next siblings. + +| tree-select-prev-node +| Extend selections to the deepest enclosing nodes' previous siblings. + +| tree-node-sexp +| Show info box with a syntax tree of the deepest enclosing node of the main selection. This time +"deepest" means "deepest", including anonymous and blacklisted nodes, without any filter applied. |=== More commands to come. diff --git a/rc/tree.kak b/rc/tree.kak index 5744502..b1d2e91 100644 --- a/rc/tree.kak +++ b/rc/tree.kak @@ -26,5 +26,6 @@ content = """ define-command tree-select-node %{ tree-command SelectNode } define-command tree-select-next-node %{ tree-command SelectNextNode } define-command tree-select-prev-node %{ tree-command SelectPrevNode } +define-command tree-node-sexp %{ tree-command NodeSExp } diff --git a/src/main.rs b/src/main.rs index c266ff3..902371f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,7 @@ enum Op { SelectNode, SelectNextNode, SelectPrevNode, + NodeSExp, } #[derive(Deserialize)] @@ -102,7 +103,7 @@ fn main() { std::io::stdin().read_to_string(&mut request).unwrap(); let request: Request = toml::from_str(&request).unwrap(); let response = handle_request(&request); - println!("select {}", response); + println!("{}", response); } fn handle_request(request: &Request) -> String { @@ -124,6 +125,7 @@ fn handle_request(request: &Request) -> String { let node = traverse_up_to_node_which_matters(node); new_ranges.push(node.range()); } + select_ranges(&buffer, &new_ranges) } Op::SelectNextNode => { for range in &ranges { @@ -135,6 +137,7 @@ fn handle_request(request: &Request) -> String { new_ranges.push(node.range()); } } + select_ranges(&buffer, &new_ranges) } Op::SelectPrevNode => { for range in &ranges { @@ -146,9 +149,17 @@ fn handle_request(request: &Request) -> String { new_ranges.push(node.range()); } } + select_ranges(&buffer, &new_ranges) } - }; - ranges_to_selections_desc(&buffer, &new_ranges) + Op::NodeSExp => { + let node = find_range_superset_deepest_node(&tree, &ranges[0]); + format!("info '{}'", node.to_sexp()) + } + } +} + +fn select_ranges(buffer: &[String], ranges: &[Range]) -> String { + format!("select {}", ranges_to_selections_desc(&buffer, &ranges)) } fn traverse_up_to_node_which_matters(node: Node) -> Node {