Skip to content

Commit

Permalink
tree-node-sexp
Browse files Browse the repository at this point in the history
  • Loading branch information
ul committed Dec 28, 2018
1 parent 7f7236c commit 09e4d92
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
21 changes: 18 additions & 3 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions rc/tree.kak
Original file line number Diff line number Diff line change
Expand Up @@ -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 }


17 changes: 14 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ enum Op {
SelectNode,
SelectNextNode,
SelectPrevNode,
NodeSExp,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 09e4d92

Please sign in to comment.