Move GrammarView into GrammarSelector package

This commit is contained in:
probablycorey
2013-03-27 17:02:13 -07:00
parent 8323b0defd
commit 5308e03096
6 changed files with 116 additions and 6 deletions

View File

@@ -150,7 +150,6 @@ class Editor extends View
'editor:toggle-line-comments': @toggleLineCommentsInSelection
'editor:log-cursor-scope': @logCursorScope
'editor:checkout-head-revision': @checkoutHead
'editor:select-grammar': @selectGrammar
'editor:copy-path': @copyPathToPasteboard
'editor:move-line-up': @moveLineUp
'editor:move-line-down': @moveLineDown
@@ -1152,10 +1151,6 @@ class Editor extends View
@activeEditSession.setGrammar(grammar)
@handleGrammarChange()
selectGrammar: ->
GrammarView = require 'grammar-view'
new GrammarView(this)
reloadGrammar: ->
grammarChanged = @activeEditSession.reloadGrammar()
@handleGrammarChange() if grammarChanged

View File

@@ -20,7 +20,6 @@
'meta-U': 'editor:lower-case'
'alt-meta-w': 'editor:close-other-edit-sessions'
'meta-P': 'editor:close-all-edit-sessions'
'meta-L': 'editor:select-grammar'
'ctrl-C': 'editor:copy-path'
'ctrl-meta-up': 'editor:move-line-up'
'ctrl-meta-down': 'editor:move-line-down'

View File

@@ -0,0 +1,3 @@
'.editor':
'meta-L': 'grammar-selector:show'

View File

@@ -0,0 +1,64 @@
SelectList = require 'select-list'
Editor = require 'editor'
{$$} = require 'space-pen'
module.exports =
class GrammarSelector extends SelectList
@viewClass: -> "#{super} grammar-selector from-top overlay mini"
@activate: ->
rootView.command 'grammar-selector:show', '.editor', => new GrammarSelector()
filterKey: 'name'
initialize: ->
@editor = rootView.getActiveView()
return unless @editor instanceof Editor
@currentGrammar = @editor.getGrammar()
@path = @editor.getPath()
@autoDetect = name: 'Auto Detect'
@command 'grammar-selector:show', =>
@cancel()
false
super
@populate()
@attach()
itemForElement: (grammar) ->
if grammar is @currentGrammar
grammarClass = 'active-item'
else
grammarClass = 'inactive-item'
$$ ->
@li grammar.name, class: grammarClass
populate: ->
grammars = new Array(syntax.grammars...)
grammars.sort (grammarA, grammarB) ->
if grammarA.scopeName is 'text.plain'
-1
else if grammarB.scopeName is 'text.plain'
1
else if grammarA.name < grammarB.name
-1
else if grammarA.name > grammarB.name
1
else
0
grammars.unshift(@autoDetect)
@setArray(grammars)
confirmed: (grammar) ->
@cancel()
if grammar is @autoDetect
syntax.clearGrammarOverrideForPath(@path)
else
syntax.setGrammarOverrideForPath(@path, grammar.scopeName)
@editor.reloadGrammar()
attach: ->
super
rootView.append(this)
@miniEditor.focus()

View File

@@ -0,0 +1,2 @@
'main': 'lib/grammar-selector'
'activationEvents': ['grammar-selector:show']

View File

@@ -0,0 +1,47 @@
GrammarSelector = require '../lib/grammar-selector'
RootView = require 'root-view'
_ = require 'underscore'
fdescribe "GrammarSelector", ->
[editor, textGrammar, jsGrammar] = []
beforeEach ->
window.rootView = new RootView
atom.activatePackage('grammar-selector')
atom.activatePackage('text.tmbundle', sync: true)
atom.activatePackage('javascript.tmbundle', sync: true)
rootView.open('sample.js')
editor = rootView.getActiveView()
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
describe "when grammar-selector:show is triggered", ->
it "displays a list of all the available grammars", ->
editor.trigger 'grammar-selector:show'
grammarView = rootView.find('.grammar-selector').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 'grammar-selector:show'
grammarView = rootView.find('.grammar-selector').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 'grammar-selector:show'
grammarView = rootView.find('.grammar-selector').view()
grammarView.confirmed(textGrammar)
expect(editor.getGrammar()).toBe textGrammar
editor.trigger 'grammar-selector:show'
grammarView = rootView.find('.grammar-selector').view()
grammarView.confirmed(grammarView.array[0])
expect(editor.getGrammar()).toBe jsGrammar