Skip to content

Commit

Permalink
tree-select-{prev,next}-node; fix root node selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ul committed Dec 28, 2018
1 parent 7ef7c33 commit 37c3b2e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ tree-sitter = "0"
[build-dependencies]
cc = { version = "1", features = ["parallel"] }

[profile.release]
lto = true
# [profile.release]
# lto = true

[features]
default = ["rust"]
Expand Down
2 changes: 2 additions & 0 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Look at `Cargo.toml` for a full list of supported languages.

|===
| 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.
|===

More commands to come.
Expand Down
12 changes: 9 additions & 3 deletions rc/tree.kak
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare-option str tree_log "/tmp/kak-tree.log"

declare-option -hidden str tree_draft

define-command tree-select-node %{
define-command tree-command -params 1 %{
evaluate-commands -draft -no-hooks %{exec '%'; set buffer tree_draft %val{selection}}
evaluate-commands %sh{
Expand All @@ -12,13 +12,19 @@ tree_draft=$(printf '%s.' "${kak_opt_tree_draft}" | sed 's/\\/\\\\/g' | sed 's/"
tree_draft=${tree_draft%.}
printf '
op = "SelectNode"
op = "%s"
filetype = "%s"
selections_desc = "%s"
content = """
%s"""
' "${kak_opt_filetype}" "${kak_selections_desc}" "${tree_draft}" | ${kak_opt_tree_cmd} 2>${kak_opt_tree_log}
' $1 "${kak_opt_filetype}" "${kak_selections_desc}" "${tree_draft}" | ${kak_opt_tree_cmd} 2>${kak_opt_tree_log}
}
}



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 }


53 changes: 37 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ extern "C" {
#[derive(Deserialize)]
enum Op {
SelectNode,
SelectNextNode,
SelectPrevNode,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -100,31 +102,50 @@ 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!("{}", response);
println!("select {}", response);
}

fn handle_request(request: &Request) -> String {
let mut parser = Parser::new();
let language = filetype_to_language(&request.filetype);
parser.set_language(language).unwrap();
let tree = parser.parse_str(&request.content, None).unwrap();
let buffer = request
.content
.split('\n')
.map(|s| format!("{}\n", s))
.collect::<Vec<_>>();
let ranges = selections_desc_to_ranges(&buffer, &request.selections_desc);
let mut new_ranges = Vec::new();
match &request.op {
Op::SelectNode => {
let mut parser = Parser::new();
let language = filetype_to_language(&request.filetype);
parser.set_language(language).unwrap();
let tree = parser.parse_str(&request.content, None).unwrap();
let buffer = request
.content
.split('\n')
.map(|s| format!("{}\n", s))
.collect::<Vec<_>>();
let ranges = selections_desc_to_ranges(&buffer, &request.selections_desc);
let mut new_ranges = Vec::new();
for range in &ranges {
let node = find_deepest_node_containing_range(&tree, range);
new_ranges.push(node.range());
}
let selections_desc = ranges_to_selections_desc(&buffer, &new_ranges);
format!("select {}", selections_desc)
}
}
Op::SelectNextNode => {
for range in &ranges {
let node = find_deepest_node_containing_range(&tree, range);
if let Some(node) = node.next_named_sibling() {
new_ranges.push(node.range());
} else {
new_ranges.push(node.range());
}
}
}
Op::SelectPrevNode => {
for range in &ranges {
let node = find_deepest_node_containing_range(&tree, range);
if let Some(node) = node.prev_named_sibling() {
new_ranges.push(node.range());
} else {
new_ranges.push(node.range());
}
}
}
};
ranges_to_selections_desc(&buffer, &new_ranges)
}

fn find_deepest_node_containing_range<'a>(tree: &'a Tree, range: &Range) -> Node<'a> {
Expand Down Expand Up @@ -213,7 +234,7 @@ fn point_to_kak_coords(buffer: &[String], p: Point) -> String {
None
}
})
.unwrap();
.unwrap_or_else(|| buffer[p.row].len());
format!("{}.{}", p.row + 1, offset + 1)
}

Expand Down

0 comments on commit 37c3b2e

Please sign in to comment.