aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/config.ts
blob: 41884543684ab004b6bd13cbf4dc102375c94cb0 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import * as os from "os";
import * as vscode from 'vscode';
import { BinarySource } from "./installation/interfaces";

const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;

export interface CargoWatchOptions {
    enable: boolean;
    arguments: string[];
    command: string;
    allTargets: boolean;
}

export interface CargoFeatures {
    noDefaultFeatures: boolean;
    allFeatures: boolean;
    features: string[];
}

export class Config {
    langServerSource!: null | BinarySource;

    highlightingOn = true;
    rainbowHighlightingOn = false;
    enableEnhancedTyping = true;
    lruCapacity: null | number = null;
    displayInlayHints = true;
    maxInlayHintLength: null | number = null;
    excludeGlobs: string[] = [];
    useClientWatching = true;
    featureFlags: Record<string, boolean> = {};
    // for internal use
    withSysroot: null | boolean = null;
    cargoWatchOptions: CargoWatchOptions = {
        enable: true,
        arguments: [],
        command: '',
        allTargets: true,
    };
    cargoFeatures: CargoFeatures = {
        noDefaultFeatures: false,
        allFeatures: true,
        features: [],
    };

    private prevEnhancedTyping: null | boolean = null;
    private prevCargoFeatures: null | CargoFeatures = null;
    private prevCargoWatchOptions: null | CargoWatchOptions = null;

    constructor(ctx: vscode.ExtensionContext) {
        vscode.workspace.onDidChangeConfiguration(_ => this.refresh(ctx), null, ctx.subscriptions);
        this.refresh(ctx);
    }

    private static expandPathResolving(path: string) {
        if (path.startsWith('~/')) {
            return path.replace('~', os.homedir());
        }
        return path;
    }

    /**
     * Name of the binary artifact for `ra_lsp_server` that is published for
     * `platform` on GitHub releases. (It is also stored under the same name when
     * downloaded by the extension).
     */
    private static prebuiltLangServerFileName(
        platform: NodeJS.Platform,
        arch: string
    ): null | string {
        // See possible `arch` values here:
        // https://nodejs.org/api/process.html#process_process_arch

        switch (platform) {

            case "linux": {
                switch (arch) {
                    case "arm":
                    case "arm64": return null;

                    default: return "ra_lsp_server-linux";
                }
            }

            case "darwin": return "ra_lsp_server-mac";
            case "win32":  return "ra_lsp_server-windows.exe";

            // Users on these platforms yet need to manually build from sources
            case "aix":
            case "android":
            case "freebsd":
            case "openbsd":
            case "sunos":
            case "cygwin":
            case "netbsd": return null;
            // The list of platforms is exhaustive (see `NodeJS.Platform` type definition)
        }
    }

    private static langServerBinarySource(
        ctx: vscode.ExtensionContext,
        config: vscode.WorkspaceConfiguration
    ): null | BinarySource {
        const langServerPath = RA_LSP_DEBUG ?? config.get<null | string>("raLspServerPath");

        if (langServerPath) {
            return {
                type: BinarySource.Type.ExplicitPath,
                path: Config.expandPathResolving(langServerPath)
            };
        }

        const prebuiltBinaryName = Config.prebuiltLangServerFileName(
            process.platform, process.arch
        );

        if (!prebuiltBinaryName) return null;

        return {
            type: BinarySource.Type.GithubRelease,
            dir: ctx.globalStoragePath,
            file: prebuiltBinaryName,
            repo: {
                name: "rust-analyzer",
                owner: "rust-analyzer",
            }
        };
    }


    // FIXME: revisit the logic for `if (.has(...)) config.get(...)` set default
    // values only in one place (i.e. remove default values from non-readonly members declarations)
    private refresh(ctx: vscode.ExtensionContext) {
        const config = vscode.workspace.getConfiguration('rust-analyzer');

        let requireReloadMessage = null;

        if (config.has('highlightingOn')) {
            this.highlightingOn = config.get('highlightingOn') as boolean;
        }

        if (config.has('rainbowHighlightingOn')) {
            this.rainbowHighlightingOn = config.get(
                'rainbowHighlightingOn',
            ) as boolean;
        }

        if (config.has('enableEnhancedTyping')) {
            this.enableEnhancedTyping = config.get(
                'enableEnhancedTyping',
            ) as boolean;

            if (this.prevEnhancedTyping === null) {
                this.prevEnhancedTyping = this.enableEnhancedTyping;
            }
        } else if (this.prevEnhancedTyping === null) {
            this.prevEnhancedTyping = this.enableEnhancedTyping;
        }

        if (this.prevEnhancedTyping !== this.enableEnhancedTyping) {
            requireReloadMessage =
                'Changing enhanced typing setting requires a reload';
            this.prevEnhancedTyping = this.enableEnhancedTyping;
        }

        this.langServerSource = Config.langServerBinarySource(ctx, config);

        if (config.has('cargo-watch.enable')) {
            this.cargoWatchOptions.enable = config.get<boolean>(
                'cargo-watch.enable',
                true,
            );
        }

        if (config.has('cargo-watch.arguments')) {
            this.cargoWatchOptions.arguments = config.get<string[]>(
                'cargo-watch.arguments',
                [],
            );
        }

        if (config.has('cargo-watch.command')) {
            this.cargoWatchOptions.command = config.get<string>(
                'cargo-watch.command',
                '',
            );
        }

        if (config.has('cargo-watch.allTargets')) {
            this.cargoWatchOptions.allTargets = config.get<boolean>(
                'cargo-watch.allTargets',
                true,
            );
        }

        if (config.has('lruCapacity')) {
            this.lruCapacity = config.get('lruCapacity') as number;
        }

        if (config.has('displayInlayHints')) {
            this.displayInlayHints = config.get('displayInlayHints') as boolean;
        }
        if (config.has('maxInlayHintLength')) {
            this.maxInlayHintLength = config.get(
                'maxInlayHintLength',
            ) as number;
        }
        if (config.has('excludeGlobs')) {
            this.excludeGlobs = config.get('excludeGlobs') || [];
        }
        if (config.has('useClientWatching')) {
            this.useClientWatching = config.get('useClientWatching') || true;
        }
        if (config.has('featureFlags')) {
            this.featureFlags = config.get('featureFlags') || {};
        }
        if (config.has('withSysroot')) {
            this.withSysroot = config.get('withSysroot') || false;
        }

        if (config.has('cargoFeatures.noDefaultFeatures')) {
            this.cargoFeatures.noDefaultFeatures = config.get(
                'cargoFeatures.noDefaultFeatures',
                false,
            );
        }
        if (config.has('cargoFeatures.allFeatures')) {
            this.cargoFeatures.allFeatures = config.get(
                'cargoFeatures.allFeatures',
                true,
            );
        }
        if (config.has('cargoFeatures.features')) {
            this.cargoFeatures.features = config.get(
                'cargoFeatures.features',
                [],
            );
        }

        if (
            this.prevCargoFeatures !== null &&
            (this.cargoFeatures.allFeatures !==
                this.prevCargoFeatures.allFeatures ||
                this.cargoFeatures.noDefaultFeatures !==
                this.prevCargoFeatures.noDefaultFeatures ||
                this.cargoFeatures.features.length !==
                this.prevCargoFeatures.features.length ||
                this.cargoFeatures.features.some(
                    (v, i) => v !== this.prevCargoFeatures!.features[i],
                ))
        ) {
            requireReloadMessage = 'Changing cargo features requires a reload';
        }
        this.prevCargoFeatures = { ...this.cargoFeatures };

        if (this.prevCargoWatchOptions !== null) {
            const changed =
                this.cargoWatchOptions.enable !== this.prevCargoWatchOptions.enable ||
                this.cargoWatchOptions.command !== this.prevCargoWatchOptions.command ||
                this.cargoWatchOptions.allTargets !== this.prevCargoWatchOptions.allTargets ||
                this.cargoWatchOptions.arguments.length !== this.prevCargoWatchOptions.arguments.length ||
                this.cargoWatchOptions.arguments.some(
                    (v, i) => v !== this.prevCargoWatchOptions!.arguments[i],
                );
            if (changed) {
                requireReloadMessage = 'Changing cargo-watch options requires a reload';
            }
        }
        this.prevCargoWatchOptions = { ...this.cargoWatchOptions };

        if (requireReloadMessage !== null) {
            const reloadAction = 'Reload now';
            vscode.window
                .showInformationMessage(requireReloadMessage, reloadAction)
                .then(selectedAction => {
                    if (selectedAction === reloadAction) {
                        vscode.commands.executeCommand(
                            'workbench.action.reloadWindow',
                        );
                    }
                });
        }
    }
}