From 3d796df4019d73af6baab269e3aed9d8e2c8cd3d Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 27 Jul 2016 14:09:17 -0700 Subject: [PATCH] Stop using GrammarRegistry in TextEditor --- spec/text-editor-registry-spec.js | 3 --- spec/text-editor-spec.coffee | 1 - spec/tokenized-buffer-iterator-spec.js | 32 +++++++++++++------------- src/text-editor.coffee | 8 +++---- src/token-iterator.coffee | 7 +++--- src/tokenized-buffer-iterator.coffee | 12 +++++----- src/tokenized-buffer.coffee | 13 +++-------- src/workspace.coffee | 4 +--- 8 files changed, 32 insertions(+), 48 deletions(-) diff --git a/spec/text-editor-registry-spec.js b/spec/text-editor-registry-spec.js index 873b17eea..6d1aae2be 100644 --- a/spec/text-editor-registry-spec.js +++ b/spec/text-editor-registry-spec.js @@ -17,7 +17,6 @@ describe('TextEditorRegistry', function () { editor = new TextEditor({ config: atom.config, clipboard: atom.clipboard, - grammarRegistry: atom.grammars }) }) @@ -113,7 +112,6 @@ describe('TextEditorRegistry', function () { const editor2 = new TextEditor({ config: atom.config, clipboard: atom.clipboard, - grammarRegistry: atom.grammars }) editor2.setGrammar(atom.grammars.selectGrammar('test.js')) @@ -449,7 +447,6 @@ describe('TextEditorRegistry', function () { const editor2 = new TextEditor({ config: atom.config, clipboard: atom.clipboard, - grammarRegistry: atom.grammars }) await atom.packages.activatePackage('language-c') diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 9c7f7390b..d0a651dc7 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -5737,7 +5737,6 @@ describe "TextEditor", -> editor = new TextEditor({ grammar: atom.grammars.grammarForScopeName('source.coffee') clipboard: atom.clipboard - grammarRegistry: atom.grammars }) expect(editor.getGrammar().name).toBe 'CoffeeScript' diff --git a/spec/tokenized-buffer-iterator-spec.js b/spec/tokenized-buffer-iterator-spec.js index 8d0e458f4..587a58f8f 100644 --- a/spec/tokenized-buffer-iterator-spec.js +++ b/spec/tokenized-buffer-iterator-spec.js @@ -12,16 +12,16 @@ describe('TokenizedBufferIterator', () => { text: '', openScopes: [] } + }, + + grammar: { + scopeForId () { + return 'foo' + } } } - const grammarRegistry = { - scopeForId () { - return 'foo' - } - } - - const iterator = new TokenizedBufferIterator(tokenizedBuffer, grammarRegistry) + const iterator = new TokenizedBufferIterator(tokenizedBuffer) iterator.seek(Point(0, 0)) expect(iterator.getPosition()).toEqual(Point(0, 0)) @@ -60,20 +60,20 @@ describe('TokenizedBufferIterator', () => { openScopes: [-1] } } - } - } + }, - const grammarRegistry = { - scopeForId (id) { - if (id === -2 || id === -1) { - return 'foo' - } else if (id === -3) { - return 'qux' + grammar: { + scopeForId (id) { + if (id === -2 || id === -1) { + return 'foo' + } else if (id === -3) { + return 'qux' + } } } } - const iterator = new TokenizedBufferIterator(tokenizedBuffer, grammarRegistry) + const iterator = new TokenizedBufferIterator(tokenizedBuffer) iterator.seek(Point(0, 0)) expect(iterator.getPosition()).toEqual(Point(0, 0)) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index c75548433..1330fdf2e 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -110,7 +110,6 @@ class TextEditor extends Model state.displayLayer = state.buffer.getDisplayLayer(state.displayLayerId) ? state.buffer.addDisplayLayer() state.selectionsMarkerLayer = state.displayLayer.getMarkerLayer(state.selectionsMarkerLayerId) state.clipboard = atomEnvironment.clipboard - state.grammarRegistry = atomEnvironment.grammars state.assert = atomEnvironment.assert.bind(atomEnvironment) editor = new this(state) if state.registered @@ -124,13 +123,12 @@ class TextEditor extends Model { @softTabs, @firstVisibleScreenRow, @firstVisibleScreenColumn, initialLine, initialColumn, @tabLength, @softWrapped, @decorationManager, @selectionsMarkerLayer, @buffer, suppressCursorCreation, - @mini, @placeholderText, lineNumberGutterVisible, @largeFileMode, @clipboard, @grammarRegistry, + @mini, @placeholderText, lineNumberGutterVisible, @largeFileMode, @clipboard, @assert, grammar, @showInvisibles, @autoHeight, @scrollPastEnd, @editorWidthInChars, @tokenizedBuffer, @ignoreInvisibles, @displayLayer } = params throw new Error("Must pass a clipboard parameter when constructing TextEditors") unless @clipboard? - throw new Error("Must pass a grammarRegistry parameter when constructing TextEditors") unless @grammarRegistry? @assert ?= (condition) -> condition @firstVisibleScreenRow ?= 0 @@ -156,7 +154,7 @@ class TextEditor extends Model @buffer ?= new TextBuffer @tokenizedBuffer ?= new TokenizedBuffer({ - @tabLength, @buffer, @largeFileMode, @grammarRegistry, @assert + @tabLength, @buffer, @largeFileMode, @assert }) @displayLayer ?= @buffer.addDisplayLayer() @displayLayer.setTextDecorationLayer(@tokenizedBuffer) @@ -560,7 +558,7 @@ class TextEditor extends Model @buffer, selectionsMarkerLayer, @tabLength, softTabs, suppressCursorCreation: true, @firstVisibleScreenRow, @firstVisibleScreenColumn, - @clipboard, @grammarRegistry, @assert, displayLayer + @clipboard, @assert, displayLayer }) newEditor diff --git a/src/token-iterator.coffee b/src/token-iterator.coffee index f9af1e4ca..f836d33d4 100644 --- a/src/token-iterator.coffee +++ b/src/token-iterator.coffee @@ -1,13 +1,12 @@ module.exports = class TokenIterator - constructor: ({@grammarRegistry}, line) -> - @reset(line) if line? + constructor: (@tokenizedBuffer) -> reset: (@line) -> @index = null @startColumn = 0 @endColumn = 0 - @scopes = @line.openScopes.map (id) => @grammarRegistry.scopeForId(id) + @scopes = @line.openScopes.map (id) => @tokenizedBuffer.grammar.scopeForId(id) @scopeStarts = @scopes.slice() @scopeEnds = [] this @@ -26,7 +25,7 @@ class TokenIterator while @index < tags.length tag = tags[@index] if tag < 0 - scope = @grammarRegistry.scopeForId(tag) + scope = @tokenizedBuffer.grammar.scopeForId(tag) if tag % 2 is 0 if @scopeStarts[@scopeStarts.length - 1] is scope @scopeStarts.pop() diff --git a/src/tokenized-buffer-iterator.coffee b/src/tokenized-buffer-iterator.coffee index 780156e42..e6804a78f 100644 --- a/src/tokenized-buffer-iterator.coffee +++ b/src/tokenized-buffer-iterator.coffee @@ -2,7 +2,7 @@ module.exports = class TokenizedBufferIterator - constructor: (@tokenizedBuffer, @grammarRegistry) -> + constructor: (@tokenizedBuffer) -> @openTags = null @closeTags = null @containingTags = null @@ -16,7 +16,7 @@ class TokenizedBufferIterator @currentTags = currentLine.tags @currentLineOpenTags = currentLine.openScopes @currentLineLength = currentLine.text.length - @containingTags = @currentLineOpenTags.map (id) => @grammarRegistry.scopeForId(id) + @containingTags = @currentLineOpenTags.map (id) => @tokenizedBuffer.grammar.scopeForId(id) currentColumn = 0 for tag, index in @currentTags if tag >= 0 @@ -28,7 +28,7 @@ class TokenizedBufferIterator @containingTags.pop() while @closeTags.shift() @containingTags.push(tag) while tag = @openTags.shift() else - scopeName = @grammarRegistry.scopeForId(tag) + scopeName = @tokenizedBuffer.grammar.scopeForId(tag) if tag % 2 is 0 if @openTags.length > 0 @tagIndex = index @@ -55,7 +55,7 @@ class TokenizedBufferIterator else if @shouldMoveToNextLine @moveToNextLine() - @openTags = @currentLineOpenTags.map (id) => @grammarRegistry.scopeForId(id) + @openTags = @currentLineOpenTags.map (id) => @tokenizedBuffer.grammar.scopeForId(id) @shouldMoveToNextLine = false else if @nextLineHasMismatchedContainingTags() @closeTags = @containingTags.slice().reverse() @@ -71,7 +71,7 @@ class TokenizedBufferIterator else @position = Point(@position.row, Math.min(@currentLineLength, @position.column + @currentTags[@tagIndex])) else - scopeName = @grammarRegistry.scopeForId(tag) + scopeName = @tokenizedBuffer.grammar.scopeForId(tag) if tag % 2 is 0 if @openTags.length > 0 break @@ -101,7 +101,7 @@ class TokenizedBufferIterator return true if line.openScopes.length isnt @containingTags.length for i in [0...@containingTags.length] by 1 - if @containingTags[i] isnt @grammarRegistry.scopeForId(line.openScopes[i]) + if @containingTags[i] isnt @tokenizedBuffer.grammar.scopeForId(line.openScopes[i]) return true false else diff --git a/src/tokenized-buffer.coffee b/src/tokenized-buffer.coffee index abac2fc20..90666b783 100644 --- a/src/tokenized-buffer.coffee +++ b/src/tokenized-buffer.coffee @@ -26,31 +26,26 @@ class TokenizedBuffer extends Model else # TODO: remove this fallback after everyone transitions to the latest version. state.buffer = atomEnvironment.project.bufferForPathSync(state.bufferPath) - state.grammarRegistry = atomEnvironment.grammars state.assert = atomEnvironment.assert new this(state) constructor: (params) -> - { - @buffer, @tabLength, @largeFileMode, - @grammarRegistry, @assert, grammarScopeName - } = params + {@buffer, @tabLength, @largeFileMode, @assert} = params @emitter = new Emitter @disposables = new CompositeDisposable - @tokenIterator = new TokenIterator({@grammarRegistry}) + @tokenIterator = new TokenIterator(this) @disposables.add @buffer.preemptDidChange (e) => @handleBufferChange(e) @rootScopeDescriptor = new ScopeDescriptor(scopes: ['text.plain']) @retokenizeLines() - @grammarToRestoreScopeName = grammarScopeName destroyed: -> @disposables.dispose() buildIterator: -> - new TokenizedBufferIterator(this, @grammarRegistry) + new TokenizedBufferIterator(this) getInvalidatedRanges: -> if @invalidatedRange? @@ -89,8 +84,6 @@ class TokenizedBuffer extends Model @grammar = grammar @rootScopeDescriptor = new ScopeDescriptor(scopes: [@grammar.scopeName]) - @grammarToRestoreScopeName = null - @grammarUpdateDisposable?.dispose() @grammarUpdateDisposable = @grammar.onDidUpdate => @retokenizeLines() @disposables.add(@grammarUpdateDisposable) diff --git a/src/workspace.coffee b/src/workspace.coffee index ed3da8a51..865027893 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -572,9 +572,7 @@ class Workspace extends Model # # Returns a {TextEditor}. buildTextEditor: (params) -> - params = Object.assign({ - @config, @clipboard, @grammarRegistry, @assert - }, params) + params = Object.assign({@clipboard, @assert}, params) editor = new TextEditor(params) @textEditorRegistry.maintainConfig(editor) @textEditorRegistry.maintainGrammar(editor)