-
Notifications
You must be signed in to change notification settings - Fork 503
/
Copy pathExtensionCommands.ts
603 lines (495 loc) · 21.9 KB
/
ExtensionCommands.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import { LanguageClient, NotificationType, Position, Range, RequestType } from "vscode-languageclient";
import { IFeature } from "../feature";
import { Logger } from "../logging";
export interface IExtensionCommand {
name: string;
displayName: string;
}
export interface IExtensionCommandQuickPickItem extends vscode.QuickPickItem {
command: IExtensionCommand;
}
export const InvokeExtensionCommandRequestType =
new RequestType<InvokeExtensionCommandRequestArguments, void, void, void>(
"powerShell/invokeExtensionCommand");
export interface IEditorContext {
currentFileContent: string;
currentFileLanguage: string;
currentFilePath: string;
cursorPosition: Position;
selectionRange: Range;
}
export interface InvokeExtensionCommandRequestArguments {
name: string;
context: IEditorContext;
}
export const ExtensionCommandAddedNotificationType =
new NotificationType<IExtensionCommandAddedNotificationBody, void>(
"powerShell/extensionCommandAdded");
export interface IExtensionCommandAddedNotificationBody {
name: string;
displayName: string;
}
// ---------- Editor Operations ----------
function asRange(value: vscode.Range): Range {
if (value === undefined) {
return undefined;
} else if (value === null) {
return null;
}
return { start: asPosition(value.start), end: asPosition(value.end) };
}
function asPosition(value: vscode.Position): Position {
if (value === undefined) {
return undefined;
} else if (value === null) {
return null;
}
return { line: value.line, character: value.character };
}
function asCodeRange(value: Range): vscode.Range {
if (value === undefined) {
return undefined;
} else if (value === null) {
return null;
}
return new vscode.Range(asCodePosition(value.start), asCodePosition(value.end));
}
function asCodePosition(value: Position): vscode.Position {
if (value === undefined) {
return undefined;
} else if (value === null) {
return null;
}
return new vscode.Position(value.line, value.character);
}
export const GetEditorContextRequestType =
new RequestType<IGetEditorContextRequestArguments, IEditorContext, void, void>(
"editor/getEditorContext");
// tslint:disable-next-line:no-empty-interface
export interface IGetEditorContextRequestArguments {
}
enum EditorOperationResponse {
Unsupported = 0,
Completed,
}
export const InsertTextRequestType =
new RequestType<IInsertTextRequestArguments, EditorOperationResponse, void, void>(
"editor/insertText");
export interface IInsertTextRequestArguments {
filePath: string;
insertText: string;
insertRange: Range;
}
export const SetSelectionRequestType =
new RequestType<ISetSelectionRequestArguments, EditorOperationResponse, void, void>(
"editor/setSelection");
export interface ISetSelectionRequestArguments {
selectionRange: Range;
}
export const OpenFileRequestType =
new RequestType<IOpenFileDetails, EditorOperationResponse, void, void>(
"editor/openFile");
export interface IOpenFileDetails {
filePath: string;
preview: boolean;
}
export const NewFileRequestType =
new RequestType<string, EditorOperationResponse, void, void>(
"editor/newFile");
export const CloseFileRequestType =
new RequestType<string, EditorOperationResponse, void, void>(
"editor/closeFile");
export const SaveFileRequestType =
new RequestType<ISaveFileDetails, EditorOperationResponse, void, void>(
"editor/saveFile");
export const ShowErrorMessageRequestType =
new RequestType<string, EditorOperationResponse, void, void>(
"editor/showErrorMessage");
export const ShowWarningMessageRequestType =
new RequestType<string, EditorOperationResponse, void, void>(
"editor/showWarningMessage");
export const ShowInformationMessageRequestType =
new RequestType<string, EditorOperationResponse, void, void>(
"editor/showInformationMessage");
export const SetStatusBarMessageRequestType =
new RequestType<IStatusBarMessageDetails, EditorOperationResponse, void, void>(
"editor/setStatusBarMessage");
export interface ISaveFileDetails {
filePath: string;
newPath?: string;
}
export interface IStatusBarMessageDetails {
message: string;
timeout?: number;
}
interface IInvokeRegisteredEditorCommandParameter {
commandName: string;
}
export class ExtensionCommandsFeature implements IFeature {
private command: vscode.Disposable;
private command2: vscode.Disposable;
private languageClient: LanguageClient;
private extensionCommands: IExtensionCommand[] = [];
constructor(private log: Logger) {
this.command = vscode.commands.registerCommand("PowerShell.ShowAdditionalCommands", () => {
if (this.languageClient === undefined) {
this.log.writeAndShowError(`<${ExtensionCommandsFeature.name}>: ` +
"Unable to instantiate; language client undefined.");
return;
}
const editor = vscode.window.activeTextEditor;
let start = editor.selection.start;
const end = editor.selection.end;
if (editor.selection.isEmpty) {
start = new vscode.Position(start.line, 0);
}
this.showExtensionCommands(this.languageClient);
});
this.command2 = vscode.commands.registerCommand("PowerShell.InvokeRegisteredEditorCommand",
(param: IInvokeRegisteredEditorCommandParameter) => {
if (this.extensionCommands.length === 0) {
return;
}
const commandToExecute = this.extensionCommands.find((x) => x.name === param.commandName);
if (commandToExecute) {
this.languageClient.sendRequest(
InvokeExtensionCommandRequestType,
{ name: commandToExecute.name,
context: this.getEditorContext() });
}
});
}
public setLanguageClient(languageclient: LanguageClient) {
// Clear the current list of extension commands since they were
// only relevant to the previous session
this.extensionCommands = [];
this.languageClient = languageclient;
if (this.languageClient !== undefined) {
this.languageClient.onNotification(
ExtensionCommandAddedNotificationType,
(command) => this.addExtensionCommand(command));
this.languageClient.onRequest(
GetEditorContextRequestType,
(details) => this.getEditorContext());
this.languageClient.onRequest(
InsertTextRequestType,
(details) => this.insertText(details));
this.languageClient.onRequest(
SetSelectionRequestType,
(details) => this.setSelection(details));
this.languageClient.onRequest(
NewFileRequestType,
(filePath) => this.newFile());
this.languageClient.onRequest(
OpenFileRequestType,
(filePath) => this.openFile(filePath));
this.languageClient.onRequest(
CloseFileRequestType,
(filePath) => this.closeFile(filePath));
this.languageClient.onRequest(
SaveFileRequestType,
(saveFileDetails) => this.saveFile(saveFileDetails));
this.languageClient.onRequest(
ShowInformationMessageRequestType,
(message) => this.showInformationMessage(message));
this.languageClient.onRequest(
ShowErrorMessageRequestType,
(message) => this.showErrorMessage(message));
this.languageClient.onRequest(
ShowWarningMessageRequestType,
(message) => this.showWarningMessage(message));
this.languageClient.onRequest(
SetStatusBarMessageRequestType,
(messageDetails) => this.setStatusBarMessage(messageDetails));
}
}
public dispose() {
this.command.dispose();
this.command2.dispose();
}
private addExtensionCommand(command: IExtensionCommandAddedNotificationBody) {
this.extensionCommands.push({
name: command.name,
displayName: command.displayName,
});
this.extensionCommands.sort(
(a: IExtensionCommand, b: IExtensionCommand) =>
a.name.localeCompare(b.name));
}
private showExtensionCommands(client: LanguageClient): Thenable<InvokeExtensionCommandRequestArguments> {
// If no extension commands are available, show a message
if (this.extensionCommands.length === 0) {
vscode.window.showInformationMessage(
"No extension commands have been loaded into the current session.");
return;
}
const quickPickItems =
this.extensionCommands.map<IExtensionCommandQuickPickItem>((command) => {
return {
label: command.displayName,
description: command.name,
command,
};
});
vscode.window
.showQuickPick(
quickPickItems,
{ placeHolder: "Select a command" })
.then((command) => this.onCommandSelected(command, client));
}
private onCommandSelected(
chosenItem: IExtensionCommandQuickPickItem,
client: LanguageClient) {
if (chosenItem !== undefined) {
client.sendRequest(
InvokeExtensionCommandRequestType,
{ name: chosenItem.command.name,
context: this.getEditorContext() });
}
}
private insertText(details: IInsertTextRequestArguments): EditorOperationResponse {
const edit = new vscode.WorkspaceEdit();
edit.set(
vscode.Uri.parse(details.filePath),
[
new vscode.TextEdit(
new vscode.Range(
details.insertRange.start.line,
details.insertRange.start.character,
details.insertRange.end.line,
details.insertRange.end.character),
details.insertText),
],
);
vscode.workspace.applyEdit(edit);
return EditorOperationResponse.Completed;
}
private getEditorContext(): IEditorContext {
return {
currentFileContent: vscode.window.activeTextEditor.document.getText(),
currentFileLanguage: vscode.window.activeTextEditor.document.languageId,
currentFilePath: vscode.window.activeTextEditor.document.uri.toString(),
cursorPosition: asPosition(vscode.window.activeTextEditor.selection.active),
selectionRange:
asRange(
new vscode.Range(
vscode.window.activeTextEditor.selection.start,
vscode.window.activeTextEditor.selection.end)),
};
}
private newFile(): Thenable<EditorOperationResponse> {
return vscode.workspace.openTextDocument({ content: ""})
.then((doc) => vscode.window.showTextDocument(doc))
.then((_) => EditorOperationResponse.Completed);
}
private openFile(openFileDetails: IOpenFileDetails): Thenable<EditorOperationResponse> {
const filePath = this.normalizeFilePath(openFileDetails.filePath);
const promise =
vscode.workspace.openTextDocument(filePath)
.then((doc) => vscode.window.showTextDocument(
doc,
{ preview: openFileDetails.preview }))
.then((_) => EditorOperationResponse.Completed);
return promise;
}
private closeFile(filePath: string): Thenable<EditorOperationResponse> {
let promise: Thenable<EditorOperationResponse>;
if (this.findTextDocument(this.normalizeFilePath(filePath))) {
promise =
vscode.workspace.openTextDocument(filePath)
.then((doc) => vscode.window.showTextDocument(doc))
.then((editor) => vscode.commands.executeCommand("workbench.action.closeActiveEditor"))
.then((_) => EditorOperationResponse.Completed);
} else {
promise = Promise.resolve(EditorOperationResponse.Completed);
}
return promise;
}
/**
* Save a file, possibly to a new path. If the save is not possible, return a completed response
* @param saveFileDetails the object detailing the path of the file to save and optionally its new path to save to
*/
private async saveFile(saveFileDetails: ISaveFileDetails): Promise<EditorOperationResponse> {
// Try to interpret the filepath as a URI, defaulting to "file://" if we don't succeed
let currentFileUri: vscode.Uri;
if (saveFileDetails.filePath.startsWith("untitled") || saveFileDetails.filePath.startsWith("file")) {
currentFileUri = vscode.Uri.parse(saveFileDetails.filePath);
} else {
currentFileUri = vscode.Uri.file(saveFileDetails.filePath);
}
let newFileAbsolutePath: string;
switch (currentFileUri.scheme) {
case "file":
// If the file to save can't be found, just complete the request
if (!this.findTextDocument(this.normalizeFilePath(currentFileUri.fsPath))) {
this.log.writeAndShowError(`File to save not found: ${currentFileUri.fsPath}.`);
return EditorOperationResponse.Completed;
}
// If no newFile is given, just save the current file
if (!saveFileDetails.newPath) {
const doc = await vscode.workspace.openTextDocument(currentFileUri.fsPath);
if (doc.isDirty) {
await doc.save();
}
return EditorOperationResponse.Completed;
}
// Make sure we have an absolute path
if (path.isAbsolute(saveFileDetails.newPath)) {
newFileAbsolutePath = saveFileDetails.newPath;
} else {
// If not, interpret the path as relative to the current file
newFileAbsolutePath = path.join(path.dirname(currentFileUri.fsPath), saveFileDetails.newPath);
}
break;
case "untitled":
// We need a new name to save an untitled file
if (!saveFileDetails.newPath) {
// TODO: Create a class handle vscode warnings and errors so we can warn easily
// without logging
this.log.writeAndShowWarning(
"Cannot save untitled file. Try SaveAs(\"path/to/file.ps1\") instead.");
return EditorOperationResponse.Completed;
}
// Make sure we have an absolute path
if (path.isAbsolute(saveFileDetails.newPath)) {
newFileAbsolutePath = saveFileDetails.newPath;
} else {
// In fresh contexts, workspaceFolders is not defined...
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
this.log.writeAndShowWarning("Cannot save file to relative path: no workspaces are open. " +
"Try saving to an absolute path, or open a workspace.");
return EditorOperationResponse.Completed;
}
// If not, interpret the path as relative to the workspace root
const workspaceRootUri = vscode.workspace.workspaceFolders[0].uri;
// We don't support saving to a non-file URI-schemed workspace
if (workspaceRootUri.scheme !== "file") {
this.log.writeAndShowWarning(
"Cannot save untitled file to a relative path in an untitled workspace. " +
"Try saving to an absolute path or opening a workspace folder.");
return EditorOperationResponse.Completed;
}
newFileAbsolutePath = path.join(workspaceRootUri.fsPath, saveFileDetails.newPath);
}
break;
default:
// Other URI schemes are not supported
const msg = JSON.stringify(saveFileDetails);
this.log.writeVerbose(
`<${ExtensionCommandsFeature.name}>: Saving a document with scheme '${currentFileUri.scheme}' ` +
`is currently unsupported. Message: '${msg}'`);
return EditorOperationResponse.Completed;
}
await this.saveDocumentContentToAbsolutePath(currentFileUri, newFileAbsolutePath);
return EditorOperationResponse.Completed;
}
/**
* Take a document available to vscode at the given URI and save it to the given absolute path
* @param documentUri the URI of the vscode document to save
* @param destinationAbsolutePath the absolute path to save the document contents to
*/
private async saveDocumentContentToAbsolutePath(
documentUri: vscode.Uri,
destinationAbsolutePath: string): Promise<void> {
// Retrieve the text out of the current document
const oldDocument = await vscode.workspace.openTextDocument(documentUri);
// Write it to the new document path
try {
// TODO: Change this to be asyncronous
await new Promise<void>((resolve, reject) => {
fs.writeFile(destinationAbsolutePath, oldDocument.getText(), (err) => {
if (err) {
return reject(err);
}
return resolve();
});
});
} catch (e) {
this.log.writeAndShowWarning(`<${ExtensionCommandsFeature.name}>: ` +
`Unable to save file to path '${destinationAbsolutePath}': ${e}`);
return;
}
// Finally open the new document
const newFileUri = vscode.Uri.file(destinationAbsolutePath);
const newFile = await vscode.workspace.openTextDocument(newFileUri);
vscode.window.showTextDocument(newFile, { preview: true });
}
private normalizeFilePath(filePath: string): string {
const platform = os.platform();
if (platform === "win32") {
// Make sure the file path is absolute
if (!path.win32.isAbsolute(filePath)) {
filePath = path.win32.resolve(
vscode.workspace.rootPath,
filePath);
}
// Normalize file path case for comparison for Windows
return filePath.toLowerCase();
} else {
// Make sure the file path is absolute
if (!path.isAbsolute(filePath)) {
filePath = path.resolve(
vscode.workspace.rootPath,
filePath);
}
// macOS is case-insensitive
if (platform === "darwin") {
filePath = filePath.toLowerCase();
}
return filePath;
}
}
private findTextDocument(filePath: string): boolean {
// since Windows and macOS are case-insensitive, we need to normalize them differently
const canFind = vscode.workspace.textDocuments.find((doc) => {
let docPath;
const platform = os.platform();
if (platform === "win32" || platform === "darwin") {
// for Windows and macOS paths, they are normalized to be lowercase
docPath = doc.fileName.toLowerCase();
} else {
docPath = doc.fileName;
}
return docPath === filePath;
});
return canFind != null;
}
private setSelection(details: ISetSelectionRequestArguments): EditorOperationResponse {
vscode.window.activeTextEditor.selections = [
new vscode.Selection(
asCodePosition(details.selectionRange.start),
asCodePosition(details.selectionRange.end)),
];
return EditorOperationResponse.Completed;
}
private showInformationMessage(message: string): Thenable<EditorOperationResponse> {
return vscode.window
.showInformationMessage(message)
.then((_) => EditorOperationResponse.Completed);
}
private showErrorMessage(message: string): Thenable<EditorOperationResponse> {
return vscode.window
.showErrorMessage(message)
.then((_) => EditorOperationResponse.Completed);
}
private showWarningMessage(message: string): Thenable<EditorOperationResponse> {
return vscode.window
.showWarningMessage(message)
.then((_) => EditorOperationResponse.Completed);
}
private setStatusBarMessage(messageDetails: IStatusBarMessageDetails): EditorOperationResponse {
if (messageDetails.timeout) {
vscode.window.setStatusBarMessage(messageDetails.message, messageDetails.timeout);
} else {
vscode.window.setStatusBarMessage(messageDetails.message);
}
return EditorOperationResponse.Completed;
}
}