[styleguide] Fix error when styleguide is shown… #648
Merged
+171
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…before certain grammars are loaded.
Fixes #560.
Identify the Bug
When a user reloads a window that happens to have a
styleguide
tab open, it’ll usually throw an error during the load process. This happens becausestyleguide
shows code samples for UI elements, and it expects to be initialized in an environment where the grammars it needs for syntax highlighting are already present.In investigating this, I found a few other bugs and inconsistencies in
GrammarRegistry
that needed to be fixed.Description of the Change
atom.grammars.addInjectionPoint
allows you to add injections for any grammar, even grammars that haven’t been seen or activated yet. It does this by creating a stub object at the given scope name. But someGrammarRegistry
APIs weren’t aware that this was a stub, so they were trying to do things with it. Added some guards to fix this.atom.grammars.grammarForId
should never return one of these stubs; if the grammar forsource.foo
hasn’t yet been added,atom.grammars.grammarForId('source.foo')
should always beundefined
. Added a spec.GrammarRegistry#onDidAddGrammar
andGrammarRegistry#onDidUpdateGrammar
callbacks only ever worked for TM-style grammars. I added parallel paths for Tree-sitter grammars, both legacy and modern. The former callback is triggered when a grammar is first added, and the latter callback is triggered when something adds an injection point for a grammar that has already loaded. Added specs for these.CodeBlock
needs to do here. But we don’t want it to wait for the first one to come along; we want to wait for the first one that matches the user’s settings around whether or not to use Tree-sitter. But we also don’t want it to wait forever for an ideal candidate!I had envisioned a method called
GrammarRegistry#whenGrammarAdded
that would resolve a promise when a grammar of a given scope name was added. But how should it behave?I think option 3 might be promising. But if I add this to
GrammarRegistry
, we have to support it, and I don’t want to support something half-baked that I dreamt up just to fix an issue withstyleguide
.So I put a simplified version of
whenGrammarAdded
intostyleguide
’sCodeBlock
class. It waits for the firsttext.html.basic
grammar to be loaded, then waits another second, then tellsGrammarRegistry
to pick the best grammar that has already been loaded (taking language mode preferences into account).Alternate Designs
Described them above.
Possible Drawbacks
The changes I made to
GrammarRegistry
could possibly have implications for other parts of the core. I verified all theGrammarRegistry
specs are passing, but I’ll wait for CI to do the whole suite and tell me if anything else has failed.I’m of the opinion that anything I touched in
GrammarRegistry
involved fixing behaviors that were flat-out wrong, so any code that would break as a result of those fixes is itself broken.Verification Process
Added a new spec to
styleguide
. Now it has two!And, as mentioned above, I’ll find out whether anything new in the main test suite is broken.
Release Notes
Fixed a syntax highlighting issue inside the
styleguide
package