mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Localize grammar reloading / setting to LanguageMode
Previously, logic associated with swapping grammars was a bit scattered. Now grammar reloading / assignment methods delegate to LanguageMode directly, and it emits a 'grammar-changed' event when the grammar changes. Now EditSession and TokenizedBuffer listen for this event and perform necessary actions for grammar change.
This commit is contained in:
committed by
Corey Johnson & Nathan Sobo
parent
cf357376b3
commit
ed1c5d3417
@@ -57,7 +57,7 @@ class EditSession
|
||||
@subscribe @displayBuffer, "changed", (e) =>
|
||||
@trigger 'screen-lines-changed', e
|
||||
|
||||
@subscribe syntax, 'grammars-loaded', => @reloadGrammar()
|
||||
@languageMode.on 'grammar-changed', => @handleGrammarChange()
|
||||
|
||||
getViewClass: ->
|
||||
require 'editor'
|
||||
@@ -846,17 +846,14 @@ class EditSession
|
||||
getGrammar: -> @languageMode.grammar
|
||||
|
||||
setGrammar: (grammar) ->
|
||||
@languageMode.grammar = grammar
|
||||
@handleGrammarChange()
|
||||
@languageMode.setGrammar(grammar)
|
||||
|
||||
reloadGrammar: ->
|
||||
@handleGrammarChange() if @languageMode.reloadGrammar()
|
||||
@languageMode.reloadGrammar()
|
||||
|
||||
handleGrammarChange: ->
|
||||
@unfoldAll()
|
||||
@displayBuffer.tokenizedBuffer.resetScreenLines()
|
||||
@trigger 'grammar-changed'
|
||||
true
|
||||
|
||||
getDebugSnapshot: ->
|
||||
[
|
||||
|
||||
@@ -1147,16 +1147,9 @@ class Editor extends View
|
||||
setGrammar: (grammar) ->
|
||||
throw new Error("Only mini-editors can explicity set their grammar") unless @mini
|
||||
@activeEditSession.setGrammar(grammar)
|
||||
@handleGrammarChange()
|
||||
|
||||
reloadGrammar: ->
|
||||
grammarChanged = @activeEditSession.reloadGrammar()
|
||||
@handleGrammarChange() if grammarChanged
|
||||
grammarChanged
|
||||
|
||||
handleGrammarChange: ->
|
||||
@clearRenderedLines()
|
||||
@updateDisplay()
|
||||
@activeEditSession.reloadGrammar()
|
||||
|
||||
bindToKeyedEvent: (key, event, callback) ->
|
||||
binding = {}
|
||||
|
||||
@@ -2,6 +2,7 @@ Range = require 'range'
|
||||
_ = require 'underscore'
|
||||
require 'underscore-extensions'
|
||||
{OnigRegExp} = require 'oniguruma'
|
||||
EventEmitter = require 'event-emitter'
|
||||
|
||||
module.exports =
|
||||
class LanguageMode
|
||||
@@ -12,14 +13,18 @@ class LanguageMode
|
||||
constructor: (@editSession) ->
|
||||
@buffer = @editSession.buffer
|
||||
@reloadGrammar()
|
||||
syntax.on 'grammars-loaded', => @reloadGrammar()
|
||||
|
||||
setGrammar: (grammar) ->
|
||||
return if grammar is @grammar
|
||||
@grammar = grammar
|
||||
@trigger 'grammar-changed', grammar
|
||||
|
||||
reloadGrammar: ->
|
||||
path = @buffer.getPath()
|
||||
pathContents = @buffer.cachedDiskContents
|
||||
previousGrammar = @grammar
|
||||
@grammar = syntax.selectGrammar(path, pathContents)
|
||||
throw new Error("No grammar found for path: #{path}") unless @grammar
|
||||
previousGrammar isnt @grammar
|
||||
if grammar = syntax.selectGrammar(@buffer.getPath(), @buffer.getText())
|
||||
@setGrammar(grammar)
|
||||
else
|
||||
throw new Error("No grammar found for path: #{path}")
|
||||
|
||||
toggleLineCommentsForBufferRows: (start, end) ->
|
||||
scopes = @editSession.scopesForBufferPosition([start, 0])
|
||||
@@ -158,3 +163,5 @@ class LanguageMode
|
||||
foldEndRegexForScopes: (scopes) ->
|
||||
if foldEndPattern = syntax.getProperty(scopes, 'editor.foldEndPattern')
|
||||
new OnigRegExp(foldEndPattern)
|
||||
|
||||
_.extend LanguageMode.prototype, EventEmitter
|
||||
|
||||
@@ -23,6 +23,7 @@ class TokenizedBuffer
|
||||
@id = @constructor.idCounter++
|
||||
@resetScreenLines()
|
||||
@buffer.on "changed.tokenized-buffer#{@id}", (e) => @handleBufferChange(e)
|
||||
@languageMode.on 'grammar-changed', => @resetScreenLines()
|
||||
|
||||
resetScreenLines: ->
|
||||
@screenLines = @buildPlaceholderScreenLinesForRows(0, @buffer.getLastRow())
|
||||
|
||||
Reference in New Issue
Block a user