Support a single scope name field on Tree-sitter grammars

* Remove the `legacyScopeName` field
* Remove the legacy scope name concept from the Config class
* Handle tree-sitter grammars and textmate grammars having the same 
scope names
This commit is contained in:
Max Brunsfeld
2018-08-18 10:35:41 -07:00
parent a03aac7652
commit 760b38c54b
8 changed files with 57 additions and 112 deletions

View File

@@ -823,21 +823,7 @@ class Config {
}
getLegacyScopeDescriptorForNewScopeDescriptor (scopeDescriptor) {
scopeDescriptor = ScopeDescriptor.fromObject(scopeDescriptor)
const legacyAlias = this.legacyScopeAliases.get(scopeDescriptor.scopes[0])
if (legacyAlias) {
const scopes = scopeDescriptor.scopes.slice()
scopes[0] = legacyAlias
return new ScopeDescriptor({scopes})
}
}
setLegacyScopeAliasForNewScope (languageId, legacyScopeName) {
this.legacyScopeAliases.set(languageId, legacyScopeName)
}
removeLegacyScopeAliasForNewScope (languageId) {
this.legacyScopeAliases.delete(languageId)
return null
}
/*

View File

@@ -122,7 +122,6 @@ class GrammarRegistry {
// found.
assignLanguageMode (buffer, languageId) {
if (buffer.getBuffer) buffer = buffer.getBuffer()
languageId = this.normalizeLanguageId(languageId)
let grammar = null
if (languageId != null) {
@@ -306,17 +305,23 @@ class GrammarRegistry {
this.textmateRegistry.grammars.forEach(callback)
for (const grammarId in this.treeSitterGrammarsById) {
const grammar = this.treeSitterGrammarsById[grammarId]
if (grammar.id) callback(grammar)
if (grammar.scopeName) callback(grammar)
}
}
grammarForId (languageId) {
languageId = this.normalizeLanguageId(languageId)
return (
this.textmateRegistry.grammarForScopeName(languageId) ||
this.treeSitterGrammarsById[languageId]
)
if (!languageId) return null
if (this.config.get('core.useTreeSitterParsers')) {
return (
this.treeSitterGrammarsById[languageId] ||
this.textmateRegistry.grammarForScopeName(languageId)
)
} else {
return (
this.textmateRegistry.grammarForScopeName(languageId) ||
this.treeSitterGrammarsById[languageId]
)
}
}
// Deprecated: Get the grammar override for the given file path.
@@ -363,8 +368,8 @@ class GrammarRegistry {
const languageMode = buffer.getLanguageMode()
const languageOverride = this.languageOverridesByBufferId.get(buffer.id)
if ((grammar.id === buffer.getLanguageMode().getLanguageId() ||
grammar.id === languageOverride)) {
if (grammar === buffer.getLanguageMode().grammar ||
grammar === this.grammarForId(languageOverride)) {
buffer.setLanguageMode(this.languageModeForGrammarAndBuffer(grammar, buffer))
return
} else if (!languageOverride) {
@@ -446,13 +451,8 @@ class GrammarRegistry {
addGrammar (grammar) {
if (grammar instanceof TreeSitterGrammar) {
const existingParams = this.treeSitterGrammarsById[grammar.id] || {}
this.treeSitterGrammarsById[grammar.id] = grammar
if (grammar.legacyScopeName) {
this.config.setLegacyScopeAliasForNewScope(grammar.id, grammar.legacyScopeName)
this.textMateScopeNamesByTreeSitterLanguageId.set(grammar.id, grammar.legacyScopeName)
this.treeSitterLanguageIdsByTextMateScopeName.set(grammar.legacyScopeName, grammar.id)
}
const existingParams = this.treeSitterGrammarsById[grammar.scopeName] || {}
if (grammar.scopeName) this.treeSitterGrammarsById[grammar.scopeName] = grammar
if (existingParams.injectionPoints) grammar.injectionPoints.push(...existingParams.injectionPoints)
this.grammarAddedOrUpdated(grammar)
return new Disposable(() => this.removeGrammar(grammar))
@@ -463,12 +463,7 @@ class GrammarRegistry {
removeGrammar (grammar) {
if (grammar instanceof TreeSitterGrammar) {
delete this.treeSitterGrammarsById[grammar.id]
if (grammar.legacyScopeName) {
this.config.removeLegacyScopeAliasForNewScope(grammar.id)
this.textMateScopeNamesByTreeSitterLanguageId.delete(grammar.id)
this.treeSitterLanguageIdsByTextMateScopeName.delete(grammar.legacyScopeName)
}
delete this.treeSitterGrammarsById[grammar.scopeName]
} else {
return this.textmateRegistry.removeGrammar(grammar)
}

View File

@@ -6,9 +6,8 @@ module.exports =
class TreeSitterGrammar {
constructor (registry, filePath, params) {
this.registry = registry
this.id = params.id
this.name = params.name
this.legacyScopeName = params.legacyScopeName
this.scopeName = params.scopeName
// TODO - Remove the `RegExp` spelling and only support `Regex`, once all of the existing
// Tree-sitter grammars are updated to spell it `Regex`.
@@ -58,6 +57,10 @@ class TreeSitterGrammar {
this.registration = null
}
inspect () {
return `TreeSitterGrammar {scopeName: ${this.scopeName}}`
}
idForScope (scope) {
let id = this.idsByScope[scope]
if (!id) {
@@ -72,10 +75,6 @@ class TreeSitterGrammar {
return this.scopesById.get(id)
}
get scopeName () {
return this.id
}
activate () {
this.registration = this.registry.addGrammar(this)
}

View File

@@ -14,13 +14,6 @@ const WORD_REGEX = /\w/
class TreeSitterLanguageMode {
static _patchSyntaxNode () {
if (!Parser.SyntaxNode.prototype.hasOwnProperty('text')) {
Object.defineProperty(Parser.SyntaxNode.prototype, 'text', {
get () {
return this.tree.buffer.getTextInRange(new Range(this.startPosition, this.endPosition))
}
})
}
if (!Parser.SyntaxNode.prototype.hasOwnProperty('range')) {
Object.defineProperty(Parser.SyntaxNode.prototype, 'range', {
get () {
@@ -41,7 +34,7 @@ class TreeSitterLanguageMode {
this.rootLanguageLayer = new LanguageLayer(this, grammar)
this.injectionsMarkerLayer = buffer.addMarkerLayer()
this.rootScopeDescriptor = new ScopeDescriptor({scopes: [this.grammar.id]})
this.rootScopeDescriptor = new ScopeDescriptor({scopes: [this.grammar.scopeName]})
this.emitter = new Emitter()
this.isFoldableCache = []
this.hasQueuedParse = false
@@ -80,7 +73,7 @@ class TreeSitterLanguageMode {
}
getLanguageId () {
return this.grammar.id
return this.grammar.scopeName
}
bufferDidChange (change) {
@@ -439,7 +432,7 @@ class TreeSitterLanguageMode {
}
}
scopes.push(this.grammar.id)
scopes.push(this.grammar.scopeName)
return new ScopeDescriptor({scopes: scopes.reverse()})
}