tree-sitter: Update parent links on reused injection layers (#10978)

When parsing injections, we skip adding a new layer if there is an
existing layer covering the same range. When doing so we did not update
the parent layer ID, so some layers could have `parent` layer IDs that
pointed to a layer that no longer existed in the `layers` HopSlotMap
which could cause a panic when using `A-o`.

To fix this we update the `parent` pointer for both newly created
injection layers and reused ones.
This commit is contained in:
Michael Davis 2024-06-22 20:06:15 -05:00 committed by GitHub
parent b55cb3aa11
commit 44e113cb76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1363,13 +1363,14 @@ impl Syntax {
let depth = layer.depth + 1; let depth = layer.depth + 1;
// TODO: can't inline this since matches borrows self.layers // TODO: can't inline this since matches borrows self.layers
for (config, ranges) in injections { for (config, ranges) in injections {
let parent = Some(layer_id);
let new_layer = LanguageLayer { let new_layer = LanguageLayer {
tree: None, tree: None,
config, config,
depth, depth,
ranges, ranges,
flags: LayerUpdateFlags::empty(), flags: LayerUpdateFlags::empty(),
parent: Some(layer_id), parent: None,
}; };
// Find an identical existing layer // Find an identical existing layer
@ -1381,6 +1382,7 @@ impl Syntax {
// ...or insert a new one. // ...or insert a new one.
let layer_id = layer.unwrap_or_else(|| self.layers.insert(new_layer)); let layer_id = layer.unwrap_or_else(|| self.layers.insert(new_layer));
self.layers[layer_id].parent = parent;
queue.push_back(layer_id); queue.push_back(layer_id);
} }