Compare commits

...

2 Commits

Author SHA1 Message Date
6e6ad7a82b Default config 2023-11-12 22:54:56 +01:00
374910704d Change select-children to not use nodes_in_range 2023-11-12 22:17:04 +01:00
5 changed files with 16 additions and 48 deletions

8
rc/default_config.toml Normal file
View File

@ -0,0 +1,8 @@
[filetype.rust]
group.identifier = ["identifier", "scoped_identifier"]
group.function = ["function_item"]
group.declaration = ["function_item", "struct_item", "enum_item"]
group.call = ["macro_invocation", "call_expression"]
group.arguments = ["arguments", "token_tree"]
group.statement = ["expression_statement", "let_declaration"]

View File

@ -1,8 +1,10 @@
# Path to the kak-tree executable.
# To load config: set-option global tree_cmd "kak-tree --config /path/to/kak-tree.toml"
# To enable debug logging: set-option global tree_cmd "kak-tree -vvv"
declare-option str tree_cmd "kak-tree"
# Path to the config file. Defaults to the built-in file default_config.toml
declare-option str tree_config %sh{ printf "$(dirname ${kak_source})/default_config.toml" }
# Path to the log file.
declare-option str tree_log "/tmp/kak-tree.log"
@ -33,7 +35,7 @@ content = """
[op]
type = "%s"
%s
' "${kak_opt_filetype}" "${kak_selections_desc}" "${tree_draft}" $1 "$2" | ${kak_opt_tree_cmd} 2>${kak_opt_tree_log}
' "${kak_opt_filetype}" "${kak_selections_desc}" "${tree_draft}" $1 "$2" | ${kak_opt_tree_cmd} --config "${kak_opt_tree_config}" 2>${kak_opt_tree_log}
}
}

View File

@ -31,8 +31,8 @@ impl Default for Config {
impl Config {
pub fn load<P: AsRef<std::path::Path>>(path: P) -> Option<Self> {
let config = std::fs::read_to_string(path).ok()?;
let mut config: Config = toml::from_str(&config).ok()?;
let config = std::fs::read_to_string(&path).map_err(|e| { eprintln!("Could not open config {}", path.as_ref().display()); e}).ok()?;
let mut config: Config = toml::from_str(&config).map_err(|e| { eprintln!("Could not read config: {e}"); e}).ok()?;
if config.filetype.get("default").is_none() {
config
.filetype

View File

@ -168,11 +168,10 @@ fn handle_request(config: &Config, request: &Request) -> String {
Some(kind) => {
let kinds = filetype_config.resolve_alias(kind);
for range in &ranges {
for node in tree::nodes_in_range(tree.root_node(), range) {
let node = tree::shrink_to_range(tree.root_node(), range);
select_nodes(&node, &kinds, &mut new_ranges);
}
}
}
None => {
for range in &ranges {
let node = tree::shrink_to_range(tree.root_node(), range);

View File

@ -22,47 +22,6 @@ pub fn shrink_to_range<'a>(root_node: Node<'a>, range: &Range) -> Node<'a> {
}
}
pub fn nodes_in_range<'a>(root_node: Node<'a>, range: &Range) -> Vec<Node<'a>> {
let mut nodes = Vec::new();
let node = shrink_to_range(root_node, range);
if node.range().start_byte >= range.start_byte && range.end_byte >= node.range().end_byte {
nodes.push(node);
return nodes;
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.range().start_byte <= range.start_byte && range.end_byte >= child.range().end_byte
{
nodes.extend(nodes_in_range(
child,
&Range {
start_byte: range.start_byte,
start_point: range.start_point,
end_byte: child.range().end_byte,
end_point: child.range().end_point,
},
));
} else if child.range().start_byte >= range.start_byte
&& range.end_byte <= child.range().end_byte
{
nodes.extend(nodes_in_range(
child,
&Range {
start_byte: child.range().start_byte,
start_point: child.range().start_point,
end_byte: range.end_byte,
end_point: range.end_point,
},
));
} else if child.range().start_byte >= range.start_byte
&& range.end_byte >= child.range().end_byte
{
nodes.push(child);
}
}
nodes
}
fn highest_node_of_same_range<'a>(current_node: Node<'a>) -> Node<'a> {
let start_byte = current_node.start_byte();
let end_byte = current_node.end_byte();