Change an edit session's grammar on meta-l

This commit is contained in:
Kevin Sawicki
2013-01-08 12:37:12 -08:00
parent 12794672b7
commit 3b47c26b4d
9 changed files with 145 additions and 3 deletions

View File

@@ -626,5 +626,12 @@ class EditSession
@setCursorBufferPosition(cursorPosition) if cursorPosition
cursorPosition = null
getGrammar: -> @languageMode.grammar
reloadGrammar: ->
@unfoldAll()
@languageMode.reloadGrammar()
@displayBuffer.tokenizedBuffer.resetScreenLines()
_.extend(EditSession.prototype, EventEmitter)
_.extend(EditSession.prototype, Subscriber)

View File

@@ -178,6 +178,7 @@ class Editor extends View
'editor:checkout-head-revision': @checkoutHead
'editor:close-other-editors': @destroyInactiveEditSessions
'editor:close-all-editors': @destroyAllEditSessions
'editor:select-grammar': @selectGrammar
documentation = {}
for name, method of editorBindings
@@ -1113,3 +1114,14 @@ class Editor extends View
@highlightedLine.addClass('cursor-line')
else
@highlightedLine = null
getGrammar: -> @activeEditSession.getGrammar()
selectGrammar: ->
GrammarView = require 'grammar-view'
new GrammarView(this)
reloadGrammar: ->
@activeEditSession.reloadGrammar()
@clearRenderedLines()
@updateDisplay()

View File

@@ -0,0 +1,46 @@
SelectList = require 'select-list'
{$$} = require 'space-pen'
module.exports =
class GrammarView extends SelectList
@viewClass: -> "#{super} grammar-view"
filterKey: 'name'
initialize: (@editor) ->
@currentGrammar = @editor.getGrammar()
@path = @editor.getPath()
requireStylesheet 'grammar-view.css'
@command 'editor:select-grammar', =>
@cancel()
false
super
@populate()
@attach()
itemForElement: (grammar) ->
if grammar is @currentGrammar
grammarClass = 'current-grammar'
else
grammarClass = 'grammar'
$$ ->
@li grammar.name, class: grammarClass
populate: ->
@setArray(syntax.grammars)
cancelled: ->
@miniEditor.setText('')
@editor.rootView()?.focus() if @miniEditor.isFocused
confirmed: (grammar) ->
@cancel()
syntax.addGrammarForPath(@path, grammar)
@editor.reloadGrammar()
attach: ->
@editor.rootView()?.append(this)
@miniEditor.focus()

View File

@@ -34,3 +34,4 @@
'meta-U': 'editor:lower-case'
'alt-meta-w': 'editor:close-other-editors'
'meta-P': 'editor:close-all-editors'
'meta-l': 'editor:select-grammar'

View File

@@ -16,7 +16,7 @@ class LanguageMode
constructor: (@editSession) ->
@buffer = @editSession.buffer
@grammar = syntax.grammarForFilePath(@buffer.getPath())
@reloadGrammar()
@bracketAnchorRanges = []
_.adviseBefore @editSession, 'insertText', (text) =>
@@ -47,6 +47,9 @@ class LanguageMode
@bracketAnchorRanges.push @editSession.addAnchorRange(range)
false
reloadGrammar: ->
@grammar = syntax.grammarForFilePath(@buffer.getPath())
isQuote: (string) ->
/'|"/.test(string)

View File

@@ -8,6 +8,7 @@ module.exports =
class Syntax
constructor: ->
@grammars = []
@grammarsByPath = {}
@grammarsByFileType = {}
@grammarsByScopeName = {}
@globalProperties = {}
@@ -20,6 +21,12 @@ class Syntax
@grammarsByFileType[fileType] = grammar
@grammarsByScopeName[grammar.scopeName] = grammar
addGrammarForPath: (path, grammar) ->
@grammarsByPath[path] = grammar
removeGrammarForPath: (path) ->
delete @grammarsByPath[path]
grammarForFilePath: (filePath) ->
return @grammarsByFileType["txt"] unless filePath
@@ -27,7 +34,8 @@ class Syntax
if filePath and extension.length == 0
extension = fs.base(filePath)
@grammarsByFileType[extension] or
@grammarsByPath[filePath] or
@grammarsByFileType[extension] or
@grammarByShebang(filePath) or
@grammarByFileTypeSuffix(filePath) or
@grammarsByFileType["txt"]

View File

@@ -21,10 +21,13 @@ class TokenizedBuffer
constructor: (@buffer, { @languageMode, @tabLength }) ->
@tabLength ?= 2
@id = @constructor.idCounter++
@resetScreenLines()
@buffer.on "changed.tokenized-buffer#{@id}", (e) => @handleBufferChange(e)
resetScreenLines: ->
@screenLines = @buildPlaceholderScreenLinesForRows(0, @buffer.getLastRow())
@invalidRows = []
@invalidateRow(0)
@buffer.on "changed.tokenized-buffer#{@id}", (e) => @handleBufferChange(e)
setVisible: (@visible) ->
@tokenizeInBackground() if @visible