Skip to content

Commit

Permalink
Slightly change the behavior of the @stop-extend capture.
Browse files Browse the repository at this point in the history
This improves the behavior in case of multiple nested extensions.
  • Loading branch information
Triton171 authored and archseer committed Oct 11, 2022
1 parent 2b02785 commit dc44348
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
9 changes: 5 additions & 4 deletions book/src/guides/indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ for languages like Python, where for the purpose of indentation some nodes

- `@stop-extend`:
Prevents the first extension of an ancestor of this node. For example, in Python
a return expression always ends the block that it is in. Note that this only prevents
the next extension of one ancestor: If multiple ancestors can be extended (for example
multiple nested conditional blocks in python), only the extension of the innermost
ancestor is prevented.
a return expression always ends the block that it is in. Note that this only stops the
extension of the next `@extend-indented` capture. If multiple ancestors are captured,
only the extension of the innermost one is prevented. All other ancestors are unaffected
(regardless of whether the innermost ancestor would actually have been extended).


## Predicates

Expand Down
21 changes: 11 additions & 10 deletions helix-core/src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,18 @@ fn extend_nodes<'a>(
let mut stop_extend = false;
while deepest_preceding != *node {
let mut extend_node = false;
// This will be set to true if this node is captured, regardless of whether
// it actually will be extended (e.g. because the cursor isn't indented
// more than the node).
let mut node_captured = false;
if let Some(captures) = extend_captures.get(&deepest_preceding.id()) {
for capture in captures {
match capture {
ExtendCapture::StopExtend => {
stop_extend = true;
}
ExtendCapture::ExtendIndented => {
node_captured = true;
// We extend the node if
// - the cursor is on the same line as the end of the node OR
// - the line that the cursor is on is more indented than the
Expand All @@ -501,16 +506,12 @@ fn extend_nodes<'a>(
}
// If we encountered some `StopExtend` capture before, we don't
// extend the node even if we otherwise would
match (extend_node, stop_extend) {
(true, true) => {
stop_extend = false;
}
(true, false) => {
*node = deepest_preceding;
break;
}
_ => {}
};
if node_captured && stop_extend {
stop_extend = false;
} else if extend_node && !stop_extend {
*node = deepest_preceding;
break;
}
// This parent always exists since node is an ancestor of deepest_preceding
deepest_preceding = deepest_preceding.parent().unwrap();
}
Expand Down

0 comments on commit dc44348

Please sign in to comment.