@@ -206,33 +206,24 @@ export class CodeWatcher implements ICodeWatcher {
206206 }
207207
208208 @captureTelemetry ( Telemetry . RunCell )
209- public async runCell ( range : Range ) {
210- if ( this . document ) {
211- // Use that to get our code.
212- const code = this . document . getText ( range ) ;
213-
214- try {
215- const activeHistory = await this . historyProvider . getOrCreateActive ( ) ;
216- await activeHistory . addCode ( code , this . getFileName ( ) , range . start . line , this . documentManager . activeTextEditor ) ;
217- } catch ( err ) {
218- this . handleError ( err ) ;
219- }
209+ public runCell ( range : Range ) : Promise < void > {
210+ if ( ! this . documentManager . activeTextEditor || ! this . documentManager . activeTextEditor . document ) {
211+ return Promise . resolve ( ) ;
220212 }
213+
214+ // Run the cell clicked. Advance if the cursor is inside this cell and we're allowed to
215+ const advance = range . contains ( this . documentManager . activeTextEditor . selection . start ) && this . configService . getSettings ( ) . datascience . enableAutoMoveToNextCell ;
216+ return this . runMatchingCell ( range , advance ) ;
221217 }
222218
223219 @captureTelemetry ( Telemetry . RunCurrentCell )
224- public async runCurrentCell ( ) {
220+ public runCurrentCell ( ) : Promise < void > {
225221 if ( ! this . documentManager . activeTextEditor || ! this . documentManager . activeTextEditor . document ) {
226- return ;
222+ return Promise . resolve ( ) ;
227223 }
228224
229- for ( const lens of this . codeLenses ) {
230- // Check to see which RunCell lens range overlaps the current selection start
231- if ( lens . range . contains ( this . documentManager . activeTextEditor . selection . start ) && lens . command && lens . command . command === Commands . RunCell ) {
232- await this . runCell ( lens . range ) ;
233- break ;
234- }
235- }
225+ // Run the cell that matches the current cursor position.
226+ return this . runMatchingCell ( this . documentManager . activeTextEditor . selection , false ) ;
236227 }
237228
238229 @captureTelemetry ( Telemetry . RunCurrentCellAndAdvance )
@@ -241,38 +232,67 @@ export class CodeWatcher implements ICodeWatcher {
241232 return ;
242233 }
243234
244- let currentRunCellLens : CodeLens | undefined ;
245- let nextRunCellLens : CodeLens | undefined ;
235+ // Run the cell that matches the current cursor position. Always advance
236+ return this . runMatchingCell ( this . documentManager . activeTextEditor . selection , true ) ;
237+ }
246238
247- for ( const lens of this . codeLenses ) {
248- // If we have already found the current code lens, then the next run cell code lens will give us the next cell
249- if ( currentRunCellLens && lens . command && lens . command . command === Commands . RunCell ) {
250- nextRunCellLens = lens ;
251- break ;
252- }
239+ public async addEmptyCellToBottom ( ) : Promise < void > {
240+ const editor = this . documentManager . activeTextEditor ;
241+ if ( editor ) {
242+ editor . edit ( ( editBuilder ) => {
243+ editBuilder . insert ( new Position ( editor . document . lineCount , 0 ) , '\n\n#%%\n' ) ;
244+ } ) ;
253245
254- // Check to see which RunCell lens range overlaps the current selection start
255- if ( lens . range . contains ( this . documentManager . activeTextEditor . selection . start ) && lens . command && lens . command . command === Commands . RunCell ) {
256- currentRunCellLens = lens ;
257- }
246+ const newPosition = new Position ( editor . document . lineCount + 3 , 0 ) ; // +3 to account for the added spaces and to position after the new mark
247+ return this . advanceToRange ( new Range ( newPosition , newPosition ) ) ;
258248 }
249+ }
250+
251+ private async runMatchingCell ( range : Range , advance ?: boolean ) {
252+ const currentRunCellLens = this . getCurrentCellLens ( range . start ) ;
253+ const nextRunCellLens = this . getNextCellLens ( range . start ) ;
259254
260255 if ( currentRunCellLens ) {
261- // Either use the next cell that we found, or add a new one into the document
262- let nextRange : Range ;
263- if ( ! nextRunCellLens ) {
264- nextRange = this . createNewCell ( currentRunCellLens . range ) ;
265- } else {
266- nextRange = nextRunCellLens . range ;
267- }
256+ // Move the next cell if allowed.
257+ if ( advance ) {
258+ // Either use the next cell that we found, or add a new one into the document
259+ let nextRange : Range ;
260+ if ( ! nextRunCellLens ) {
261+ nextRange = this . createNewCell ( currentRunCellLens . range ) ;
262+ } else {
263+ nextRange = nextRunCellLens . range ;
264+ }
268265
269- if ( nextRange ) {
270- this . advanceToRange ( nextRange ) ;
266+ if ( nextRange ) {
267+ this . advanceToRange ( nextRange ) ;
268+ }
271269 }
272270
273271 // Run the cell after moving the selection
274- await this . runCell ( currentRunCellLens . range ) ;
272+ if ( this . document ) {
273+ // Use that to get our code.
274+ const code = this . document . getText ( currentRunCellLens . range ) ;
275+
276+ try {
277+ const activeHistory = await this . historyProvider . getOrCreateActive ( ) ;
278+ await activeHistory . addCode ( code , this . getFileName ( ) , range . start . line , this . documentManager . activeTextEditor ) ;
279+ } catch ( err ) {
280+ this . handleError ( err ) ;
281+ }
282+ }
283+ }
284+ }
285+
286+ private getCurrentCellLens ( pos : Position ) : CodeLens | undefined {
287+ return this . codeLenses . find ( l => l . range . contains ( pos ) && l . command !== undefined && l . command . command === Commands . RunCell ) ;
288+ }
289+
290+ private getNextCellLens ( pos : Position ) : CodeLens | undefined {
291+ const currentIndex = this . codeLenses . findIndex ( l => l . range . contains ( pos ) && l . command !== undefined && l . command . command === Commands . RunCell ) ;
292+ if ( currentIndex >= 0 ) {
293+ return this . codeLenses . find ( ( l : CodeLens , i : number ) => l . command !== undefined && l . command . command === Commands . RunCell && i > currentIndex ) ;
275294 }
295+ return undefined ;
276296 }
277297
278298 private async runFileInteractiveInternal ( ) {
0 commit comments