aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/config.ts
blob: 637aea27dae7c0bf33f57b45c2cdd26bf495f45c (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import * as vscode from 'vscode';
import { log } from "./util";

export type UpdatesChannel = "stable" | "nightly";

export const NIGHTLY_TAG = "nightly";

export class Config {
    readonly extensionId = "matklad.rust-analyzer";

    private readonly rootSection = "rust-analyzer";
    private readonly requiresReloadOpts = [
        "serverPath",
        "cargoFeatures",
        "cargo-watch",
        "highlighting.semanticTokens",
        "inlayHints",
        "updates.channel",
    ]
        .map(opt => `${this.rootSection}.${opt}`);

    readonly package: {
        version: string;
        releaseTag: string | undefined;
        enableProposedApi: boolean | undefined;
    } = vscode.extensions.getExtension(this.extensionId)!.packageJSON;

    readonly globalStoragePath: string;

    constructor(ctx: vscode.ExtensionContext) {
        this.globalStoragePath = ctx.globalStoragePath;
        vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
        this.refreshLogging();
    }

    private refreshLogging() {
        log.setEnabled(this.traceExtension);
        log.debug(
            "Extension version:", this.package.version,
            "using configuration:", this.cfg
        );
    }

    private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
        this.refreshLogging();

        const requiresReloadOpt = this.requiresReloadOpts.find(
            opt => event.affectsConfiguration(opt)
        );

        if (!requiresReloadOpt) return;

        const userResponse = await vscode.window.showInformationMessage(
            `Changing "${requiresReloadOpt}" requires a reload`,
            "Reload now"
        );

        if (userResponse === "Reload now") {
            await vscode.commands.executeCommand("workbench.action.reloadWindow");
        }
    }

    // We don't do runtime config validation here for simplicity. More on stackoverflow:
    // https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension

    private get cfg(): vscode.WorkspaceConfiguration {
        return vscode.workspace.getConfiguration(this.rootSection);
    }

    get serverPath() { return this.cfg.get<null | string>("serverPath")!; }
    get channel() { return this.cfg.get<UpdatesChannel>("updates.channel")!; }
    get askBeforeDownload() { return this.cfg.get<boolean>("updates.askBeforeDownload")!; }
    get highlightingSemanticTokens() { return this.cfg.get<boolean>("highlighting.semanticTokens")!; }
    get highlightingOn() { return this.cfg.get<boolean>("highlightingOn")!; }
    get rainbowHighlightingOn() { return this.cfg.get<boolean>("rainbowHighlightingOn")!; }
    get lruCapacity() { return this.cfg.get<null | number>("lruCapacity")!; }
    get excludeGlobs() { return this.cfg.get<string[]>("excludeGlobs")!; }
    get useClientWatching() { return this.cfg.get<boolean>("useClientWatching")!; }
    get featureFlags() { return this.cfg.get<Record<string, boolean>>("featureFlags")!; }
    get rustfmtArgs() { return this.cfg.get<string[]>("rustfmtArgs")!; }
    get loadOutDirsFromCheck() { return this.cfg.get<boolean>("loadOutDirsFromCheck")!; }
    get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; }

    // for internal use
    get withSysroot() { return this.cfg.get<boolean>("withSysroot", true)!; }

    get inlayHints() {
        return {
            typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!,
            parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!,
            chainingHints: this.cfg.get<boolean>("inlayHints.chainingHints")!,
            maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!,
        };
    }

    get cargoWatchOptions() {
        return {
            enable: this.cfg.get<boolean>("cargo-watch.enable")!,
            arguments: this.cfg.get<string[]>("cargo-watch.arguments")!,
            allTargets: this.cfg.get<boolean>("cargo-watch.allTargets")!,
            command: this.cfg.get<string>("cargo-watch.command")!,
        };
    }

    get cargoFeatures() {
        return {
            noDefaultFeatures: this.cfg.get<boolean>("cargoFeatures.noDefaultFeatures")!,
            allFeatures: this.cfg.get<boolean>("cargoFeatures.allFeatures")!,
            features: this.cfg.get<string[]>("cargoFeatures.features")!,
            loadOutDirsFromCheck: this.cfg.get<boolean>("cargoFeatures.loadOutDirsFromCheck")!,
        };
    }
}