From 0ddf47a7ab9d0f616e7296fa9a0b0eb786e4ee59 Mon Sep 17 00:00:00 2001 From: Seivan Heidari Date: Sun, 27 Oct 2019 17:57:11 +0100 Subject: Introducing a Scopes Mapper to map from RA scopes to TextMate scopes with fallbacks. Current scopes defined: ``` ['keyword.unsafe', ['storage.modifier', 'keyword.other', 'keyword.control']], ['function', ['entity.name.function']], ['parameter', ['variable.parameter']], ['type', ['entity.name.type']], ['builtin', ['variable.language', 'support.type', 'support.type']], ['text', ['string', 'string.quoted', 'string.regexp']], ['attribute', ['keyword']], ['literal', ['string', 'string.quoted', 'string.regexp']], ['macro', ['support.other']], ['variable.mut', ['variable']], ['field', ['variable.object.property']], ['module', ['entity.name.section']] ``` Need to complement with further fallbacks as some themes fail. --- editors/code/src/config.ts | 2 ++ editors/code/src/highlighting.ts | 11 ++++++---- editors/code/src/scopes.ts | 18 ++++++++--------- editors/code/src/scopes_mapper.ts | 42 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 editors/code/src/scopes_mapper.ts (limited to 'editors/code/src') diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 9f8c810b6..a3fe39098 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; import * as scopes from './scopes'; +import * as scopesMapper from './scopes_mapper'; import { Server } from './server'; const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; @@ -49,6 +50,7 @@ export class Config { Server.highlighter.removeHighlights(); scopes.load() + scopesMapper.load() if (config.has('highlightingOn')) { this.highlightingOn = config.get('highlightingOn') as boolean; diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 4b961170b..bc19fae2f 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -2,7 +2,7 @@ import seedrandom = require('seedrandom'); import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import * as scopes from './scopes' - +import * as scopesMapper from './scopes_mapper'; import { Server } from './server'; @@ -65,10 +65,13 @@ export class Highlighter { tag: string, textDecoration?: string ): [string, vscode.TextEditorDecorationType] => { - const scope = scopes.find(tag) - if (scope) { - const decor = createDecorationFromTextmate(scope); + const foundRule = scopesMapper.toRule(tag, scopes.find) || scopes.find(tag) + + + + if (foundRule) { + const decor = createDecorationFromTextmate(foundRule); return [tag, decor]; } else { diff --git a/editors/code/src/scopes.ts b/editors/code/src/scopes.ts index c9c01ba1d..470ee716f 100644 --- a/editors/code/src/scopes.ts +++ b/editors/code/src/scopes.ts @@ -16,16 +16,16 @@ export interface TextMateRuleSettings { } // Current theme colors -const colors = new Map() +const rules = new Map() export function find(scope: string): TextMateRuleSettings | undefined { - return colors.get(scope) + return rules.get(scope) } // Load all textmate scopes in the currently active theme export function load() { // Remove any previous theme - colors.clear() + rules.clear() // Find out current color theme const themeName = vscode.workspace.getConfiguration('workbench').get('colorTheme') @@ -95,21 +95,21 @@ function loadColors(textMateRules: TextMateRule[]): void { for (const rule of textMateRules) { if (typeof rule.scope === 'string') { - const existingRule = colors.get(rule.scope); + const existingRule = rules.get(rule.scope); if (existingRule) { - colors.set(rule.scope, mergeRuleSettings(existingRule, rule.settings)) + rules.set(rule.scope, mergeRuleSettings(existingRule, rule.settings)) } else { - colors.set(rule.scope, rule.settings) + rules.set(rule.scope, rule.settings) } } else if (rule.scope instanceof Array) { for (const scope of rule.scope) { - const existingRule = colors.get(scope); + const existingRule = rules.get(scope); if (existingRule) { - colors.set(scope, mergeRuleSettings(existingRule, rule.settings)) + rules.set(scope, mergeRuleSettings(existingRule, rule.settings)) } else { - colors.set(scope, rule.settings) + rules.set(scope, rule.settings) } } } diff --git a/editors/code/src/scopes_mapper.ts b/editors/code/src/scopes_mapper.ts new file mode 100644 index 000000000..995adae2d --- /dev/null +++ b/editors/code/src/scopes_mapper.ts @@ -0,0 +1,42 @@ +import * as vscode from 'vscode' +import { TextMateRuleSettings } from './scopes' + + + + +let mappings = new Map() + + +const defaultMapping = new Map([ + ['keyword.unsafe', ['storage.modifier', 'keyword.other', 'keyword.control']], + ['function', ['entity.name.function']], + ['parameter', ['variable.parameter']], + ['type', ['entity.name.type']], + ['builtin', ['variable.language', 'support.type', 'support.type']], + ['text', ['string', 'string.quoted', 'string.regexp']], + ['attribute', ['keyword']], + ['literal', ['string', 'string.quoted', 'string.regexp']], + ['macro', ['support.other']], + ['variable.mut', ['variable']], + ['field', ['variable.object.property']], + ['module', ['entity.name.section']] +] +) +function find(scope: string): string[] { + return mappings.get(scope) || [] +} + +export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined { + return find(scope).map(intoRule).find(rule => rule !== null) +} + + +export function load() { + const configuration = vscode.workspace + .getConfiguration('rust-analyzer') + .get('scopeMappings') as Map | undefined || new Map() + + mappings = new Map([...Array.from(defaultMapping.entries()), ...Array.from(configuration.entries())]); + + +} \ No newline at end of file -- cgit v1.2.3