forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathcommandRegistry.ts
More file actions
499 lines (445 loc) · 20.4 KB
/
commandRegistry.ts
File metadata and controls
499 lines (445 loc) · 20.4 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable, multiInject, named, optional } from 'inversify';
import { CodeLens, ConfigurationTarget, env, Range, Uri } from 'vscode';
import { DebugProtocol } from 'vscode-debugprotocol';
import { ICommandNameArgumentTypeMapping } from '../../common/application/commands';
import { IApplicationShell, ICommandManager, IDebugService, IDocumentManager } from '../../common/application/types';
import { Commands as coreCommands } from '../../common/constants';
import { IStartPage } from '../../common/startPage/types';
import { IConfigurationService, IDisposable, IOutputChannel } from '../../common/types';
import { DataScience } from '../../common/utils/localize';
import { noop } from '../../common/utils/misc';
import { captureTelemetry, sendTelemetryEvent } from '../../telemetry';
import { Commands, JUPYTER_OUTPUT_CHANNEL, Telemetry } from '../constants';
import { ColumnWarningSize, IDataViewerFactory } from '../data-viewing/types';
import { IShowDataViewerFromVariablePanel } from '../interactive-common/interactiveWindowTypes';
import { convertDebugProtocolVariableToIJupyterVariable } from '../jupyter/debuggerVariables';
import {
ICodeWatcher,
IDataScienceCodeLensProvider,
IDataScienceCommandListener,
IDataScienceFileSystem,
IJupyterVariableDataProviderFactory,
INotebookEditorProvider
} from '../types';
import { JupyterCommandLineSelectorCommand } from './commandLineSelector';
import { ExportCommands } from './exportCommands';
import { NotebookCommands } from './notebookCommands';
import { JupyterServerSelectorCommand } from './serverSelector';
@injectable()
export class CommandRegistry implements IDisposable {
private readonly disposables: IDisposable[] = [];
constructor(
@inject(IDocumentManager) private documentManager: IDocumentManager,
@inject(IDataScienceCodeLensProvider) private dataScienceCodeLensProvider: IDataScienceCodeLensProvider,
@multiInject(IDataScienceCommandListener)
@optional()
private commandListeners: IDataScienceCommandListener[] | undefined,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(JupyterServerSelectorCommand) private readonly serverSelectedCommand: JupyterServerSelectorCommand,
@inject(NotebookCommands) private readonly notebookCommands: NotebookCommands,
@inject(JupyterCommandLineSelectorCommand)
private readonly commandLineCommand: JupyterCommandLineSelectorCommand,
@inject(INotebookEditorProvider) private notebookEditorProvider: INotebookEditorProvider,
@inject(IDebugService) private debugService: IDebugService,
@inject(IConfigurationService) private configService: IConfigurationService,
@inject(IApplicationShell) private appShell: IApplicationShell,
@inject(IOutputChannel) @named(JUPYTER_OUTPUT_CHANNEL) private jupyterOutput: IOutputChannel,
@inject(IStartPage) private startPage: IStartPage,
@inject(ExportCommands) private readonly exportCommand: ExportCommands,
@inject(IJupyterVariableDataProviderFactory)
private readonly jupyterVariableDataProviderFactory: IJupyterVariableDataProviderFactory,
@inject(IDataViewerFactory) private readonly dataViewerFactory: IDataViewerFactory,
@inject(IDataScienceFileSystem) private readonly fs: IDataScienceFileSystem
) {
this.disposables.push(this.serverSelectedCommand);
this.disposables.push(this.notebookCommands);
}
public register() {
this.commandLineCommand.register();
this.serverSelectedCommand.register();
this.notebookCommands.register();
this.exportCommand.register();
this.registerCommand(Commands.RunAllCells, this.runAllCells);
this.registerCommand(Commands.RunCell, this.runCell);
this.registerCommand(Commands.RunCurrentCell, this.runCurrentCell);
this.registerCommand(Commands.RunCurrentCellAdvance, this.runCurrentCellAndAdvance);
this.registerCommand(Commands.ExecSelectionInInteractiveWindow, this.runSelectionOrLine);
this.registerCommand(Commands.RunAllCellsAbove, this.runAllCellsAbove);
this.registerCommand(Commands.RunCellAndAllBelow, this.runCellAndAllBelow);
this.registerCommand(Commands.InsertCellBelowPosition, this.insertCellBelowPosition);
this.registerCommand(Commands.InsertCellBelow, this.insertCellBelow);
this.registerCommand(Commands.InsertCellAbove, this.insertCellAbove);
this.registerCommand(Commands.DeleteCells, this.deleteCells);
this.registerCommand(Commands.SelectCell, this.selectCell);
this.registerCommand(Commands.SelectCellContents, this.selectCellContents);
this.registerCommand(Commands.ExtendSelectionByCellAbove, this.extendSelectionByCellAbove);
this.registerCommand(Commands.ExtendSelectionByCellBelow, this.extendSelectionByCellBelow);
this.registerCommand(Commands.MoveCellsUp, this.moveCellsUp);
this.registerCommand(Commands.MoveCellsDown, this.moveCellsDown);
this.registerCommand(Commands.ChangeCellToMarkdown, this.changeCellToMarkdown);
this.registerCommand(Commands.ChangeCellToCode, this.changeCellToCode);
this.registerCommand(Commands.GotoNextCellInFile, this.gotoNextCellInFile);
this.registerCommand(Commands.GotoPrevCellInFile, this.gotoPrevCellInFile);
this.registerCommand(Commands.RunAllCellsAbovePalette, this.runAllCellsAboveFromCursor);
this.registerCommand(Commands.RunCellAndAllBelowPalette, this.runCellAndAllBelowFromCursor);
this.registerCommand(Commands.RunToLine, this.runToLine);
this.registerCommand(Commands.RunFromLine, this.runFromLine);
this.registerCommand(Commands.RunFileInInteractiveWindows, this.runFileInteractive);
this.registerCommand(Commands.DebugFileInInteractiveWindows, this.debugFileInteractive);
this.registerCommand(Commands.AddCellBelow, this.addCellBelow);
this.registerCommand(Commands.RunCurrentCellAndAddBelow, this.runCurrentCellAndAddBelow);
this.registerCommand(Commands.DebugCell, this.debugCell);
this.registerCommand(Commands.DebugStepOver, this.debugStepOver);
this.registerCommand(Commands.DebugContinue, this.debugContinue);
this.registerCommand(Commands.DebugStop, this.debugStop);
this.registerCommand(Commands.DebugCurrentCellPalette, this.debugCurrentCellFromCursor);
this.registerCommand(Commands.CreateNewNotebook, this.createNewNotebook);
this.registerCommand(Commands.ViewJupyterOutput, this.viewJupyterOutput);
this.registerCommand(Commands.GatherQuality, this.reportGatherQuality);
this.registerCommand(Commands.LatestExtension, this.openPythonExtensionPage);
this.registerCommand(Commands.ShowDataViewer, this.onVariablePanelShowDataViewerRequest);
this.registerCommand(
Commands.EnableLoadingWidgetsFrom3rdPartySource,
this.enableLoadingWidgetScriptsFromThirdParty
);
this.registerCommand(coreCommands.OpenStartPage, this.openStartPage);
if (this.commandListeners) {
this.commandListeners.forEach((listener: IDataScienceCommandListener) => {
listener.register(this.commandManager);
});
}
}
public dispose() {
this.disposables.forEach((d) => d.dispose());
}
private registerCommand<
E extends keyof ICommandNameArgumentTypeMapping,
U extends ICommandNameArgumentTypeMapping[E]
// tslint:disable-next-line: no-any
>(command: E, callback: (...args: U) => any) {
const disposable = this.commandManager.registerCommand(command, callback, this);
this.disposables.push(disposable);
}
private getCodeWatcher(file: Uri | undefined): ICodeWatcher | undefined {
if (file) {
const possibleDocuments = this.documentManager.textDocuments.filter((d) =>
this.fs.arePathsSame(d.uri, file)
);
if (possibleDocuments && possibleDocuments.length === 1) {
return this.dataScienceCodeLensProvider.getCodeWatcher(possibleDocuments[0]);
} else if (possibleDocuments && possibleDocuments.length > 1) {
throw new Error(DataScience.documentMismatch().format(file.fsPath));
}
}
return undefined;
}
private enableLoadingWidgetScriptsFromThirdParty(): void {
if (this.configService.getSettings(undefined).datascience.widgetScriptSources.length > 0) {
return;
}
// Update the setting and once updated, notify user to restart kernel.
this.configService
.updateSetting(
'dataScience.widgetScriptSources',
['jsdelivr.com', 'unpkg.com'],
undefined,
ConfigurationTarget.Global
)
.then(() => {
// Let user know they'll need to restart the kernel.
this.appShell
.showInformationMessage(DataScience.loadThirdPartyWidgetScriptsPostEnabled())
.then(noop, noop);
})
.catch(noop);
}
private async runAllCells(file: Uri | undefined): Promise<void> {
let codeWatcher = this.getCodeWatcher(file);
if (!codeWatcher) {
codeWatcher = this.getCurrentCodeWatcher();
}
if (codeWatcher) {
return codeWatcher.runAllCells();
} else {
return;
}
}
private async runFileInteractive(file: Uri): Promise<void> {
let codeWatcher = this.getCodeWatcher(file);
if (!codeWatcher) {
codeWatcher = this.getCurrentCodeWatcher();
}
if (codeWatcher) {
return codeWatcher.runFileInteractive();
} else {
return;
}
}
private async debugFileInteractive(file: Uri): Promise<void> {
let codeWatcher = this.getCodeWatcher(file);
if (!codeWatcher) {
codeWatcher = this.getCurrentCodeWatcher();
}
if (codeWatcher) {
return codeWatcher.debugFileInteractive();
} else {
return;
}
}
// Note: see codewatcher.ts where the runcell command args are attached. The reason we don't have any
// objects for parameters is because they can't be recreated when passing them through the LiveShare API
private async runCell(
file: Uri,
startLine: number,
startChar: number,
endLine: number,
endChar: number
): Promise<void> {
const codeWatcher = this.getCodeWatcher(file);
if (codeWatcher) {
return codeWatcher.runCell(new Range(startLine, startChar, endLine, endChar));
}
}
private async runAllCellsAbove(file: Uri, stopLine: number, stopCharacter: number): Promise<void> {
if (file) {
const codeWatcher = this.getCodeWatcher(file);
if (codeWatcher) {
return codeWatcher.runAllCellsAbove(stopLine, stopCharacter);
}
}
}
private async runCellAndAllBelow(file: Uri | undefined, startLine: number, startCharacter: number): Promise<void> {
if (file) {
const codeWatcher = this.getCodeWatcher(file);
if (codeWatcher) {
return codeWatcher.runCellAndAllBelow(startLine, startCharacter);
}
}
}
private async runToLine(): Promise<void> {
const activeCodeWatcher = this.getCurrentCodeWatcher();
const textEditor = this.documentManager.activeTextEditor;
if (activeCodeWatcher && textEditor && textEditor.selection) {
return activeCodeWatcher.runToLine(textEditor.selection.start.line);
}
}
private async runFromLine(): Promise<void> {
const activeCodeWatcher = this.getCurrentCodeWatcher();
const textEditor = this.documentManager.activeTextEditor;
if (activeCodeWatcher && textEditor && textEditor.selection) {
return activeCodeWatcher.runFromLine(textEditor.selection.start.line);
}
}
private async runCurrentCell(): Promise<void> {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.runCurrentCell();
} else {
return;
}
}
private async runCurrentCellAndAdvance(): Promise<void> {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.runCurrentCellAndAdvance();
} else {
return;
}
}
private async runSelectionOrLine(): Promise<void> {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.runSelectionOrLine(this.documentManager.activeTextEditor);
} else {
return;
}
}
private async debugCell(
file: Uri,
startLine: number,
startChar: number,
endLine: number,
endChar: number
): Promise<void> {
if (file) {
const codeWatcher = this.getCodeWatcher(file);
if (codeWatcher) {
return codeWatcher.debugCell(new Range(startLine, startChar, endLine, endChar));
}
}
}
@captureTelemetry(Telemetry.DebugStepOver)
private async debugStepOver(): Promise<void> {
// Make sure that we are in debug mode
if (this.debugService.activeDebugSession) {
this.commandManager.executeCommand('workbench.action.debug.stepOver');
}
}
@captureTelemetry(Telemetry.DebugStop)
private async debugStop(): Promise<void> {
// Make sure that we are in debug mode
if (this.debugService.activeDebugSession) {
this.commandManager.executeCommand('workbench.action.debug.stop');
}
}
@captureTelemetry(Telemetry.DebugContinue)
private async debugContinue(): Promise<void> {
// Make sure that we are in debug mode
if (this.debugService.activeDebugSession) {
this.commandManager.executeCommand('workbench.action.debug.continue');
}
}
@captureTelemetry(Telemetry.AddCellBelow)
private async addCellBelow(): Promise<void> {
await this.getCurrentCodeWatcher()?.addEmptyCellToBottom();
}
private async runCurrentCellAndAddBelow(): Promise<void> {
this.getCurrentCodeWatcher()?.runCurrentCellAndAddBelow();
}
private async insertCellBelowPosition(): Promise<void> {
this.getCurrentCodeWatcher()?.insertCellBelowPosition();
}
private async insertCellBelow(): Promise<void> {
this.getCurrentCodeWatcher()?.insertCellBelow();
}
private async insertCellAbove(): Promise<void> {
this.getCurrentCodeWatcher()?.insertCellAbove();
}
private async deleteCells(): Promise<void> {
this.getCurrentCodeWatcher()?.deleteCells();
}
private async selectCell(): Promise<void> {
this.getCurrentCodeWatcher()?.selectCell();
}
private async selectCellContents(): Promise<void> {
this.getCurrentCodeWatcher()?.selectCellContents();
}
private async extendSelectionByCellAbove(): Promise<void> {
this.getCurrentCodeWatcher()?.extendSelectionByCellAbove();
}
private async extendSelectionByCellBelow(): Promise<void> {
this.getCurrentCodeWatcher()?.extendSelectionByCellBelow();
}
private async moveCellsUp(): Promise<void> {
this.getCurrentCodeWatcher()?.moveCellsUp();
}
private async moveCellsDown(): Promise<void> {
this.getCurrentCodeWatcher()?.moveCellsDown();
}
private async changeCellToMarkdown(): Promise<void> {
this.getCurrentCodeWatcher()?.changeCellToMarkdown();
}
private async changeCellToCode(): Promise<void> {
this.getCurrentCodeWatcher()?.changeCellToCode();
}
private async gotoNextCellInFile(): Promise<void> {
this.getCurrentCodeWatcher()?.gotoNextCell();
}
private async gotoPrevCellInFile(): Promise<void> {
this.getCurrentCodeWatcher()?.gotoPreviousCell();
}
private async runAllCellsAboveFromCursor(): Promise<void> {
const currentCodeLens = this.getCurrentCodeLens();
if (currentCodeLens) {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.runAllCellsAbove(
currentCodeLens.range.start.line,
currentCodeLens.range.start.character
);
}
} else {
return;
}
}
private async runCellAndAllBelowFromCursor(): Promise<void> {
const currentCodeLens = this.getCurrentCodeLens();
if (currentCodeLens) {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.runCellAndAllBelow(
currentCodeLens.range.start.line,
currentCodeLens.range.start.character
);
}
} else {
return;
}
}
private async debugCurrentCellFromCursor(): Promise<void> {
const currentCodeLens = this.getCurrentCodeLens();
if (currentCodeLens) {
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeCodeWatcher) {
return activeCodeWatcher.debugCurrentCell();
}
} else {
return;
}
}
private async createNewNotebook(): Promise<void> {
await this.notebookEditorProvider.createNew();
}
private async openStartPage(): Promise<void> {
sendTelemetryEvent(Telemetry.StartPageOpenedFromCommandPalette);
return this.startPage.open();
}
private viewJupyterOutput() {
this.jupyterOutput.show(true);
}
private getCurrentCodeLens(): CodeLens | undefined {
const activeEditor = this.documentManager.activeTextEditor;
const activeCodeWatcher = this.getCurrentCodeWatcher();
if (activeEditor && activeCodeWatcher) {
// Find the cell that matches
return activeCodeWatcher.getCodeLenses().find((c: CodeLens) => {
if (
c.range.end.line >= activeEditor.selection.anchor.line &&
c.range.start.line <= activeEditor.selection.anchor.line
) {
return true;
}
return false;
});
}
}
// Get our matching code watcher for the active document
private getCurrentCodeWatcher(): ICodeWatcher | undefined {
const activeEditor = this.documentManager.activeTextEditor;
if (!activeEditor || !activeEditor.document) {
return undefined;
}
// Ask our code lens provider to find the matching code watcher for the current document
return this.dataScienceCodeLensProvider.getCodeWatcher(activeEditor.document);
}
private reportGatherQuality(val: string) {
sendTelemetryEvent(Telemetry.GatherQualityReport, undefined, { result: val[0] === 'no' ? 'no' : 'yes' });
env.openExternal(Uri.parse(`https://aka.ms/gatherfeedback?succeed=${val[0]}`));
}
private openPythonExtensionPage() {
env.openExternal(Uri.parse(`https://marketplace.visualstudio.com/items?itemName=ms-python.python`));
}
private async onVariablePanelShowDataViewerRequest(request: IShowDataViewerFromVariablePanel) {
if (this.debugService.activeDebugSession) {
const jupyterVariable = convertDebugProtocolVariableToIJupyterVariable(
request.variable as DebugProtocol.Variable
);
try {
const jupyterVariableDataProvider = await this.jupyterVariableDataProviderFactory.create(
jupyterVariable
);
const dataFrameInfo = await jupyterVariableDataProvider.getDataFrameInfo();
const columnSize = dataFrameInfo?.columns?.length;
if (columnSize && columnSize <= ColumnWarningSize) {
const title: string = `${DataScience.dataExplorerTitle()} - ${jupyterVariable.name}`;
await this.dataViewerFactory.create(jupyterVariableDataProvider, title);
}
} catch (e) {
this.appShell.showErrorMessage(e.toString());
}
}
}
}