aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/scopes_mapper.ts
blob: dfb8bf217e20ae7073954d7631d25517a230f087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as vscode from 'vscode';
import { TextMateRuleSettings } from './scopes';



let mappings = new Map<string, string[]>();


const defaultMapping = new Map<string, string[]>([
    ['comment', ['comment', 'comment.block', 'comment.line', 'comment.block.documentation']],
    ['string', ['string']],
    ['keyword', ['keyword']],
    ['keyword.control', ['keyword.control', 'keyword', 'keyword.other']],
    ['keyword.unsafe', ['storage.modifier', 'keyword.other', 'keyword.control', 'keyword']],
    ['function', ['entity.name.function']],
    ['parameter', ['variable.parameter']],
    ['constant', ['constant', 'variable']],
    ['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', ['variable']],
    ['variable.mut', ['variable', 'storage.modifier']],
    ['field', ['variable.object.property', 'meta.field.declaration', 'meta.definition.property', 'variable.other',]],
    ['module', ['entity.name.section', 'entity.other']]
]
);

// Temporary exported for debugging for now. 
export 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).filter(rule => rule !== undefined)[0];
}


function isString(value: any): value is string {
    return typeof value === 'string';
}

function isArrayOfString(value: any): value is string[] {
    return Array.isArray(value) && value.every(item => isString(item));
}


export function load() {
    const rawConfig: { [key: string]: any } = vscode.workspace
        .getConfiguration('rust-analyzer')
        .get('scopeMappings')
        || {};

    mappings = Object
        .entries(rawConfig)
        .filter(([_, value]) => isString(value) || isArrayOfString(value))
        .reduce((list, [key, value]: [string, string | string[]]) => {
            return list.set(key, isString(value) ? [value] : value);
        }, defaultMapping);

}