on master help sign in
Slightly change the behavior of the `@stop-extend` capture.

This improves the behavior in case of multiple nested extensions.

commitdc443487d4be520efb667f9f93b6c3cbd22e81fd authorDaniel Ebert <daniel.ebert@protonmail.com> dateSat 17 Sep 2022, 15:03 (4 years ago) parent2b02785
2 files changed+16−14
book/src/guides/indent.md+5−4 helix-core/src/indent.rs+11−10
book/src/guides/indent.md+5−4
@@ -54,10 +54,11 @@ for languages like Python, where for the purpose of indentation some nodes
5454
5555- `@stop-extend`:
5656Prevents the first extension of an ancestor of this node. For example, in Python
57−a return expression always ends the block that it is in. Note that this only prevents
58−the next extension of one ancestor: If multiple ancestors can be extended (for example
59−multiple nested conditional blocks in python), only the extension of the innermost
60−ancestor is prevented.
57+a return expression always ends the block that it is in. Note that this only stops the
58+extension of the next `@extend-indented` capture. If multiple ancestors are captured,
59+only the extension of the innermost one is prevented. All other ancestors are unaffected
60+(regardless of whether the innermost ancestor would actually have been extended).
61+
6162
6263## Predicates
6364
helix-core/src/indent.rs+11−10
@@ -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 }