From 1cdb804824546e8572912df8cf51c9d5072c33ba Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 20 Feb 2018 10:52:16 -0800 Subject: [PATCH] Tweak config APIs for dealing w/ legacy scope aliases --- spec/config-spec.js | 4 ++-- src/config.js | 33 +++++++++++++++++---------------- src/grammar-registry.js | 4 ++-- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/spec/config-spec.js b/spec/config-spec.js index 3754e517f..b999d5157 100644 --- a/spec/config-spec.js +++ b/spec/config-spec.js @@ -115,7 +115,7 @@ describe('Config', () => { describe('when the first component of the scope descriptor matches a legacy scope alias', () => it('falls back to properties defined for the legacy scope if no value is found for the original scope descriptor', () => { - atom.config.addLegacyScopeAlias('javascript', '.source.js') + atom.config.setLegacyScopeAliasForNewScope('javascript', '.source.js') atom.config.set('foo', 100, {scopeSelector: '.source.js'}) atom.config.set('foo', 200, {scopeSelector: 'javascript for_statement'}) @@ -154,7 +154,7 @@ describe('Config', () => { describe('when the first component of the scope descriptor matches a legacy scope alias', () => it('includes the values defined for the legacy scope', () => { - atom.config.addLegacyScopeAlias('javascript', '.source.js') + atom.config.setLegacyScopeAliasForNewScope('javascript', '.source.js') expect(atom.config.set('foo', 41)).toBe(true) expect(atom.config.set('foo', 42, {scopeSelector: 'javascript'})).toBe(true) diff --git a/src/config.js b/src/config.js index a589654c9..4753d148d 100644 --- a/src/config.js +++ b/src/config.js @@ -429,7 +429,7 @@ class Config { this.settingsLoaded = false this.transactDepth = 0 this.pendingOperations = [] - this.legacyScopeAliases = {} + this.legacyScopeAliases = new Map() this.requestSave = _.debounce(() => this.save(), 1) } @@ -618,7 +618,7 @@ class Config { keyPath, options ) - legacyScopeDescriptor = this.getLegacyScopeDescriptor(scopeDescriptor) + legacyScopeDescriptor = this.getLegacyScopeDescriptorForNewScopeDescriptor(scopeDescriptor) if (legacyScopeDescriptor) { result.push(...Array.from(this.scopedSettingsStore.getAll( legacyScopeDescriptor.getScopeChain(), @@ -818,12 +818,22 @@ class Config { } } - addLegacyScopeAlias (languageId, legacyScopeName) { - this.legacyScopeAliases[languageId] = legacyScopeName + 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}) + } } - removeLegacyScopeAlias (languageId) { - delete this.legacyScopeAliases[languageId] + setLegacyScopeAliasForNewScope (languageId, legacyScopeName) { + this.legacyScopeAliases.set(languageId, legacyScopeName) + } + + removeLegacyScopeAliasForNewScope (languageId) { + this.legacyScopeAliases.delete(languageId) } /* @@ -1202,7 +1212,7 @@ class Config { options ) - const legacyScopeDescriptor = this.getLegacyScopeDescriptor(scopeDescriptor) + const legacyScopeDescriptor = this.getLegacyScopeDescriptorForNewScopeDescriptor(scopeDescriptor) if (result != null) { return result } else if (legacyScopeDescriptor) { @@ -1230,15 +1240,6 @@ class Config { } }) } - - getLegacyScopeDescriptor (scopeDescriptor) { - const legacyAlias = this.legacyScopeAliases[scopeDescriptor.scopes[0]] - if (legacyAlias) { - const scopes = scopeDescriptor.scopes.slice() - scopes[0] = legacyAlias - return new ScopeDescriptor({scopes}) - } - } }; // Base schema enforcers. These will coerce raw input into the specified type, diff --git a/src/grammar-registry.js b/src/grammar-registry.js index 618e5022e..20757fb0b 100644 --- a/src/grammar-registry.js +++ b/src/grammar-registry.js @@ -399,7 +399,7 @@ class GrammarRegistry { if (grammar instanceof TreeSitterGrammar) { this.treeSitterGrammarsById[grammar.id] = grammar if (grammar.legacyScopeName) { - this.config.addLegacyScopeAlias(grammar.id, grammar.legacyScopeName) + this.config.setLegacyScopeAliasForNewScope(grammar.id, grammar.legacyScopeName) this.textMateScopeNamesByTreeSitterLanguageId.set(grammar.id, grammar.legacyScopeName) this.treeSitterLanguageIdsByTextMateScopeName.set(grammar.legacyScopeName, grammar.id) } @@ -414,7 +414,7 @@ class GrammarRegistry { if (grammar instanceof TreeSitterGrammar) { delete this.treeSitterGrammarsById[grammar.id] if (grammar.legacyScopeName) { - this.config.removeLegacyScopeAlias(grammar.id) + this.config.removeLegacyScopeAliasForNewScope(grammar.id) this.textMateScopeNamesByTreeSitterLanguageId.delete(grammar.id) this.treeSitterLanguageIdsByTextMateScopeName.delete(grammar.legacyScopeName) }