diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 3337d526b..06a0a047c 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -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", -> diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index ff1dea641..a16a498d8 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -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: -> diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index 9f00429fb..3aebccbb8 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -29,6 +29,7 @@ class Syntax addGrammar: (grammar) -> @grammars.push(grammar) @grammarsByScopeName[grammar.scopeName] = grammar + @trigger 'grammar-added', grammar removeGrammar: (grammar) -> _.remove(@grammars, grammar) diff --git a/src/packages/status-bar/spec/status-bar-spec.coffee b/src/packages/status-bar/spec/status-bar-spec.coffee index d9bf04274..ead338eae 100644 --- a/src/packages/status-bar/spec/status-bar-spec.coffee +++ b/src/packages/status-bar/spec/status-bar-spec.coffee @@ -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'