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