-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathsettings.ts
364 lines (319 loc) · 13 KB
/
settings.ts
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
"use strict";
import vscode = require("vscode");
import utils = require("./utils");
import os = require("os");
enum CodeFormattingPreset {
Custom,
Allman,
OTBS,
Stroustrup,
}
enum PipelineIndentationStyle {
IncreaseIndentationForFirstPipeline,
IncreaseIndentationAfterEveryPipeline,
NoIndentation,
None,
}
export enum CommentType {
Disabled = "Disabled",
BlockComment = "BlockComment",
LineComment = "LineComment",
}
export interface IPowerShellAdditionalExePathSettings {
[versionName: string]: string;
}
export interface IBugReportingSettings {
project: string;
}
export interface ICodeFoldingSettings {
enable?: boolean;
showLastLine?: boolean;
}
export interface ICodeFormattingSettings {
autoCorrectAliases: boolean;
preset: CodeFormattingPreset;
openBraceOnSameLine: boolean;
newLineAfterOpenBrace: boolean;
newLineAfterCloseBrace: boolean;
pipelineIndentationStyle: PipelineIndentationStyle;
whitespaceBeforeOpenBrace: boolean;
whitespaceBeforeOpenParen: boolean;
whitespaceAroundOperator: boolean;
whitespaceAfterSeparator: boolean;
whitespaceBetweenParameters: boolean;
whitespaceInsideBrace: boolean;
addWhitespaceAroundPipe: boolean;
trimWhitespaceAroundPipe: boolean;
ignoreOneLineBlock: boolean;
alignPropertyValuePairs: boolean;
useConstantStrings: boolean;
useCorrectCasing: boolean;
}
export interface IScriptAnalysisSettings {
enable?: boolean;
settingsPath: string;
}
export interface IDebuggingSettings {
createTemporaryIntegratedConsole?: boolean;
}
export interface IDeveloperSettings {
featureFlags?: string[];
bundledModulesPath?: string;
editorServicesLogLevel?: string;
editorServicesWaitForDebugger?: boolean;
waitForSessionFileTimeoutSeconds?: number;
}
export interface ISettings {
powerShellAdditionalExePaths?: IPowerShellAdditionalExePathSettings;
powerShellDefaultVersion?: string;
// This setting is no longer used but is here to assist in cleaning up the users settings.
powerShellExePath?: string;
promptToUpdatePowerShell?: boolean;
bundledModulesPath?: string;
startAsLoginShell?: IStartAsLoginShellSettings;
startAutomatically?: boolean;
enableProfileLoading?: boolean;
helpCompletion: string;
scriptAnalysis?: IScriptAnalysisSettings;
debugging?: IDebuggingSettings;
developer?: IDeveloperSettings;
codeFolding?: ICodeFoldingSettings;
codeFormatting?: ICodeFormattingSettings;
integratedConsole?: IIntegratedConsoleSettings;
bugReporting?: IBugReportingSettings;
sideBar?: ISideBarSettings;
pester?: IPesterSettings;
buttons?: IButtonSettings;
cwd?: string;
notebooks?: INotebooksSettings;
enableReferencesCodeLens?: boolean;
}
export interface IStartAsLoginShellSettings {
osx?: boolean;
linux?: boolean;
}
export interface IIntegratedConsoleSettings {
showOnStartup?: boolean;
startInBackground?: boolean;
focusConsoleOnExecute?: boolean;
useLegacyReadLine?: boolean;
forceClearScrollbackBuffer?: boolean;
suppressStartupBanner?: boolean;
}
export interface ISideBarSettings {
CommandExplorerVisibility?: boolean;
}
export interface IPesterSettings {
useLegacyCodeLens?: boolean;
outputVerbosity?: string;
debugOutputVerbosity?: string;
}
export interface IButtonSettings {
showRunButtons?: boolean;
showPanelMovementButtons?: boolean;
}
export interface INotebooksSettings {
saveMarkdownCellsAs?: CommentType;
}
// TODO: This could probably be async, and call `validateCwdSetting()` directly.
export function load(): ISettings {
const configuration: vscode.WorkspaceConfiguration =
vscode.workspace.getConfiguration(utils.PowerShellLanguageId);
const defaultBugReportingSettings: IBugReportingSettings = {
project: "https://github.com/PowerShell/vscode-powershell",
};
const defaultScriptAnalysisSettings: IScriptAnalysisSettings = {
enable: true,
settingsPath: "PSScriptAnalyzerSettings.psd1",
};
const defaultDebuggingSettings: IDebuggingSettings = {
createTemporaryIntegratedConsole: false,
};
const defaultDeveloperSettings: IDeveloperSettings = {
featureFlags: [],
// From `<root>/out/main.js` we go to the directory before <root> and
// then into the other repo.
bundledModulesPath: "../../PowerShellEditorServices/module",
editorServicesLogLevel: "Normal",
editorServicesWaitForDebugger: false,
waitForSessionFileTimeoutSeconds: 240,
};
const defaultCodeFoldingSettings: ICodeFoldingSettings = {
enable: true,
showLastLine: false,
};
const defaultCodeFormattingSettings: ICodeFormattingSettings = {
autoCorrectAliases: false,
preset: CodeFormattingPreset.Custom,
openBraceOnSameLine: true,
newLineAfterOpenBrace: true,
newLineAfterCloseBrace: true,
pipelineIndentationStyle: PipelineIndentationStyle.NoIndentation,
whitespaceBeforeOpenBrace: true,
whitespaceBeforeOpenParen: true,
whitespaceAroundOperator: true,
whitespaceAfterSeparator: true,
whitespaceBetweenParameters: false,
whitespaceInsideBrace: true,
addWhitespaceAroundPipe: true,
trimWhitespaceAroundPipe: false,
ignoreOneLineBlock: true,
alignPropertyValuePairs: true,
useConstantStrings: false,
useCorrectCasing: false,
};
const defaultStartAsLoginShellSettings: IStartAsLoginShellSettings = {
osx: true,
linux: false,
};
const defaultIntegratedConsoleSettings: IIntegratedConsoleSettings = {
showOnStartup: true,
startInBackground: false,
focusConsoleOnExecute: true,
useLegacyReadLine: false,
forceClearScrollbackBuffer: false,
};
const defaultSideBarSettings: ISideBarSettings = {
CommandExplorerVisibility: true,
};
const defaultButtonSettings: IButtonSettings = {
showRunButtons: true,
showPanelMovementButtons: false
};
const defaultPesterSettings: IPesterSettings = {
useLegacyCodeLens: true,
outputVerbosity: "FromPreference",
debugOutputVerbosity: "Diagnostic",
};
const defaultNotebooksSettings: INotebooksSettings = {
saveMarkdownCellsAs: CommentType.BlockComment,
};
return {
startAutomatically:
configuration.get<boolean>("startAutomatically", true),
powerShellAdditionalExePaths:
configuration.get<IPowerShellAdditionalExePathSettings>("powerShellAdditionalExePaths", undefined),
powerShellDefaultVersion:
configuration.get<string>("powerShellDefaultVersion", undefined),
powerShellExePath:
configuration.get<string>("powerShellExePath", undefined),
promptToUpdatePowerShell:
configuration.get<boolean>("promptToUpdatePowerShell", true),
bundledModulesPath:
"../modules", // Because the extension is always at `<root>/out/main.js`
enableProfileLoading:
configuration.get<boolean>("enableProfileLoading", false),
helpCompletion:
configuration.get<string>("helpCompletion", CommentType.BlockComment),
scriptAnalysis:
configuration.get<IScriptAnalysisSettings>("scriptAnalysis", defaultScriptAnalysisSettings),
debugging:
configuration.get<IDebuggingSettings>("debugging", defaultDebuggingSettings),
developer:
getWorkspaceSettingsWithDefaults<IDeveloperSettings>(configuration, "developer", defaultDeveloperSettings),
codeFolding:
configuration.get<ICodeFoldingSettings>("codeFolding", defaultCodeFoldingSettings),
codeFormatting:
configuration.get<ICodeFormattingSettings>("codeFormatting", defaultCodeFormattingSettings),
integratedConsole:
configuration.get<IIntegratedConsoleSettings>("integratedConsole", defaultIntegratedConsoleSettings),
bugReporting:
configuration.get<IBugReportingSettings>("bugReporting", defaultBugReportingSettings),
sideBar:
configuration.get<ISideBarSettings>("sideBar", defaultSideBarSettings),
pester:
configuration.get<IPesterSettings>("pester", defaultPesterSettings),
buttons:
configuration.get<IButtonSettings>("buttons", defaultButtonSettings),
notebooks:
configuration.get<INotebooksSettings>("notebooks", defaultNotebooksSettings),
startAsLoginShell:
// tslint:disable-next-line
// We follow the same convention as VS Code - https://github.com/microsoft/vscode/blob/ff00badd955d6cfcb8eab5f25f3edc86b762f49f/src/vs/workbench/contrib/terminal/browser/terminal.contribution.ts#L105-L107
// "Unlike on Linux, ~/.profile is not sourced when logging into a macOS session. This
// is the reason terminals on macOS typically run login shells by default which set up
// the environment. See http://unix.stackexchange.com/a/119675/115410"
configuration.get<IStartAsLoginShellSettings>("startAsLoginShell", defaultStartAsLoginShellSettings),
cwd: // NOTE: This must be validated at startup via `validateCwdSetting()`. There's probably a better way to do this.
configuration.get<string>("cwd", undefined),
enableReferencesCodeLens:
configuration.get<boolean>("enableReferencesCodeLens", true),
};
}
// Get the ConfigurationTarget (read: scope) of where the *effective* setting value comes from
export async function getEffectiveConfigurationTarget(settingName: string): Promise<vscode.ConfigurationTarget> {
const configuration = vscode.workspace.getConfiguration(utils.PowerShellLanguageId);
const detail = configuration.inspect(settingName);
let configurationTarget = null;
if (typeof detail.workspaceFolderValue !== "undefined") {
configurationTarget = vscode.ConfigurationTarget.WorkspaceFolder;
}
else if (typeof detail.workspaceValue !== "undefined") {
configurationTarget = vscode.ConfigurationTarget.Workspace;
}
else if (typeof detail.globalValue !== "undefined") {
configurationTarget = vscode.ConfigurationTarget.Global;
}
return configurationTarget;
}
export async function change(
settingName: string,
newValue: any,
configurationTarget?: vscode.ConfigurationTarget | boolean): Promise<void> {
const configuration = vscode.workspace.getConfiguration(utils.PowerShellLanguageId);
// TODO: Consider wrapping with try/catch, but we can't log the error.
await configuration.update(settingName, newValue, configurationTarget);
}
function getWorkspaceSettingsWithDefaults<TSettings>(
workspaceConfiguration: vscode.WorkspaceConfiguration,
settingName: string,
defaultSettings: TSettings): TSettings {
const importedSettings: TSettings = workspaceConfiguration.get<TSettings>(settingName, defaultSettings);
for (const setting in importedSettings) {
if (importedSettings[setting]) {
defaultSettings[setting] = importedSettings[setting];
}
}
return defaultSettings;
}
// We don't want to query the user more than once, so this is idempotent.
let hasPrompted: boolean = false;
export async function validateCwdSetting(): Promise<string> {
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", undefined);
// Only use the cwd setting if it exists.
if (await utils.checkIfDirectoryExists(cwd)) {
return cwd;
}
// If there is no workspace, or there is but it has no folders, fallback.
if (vscode.workspace.workspaceFolders === undefined
|| vscode.workspace.workspaceFolders?.length === 0) {
cwd = undefined;
// If there is exactly one workspace folder, use that.
} if (vscode.workspace.workspaceFolders?.length === 1) {
cwd = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
// If there is more than one workspace folder, prompt the user once.
} else if (vscode.workspace.workspaceFolders?.length > 1 && !hasPrompted) {
hasPrompted = true;
const options: vscode.WorkspaceFolderPickOptions = {
placeHolder: "Select a folder to use as the PowerShell extension's working directory.",
}
cwd = (await vscode.window.showWorkspaceFolderPick(options))?.uri.fsPath;
// Save the picked 'cwd' to the workspace settings.
// We have to check again because the user may not have picked.
if (await utils.checkIfDirectoryExists(cwd)) {
try {
await change("cwd", cwd);
} catch {
// Could fail if workspace file is invalid.
}
}
}
// If there were no workspace folders, or somehow they don't exist, use
// the home directory.
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
return os.homedir();
}
return cwd;
}