Skip to content

Commit b6c4c99

Browse files
committed
cell navigation
1 parent 5c2d04f commit b6c4c99

4 files changed

Lines changed: 62 additions & 13 deletions

File tree

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@
120120
"command": "jupyter:execCurrentCellAndAdvance",
121121
"title": "Run cell and advance",
122122
"category": "Jupyter"
123+
},
124+
{
125+
"command": "jupyter:gotToPreviousCell",
126+
"title": "Go to previous cell",
127+
"category": "Jupyter"
128+
},
129+
{
130+
"command": "jupyter:gotToNextCell",
131+
"title": "Go to next cell",
132+
"category": "Jupyter"
123133
}
124134
],
125135
"menus": {

src/client/common/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export namespace Commands {
3131
export const ExecuteCurrentCellAndAdvance = 'jupyter:execCurrentCellAndAdvance';
3232
export const AdcanceToCell = 'jupyter:advanceToNextCell';
3333
export const DisplayCellMenu = 'jupyter:displayCellMenu';
34+
export const GoToPreviousCell = 'jupyter:gotToPreviousCell';
35+
export const GoToNextCell = 'jupyter:gotToNextCell';
3436
}
3537
export namespace Kernel {
3638
export const Kernel_Interrupt = 'jupyter:kernelInterrupt';

src/client/jupyter/display/cellOptions.ts

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,75 @@ export class CellOptions extends vscode.Disposable {
1919
this.disposables.push(vscode.commands.registerCommand(Commands.Jupyter.Cell.AdcanceToCell, this.advanceToCell.bind(this)));
2020
this.disposables.push(vscode.commands.registerCommand(Commands.Jupyter.Cell.ExecuteCurrentCell, this.executeCell.bind(this, false)));
2121
this.disposables.push(vscode.commands.registerCommand(Commands.Jupyter.Cell.ExecuteCurrentCellAndAdvance, this.executeCell.bind(this, true)));
22+
this.disposables.push(vscode.commands.registerCommand(Commands.Jupyter.Cell.GoToNextCell, this.goToNextCell.bind(this)));
23+
this.disposables.push(vscode.commands.registerCommand(Commands.Jupyter.Cell.GoToPreviousCell, this.goToPreviousCell.bind(this)));
2224
}
23-
24-
private executeCell(advanceToNext: boolean): Thenable<any> {
25+
private getActiveCell(): Thenable<{ cell: vscode.Range, nextCell?: vscode.Range, previousCell?: vscode.Range }> {
2526
const activeEditor = vscode.window.activeTextEditor;
2627
if (!activeEditor || !activeEditor.document) {
27-
return Promise.resolve();
28+
return Promise.resolve(null);
2829
}
2930

3031
return this.cellCodeLenses.provideCodeLenses(activeEditor.document, null).then(lenses => {
3132
let currentCellRange: vscode.Range;
3233
let nextCellRange: vscode.Range;
34+
let previousCellRange: vscode.Range;
3335
lenses.forEach((lens, index) => {
3436
if (lens.range.contains(activeEditor.selection.start)) {
3537
currentCellRange = lens.range;
3638
if (index < (lenses.length - 1)) {
3739
nextCellRange = lenses[index + 1].range;
3840
}
41+
if (index > 0) {
42+
previousCellRange = lenses[index - 1].range;
43+
}
3944
}
4045
});
4146
if (!currentCellRange) {
47+
return null;
48+
}
49+
return { cell: currentCellRange, nextCell: nextCellRange, previousCell: previousCellRange };
50+
});
51+
}
52+
private goToPreviousCell(): Thenable<any> {
53+
const activeEditor = vscode.window.activeTextEditor;
54+
if (!activeEditor || !activeEditor.document) {
55+
return Promise.resolve();
56+
}
57+
return this.getActiveCell().then(cellInfo => {
58+
if (!cellInfo || !cellInfo.previousCell) {
59+
return;
60+
}
61+
return this.advanceToCell(activeEditor.document, cellInfo.previousCell);
62+
});
63+
}
64+
private goToNextCell(): Thenable<any> {
65+
const activeEditor = vscode.window.activeTextEditor;
66+
if (!activeEditor || !activeEditor.document) {
67+
return Promise.resolve();
68+
}
69+
return this.getActiveCell().then(cellInfo => {
70+
if (!cellInfo || !cellInfo.nextCell) {
71+
return;
72+
}
73+
return this.advanceToCell(activeEditor.document, cellInfo.nextCell);
74+
});
75+
}
76+
private executeCell(advanceToNext: boolean): Thenable<any> {
77+
const activeEditor = vscode.window.activeTextEditor;
78+
if (!activeEditor || !activeEditor.document) {
79+
return Promise.resolve();
80+
}
81+
82+
return this.getActiveCell().then(cellInfo => {
83+
if (!cellInfo || !cellInfo.cell) {
4284
return;
4385
}
44-
return vscode.commands.executeCommand(Commands.Jupyter.ExecuteRangeInKernel, activeEditor.document, currentCellRange).then(() => {
86+
return vscode.commands.executeCommand(Commands.Jupyter.ExecuteRangeInKernel, activeEditor.document, cellInfo.cell).then(() => {
4587
if (!advanceToNext) {
4688
return;
4789
}
48-
return this.advanceToCell(activeEditor.document, nextCellRange);
90+
return this.advanceToCell(activeEditor.document, cellInfo.nextCell);
4991
});
5092
});
5193
}
@@ -125,17 +167,13 @@ export class CellOptions extends vscode.Disposable {
125167
if (trimmedLine.startsWith('#')) {
126168
continue;
127169
}
128-
// if (trimmedLine.startsWith('%%') && trimmedLine.length > 2) {
129-
// return new vscode.Position(lineNumber, lineText.indexOf(trimmedLine) + 2);
130-
// }
131-
// if (trimmedLine.startsWith('%') && trimmedLine.length > 1) {
132-
// return new vscode.Position(lineNumber, lineText.indexOf(trimmedLine) + 1);
133-
// }
134170
// Yay we have a line
171+
// Remember, we need to set the cursor to a character other than white space
172+
// Highlighting doesn't kick in for comments or white space
135173
return new vscode.Position(lineNumber, lineText.indexOf(trimmedLine));
136174
}
137175

138-
// We give up
176+
// give up
139177
return new vscode.Position(startLine, 0);
140178
}
141179
}

src/client/jupyter/todo.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Use existing code that will use the path of the interpretter when looking for executables
1616
Similar to what we do today in utils.execPythonFile
1717

18-
- code lens (run should just run)
1918
- cell line height
2019
- navigate to cells (code navigation)
2120
- shortcuts

0 commit comments

Comments
 (0)