]> git.lizzy.rs Git - rust.git/blob - editors/code/src/config.ts
Default to client watching on VS Code
[rust.git] / editors / code / src / config.ts
1 import * as vscode from 'vscode';
2
3 import { Server } from './server';
4
5 const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
6
7 export type CargoWatchStartupOptions = 'ask' | 'enabled' | 'disabled';
8 export type CargoWatchTraceOptions = 'off' | 'error' | 'verbose';
9
10 export interface CargoWatchOptions {
11     enableOnStartup: CargoWatchStartupOptions;
12     arguments: string;
13     command: string;
14     trace: CargoWatchTraceOptions;
15     ignore: string[];
16 }
17
18 export interface CargoFeatures {
19     noDefaultFeatures: boolean;
20     allFeatures: boolean;
21     features: string[];
22 }
23
24 export class Config {
25     public highlightingOn = true;
26     public rainbowHighlightingOn = false;
27     public enableEnhancedTyping = true;
28     public raLspServerPath = RA_LSP_DEBUG || 'ra_lsp_server';
29     public lruCapacity: null | number = null;
30     public displayInlayHints = true;
31     public maxInlayHintLength: null | number = null;
32     public excludeGlobs = [];
33     public useClientWatching = true;
34     public featureFlags = {};
35     // for internal use
36     public withSysroot: null | boolean = null;
37     public cargoWatchOptions: CargoWatchOptions = {
38         enableOnStartup: 'ask',
39         trace: 'off',
40         arguments: '',
41         command: '',
42         ignore: [],
43     };
44     public cargoFeatures: CargoFeatures = {
45         noDefaultFeatures: false,
46         allFeatures: true,
47         features: [],
48     };
49
50     private prevEnhancedTyping: null | boolean = null;
51     private prevCargoFeatures: null | CargoFeatures = null;
52
53     constructor() {
54         vscode.workspace.onDidChangeConfiguration(_ =>
55             this.userConfigChanged(),
56         );
57         this.userConfigChanged();
58     }
59
60     public userConfigChanged() {
61         const config = vscode.workspace.getConfiguration('rust-analyzer');
62         let requireReloadMessage = null;
63
64         if (config.has('highlightingOn')) {
65             this.highlightingOn = config.get('highlightingOn') as boolean;
66         }
67
68         if (config.has('rainbowHighlightingOn')) {
69             this.rainbowHighlightingOn = config.get(
70                 'rainbowHighlightingOn',
71             ) as boolean;
72         }
73
74         if (!this.highlightingOn && Server) {
75             Server.highlighter.removeHighlights();
76         }
77
78         if (config.has('enableEnhancedTyping')) {
79             this.enableEnhancedTyping = config.get(
80                 'enableEnhancedTyping',
81             ) as boolean;
82
83             if (this.prevEnhancedTyping === null) {
84                 this.prevEnhancedTyping = this.enableEnhancedTyping;
85             }
86         } else if (this.prevEnhancedTyping === null) {
87             this.prevEnhancedTyping = this.enableEnhancedTyping;
88         }
89
90         if (this.prevEnhancedTyping !== this.enableEnhancedTyping) {
91             requireReloadMessage =
92                 'Changing enhanced typing setting requires a reload';
93             this.prevEnhancedTyping = this.enableEnhancedTyping;
94         }
95
96         if (config.has('raLspServerPath')) {
97             this.raLspServerPath =
98                 RA_LSP_DEBUG || (config.get('raLspServerPath') as string);
99         }
100
101         if (config.has('enableCargoWatchOnStartup')) {
102             this.cargoWatchOptions.enableOnStartup = config.get<
103                 CargoWatchStartupOptions
104             >('enableCargoWatchOnStartup', 'ask');
105         }
106
107         if (config.has('trace.cargo-watch')) {
108             this.cargoWatchOptions.trace = config.get<CargoWatchTraceOptions>(
109                 'trace.cargo-watch',
110                 'off',
111             );
112         }
113
114         if (config.has('cargo-watch.arguments')) {
115             this.cargoWatchOptions.arguments = config.get<string>(
116                 'cargo-watch.arguments',
117                 '',
118             );
119         }
120
121         if (config.has('cargo-watch.command')) {
122             this.cargoWatchOptions.command = config.get<string>(
123                 'cargo-watch.command',
124                 '',
125             );
126         }
127
128         if (config.has('cargo-watch.ignore')) {
129             this.cargoWatchOptions.ignore = config.get<string[]>(
130                 'cargo-watch.ignore',
131                 [],
132             );
133         }
134
135         if (config.has('lruCapacity')) {
136             this.lruCapacity = config.get('lruCapacity') as number;
137         }
138
139         if (config.has('displayInlayHints')) {
140             this.displayInlayHints = config.get('displayInlayHints') as boolean;
141         }
142         if (config.has('maxInlayHintLength')) {
143             this.maxInlayHintLength = config.get(
144                 'maxInlayHintLength',
145             ) as number;
146         }
147         if (config.has('excludeGlobs')) {
148             this.excludeGlobs = config.get('excludeGlobs') || [];
149         }
150         if (config.has('useClientWatching')) {
151             this.useClientWatching = config.get('useClientWatching') || true;
152         }
153         if (config.has('featureFlags')) {
154             this.featureFlags = config.get('featureFlags') || {};
155         }
156         if (config.has('withSysroot')) {
157             this.withSysroot = config.get('withSysroot') || false;
158         }
159
160         if (config.has('cargoFeatures.noDefaultFeatures')) {
161             this.cargoFeatures.noDefaultFeatures = config.get(
162                 'cargoFeatures.noDefaultFeatures',
163                 false,
164             );
165         }
166         if (config.has('cargoFeatures.allFeatures')) {
167             this.cargoFeatures.allFeatures = config.get(
168                 'cargoFeatures.allFeatures',
169                 true,
170             );
171         }
172         if (config.has('cargoFeatures.features')) {
173             this.cargoFeatures.features = config.get(
174                 'cargoFeatures.features',
175                 [],
176             );
177         }
178
179         if (
180             this.prevCargoFeatures !== null &&
181             (this.cargoFeatures.allFeatures !==
182                 this.prevCargoFeatures.allFeatures ||
183                 this.cargoFeatures.noDefaultFeatures !==
184                     this.prevCargoFeatures.noDefaultFeatures ||
185                 this.cargoFeatures.features.length !==
186                     this.prevCargoFeatures.features.length ||
187                 this.cargoFeatures.features.some(
188                     (v, i) => v !== this.prevCargoFeatures!.features[i],
189                 ))
190         ) {
191             requireReloadMessage = 'Changing cargo features requires a reload';
192         }
193         this.prevCargoFeatures = { ...this.cargoFeatures };
194
195         if (requireReloadMessage !== null) {
196             const reloadAction = 'Reload now';
197             vscode.window
198                 .showInformationMessage(requireReloadMessage, reloadAction)
199                 .then(selectedAction => {
200                     if (selectedAction === reloadAction) {
201                         vscode.commands.executeCommand(
202                             'workbench.action.reloadWindow',
203                         );
204                     }
205                 });
206         }
207     }
208 }