Support unsetting explicit grammar for path

This commit is contained in:
Kevin Sawicki
2013-01-08 12:57:58 -08:00
parent 3b47c26b4d
commit 321d424c44
2 changed files with 57 additions and 2 deletions

View File

@@ -0,0 +1,49 @@
RootView = require 'root-view'
GrammarView = require 'grammar-view'
_ = require 'underscore'
describe "GrammarView", ->
[editor, rootView, textGrammar, jsGrammar] = []
beforeEach ->
path = require.resolve('fixtures/sample.js')
syntax.removeGrammarForPath(path)
rootView = new RootView(path)
editor = rootView.getActiveEditor()
rootView.attachToDom()
textGrammar = _.find syntax.grammars, (grammar) -> grammar.name is 'Plain Text'
expect(textGrammar).toBeTruthy()
jsGrammar = _.find syntax.grammars, (grammar) -> grammar.name is 'JavaScript'
expect(jsGrammar).toBeTruthy()
expect(editor.getGrammar()).toBe jsGrammar
afterEach ->
rootView.deactivate()
describe "when editor:select-grammar is toggled", ->
it "displays a list of all the available grammars", ->
editor.trigger 'editor:select-grammar'
grammarView = rootView.find('.grammar-view').view()
expect(grammarView).toExist()
grammars = syntax.grammars
expect(grammarView.list.children('li').length).toBe grammars.length + 1
expect(grammarView.list.children('li:first').text()).toBe 'Auto Detect'
describe "when a grammar is selected", ->
it "sets the new grammar on the editor", ->
editor.trigger 'editor:select-grammar'
grammarView = rootView.find('.grammar-view').view()
grammarView.confirmed(textGrammar)
expect(editor.getGrammar()).toBe textGrammar
describe "when auto-detect is selected", ->
it "restores the auto-detected grammar on the editor", ->
editor.trigger 'editor:select-grammar'
grammarView = rootView.find('.grammar-view').view()
grammarView.confirmed(textGrammar)
expect(editor.getGrammar()).toBe textGrammar
editor.trigger 'editor:select-grammar'
grammarView = rootView.find('.grammar-view').view()
grammarView.confirmed(grammarView.array[0])
expect(editor.getGrammar()).toBe jsGrammar

View File

@@ -11,6 +11,7 @@ class GrammarView extends SelectList
initialize: (@editor) ->
@currentGrammar = @editor.getGrammar()
@path = @editor.getPath()
@autoDetect = name: 'Auto Detect'
requireStylesheet 'grammar-view.css'
@command 'editor:select-grammar', =>
@cancel()
@@ -30,7 +31,9 @@ class GrammarView extends SelectList
@li grammar.name, class: grammarClass
populate: ->
@setArray(syntax.grammars)
grammars = new Array(syntax.grammars...)
grammars.unshift(@autoDetect)
@setArray(grammars)
cancelled: ->
@miniEditor.setText('')
@@ -38,7 +41,10 @@ class GrammarView extends SelectList
confirmed: (grammar) ->
@cancel()
syntax.addGrammarForPath(@path, grammar)
if grammar is @autoDetect
syntax.removeGrammarForPath(@path)
else
syntax.addGrammarForPath(@path, grammar)
@editor.reloadGrammar()
attach: ->