aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Davis2022-11-09 00:54:41 +0000
committerBlaž Hrastnik2022-11-09 03:41:07 +0000
commit260ae3a0f4b94eb0af3477c3e7b94b7a0033431c (patch)
tree9823ba64abe796ef79c918a1fa99d038b2fae9af
parenta19eee6450764ec2be0e8ad25582f33bd68a5d8c (diff)
style: Only call extend_nodes when deepest_preceding is Some
-rw-r--r--helix-core/src/indent.rs100
1 files changed, 50 insertions, 50 deletions
diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs
index 9526fc8a..1c813c9a 100644
--- a/helix-core/src/indent.rs
+++ b/helix-core/src/indent.rs
@@ -461,60 +461,58 @@ fn query_indents(
/// so that the indent computation starts with the correct syntax node.
fn extend_nodes<'a>(
node: &mut Node<'a>,
- deepest_preceding: Option<Node<'a>>,
+ mut deepest_preceding: Node<'a>,
extend_captures: &HashMap<usize, Vec<ExtendCapture>>,
text: RopeSlice,
line: usize,
tab_width: usize,
) {
- if let Some(mut deepest_preceding) = deepest_preceding {
- let mut stop_extend = false;
- while deepest_preceding != *node {
- let mut extend_node = false;
- // This will be set to true if this node is captured, regardless of whether
- // it actually will be extended (e.g. because the cursor isn't indented
- // more than the node).
- let mut node_captured = false;
- if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
- for capture in captures {
- match capture {
- ExtendCapture::PreventOnce => {
- stop_extend = true;
- }
- ExtendCapture::Extend => {
- node_captured = true;
- // We extend the node if
- // - the cursor is on the same line as the end of the node OR
- // - the line that the cursor is on is more indented than the
- // first line of the node
- if deepest_preceding.end_position().row == line {
+ let mut stop_extend = false;
+
+ while deepest_preceding != *node {
+ let mut extend_node = false;
+ // This will be set to true if this node is captured, regardless of whether
+ // it actually will be extended (e.g. because the cursor isn't indented
+ // more than the node).
+ let mut node_captured = false;
+ if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
+ for capture in captures {
+ match capture {
+ ExtendCapture::PreventOnce => {
+ stop_extend = true;
+ }
+ ExtendCapture::Extend => {
+ node_captured = true;
+ // We extend the node if
+ // - the cursor is on the same line as the end of the node OR
+ // - the line that the cursor is on is more indented than the
+ // first line of the node
+ if deepest_preceding.end_position().row == line {
+ extend_node = true;
+ } else {
+ let cursor_indent = indent_level_for_line(text.line(line), tab_width);
+ let node_indent = indent_level_for_line(
+ text.line(deepest_preceding.start_position().row),
+ tab_width,
+ );
+ if cursor_indent > node_indent {
extend_node = true;
- } else {
- let cursor_indent =
- indent_level_for_line(text.line(line), tab_width);
- let node_indent = indent_level_for_line(
- text.line(deepest_preceding.start_position().row),
- tab_width,
- );
- if cursor_indent > node_indent {
- extend_node = true;
- }
}
}
}
}
}
- // If we encountered some `StopExtend` capture before, we don't
- // extend the node even if we otherwise would
- if node_captured && stop_extend {
- stop_extend = false;
- } else if extend_node && !stop_extend {
- *node = deepest_preceding;
- break;
- }
- // This parent always exists since node is an ancestor of deepest_preceding
- deepest_preceding = deepest_preceding.parent().unwrap();
}
+ // If we encountered some `StopExtend` capture before, we don't
+ // extend the node even if we otherwise would
+ if node_captured && stop_extend {
+ stop_extend = false;
+ } else if extend_node && !stop_extend {
+ *node = deepest_preceding;
+ break;
+ }
+ // This parent always exists since node is an ancestor of deepest_preceding
+ deepest_preceding = deepest_preceding.parent().unwrap();
}
}
@@ -612,14 +610,16 @@ pub fn treesitter_indent_for_pos(
let extend_captures = query_result.extend_captures;
// Check for extend captures, potentially changing the node that the indent calculation starts with
- extend_nodes(
- &mut node,
- deepest_preceding,
- &extend_captures,
- text,
- line,
- tab_width,
- );
+ if let Some(deepest_preceding) = deepest_preceding {
+ extend_nodes(
+ &mut node,
+ deepest_preceding,
+ &extend_captures,
+ text,
+ line,
+ tab_width,
+ );
+ }
let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos));
let mut result = Indentation::default();