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.
This commit is contained in:
Seivan Heidari 2019-10-27 17:57:11 +01:00
parent 5957b851e4
commit 0ddf47a7ab
4 changed files with 60 additions and 13 deletions

View file

@ -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;

View file

@ -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 {

View file

@ -16,16 +16,16 @@ export interface TextMateRuleSettings {
}
// Current theme colors
const colors = new Map<string, TextMateRuleSettings>()
const rules = new Map<string, TextMateRuleSettings>()
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)
}
}
}

View file

@ -0,0 +1,42 @@
import * as vscode from 'vscode'
import { TextMateRuleSettings } from './scopes'
let mappings = new Map<string, string[]>()
const defaultMapping = new Map<string, string[]>([
['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<string, string[]> | undefined || new Map()
mappings = new Map([...Array.from(defaultMapping.entries()), ...Array.from(configuration.entries())]);
}