Skip to content

Commit c08ef0b

Browse files
greazerJim Griesmer
and
Jim Griesmer
authored
Add cell navigation commands as well as a default set of keyboard bindings for most new interactive window cell commands (#13334)
* Add Goto Prev/Next Cell * Add goto previous cell * Remove unneeded changes * Add default keybindings for cell commands * Goto should be Go to Co-authored-by: Jim Griesmer <[email protected]>
1 parent 69fe281 commit c08ef0b

File tree

8 files changed

+150
-0
lines changed

8 files changed

+150
-0
lines changed

package.json

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,66 @@
229229
"linux": "ctrl+shift+-",
230230
"when": "editorTextFocus && inputFocus && notebookEditorFocused && notebookViewType == jupyter-notebook",
231231
"command": "notebook.cell.split"
232+
},
233+
{
234+
"command": "python.datascience.insertCellBelowPosition",
235+
"key": "ctrl+; s",
236+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
237+
},
238+
{
239+
"command": "python.datascience.insertCellBelow",
240+
"key": "ctrl+; b",
241+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
242+
},
243+
{
244+
"command": "python.datascience.insertCellAbove",
245+
"key": "ctrl+; a",
246+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
247+
},
248+
{
249+
"command": "python.datascience.deleteCells",
250+
"key": "ctrl+; x",
251+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
252+
},
253+
{
254+
"command": "python.datascience.extendSelectionByCellAbove",
255+
"key": "ctrl+alt+shift+[",
256+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
257+
},
258+
{
259+
"command": "python.datascience.extendSelectionByCellBelow",
260+
"key": "ctrl+alt+shift+]",
261+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
262+
},
263+
{
264+
"command": "python.datascience.moveCellsUp",
265+
"key": "ctrl+; u",
266+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
267+
},
268+
{
269+
"command": "python.datascience.moveCellsDown",
270+
"key": "ctrl+; d",
271+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
272+
},
273+
{
274+
"command": "python.datascience.changeCellToMarkdown",
275+
"key": "ctrl+; m",
276+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
277+
},
278+
{
279+
"command": "python.datascience.changeCellToCode",
280+
"key": "ctrl+; c",
281+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
282+
},
283+
{
284+
"command": "python.datascience.gotoNextCellInFile",
285+
"key": "ctrl+alt+]",
286+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
287+
},
288+
{
289+
"command": "python.datascience.gotoPrevCellInFile",
290+
"key": "ctrl+alt+[",
291+
"when": "editorTextFocus && python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
232292
}
233293
],
234294
"commands": [
@@ -588,6 +648,16 @@
588648
"title": "%python.command.python.datascience.changeCellToCode.title%",
589649
"category": "Python"
590650
},
651+
{
652+
"command": "python.datascience.gotoNextCellInFile",
653+
"title": "%python.command.python.datascience.gotoNextCellInFile.title%",
654+
"category": "Python"
655+
},
656+
{
657+
"command": "python.datascience.gotoPrevCellInFile",
658+
"title": "%python.command.python.datascience.gotoPrevCellInFile.title%",
659+
"category": "Python"
660+
},
591661
{
592662
"command": "python.datascience.runcurrentcelladvance",
593663
"title": "%python.command.python.datascience.runcurrentcelladvance.title%",
@@ -1148,6 +1218,18 @@
11481218
"category": "Python",
11491219
"when": "python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
11501220
},
1221+
{
1222+
"command": "python.datascience.gotoNextCellInFile",
1223+
"title": "%python.command.python.datascience.gotoNextCellInFile.title%",
1224+
"category": "Python",
1225+
"when": "python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
1226+
},
1227+
{
1228+
"command": "python.datascience.gotoPrevCellInFile",
1229+
"title": "%python.command.python.datascience.gotoPrevCellInFile.title%",
1230+
"category": "Python",
1231+
"when": "python.datascience.hascodecells && python.datascience.featureenabled && !notebookEditorFocused"
1232+
},
11511233
{
11521234
"command": "python.datascience.runcurrentcell",
11531235
"title": "%python.command.python.datascience.runcurrentcell.title%",

package.nls.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
"python.command.python.datascience.moveCellsDown.title": "Move Selected Cells Down",
8686
"python.command.python.datascience.changeCellToMarkdown.title": "Change Cell to Markdown",
8787
"python.command.python.datascience.changeCellToCode.title": "Change Cell to Code",
88+
"python.command.python.datascience.gotoNextCellInFile.title": "Go to Next Cell",
89+
"python.command.python.datascience.gotoPrevCellInFile.title": "Go to Previous Cell",
8890
"python.command.python.datascience.showhistorypane.title": "Show Python Interactive Window",
8991
"python.command.python.datascience.createnewinteractive.title": "Create Python Interactive Window",
9092
"python.command.python.datascience.selectjupyteruri.title": "Specify local or remote Jupyter server for connections",

src/client/common/application/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ export interface ICommandNameArgumentTypeMapping extends ICommandNameWithoutArgu
184184
[DSCommands.MoveCellsDown]: [];
185185
[DSCommands.ChangeCellToMarkdown]: [];
186186
[DSCommands.ChangeCellToCode]: [];
187+
[DSCommands.GotoNextCellInFile]: [];
188+
[DSCommands.GotoPrevCellInFile]: [];
187189
[DSCommands.ScrollToCell]: [Uri, string];
188190
[DSCommands.ViewJupyterOutput]: [];
189191
[DSCommands.ExportAsPythonScript]: [INotebookModel];

src/client/datascience/commands/commandRegistry.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export class CommandRegistry implements IDisposable {
7777
this.registerCommand(Commands.MoveCellsDown, this.moveCellsDown);
7878
this.registerCommand(Commands.ChangeCellToMarkdown, this.changeCellToMarkdown);
7979
this.registerCommand(Commands.ChangeCellToCode, this.changeCellToCode);
80+
this.registerCommand(Commands.GotoNextCellInFile, this.gotoNextCellInFile);
81+
this.registerCommand(Commands.GotoPrevCellInFile, this.gotoPrevCellInFile);
8082
this.registerCommand(Commands.RunAllCellsAbovePalette, this.runAllCellsAboveFromCursor);
8183
this.registerCommand(Commands.RunCellAndAllBelowPalette, this.runCellAndAllBelowFromCursor);
8284
this.registerCommand(Commands.RunToLine, this.runToLine);
@@ -366,6 +368,14 @@ export class CommandRegistry implements IDisposable {
366368
this.getCurrentCodeWatcher()?.changeCellToCode();
367369
}
368370

371+
private async gotoNextCellInFile(): Promise<void> {
372+
this.getCurrentCodeWatcher()?.gotoNextCell();
373+
}
374+
375+
private async gotoPrevCellInFile(): Promise<void> {
376+
this.getCurrentCodeWatcher()?.gotoPreviousCell();
377+
}
378+
369379
private async runAllCellsAboveFromCursor(): Promise<void> {
370380
const currentCodeLens = this.getCurrentCodeLens();
371381
if (currentCodeLens) {

src/client/datascience/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export namespace Commands {
9292
export const MoveCellsDown = 'python.datascience.moveCellsDown';
9393
export const ChangeCellToMarkdown = 'python.datascience.changeCellToMarkdown';
9494
export const ChangeCellToCode = 'python.datascience.changeCellToCode';
95+
export const GotoNextCellInFile = 'python.datascience.gotoNextCellInFile';
96+
export const GotoPrevCellInFile = 'python.datascience.gotoPrevCellInFile';
9597
export const ScrollToCell = 'python.datascience.scrolltocell';
9698
export const CreateNewNotebook = 'python.datascience.createnewnotebook';
9799
export const ViewJupyterOutput = 'python.datascience.viewJupyterOutput';
@@ -189,6 +191,8 @@ export enum Telemetry {
189191
MoveCellsDown = 'DATASCIENCE.RUN_MOVE_CELLS_DOWN',
190192
ChangeCellToMarkdown = 'DATASCIENCE.RUN_CHANGE_CELL_TO_MARKDOWN',
191193
ChangeCellToCode = 'DATASCIENCE.RUN_CHANGE_CELL_TO_CODE',
194+
GotoNextCellInFile = 'DATASCIENCE.GOTO_NEXT_CELL_IN_FILE',
195+
GotoPrevCellInFile = 'DATASCIENCE.GOTO_PREV_CELL_IN_FILE',
192196
RunSelectionOrLine = 'DATASCIENCE.RUN_SELECTION_OR_LINE',
193197
RunToLine = 'DATASCIENCE.RUN_TO_LINE',
194198
RunFromLine = 'DATASCIENCE.RUN_FROM_LINE',

src/client/datascience/editor-integration/codewatcher.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,40 @@ export class CodeWatcher implements ICodeWatcher {
654654
});
655655
}
656656

657+
@captureTelemetry(Telemetry.GotoNextCellInFile)
658+
public gotoNextCell() {
659+
const editor = this.documentManager.activeTextEditor;
660+
if (!editor || !editor.selection) {
661+
return;
662+
}
663+
664+
const currentSelection = editor.selection;
665+
666+
const currentRunCellLens = this.getCurrentCellLens(currentSelection.start);
667+
const nextRunCellLens = this.getNextCellLens(currentSelection.start);
668+
669+
if (currentRunCellLens && nextRunCellLens) {
670+
this.advanceToRange(nextRunCellLens.range);
671+
}
672+
}
673+
674+
@captureTelemetry(Telemetry.GotoPrevCellInFile)
675+
public gotoPreviousCell() {
676+
const editor = this.documentManager.activeTextEditor;
677+
if (!editor || !editor.selection) {
678+
return;
679+
}
680+
681+
const currentSelection = editor.selection;
682+
683+
const currentRunCellLens = this.getCurrentCellLens(currentSelection.start);
684+
const prevRunCellLens = this.getPreviousCellLens(currentSelection.start);
685+
686+
if (currentRunCellLens && prevRunCellLens) {
687+
this.advanceToRange(prevRunCellLens.range);
688+
}
689+
}
690+
657691
private applyToCells(callback: (editor: TextEditor, cell: ICellRange, cellIndex: number) => void) {
658692
const editor = this.documentManager.activeTextEditor;
659693
const startEndCellIndex = this.getStartEndCellIndex(editor?.selection);
@@ -1053,6 +1087,18 @@ export class CodeWatcher implements ICodeWatcher {
10531087
return undefined;
10541088
}
10551089

1090+
private getPreviousCellLens(pos: Position): CodeLens | undefined {
1091+
const currentIndex = this.codeLenses.findIndex(
1092+
(l) => l.range.contains(pos) && l.command !== undefined && l.command.command === Commands.RunCell
1093+
);
1094+
if (currentIndex >= 1) {
1095+
return this.codeLenses.find(
1096+
(l: CodeLens, i: number) => l.command !== undefined && i < currentIndex && i + 1 === currentIndex
1097+
);
1098+
}
1099+
return undefined;
1100+
}
1101+
10561102
private async runFileInteractiveInternal(debug: boolean) {
10571103
if (this.document) {
10581104
const code = this.document.getText();

src/client/datascience/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ export interface ICodeWatcher {
682682
changeCellToMarkdown(): void;
683683
changeCellToCode(): void;
684684
debugCurrentCell(): Promise<void>;
685+
gotoNextCell(): void;
686+
gotoPreviousCell(): void;
685687
}
686688

687689
export const ICodeLensFactory = Symbol('ICodeLensFactory');

src/client/telemetry/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,8 @@ export interface IEventNamePropertyMapping {
17471747
[Telemetry.MoveCellsDown]: never | undefined;
17481748
[Telemetry.ChangeCellToMarkdown]: never | undefined;
17491749
[Telemetry.ChangeCellToCode]: never | undefined;
1750+
[Telemetry.GotoNextCellInFile]: never | undefined;
1751+
[Telemetry.GotoPrevCellInFile]: never | undefined;
17501752
/**
17511753
* Misc
17521754
*/

0 commit comments

Comments
 (0)