Skip to content

Commit

Permalink
tree-select-parent-kind; tree-select-node -> tree-select-parent-node;…
Browse files Browse the repository at this point in the history
… tree-select-nodes -> tree-select-kind
  • Loading branch information
ul committed Dec 31, 2018
1 parent 13b1cd8 commit bc3b851
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
11 changes: 7 additions & 4 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

kak-tree is a plugin for Kakoune which enables selection of syntax tree nodes. Parsing is performed with https://github.com/tree-sitter/tree-sitter[tree-sitter].

Status: proof of concept.
Status: proof of concept, API and overall development direction could change drastically based on feedback.

== Installation

Expand Down Expand Up @@ -37,7 +37,7 @@ white/blacklisting.

[cols=2*]
|===
| tree-select-node
| tree-select-parent-node
| Extend selections to the deepest enclosing nodes. If any selection is equal to node range, then
extend to one's parent range.

Expand All @@ -55,14 +55,17 @@ extend to one's parent range.
| Find the deepest enclosing nodes of selections and then select each node's first child. If node
has no children then keep selection on node.


| tree-select-children
| Find the deepest enclosing nodes of selections and then select each node's children. If node has
no children then keep selection on node.

| tree-select-nodes <KIND>
| tree-select-kind <KIND>
| Selects all the KIND nodes wholly contained within an existing selection.

| tree-select-parent-kind <KIND>
| Select the closest ancestors of KIND of selections' deepest enclosing nodes. If any of selections
don't have such ancestor, remove that selection.

|===

More commands to come.
Expand Down
5 changes: 3 additions & 2 deletions rc/tree.kak
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ content = """



define-command tree-select-node %{ tree-command SelectNode }
define-command tree-select-parent-node %{ tree-command SelectParentNode }
define-command tree-select-next-node %{ tree-command SelectNextNode }
define-command tree-select-prev-node %{ tree-command SelectPrevNode }
define-command tree-select-first-child %{ tree-command SelectFirstChild }
define-command tree-select-children %{ tree-command SelectChildren }
define-command tree-node-sexp %{ tree-command NodeSExp }
define-command tree-select-nodes -params 1 %{ tree-command SelectNodes %arg{1} }
define-command tree-select-kind -params 1 %{ tree-command SelectKind %arg{1} }
define-command tree-select-parent-kind -params 1 %{ tree-command SelectParentKind %arg{1} }


29 changes: 22 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ extern "C" {

#[derive(Deserialize)]
enum Op {
SelectNode,
SelectNodes,
NodeSExp,
SelectChildren,
SelectFirstChild,
SelectKind,
SelectNextNode,
SelectParentKind,
SelectParentNode,
SelectPrevNode,
SelectFirstChild,
SelectChildren,
NodeSExp,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -158,7 +159,7 @@ fn handle_request(config: &Config, request: &Request) -> String {
let mut new_ranges = Vec::new();
let filetype_config = config.filetype.get(&request.filetype);
match &request.op {
Op::SelectNode => {
Op::SelectParentNode => {
for range in &ranges {
let node = find_range_strict_superset_deepest_node(tree.root_node(), range);
let node = traverse_up_to_node_which_matters(filetype_config, node);
Expand Down Expand Up @@ -219,7 +220,7 @@ fn handle_request(config: &Config, request: &Request) -> String {
let node = find_range_superset_deepest_node(tree.root_node(), &ranges[0]);
format!("info '{}'", node.to_sexp())
}
Op::SelectNodes => {
Op::SelectKind => {
let kind = &request.param;
for range in &ranges {
for node in find_nodes_in_range(tree.root_node(), range) {
Expand All @@ -228,6 +229,20 @@ fn handle_request(config: &Config, request: &Request) -> String {
}
select_ranges(&buffer, &new_ranges)
}
Op::SelectParentKind => {
let kind = &request.param;
for range in &ranges {
let mut cursor = Some(find_range_superset_deepest_node(tree.root_node(), range));
while let Some(node) = cursor {
if node.kind() == kind {
new_ranges.push(node.range());
break;
}
cursor = node.parent();
}
}
select_ranges(&buffer, &new_ranges)
}
}
}

Expand Down

0 comments on commit bc3b851

Please sign in to comment.