5
5
6
6
import { Event } from 'vs/base/common/event' ;
7
7
import { IDisposable } from 'vs/base/common/lifecycle' ;
8
+ import { assertType } from 'vs/base/common/types' ;
8
9
import { ICodeEditor } from 'vs/editor/browser/editorBrowser' ;
9
10
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService' ;
10
11
import { StableEditorScrollState } from 'vs/editor/browser/stableEditorScroll' ;
@@ -27,6 +28,8 @@ import { IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/
27
28
28
29
export abstract class EditModeStrategy {
29
30
31
+ protected _initialPosition : Position | undefined ;
32
+
30
33
abstract dispose ( ) : void ;
31
34
32
35
abstract checkChanges ( response : EditResponse ) : boolean ;
@@ -41,16 +44,15 @@ export abstract class EditModeStrategy {
41
44
42
45
abstract toggleDiff ( ) : void ;
43
46
44
- abstract getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined ;
45
-
46
47
abstract hasFocus ( ) : boolean ;
48
+
49
+ abstract getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined ;
47
50
}
48
51
49
52
export class PreviewStrategy extends EditModeStrategy {
50
53
51
54
private readonly _ctxDocumentChanged : IContextKey < boolean > ;
52
55
private readonly _listener : IDisposable ;
53
- private _initialPosition : Position | undefined ;
54
56
55
57
constructor (
56
58
private readonly _session : Session ,
@@ -134,28 +136,13 @@ export class PreviewStrategy extends EditModeStrategy {
134
136
}
135
137
136
138
getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
137
- console . log ( 'inside of preview strategy' ) ;
138
139
const viewModel = this . _editor . _getViewModel ( ) ;
139
- if ( ! viewModel ) {
140
- return ;
141
- }
142
- const primaryCursorPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
140
+ assertType ( viewModel ) ;
143
141
if ( initialRender ) {
144
- this . _initialPosition = primaryCursorPosition ;
142
+ this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
145
143
return this . _initialPosition ;
146
144
} else {
147
- if ( hasEditResponse ) {
148
- const visibleRange = viewModel . getCompletelyVisibleViewRange ( ) ;
149
- const endPosition = range . getEndPosition ( ) ;
150
- const endLine = endPosition . lineNumber ;
151
- if ( endLine >= visibleRange . startLineNumber && endLine <= visibleRange . endLineNumber ) {
152
- return endPosition ;
153
- } else {
154
- return this . _initialPosition ;
155
- }
156
- } else {
157
- return this . _initialPosition ;
158
- }
145
+ return minimalJumpPosition ( this . _editor , this . _initialPosition , range , hasEditResponse ) ;
159
146
}
160
147
}
161
148
@@ -240,7 +227,6 @@ export class LiveStrategy extends EditModeStrategy {
240
227
private readonly _ctxShowingDiff : IContextKey < boolean > ;
241
228
private _lastResponse ?: EditResponse ;
242
229
private _editCount : number = 0 ;
243
- protected _initialPosition : Position | undefined ;
244
230
245
231
constructor (
246
232
protected readonly _session : Session ,
@@ -315,7 +301,7 @@ export class LiveStrategy extends EditModeStrategy {
315
301
const cursorStateComputerAndInlineDiffCollection : ICursorStateComputer = ( undoEdits ) => {
316
302
let last : Position | null = null ;
317
303
for ( const edit of undoEdits ) {
318
- last = ! last || last . isBefore ( edit . range . getEndPosition ( ) ) ? edit . range . getEndPosition ( ) : last ;
304
+ last = ! last || last . isBefore ( edit . range . getStartPosition ( ) ) ? edit . range . getStartPosition ( ) : last ;
319
305
this . _inlineDiffDecorations . collectEditOperation ( edit ) ;
320
306
}
321
307
return last && [ Selection . fromPositions ( last ) ] ;
@@ -356,27 +342,14 @@ export class LiveStrategy extends EditModeStrategy {
356
342
this . _widget . updateStatus ( message ) ;
357
343
}
358
344
359
- getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
345
+ override getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
360
346
const viewModel = this . _editor . _getViewModel ( ) ;
361
- if ( ! viewModel ) {
362
- return ;
363
- }
347
+ assertType ( viewModel ) ;
364
348
if ( initialRender ) {
365
349
this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
366
350
return this . _initialPosition ;
367
351
} else {
368
- if ( hasEditResponse ) {
369
- const visibleRange = viewModel . getCompletelyVisibleViewRange ( ) ;
370
- const endPosition = range . getEndPosition ( ) ;
371
- const endLine = endPosition . lineNumber ;
372
- if ( endLine >= visibleRange . startLineNumber && endLine <= visibleRange . endLineNumber ) {
373
- return endPosition ;
374
- } else {
375
- return this . _initialPosition ;
376
- }
377
- } else {
378
- return this . _initialPosition ;
379
- }
352
+ return minimalJumpPosition ( this . _editor , this . _initialPosition , range , hasEditResponse ) ;
380
353
}
381
354
}
382
355
@@ -439,14 +412,14 @@ export class LivePreviewStrategy extends LiveStrategy {
439
412
}
440
413
441
414
override getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
442
- console . log ( 'inside of live preview strategy' ) ;
443
- const primaryCursorPosition = this . _editor . _getViewModel ( ) ?. getPrimaryCursorState ( ) . viewState . position ;
444
415
if ( initialRender ) {
445
- this . _initialPosition = primaryCursorPosition ;
416
+ const viewModel = this . _editor . _getViewModel ( ) ;
417
+ assertType ( viewModel ) ;
418
+ this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
446
419
return this . _initialPosition ;
447
420
} else {
448
421
if ( hasEditResponse ) {
449
- return range . getEndPosition ( ) ;
422
+ return range . getStartPosition ( ) ;
450
423
} else {
451
424
return this . _initialPosition ;
452
425
}
@@ -464,3 +437,19 @@ function showSingleCreateFile(accessor: ServicesAccessor, edit: EditResponse) {
464
437
editorService . openEditor ( { resource : edit . singleCreateFileEdit . uri } , SIDE_GROUP ) ;
465
438
}
466
439
}
440
+
441
+ function minimalJumpPosition ( editor : ICodeEditor , initialPosition : Position | undefined , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
442
+ const viewModel = editor . _getViewModel ( ) ;
443
+ assertType ( viewModel ) ;
444
+ if ( hasEditResponse ) {
445
+ const endPosition = range . getStartPosition ( ) ;
446
+ const visibleRange = viewModel . getCompletelyVisibleViewRange ( ) ;
447
+ if ( visibleRange . containsPosition ( endPosition ) ) {
448
+ return endPosition ;
449
+ } else {
450
+ return initialPosition ;
451
+ }
452
+ } else {
453
+ return initialPosition ;
454
+ }
455
+ }
0 commit comments