Fix Cannot read properties of undefined (reading 'languageScopeId') - #1640
mauricioszabo wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Beg forgiveness, but let's fix this another way:
The problem is how the NullLayerHighlightIterator behaves. Certain pieces of code expect a certain interface on a HighlightIterator, but NullLayerHighlightIterator doesn't implement some of that interface.
class NullLayerHighlightIterator {
constructor(languageLayer) {
this.languageLayer = languageLayer;
this.depth = languageLayer.depth;
this.coverShallowerScopes = false;
}
seek() {
return [false, new OpenScopeMap];
}
compare() {
return 1;
}
moveToSuccessor() {}
getPosition() {
return Point.INFINITY;
}
getOpenScopeIds() {
return [];
}
getCloseScopeIds() {
return [];
}
}That seems to give it the things it needs: depth, coverShallowerScopes, and languageLayer.languageScopeId.
Also, when it's instantiated (around line 3942), it should take this instead of zero arguments.
I think that gets us what we want without using openScopes.size as a proxy.
|
@savetheclocktower applied the fix, can you check again? |
savetheclocktower
left a comment
There was a problem hiding this comment.
Gonna approve because this makes the code better, but I have one more (smaller) fix to request:
First:
let ranges = iterator.languageLayer.getCurrentRanges();This could now return null, so changing it to
let ranges = iterator.languageLayer.getCurrentRanges() ?? [];would be sounder. This is unlikely to occur very often, but worth closing it off altogether.
Claude also offers the following feedback about the new prepareTSLanguageMode helper, none of which I caught myself:
- Grammar subscriptions now leak. The
afterEachdisposes the describe-levelgrammar; several refactored tests used to assign it (grammar = new WASMTreeSitterGrammar(...), noconst). The helper createsjsGrammar/regexGrammarlocally and never hands them back, so nothing disposes them. Worth assigning to the outergrammaror returning them.- Every helper call now installs a
regex_patterninjection point withcoverShallowerScopes: trueand registers the regex grammar — including tests that previously had neither. Most notablyreports results correctly when scope ranges have been adjusteduseslet foo = /patt?ern/;and asserts thekeywordscope is found, while its sibling test asserts the same scope is not found precisely because ofcoverShallowerScopes. The only thing keeping them apart now is that the first one omitsawait wait(100), so the injection hasn't populated when the assertions run. CI is green onTests (ubuntu-latest), so it works today, but that test's outcome is now timing-dependent in a way it wasn't. I'd make the injection opt-in (or at least defaultcoverShallowerScopestofalse).regexQuery = ''blanks the query rather than emptying it.setQueryForTestassignsthis[queryType] = contents, andLanguageLayergates onif (grammar[queryType])— so''means the regex grammar ends up with nohighlightsQueryat all (its real one from the cson is clobbered too). Probably fine, but'; (placeholder)'would be more honest about intent.- Minor: the new regression test's
expect(...).not.toThrow()passes trivially if the injection layer isn't built yet. The twoseek()assertions above it help, but asserting the actual scopes fromscopeDescriptorForPositionwould make it harder to silently stop testing the bug.
So please fix the thing I mentioned above, and perhaps tweak the helper a bit… but there's no point in rejecting when I did so already and caught new stuff on the second. After that change, as long as CI is green, you can merge this.
Under vim-mode-plus, sometimes the plug-in wants to highlight searches and elements in the screen - but there are moments that it can try to destroy a marker "mid-parsing". The issue - it calls
updateSyncon the text editor component, and it might cause a crash when there are no "open scopes" because we're falling back to the null layer.This PR fixes this very hard to find issue, and it also adds some refactorings to the test (essentially, building a "Tree Sitter Grammar" with injections was using too many lines - I extracted that to a helper method).