LanguageMode switches to a better-matching grammar when it is added

This commit is contained in:
Corey Johnson & Nathan Sobo 2013-04-03 18:12:26 -06:00
parent 16b53d3183
commit 1b56cfb270
4 changed files with 13 additions and 9 deletions

View File

@ -2060,18 +2060,18 @@ describe "EditSession", ->
editSession.buffer.reload()
expect(editSession.getCursorScreenPosition()).toEqual [0,1]
describe "when the 'grammars-loaded' event is triggered on the syntax global", ->
it "reloads the edit session's grammar and re-tokenizes the buffer if it changes", ->
describe "when a better-matched grammar is added to syntax", ->
it "switches to the better-matched grammar and re-tokenizes the buffer", ->
editSession.destroy()
jsGrammar = syntax.selectGrammar('a.js')
grammarToReturn = syntax.nullGrammar
spyOn(syntax, 'selectGrammar').andCallFake -> grammarToReturn
syntax.removeGrammar(jsGrammar)
editSession = project.buildEditSession('sample.js', autoIndent: false)
expect(editSession.getGrammar()).toBe syntax.nullGrammar
expect(editSession.lineForScreenRow(0).tokens.length).toBe 1
grammarToReturn = jsGrammar
syntax.trigger 'grammars-loaded'
syntax.addGrammar(jsGrammar)
expect(editSession.getGrammar()).toBe jsGrammar
expect(editSession.lineForScreenRow(0).tokens.length).toBeGreaterThan 1
describe "auto-indent", ->

View File

@ -9,15 +9,19 @@ class LanguageMode
buffer = null
grammar = null
editSession = null
currentGrammarScore: null
constructor: (@editSession) ->
@buffer = @editSession.buffer
@reloadGrammar()
syntax.on 'grammars-loaded', => @reloadGrammar()
syntax.on 'grammar-added', (grammar) =>
newScore = grammar.getScore(@buffer.getPath(), @buffer.getText())
@setGrammar(grammar, newScore) if newScore > @currentGrammarScore
setGrammar: (grammar) ->
setGrammar: (grammar, score) ->
return if grammar is @grammar
@grammar = grammar
@currentGrammarScore = score ? grammar.getScore(@buffer.getPath(), @buffer.getText())
@trigger 'grammar-changed', grammar
reloadGrammar: ->

View File

@ -29,6 +29,7 @@ class Syntax
addGrammar: (grammar) ->
@grammars.push(grammar)
@grammarsByScopeName[grammar.scopeName] = grammar
@trigger 'grammar-added', grammar
removeGrammar: (grammar) ->
_.remove(@grammars, grammar)

View File

@ -183,7 +183,6 @@ describe "StatusBar", ->
beforeEach ->
atom.activatePackage('text.tmbundle', sync: true)
atom.activatePackage('javascript.tmbundle', sync: true)
syntax.trigger 'grammars-loaded'
it "displays the name of the current grammar", ->
expect(statusBar.find('.grammar-name').text()).toBe 'JavaScript'