@@ -471,6 +471,10 @@ fn extend_nodes<'a>(
471 let mut stop_extend = false;
471 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
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;
474 if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
478 if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
475 for capture in captures {
479 for capture in captures {
476 match capture {
480 match capture {
@@ -478,6 +482,7 @@ fn extend_nodes<'a>(
478 stop_extend = true;
482 stop_extend = true;
480 ExtendCapture::ExtendIndented => {
484 ExtendCapture::ExtendIndented => {
485+ node_captured = true;
481 // We extend the node if
486 // We extend the node if
482 // - 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
483 // - 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
@@ -501,16 +506,12 @@ fn extend_nodes<'a>(
502 // If we encountered some `StopExtend` capture before, we don't
507 // If we encountered some `StopExtend` capture before, we don't
503 // extend the node even if we otherwise would
508 // extend the node even if we otherwise would
509+ if node_captured && stop_extend {
510+ stop_extend = false;
504− match (extend_node, stop_extend) {
511+ } else if extend_node && !stop_extend {
506− stop_extend = false;
509− *node = deepest_preceding;
512+ *node = deepest_preceding;
514 // This parent always exists since node is an ancestor of deepest_preceding
515 // This parent always exists since node is an ancestor of deepest_preceding
515 deepest_preceding = deepest_preceding.parent().unwrap();
516 deepest_preceding = deepest_preceding.parent().unwrap();