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.ts79
1 files changed, 79 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..85c791ff5
--- /dev/null
+++ b/editors/code/src/scopes_mapper.ts
@@ -0,0 +1,79 @@
1import * as vscode from 'vscode';
2import { TextMateRuleSettings } from './scopes';
3
4let mappings = new Map<string, string[]>();
5
6const defaultMapping = new Map<string, string[]>([
7 [
8 'comment',
9 [
10 'comment',
11 'comment.block',
12 'comment.line',
13 'comment.block.documentation'
14 ]
15 ],
16 ['string', ['string']],
17 ['keyword', ['keyword']],
18 ['keyword.control', ['keyword.control', 'keyword', 'keyword.other']],
19 [
20 'keyword.unsafe',
21 ['storage.modifier', 'keyword.other', 'keyword.control', 'keyword']
22 ],
23 ['function', ['entity.name.function']],
24 ['parameter', ['variable.parameter']],
25 ['constant', ['constant', 'variable']],
26 ['type', ['entity.name.type']],
27 ['builtin', ['variable.language', 'support.type', 'support.type']],
28 ['text', ['string', 'string.quoted', 'string.regexp']],
29 ['attribute', ['keyword']],
30 ['literal', ['string', 'string.quoted', 'string.regexp']],
31 ['macro', ['support.other']],
32 ['variable', ['variable']],
33 ['variable.mut', ['variable', 'storage.modifier']],
34 [
35 'field',
36 [
37 'variable.object.property',
38 'meta.field.declaration',
39 'meta.definition.property',
40 'variable.other'
41 ]
42 ],
43 ['module', ['entity.name.section', 'entity.other']]
44]);
45
46// Temporary exported for debugging for now.
47export function find(scope: string): string[] {
48 return mappings.get(scope) || [];
49}
50
51export function toRule(
52 scope: string,
53 intoRule: (scope: string) => TextMateRuleSettings | undefined
54): TextMateRuleSettings | undefined {
55 return find(scope)
56 .map(intoRule)
57 .filter(rule => rule !== undefined)[0];
58}
59
60function isString(value: any): value is string {
61 return typeof value === 'string';
62}
63
64function isArrayOfString(value: any): value is string[] {
65 return Array.isArray(value) && value.every(item => isString(item));
66}
67
68export function load() {
69 const rawConfig: { [key: string]: any } =
70 vscode.workspace
71 .getConfiguration('rust-analyzer')
72 .get('scopeMappings') || {};
73
74 mappings = Object.entries(rawConfig)
75 .filter(([_, value]) => isString(value) || isArrayOfString(value))
76 .reduce((list, [key, value]: [string, string | string[]]) => {
77 return list.set(key, isString(value) ? [value] : value);
78 }, defaultMapping);
79}