]> git.lizzy.rs Git - rust.git/blob - editors/code/src/config.ts
331936b5ece8deada79ed57091dcccae7b437a36
[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 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 showWorkspaceLoadedNotification = true;
24     public lruCapacity: null | number = null;
25     public displayInlayHints = true;
26     public maxInlayHintLength: null | number = null;
27     public excludeGlobs = [];
28     public useClientWatching = false;
29     public featureFlags = {};
30     public cargoWatchOptions: CargoWatchOptions = {
31         enableOnStartup: 'ask',
32         trace: 'off',
33         arguments: '',
34         command: '',
35         ignore: []
36     };
37
38     private prevEnhancedTyping: null | boolean = null;
39
40     constructor() {
41         vscode.workspace.onDidChangeConfiguration(_ =>
42             this.userConfigChanged()
43         );
44         this.userConfigChanged();
45     }
46
47     public userConfigChanged() {
48         const config = vscode.workspace.getConfiguration('rust-analyzer');
49         if (config.has('highlightingOn')) {
50             this.highlightingOn = config.get('highlightingOn') as boolean;
51         }
52
53         if (config.has('rainbowHighlightingOn')) {
54             this.rainbowHighlightingOn = config.get(
55                 'rainbowHighlightingOn'
56             ) as boolean;
57         }
58
59         if (config.has('showWorkspaceLoadedNotification')) {
60             this.showWorkspaceLoadedNotification = config.get(
61                 'showWorkspaceLoadedNotification'
62             ) as boolean;
63         }
64
65         if (!this.highlightingOn && Server) {
66             Server.highlighter.removeHighlights();
67         }
68
69         if (config.has('enableEnhancedTyping')) {
70             this.enableEnhancedTyping = config.get(
71                 'enableEnhancedTyping'
72             ) as boolean;
73
74             if (this.prevEnhancedTyping === null) {
75                 this.prevEnhancedTyping = this.enableEnhancedTyping;
76             }
77         } else if (this.prevEnhancedTyping === null) {
78             this.prevEnhancedTyping = this.enableEnhancedTyping;
79         }
80
81         if (this.prevEnhancedTyping !== this.enableEnhancedTyping) {
82             const reloadAction = 'Reload now';
83             vscode.window
84                 .showInformationMessage(
85                     'Changing enhanced typing setting requires a reload',
86                     reloadAction
87                 )
88                 .then(selectedAction => {
89                     if (selectedAction === reloadAction) {
90                         vscode.commands.executeCommand(
91                             'workbench.action.reloadWindow'
92                         );
93                     }
94                 });
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('lruCapacity')) {
138             this.lruCapacity = config.get('lruCapacity') as number;
139         }
140
141         if (config.has('displayInlayHints')) {
142             this.displayInlayHints = config.get('displayInlayHints') as boolean;
143         }
144         if (config.has('maxInlayHintLength')) {
145             this.maxInlayHintLength = config.get(
146                 'maxInlayHintLength'
147             ) as number;
148         }
149         if (config.has('excludeGlobs')) {
150             this.excludeGlobs = config.get('excludeGlobs') || [];
151         }
152         if (config.has('useClientWatching')) {
153             this.useClientWatching = config.get('useClientWatching') || false;
154         }
155         if (config.has('featureFlags')) {
156             this.featureFlags = config.get('featureFlags') || {};
157         }
158     }
159 }