Stop using GrammarRegistry in TextEditor

This commit is contained in:
Max Brunsfeld
2016-07-27 14:09:17 -07:00
parent e6c83521c9
commit 3d796df401
8 changed files with 32 additions and 48 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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

View File

@@ -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)

View File

@@ -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)