From 32a77b3a314b257ee3cbbc13096c4e3710421721 Mon Sep 17 00:00:00 2001 From: xenia Date: Sun, 12 Nov 2023 23:08:35 +0100 Subject: [PATCH] Change behaviour of next and previous node --- src/main.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index ff51686..9f0accc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -132,13 +132,21 @@ fn handle_request(config: &Config, request: &Request) -> String { 'outer_next: for range in &ranges { let node = tree::shrink_to_range(tree.root_node(), range); let mut cursor = traverse_up_to_node_which_matters(filetype_config, node); - while let Some(node) = cursor.next_named_sibling() { - if filetype_config.is_node_visible(node) && node_of_kinds(node, &kinds) { - new_ranges.push(node.range()); - continue 'outer_next; + loop { + while let Some(node) = cursor.next_named_sibling() { + if filetype_config.is_node_visible(node) && node_of_kinds(node, &kinds) { + new_ranges.push(node.range()); + continue 'outer_next; + } + cursor = node; + } + if let Some(parent) = cursor.parent() { + cursor = parent; + } else { + break; } - cursor = node; } + // TODO: Do we want this behaviour? let node = find_parent_of_interest(filetype_config, node, &kinds); new_ranges.push(node.range()); } @@ -151,12 +159,19 @@ fn handle_request(config: &Config, request: &Request) -> String { 'outer_prev: for range in &ranges { let node = tree::shrink_to_range(tree.root_node(), range); let mut cursor = traverse_up_to_node_which_matters(filetype_config, node); - while let Some(node) = cursor.prev_named_sibling() { - if filetype_config.is_node_visible(node) && node_of_kinds(node, &kinds) { - new_ranges.push(node.range()); - continue 'outer_prev; + loop { + while let Some(node) = cursor.prev_named_sibling() { + if filetype_config.is_node_visible(node) && node_of_kinds(node, &kinds) { + new_ranges.push(node.range()); + continue 'outer_prev; + } + cursor = node; + } + if let Some(parent) = cursor.parent() { + cursor = parent; + } else { + break; } - cursor = node; } let node = find_parent_of_interest(filetype_config, node, &kinds); new_ranges.push(node.range());