on master help sign in
style: Only call extend_nodes when deepest_preceding is Some
commit260ae3a0f4b94eb0af3477c3e7b94b7a0033431c authorMichael Davis <mcarsondavis@gmail.com> dateTue 8 Nov 2022, 18:54 (4 years ago) parenta19eee6
1 file changed+50−50
helix-core/src/indent.rs+50−50
helix-core/src/indent.rs+50−50
@@ -461,60 +461,58 @@ fn query_indents(
461461/// so that the indent computation starts with the correct syntax node.
462462fn extend_nodes<'a>(
463463 node: &mut Node<'a>,
464− deepest_preceding: Option<Node<'a>>,
464+ mut deepest_preceding: Node<'a>,
465465 extend_captures: &HashMap<usize, Vec<ExtendCapture>>,
466466 text: RopeSlice,
467467 line: usize,
468468 tab_width: usize,
469469) {
470− if let Some(mut deepest_preceding) = deepest_preceding {
471− let mut stop_extend = false;
472− while deepest_preceding != *node {
473− let mut extend_node = false;
474− // This will be set to true if this node is captured, regardless of whether
475− // it actually will be extended (e.g. because the cursor isn't indented
476− // more than the node).
477− let mut node_captured = false;
478− if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
479− for capture in captures {
480− match capture {
481− ExtendCapture::PreventOnce => {
482− stop_extend = true;
483− }
484− ExtendCapture::Extend => {
485− node_captured = true;
486− // We extend the node if
487− // - the cursor is on the same line as the end of the node OR
488− // - the line that the cursor is on is more indented than the
489− // first line of the node
490− if deepest_preceding.end_position().row == line {
470+ let mut stop_extend = false;
471+
472+ while deepest_preceding != *node {
473+ let mut extend_node = false;
474+ // This will be set to true if this node is captured, regardless of whether
475+ // it actually will be extended (e.g. because the cursor isn't indented
476+ // more than the node).
477+ let mut node_captured = false;
478+ if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
479+ for capture in captures {
480+ match capture {
481+ ExtendCapture::PreventOnce => {
482+ stop_extend = true;
483+ }
484+ ExtendCapture::Extend => {
485+ node_captured = true;
486+ // We extend the node if
487+ // - the cursor is on the same line as the end of the node OR
488+ // - the line that the cursor is on is more indented than the
489+ // first line of the node
490+ if deepest_preceding.end_position().row == line {
491+ extend_node = true;
492+ } else {
493+ let cursor_indent = indent_level_for_line(text.line(line), tab_width);
494+ let node_indent = indent_level_for_line(
495+ text.line(deepest_preceding.start_position().row),
496+ tab_width,
497+ );
498+ if cursor_indent > node_indent {
491499 extend_node = true;
492− } else {
493− let cursor_indent =
494− indent_level_for_line(text.line(line), tab_width);
495− let node_indent = indent_level_for_line(
496− text.line(deepest_preceding.start_position().row),
497− tab_width,
498− );
499− if cursor_indent > node_indent {
500− extend_node = true;
501− }
502500 }
503501 }
504502 }
505503 }
506504 }
507− // If we encountered some `StopExtend` capture before, we don't
508− // extend the node even if we otherwise would
509− if node_captured && stop_extend {
510− stop_extend = false;
511− } else if extend_node && !stop_extend {
512− *node = deepest_preceding;
513− break;
514− }
515− // This parent always exists since node is an ancestor of deepest_preceding
516− deepest_preceding = deepest_preceding.parent().unwrap();
517505 }
506+ // If we encountered some `StopExtend` capture before, we don't
507+ // extend the node even if we otherwise would
508+ if node_captured && stop_extend {
509+ stop_extend = false;
510+ } else if extend_node && !stop_extend {
511+ *node = deepest_preceding;
512+ break;
513+ }
514+ // This parent always exists since node is an ancestor of deepest_preceding
515+ deepest_preceding = deepest_preceding.parent().unwrap();
518516 }
519517}
520518
@@ -612,14 +610,16 @@ pub fn treesitter_indent_for_pos(
612610 let extend_captures = query_result.extend_captures;
613611
614612 // Check for extend captures, potentially changing the node that the indent calculation starts with
615− extend_nodes(
616− &mut node,
617− deepest_preceding,
618− &extend_captures,
619− text,
620− line,
621− tab_width,
622− );
613+ if let Some(deepest_preceding) = deepest_preceding {
614+ extend_nodes(
615+ &mut node,
616+ deepest_preceding,
617+ &extend_captures,
618+ text,
619+ line,
620+ tab_width,
621+ );
622+ }
623623 let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos));
624624
625625 let mut result = Indentation::default();