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