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.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/editors/code/src/scopes_mapper.ts b/editors/code/src/scopes_mapper.ts
new file mode 100644
index 000000000..dfb8bf217
--- /dev/null
+++ b/editors/code/src/scopes_mapper.ts
@@ -0,0 +1,63 @@
1import * as vscode from 'vscode';
2import { TextMateRuleSettings } from './scopes';
3
4
5
6let mappings = new Map<string, string[]>();
7
8
9const defaultMapping = new Map<string, string[]>([
10 ['comment', ['comment', 'comment.block', 'comment.line', 'comment.block.documentation']],
11 ['string', ['string']],
12 ['keyword', ['keyword']],
13 ['keyword.control', ['keyword.control', 'keyword', 'keyword.other']],
14 ['keyword.unsafe', ['storage.modifier', 'keyword.other', 'keyword.control', 'keyword']],
15 ['function', ['entity.name.function']],
16 ['parameter', ['variable.parameter']],
17 ['constant', ['constant', 'variable']],
18 ['type', ['entity.name.type']],
19 ['builtin', ['variable.language', 'support.type', 'support.type']],
20 ['text', ['string', 'string.quoted', 'string.regexp']],
21 ['attribute', ['keyword']],
22 ['literal', ['string', 'string.quoted', 'string.regexp']],
23 ['macro', ['support.other']],
24 ['variable', ['variable']],
25 ['variable.mut', ['variable', 'storage.modifier']],
26 ['field', ['variable.object.property', 'meta.field.declaration', 'meta.definition.property', 'variable.other',]],
27 ['module', ['entity.name.section', 'entity.other']]
28]
29);
30
31// Temporary exported for debugging for now.
32export function find(scope: string): string[] {
33 return mappings.get(scope) || [];
34}
35
36export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined {
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));
47}
48
49
50export function load() {
51 const rawConfig: { [key: string]: any } = vscode.workspace
52 .getConfiguration('rust-analyzer')
53 .get('scopeMappings')
54 || {};
55
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);
61 }, defaultMapping);
62
63} \ No newline at end of file