@@ -461,60 +461,58 @@ fn query_indents(
461/// so that the indent computation starts with the correct syntax node.
461/// so that the indent computation starts with the correct syntax node.
462fn extend_nodes<'a>(
462fn extend_nodes<'a>(
463 node: &mut Node<'a>,
463 node: &mut Node<'a>,
464− deepest_preceding: Option<Node<'a>>,
464+ mut deepest_preceding: Node<'a>,
465 extend_captures: &HashMap<usize, Vec<ExtendCapture>>,
465 extend_captures: &HashMap<usize, Vec<ExtendCapture>>,
466 text: RopeSlice,
466 text: RopeSlice,
467 line: usize,
467 line: usize,
468 tab_width: usize,
468 tab_width: usize,
470− if let Some(mut deepest_preceding) = deepest_preceding {
471− let mut stop_extend = false;
470+ let mut stop_extend = false;
472− while deepest_preceding != *node {
472+ while deepest_preceding != *node {
473− let mut extend_node = false;
473+ let mut extend_node = false;
474− // This will be set to true if this node is captured, regardless of whether
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
475+ // it actually will be extended (e.g. because the cursor isn't indented
476− // more than the node).
476+ // more than the node).
477− let mut node_captured = false;
477+ let mut node_captured = false;
478− if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
478+ if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
479− for capture in captures {
479+ for capture in captures {
480− match capture {
480+ match capture {
481− ExtendCapture::PreventOnce => {
481+ ExtendCapture::PreventOnce => {
482− stop_extend = true;
482+ stop_extend = true;
484− ExtendCapture::Extend => {
484+ ExtendCapture::Extend => {
485− node_captured = true;
485+ node_captured = true;
486− // We extend the node if
486+ // We extend the node if
487− // - the cursor is on the same line as the end of the node OR
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
488+ // - the line that the cursor is on is more indented than the
489− // first line of the node
489+ // first line of the node
490− if deepest_preceding.end_position().row == line {
490+ if deepest_preceding.end_position().row == line {
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),
498+ if cursor_indent > node_indent {
491 extend_node = true;
499 extend_node = true;
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),
499− if cursor_indent > node_indent {
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;
515− // This parent always exists since node is an ancestor of deepest_preceding
516− deepest_preceding = deepest_preceding.parent().unwrap();
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;
514+ // This parent always exists since node is an ancestor of deepest_preceding
515+ deepest_preceding = deepest_preceding.parent().unwrap();
@@ -612,14 +610,16 @@ pub fn treesitter_indent_for_pos(
612 let extend_captures = query_result.extend_captures;
610 let extend_captures = query_result.extend_captures;
614 // Check for extend captures, potentially changing the node that the indent calculation starts with
612 // Check for extend captures, potentially changing the node that the indent calculation starts with
613+ if let Some(deepest_preceding) = deepest_preceding {
615− extend_nodes(
614+ extend_nodes(
616− &mut node,
615+ &mut node,
617− deepest_preceding,
616+ deepest_preceding,
618− &extend_captures,
617+ &extend_captures,
621− tab_width,
620+ tab_width,
623 let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos));
623 let mut first_in_line = get_first_in_line(node, new_line.then(|| byte_pos));
625 let mut result = Indentation::default();
625 let mut result = Indentation::default();