@@ -471,6 +471,10 @@ fn extend_nodes<'a>(
471471 let mut stop_extend = false;
472472 while deepest_preceding != *node {
473473 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;
474478 if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
475479 for capture in captures {
476480 match capture {
@@ -478,6 +482,7 @@ fn extend_nodes<'a>(
478482 stop_extend = true;
479483 }
480484 ExtendCapture::ExtendIndented => {
485+ node_captured = true;
481486 // We extend the node if
482487 // - the cursor is on the same line as the end of the node OR
483488 // - the line that the cursor is on is more indented than the
@@ -501,16 +506,12 @@ fn extend_nodes<'a>(
501506 }
502507 // If we encountered some `StopExtend` capture before, we don't
503508 // extend the node even if we otherwise would
504− match (extend_node, stop_extend) {
505− (true, true) => {
506− stop_extend = false;
507− }
508− (true, false) => {
509− *node = deepest_preceding;
510− break;
511− }
512− _ => {}
513− };
509+ if node_captured && stop_extend {
510+ stop_extend = false;
511+ } else if extend_node && !stop_extend {
512+ *node = deepest_preceding;
513+ break;
514+ }
514515 // This parent always exists since node is an ancestor of deepest_preceding
515516 deepest_preceding = deepest_preceding.parent().unwrap();
516517 }