From 9006d05d4f04f17b9bf96ca9a7d5fa8fdd97afdd Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 29 Nov 2017 10:03:00 -0800 Subject: [PATCH 1/2] Remove GrammarRegistry's inheritance from first-mate's registry --- src/grammar-registry.js | 117 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 8 deletions(-) diff --git a/src/grammar-registry.js b/src/grammar-registry.js index e6667985b..b3745af9d 100644 --- a/src/grammar-registry.js +++ b/src/grammar-registry.js @@ -14,23 +14,24 @@ const PATH_SPLIT_REGEX = new RegExp('[/.]') // // An instance of this class is always available as the `atom.grammars` global. module.exports = -class GrammarRegistry extends FirstMate.GrammarRegistry { +class GrammarRegistry { constructor ({config} = {}) { - super({maxTokensPerLine: 100, maxLineLength: 1000}) this.config = config this.subscriptions = new CompositeDisposable() + this.textmateRegistry = new FirstMate.GrammarRegistry({maxTokensPerLine: 100, maxLineLength: 1000}) + this.clear() } clear () { - super.clear() + this.textmateRegistry.clear() if (this.subscriptions) this.subscriptions.dispose() this.subscriptions = new CompositeDisposable() this.languageOverridesByBufferId = new Map() this.grammarScoresByBuffer = new Map() const grammarAddedOrUpdated = this.grammarAddedOrUpdated.bind(this) - this.onDidAddGrammar(grammarAddedOrUpdated) - this.onDidUpdateGrammar(grammarAddedOrUpdated) + this.textmateRegistry.onDidAddGrammar(grammarAddedOrUpdated) + this.textmateRegistry.onDidUpdateGrammar(grammarAddedOrUpdated) } serialize () { @@ -111,12 +112,12 @@ class GrammarRegistry extends FirstMate.GrammarRegistry { let grammar = null if (languageId != null) { - grammar = this.grammarForScopeName(languageId) + grammar = this.textmateRegistry.grammarForScopeName(languageId) if (!grammar) return false this.languageOverridesByBufferId.set(buffer.id, languageId) } else { this.languageOverridesByBufferId.set(buffer.id, null) - grammar = this.nullGrammar + grammar = this.textmateRegistry.nullGrammar } this.grammarScoresByBuffer.set(buffer, null) @@ -164,7 +165,7 @@ class GrammarRegistry extends FirstMate.GrammarRegistry { selectGrammarWithScore (filePath, fileContents) { let bestMatch = null let highestScore = -Infinity - for (let grammar of this.grammars) { + for (let grammar of this.textmateRegistry.grammars) { const score = this.getGrammarScore(grammar, filePath, fileContents) if ((score > highestScore) || (bestMatch == null)) { bestMatch = grammar @@ -312,4 +313,104 @@ class GrammarRegistry extends FirstMate.GrammarRegistry { } }) } + + // Extended: Invoke the given callback when a grammar is added to the registry. + // + // * `callback` {Function} to call when a grammar is added. + // * `grammar` {Grammar} that was added. + // + // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onDidAddGrammar (callback) { + return this.textmateRegistry.onDidAddGrammar(callback) + } + + // Extended: Invoke the given callback when a grammar is updated due to a grammar + // it depends on being added or removed from the registry. + // + // * `callback` {Function} to call when a grammar is updated. + // * `grammar` {Grammar} that was updated. + // + // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onDidUpdateGrammar (callback) { + return this.textmateRegistry.onDidUpdateGrammar(callback) + } + + get nullGrammar () { + return this.textmateRegistry.nullGrammar + } + + decodeTokens () { + return this.textmateRegistry.decodeTokens.apply(this.textmateRegistry, arguments) + } + + grammarForScopeName (scopeName) { + return this.textmateRegistry.grammarForScopeName(scopeName) + } + + addGrammar (grammar) { + return this.textmateRegistry.addGrammar(grammar) + } + + removeGrammar (grammar) { + return this.textmateRegistry.removeGrammar(grammar) + } + + removeGrammarForScopeName (scopeName) { + return this.textmateRegistry.removeGrammarForScopeName(scopeName) + } + + // Extended: Read a grammar asynchronously and add it to the registry. + // + // * `grammarPath` A {String} absolute file path to a grammar file. + // * `callback` A {Function} to call when loaded with the following arguments: + // * `error` An {Error}, may be null. + // * `grammar` A {Grammar} or null if an error occured. + loadGrammar (grammarPath, callback) { + return this.textmateRegistry.loadGrammar(grammarPath, callback) + } + + // Extended: Read a grammar synchronously and add it to this registry. + // + // * `grammarPath` A {String} absolute file path to a grammar file. + // + // Returns a {Grammar}. + loadGrammarSync (grammarPath) { + return this.textmateRegistry.loadGrammarSync(grammarPath) + } + + // Extended: Read a grammar asynchronously but don't add it to the registry. + // + // * `grammarPath` A {String} absolute file path to a grammar file. + // * `callback` A {Function} to call when read with the following arguments: + // * `error` An {Error}, may be null. + // * `grammar` A {Grammar} or null if an error occured. + // + // Returns undefined. + readGrammar (grammarPath, callback) { + return this.textmateRegistry.readGrammar(grammarPath, callback) + } + + // Extended: Read a grammar synchronously but don't add it to the registry. + // + // * `grammarPath` A {String} absolute file path to a grammar file. + // + // Returns a {Grammar}. + readGrammarSync (grammarPath) { + return this.textmateRegistry.readGrammarSync(grammarPath) + } + + createGrammar (grammarPath, params) { + return this.textmateRegistry.createGrammar(grammarPath, params) + } + + // Extended: Get all the grammars in this registry. + // + // Returns a non-empty {Array} of {Grammar} instances. + getGrammars () { + return this.textmateRegistry.getGrammars() + } + + scopeForId (id) { + return this.textmateRegistry.scopeForId(id) + } } From 515ed5602e77af4aef6fe923592f79b79ebf05d1 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 29 Nov 2017 11:23:56 -0800 Subject: [PATCH 2/2] Add a shim for the GrammarRegistry.grammars property --- src/grammar-registry.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/grammar-registry.js b/src/grammar-registry.js index b3745af9d..db86958fd 100644 --- a/src/grammar-registry.js +++ b/src/grammar-registry.js @@ -339,6 +339,10 @@ class GrammarRegistry { return this.textmateRegistry.nullGrammar } + get grammars () { + return this.textmateRegistry.grammars + } + decodeTokens () { return this.textmateRegistry.decodeTokens.apply(this.textmateRegistry, arguments) }