@@ -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}
0 commit comments