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:
@@ -1,5 +1,6 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as scopes from './scopes';
|
import * as scopes from './scopes';
|
||||||
|
import * as scopesMapper from './scopes_mapper';
|
||||||
import { Server } from './server';
|
import { Server } from './server';
|
||||||
|
|
||||||
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
|
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
|
||||||
@@ -49,6 +50,7 @@ export class Config {
|
|||||||
|
|
||||||
Server.highlighter.removeHighlights();
|
Server.highlighter.removeHighlights();
|
||||||
scopes.load()
|
scopes.load()
|
||||||
|
scopesMapper.load()
|
||||||
if (config.has('highlightingOn')) {
|
if (config.has('highlightingOn')) {
|
||||||
|
|
||||||
this.highlightingOn = config.get('highlightingOn') as boolean;
|
this.highlightingOn = config.get('highlightingOn') as boolean;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import seedrandom = require('seedrandom');
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
import * as scopes from './scopes'
|
import * as scopes from './scopes'
|
||||||
|
import * as scopesMapper from './scopes_mapper';
|
||||||
|
|
||||||
import { Server } from './server';
|
import { Server } from './server';
|
||||||
|
|
||||||
@@ -65,10 +65,13 @@ export class Highlighter {
|
|||||||
tag: string,
|
tag: string,
|
||||||
textDecoration?: string
|
textDecoration?: string
|
||||||
): [string, vscode.TextEditorDecorationType] => {
|
): [string, vscode.TextEditorDecorationType] => {
|
||||||
const scope = scopes.find(tag)
|
|
||||||
|
|
||||||
if (scope) {
|
const foundRule = scopesMapper.toRule(tag, scopes.find) || scopes.find(tag)
|
||||||
const decor = createDecorationFromTextmate(scope);
|
|
||||||
|
|
||||||
|
|
||||||
|
if (foundRule) {
|
||||||
|
const decor = createDecorationFromTextmate(foundRule);
|
||||||
return [tag, decor];
|
return [tag, decor];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -16,16 +16,16 @@ export interface TextMateRuleSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Current theme colors
|
// Current theme colors
|
||||||
const colors = new Map<string, TextMateRuleSettings>()
|
const rules = new Map<string, TextMateRuleSettings>()
|
||||||
|
|
||||||
export function find(scope: string): TextMateRuleSettings | undefined {
|
export function find(scope: string): TextMateRuleSettings | undefined {
|
||||||
return colors.get(scope)
|
return rules.get(scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load all textmate scopes in the currently active theme
|
// Load all textmate scopes in the currently active theme
|
||||||
export function load() {
|
export function load() {
|
||||||
// Remove any previous theme
|
// Remove any previous theme
|
||||||
colors.clear()
|
rules.clear()
|
||||||
// Find out current color theme
|
// Find out current color theme
|
||||||
const themeName = vscode.workspace.getConfiguration('workbench').get('colorTheme')
|
const themeName = vscode.workspace.getConfiguration('workbench').get('colorTheme')
|
||||||
|
|
||||||
@@ -95,21 +95,21 @@ function loadColors(textMateRules: TextMateRule[]): void {
|
|||||||
for (const rule of textMateRules) {
|
for (const rule of textMateRules) {
|
||||||
|
|
||||||
if (typeof rule.scope === 'string') {
|
if (typeof rule.scope === 'string') {
|
||||||
const existingRule = colors.get(rule.scope);
|
const existingRule = rules.get(rule.scope);
|
||||||
if (existingRule) {
|
if (existingRule) {
|
||||||
colors.set(rule.scope, mergeRuleSettings(existingRule, rule.settings))
|
rules.set(rule.scope, mergeRuleSettings(existingRule, rule.settings))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
colors.set(rule.scope, rule.settings)
|
rules.set(rule.scope, rule.settings)
|
||||||
}
|
}
|
||||||
} else if (rule.scope instanceof Array) {
|
} else if (rule.scope instanceof Array) {
|
||||||
for (const scope of rule.scope) {
|
for (const scope of rule.scope) {
|
||||||
const existingRule = colors.get(scope);
|
const existingRule = rules.get(scope);
|
||||||
if (existingRule) {
|
if (existingRule) {
|
||||||
colors.set(scope, mergeRuleSettings(existingRule, rule.settings))
|
rules.set(scope, mergeRuleSettings(existingRule, rule.settings))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
colors.set(scope, rule.settings)
|
rules.set(scope, rule.settings)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
42
editors/code/src/scopes_mapper.ts
Normal file
42
editors/code/src/scopes_mapper.ts
Normal 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())]);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user