mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Change an edit session's grammar on meta-l
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
46
src/app/grammar-view.coffee
Normal file
46
src/app/grammar-view.coffee
Normal 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()
|
||||
@@ -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'
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user