aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/scopes_mapper.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/scopes_mapper.ts')
-rw-r--r--editors/code/src/scopes_mapper.ts35
1 files changed, 24 insertions, 11 deletions
diff --git a/editors/code/src/scopes_mapper.ts b/editors/code/src/scopes_mapper.ts
index 19a4213d4..7056a8e24 100644
--- a/editors/code/src/scopes_mapper.ts
+++ b/editors/code/src/scopes_mapper.ts
@@ -1,10 +1,9 @@
1import * as vscode from 'vscode' 1import * as vscode from 'vscode';
2import { TextMateRuleSettings } from './scopes' 2import { TextMateRuleSettings } from './scopes';
3 3
4 4
5 5
6 6let mappings = new Map<string, string[]>();
7let mappings = new Map<string, string[]>()
8 7
9 8
10const defaultMapping = new Map<string, string[]>([ 9const defaultMapping = new Map<string, string[]>([
@@ -27,25 +26,39 @@ const defaultMapping = new Map<string, string[]>([
27 ['field', ['variable.object.property', 'meta.field.declaration', 'meta.definition.property', 'variable.other',]], 26 ['field', ['variable.object.property', 'meta.field.declaration', 'meta.definition.property', 'variable.other',]],
28 ['module', ['entity.name.section', 'entity.other']] 27 ['module', ['entity.name.section', 'entity.other']]
29] 28]
30) 29);
31 30
32// Temporary exported for debugging for now. 31// Temporary exported for debugging for now.
33export function find(scope: string): string[] { 32export function find(scope: string): string[] {
34 return mappings.get(scope) || [] 33 return mappings.get(scope) || [];
35} 34}
36 35
37export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined { 36export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined {
38 return find(scope).map(intoRule).filter(rule => rule !== undefined)[0] 37 return find(scope).map(intoRule).filter(rule => rule !== undefined)[0];
38}
39
40
41function isString(value: any): value is string {
42 return typeof value === 'string';
43}
44
45function isArrayOfString(value: any): value is string[] {
46 return Array.isArray(value) && value.every(item => isString(item));
39} 47}
40 48
41 49
42export function load() { 50export function load() {
43 const configuration = vscode.workspace 51 const rawConfig: { [key: string]: any } = vscode.workspace
44 .getConfiguration('rust-analyzer') 52 .getConfiguration('rust-analyzer')
45 .get('scopeMappings') as Map<string, string[]> | undefined 53 .get('scopeMappings')
46 || new Map() 54 || {};
47 55
48 mappings = new Map([...Array.from(defaultMapping.entries()), ...Array.from(configuration.entries())]) 56 mappings = Object
57 .entries(rawConfig)
58 .filter(([_, value]) => isString(value) || isArrayOfString(value))
59 .reduce((list, [key, value]: [string, string | string[]]) => {
60 return list.set(key, isString(value) ? [value] : value);
49 61
62 }, defaultMapping);
50 63
51} \ No newline at end of file 64} \ No newline at end of file