diff options
author | Michael Davis | 2022-11-09 00:58:27 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2022-11-09 03:41:07 +0000 |
commit | 3e84434c695379dd2b56415c5cec46990488d007 (patch) | |
tree | 2e32881552c64061d1414b01ea5192f97a94db1e /helix-core | |
parent | 260ae3a0f4b94eb0af3477c3e7b94b7a0033431c (diff) |
Fix panic from indenting on tree with errors
`deepest_preceding` is known to be a descendant of `node`. Repeated
calls of `Node::parent` _should_ eventually turn `deepest_preceding`
into `node`, but when the node is errored (the tree contains a syntax
error), `Node::parent` returns None.
In the typescript case:
if(true) &&true
// ^ press enter here
The tree is:
(program [0, 0] - [1, 0]
(if_statement [0, 0] - [0, 15]
condition: (parenthesized_expression [0, 2] - [0, 8]
(true [0, 3] - [0, 7]))
consequence: (expression_statement [0, 8] - [0, 15]
(binary_expression [0, 8] - [0, 15]
left: (identifier [0, 8] - [0, 8])
right: (true [0, 11] - [0, 15])))))
`node` is the `program` node and `deepest_preceding` is the
`binary_expression`. The tree is errored on the `binary_expression`
node with `(MISSING identifier [0, 8] - [0, 8])`.
In the C++ case:
; <<
// press enter after the ';'
The tree is:
(translation_unit [0, 0] - [1, 0]
(expression_statement [0, 0] - [0, 1])
(ERROR [0, 1] - [0, 4]
(identifier [0, 1] - [0, 1])))
`node` is the `translation_unit` and `deepest_preceding` is the `ERROR`
node.
In both cases, `Node::parent` on the errored node returns None.
Diffstat (limited to 'helix-core')
-rw-r--r-- | helix-core/src/indent.rs | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 1c813c9a..d6aa5edb 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -511,8 +511,12 @@ fn extend_nodes<'a>( *node = deepest_preceding; break; } - // This parent always exists since node is an ancestor of deepest_preceding - deepest_preceding = deepest_preceding.parent().unwrap(); + // If the tree contains a syntax error, `deepest_preceding` may not + // have a parent despite being a descendant of `node`. + deepest_preceding = match deepest_preceding.parent() { + Some(parent) => parent, + None => return, + } } } |