Pull out grammar-selector package into a separate repo

This commit is contained in:
Kevin Sawicki
2013-08-13 09:54:07 -07:00
parent a1847b5355
commit 7bc135d82e
5 changed files with 1 additions and 122 deletions

View File

@@ -74,6 +74,7 @@
"git-diff": "0.1.0",
"gists": "0.1.0",
"go-to-line": "0.1.0",
"grammar-selector": "0.1.0",
"image-view": "0.1.0",
"spell-check": "0.1.0",
"terminal": "0.3.0",

View File

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

View File

@@ -1,66 +0,0 @@
SelectList = require 'select-list'
Editor = require 'editor'
{$$} = require 'space-pen'
_ = require 'underscore'
module.exports =
class GrammarSelector extends SelectList
@viewClass: -> "#{super} grammar-selector from-top overlay"
@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 = _.reject grammars, (grammar) -> grammar is syntax.nullGrammar
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

@@ -1,3 +0,0 @@
'main': './lib/grammar-selector'
'description': 'Select the grammar to use for the current editor.'
'activationEvents': 'grammar-selector:show'

View File

@@ -1,50 +0,0 @@
GrammarSelector = require '../lib/grammar-selector'
RootView = require 'root-view'
_ = require 'underscore'
$ = require 'jquery'
describe "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
expect(grammarView.list.children('li:first').text()).toBe 'Auto Detect'
for li in grammarView.list.children('li')
expect($(li).text()).not.toBe syntax.nullGrammar.name
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