Make method to assign specific grammar

This commit is contained in:
Benjamin Gray 2019-02-18 09:28:56 +11:00
parent 8d4b9c2485
commit e9e52f5052
2 changed files with 17 additions and 1 deletions

View File

@ -43,7 +43,7 @@ class GrammarListView {
if (grammar === this.autoDetect) {
atom.textEditors.clearGrammarOverride(this.editor)
} else {
atom.textEditors.setGrammarOverride(this.editor, grammar.scopeName)
atom.grammars.assignGrammar(this.editor, grammar)
}
},
didCancelSelection: () => {

View File

@ -142,6 +142,22 @@ class GrammarRegistry {
return true
}
// Extended: Force a {TextBuffer} to use a different grammar than the
// one that would otherwise be selected for it.
//
// * `buffer` The {TextBuffer} whose grammar will be set.
// * `grammar` The {Grammar} of the desired languageMode.
//
// Returns a {Boolean} that indicates whether the assignment was sucessful
assignGrammar (buffer, grammar) {
if (buffer.getBuffer) buffer = buffer.getBuffer()
if (!grammar) return false
this.languageOverridesByBufferId.set(buffer.id, grammar.scopeName || null)
this.grammarScoresByBuffer.set(buffer, null)
buffer.setLanguageMode(this.languageModeForGrammarAndBuffer(grammar, buffer))
return true
}
// Extended: Get the `languageId` that has been explicitly assigned to
// to the given buffer, if any.
//