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' ;
9
8
import { ICodeEditor } from 'vs/editor/browser/editorBrowser' ;
10
9
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService' ;
11
10
import { StableEditorScrollState } from 'vs/editor/browser/stableEditorScroll' ;
@@ -21,14 +20,18 @@ import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/c
21
20
import { IInstantiationService , ServicesAccessor } from 'vs/platform/instantiation/common/instantiation' ;
22
21
import { IStorageService , StorageScope , StorageTarget } from 'vs/platform/storage/common/storage' ;
23
22
import { InlineChatFileCreatePreviewWidget , InlineChatLivePreviewWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatLivePreviewWidget' ;
24
- import { EditResponse , Session , SessionResponse } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession' ;
23
+ import { EditResponse , Session } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession' ;
25
24
import { InlineChatWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget' ;
26
25
import { CTX_INLINE_CHAT_SHOWING_DIFF , CTX_INLINE_CHAT_DOCUMENT_CHANGED } from 'vs/workbench/contrib/inlineChat/common/inlineChat' ;
27
26
import { IEditorService , SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService' ;
28
27
29
28
export abstract class EditModeStrategy {
30
29
31
- protected _initialPosition : Position | undefined ;
30
+ constructor ( protected readonly _editor : ICodeEditor ) {
31
+ this . _initialPosition = this . _editor . getPosition ( ) ;
32
+ }
33
+
34
+ protected _initialPosition : Position | null ;
32
35
33
36
abstract dispose ( ) : void ;
34
37
@@ -46,7 +49,7 @@ export abstract class EditModeStrategy {
46
49
47
50
abstract hasFocus ( ) : boolean ;
48
51
49
- abstract getWidgetPosition ( initialRender : boolean , range ? : Range , response ?: SessionResponse ) : Position | undefined ;
52
+ abstract getWidgetPosition ( initialRender : boolean , range : Range ) : Position | null ;
50
53
}
51
54
52
55
export class PreviewStrategy extends EditModeStrategy {
@@ -55,14 +58,14 @@ export class PreviewStrategy extends EditModeStrategy {
55
58
private readonly _listener : IDisposable ;
56
59
57
60
constructor (
61
+ _editor : ICodeEditor ,
58
62
private readonly _session : Session ,
59
- private readonly _editor : ICodeEditor ,
60
63
private readonly _widget : InlineChatWidget ,
61
64
@IContextKeyService contextKeyService : IContextKeyService ,
62
65
@IBulkEditService private readonly _bulkEditService : IBulkEditService ,
63
66
@IInstantiationService private readonly _instaService : IInstantiationService ,
64
67
) {
65
- super ( ) ;
68
+ super ( _editor ) ;
66
69
67
70
this . _ctxDocumentChanged = CTX_INLINE_CHAT_DOCUMENT_CHANGED . bindTo ( contextKeyService ) ;
68
71
this . _listener = Event . debounce ( _session . textModelN . onDidChangeContent . bind ( _session . textModelN ) , ( ) => { } , 350 ) ( _ => {
@@ -135,12 +138,7 @@ export class PreviewStrategy extends EditModeStrategy {
135
138
// nothing to do
136
139
}
137
140
138
- getWidgetPosition ( initialRender : boolean , _range : Range , _response : SessionResponse ) : Position | undefined {
139
- const viewModel = this . _editor . _getViewModel ( ) ;
140
- assertType ( viewModel ) ;
141
- if ( initialRender ) {
142
- this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
143
- }
141
+ getWidgetPosition ( _initialRender : boolean , _range : Range ) : Position | null {
144
142
return this . _initialPosition ;
145
143
}
146
144
@@ -227,16 +225,16 @@ export class LiveStrategy extends EditModeStrategy {
227
225
private _editCount : number = 0 ;
228
226
229
227
constructor (
228
+ _editor : ICodeEditor ,
230
229
protected readonly _session : Session ,
231
- protected readonly _editor : ICodeEditor ,
232
230
protected readonly _widget : InlineChatWidget ,
233
231
@IContextKeyService contextKeyService : IContextKeyService ,
234
232
@IStorageService protected _storageService : IStorageService ,
235
233
@IBulkEditService protected readonly _bulkEditService : IBulkEditService ,
236
234
@IEditorWorkerService protected readonly _editorWorkerService : IEditorWorkerService ,
237
235
@IInstantiationService private readonly _instaService : IInstantiationService ,
238
236
) {
239
- super ( ) ;
237
+ super ( _editor ) ;
240
238
this . _diffEnabled = _storageService . getBoolean ( LiveStrategy . _inlineDiffStorageKey , StorageScope . PROFILE , true ) ;
241
239
242
240
this . _inlineDiffDecorations = new InlineDiffDecorations ( this . _editor , this . _diffEnabled ) ;
@@ -340,36 +338,26 @@ export class LiveStrategy extends EditModeStrategy {
340
338
this . _widget . updateStatus ( message ) ;
341
339
}
342
340
343
- private _findEditsMaxLineNumber ( response : EditResponse ) : number | void {
344
- const edits = response . localEdits ;
345
- if ( edits . length ) {
346
- let editsMaxLineNumber = 0 ;
347
- for ( const edit of edits ) {
348
- const editStartLine = edit . range . startLineNumber ;
349
- const editNumberOfLines = ( edit . text . match ( / \n / g) || [ ] ) . length ;
350
- const endLine = editStartLine + editNumberOfLines - 1 ;
351
- if ( endLine > editsMaxLineNumber ) {
352
- editsMaxLineNumber = endLine ;
353
- }
341
+ private _lastLineOfLocalEdits ( ) : number | undefined {
342
+ const lastTextModelChanges = this . _session . lastTextModelChanges ;
343
+ let lastLineOfLocalEdits : number | undefined ;
344
+ for ( const change of lastTextModelChanges ) {
345
+ const changeEndLineNumber = change . modifiedRange . endLineNumberExclusive - 1 ;
346
+ if ( ! lastLineOfLocalEdits || lastLineOfLocalEdits < changeEndLineNumber ) {
347
+ lastLineOfLocalEdits = changeEndLineNumber ;
354
348
}
355
- return editsMaxLineNumber ;
356
349
}
350
+ return lastLineOfLocalEdits ;
357
351
}
358
352
359
- override getWidgetPosition ( initialRender : boolean , _range : Range , response : SessionResponse ) : Position | undefined {
360
- const viewModel = this . _editor . _getViewModel ( ) ;
361
- assertType ( viewModel ) ;
353
+ override getWidgetPosition ( initialRender : boolean , _range : Range ) : Position | null {
362
354
if ( initialRender ) {
363
- this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
364
355
return this . _initialPosition ;
365
356
} else {
366
- if ( response instanceof EditResponse ) {
367
- const editsMaxLineNumber = this . _findEditsMaxLineNumber ( response ) ;
368
- if ( editsMaxLineNumber ) {
369
- return new Position ( editsMaxLineNumber , 1 ) ;
370
- } else {
371
- return this . _initialPosition ;
372
- }
357
+ const isEditResponse = this . _session . lastExchange ?. response instanceof EditResponse ;
358
+ if ( isEditResponse ) {
359
+ const lastLineOfLocalEdits = this . _lastLineOfLocalEdits ( ) ;
360
+ return lastLineOfLocalEdits ? new Position ( lastLineOfLocalEdits , 1 ) : this . _initialPosition ;
373
361
} else {
374
362
return this . _initialPosition ;
375
363
}
@@ -387,16 +375,16 @@ export class LivePreviewStrategy extends LiveStrategy {
387
375
private readonly _previewZone : InlineChatFileCreatePreviewWidget ;
388
376
389
377
constructor (
390
- session : Session ,
391
378
editor : ICodeEditor ,
379
+ session : Session ,
392
380
widget : InlineChatWidget ,
393
381
@IContextKeyService contextKeyService : IContextKeyService ,
394
382
@IStorageService storageService : IStorageService ,
395
383
@IBulkEditService bulkEditService : IBulkEditService ,
396
384
@IEditorWorkerService editorWorkerService : IEditorWorkerService ,
397
385
@IInstantiationService instaService : IInstantiationService ,
398
386
) {
399
- super ( session , editor , widget , contextKeyService , storageService , bulkEditService , editorWorkerService , instaService ) ;
387
+ super ( editor , session , widget , contextKeyService , storageService , bulkEditService , editorWorkerService , instaService ) ;
400
388
401
389
this . _diffZone = instaService . createInstance ( InlineChatLivePreviewWidget , editor , session ) ;
402
390
this . _previewZone = instaService . createInstance ( InlineChatFileCreatePreviewWidget , editor ) ;
@@ -434,14 +422,12 @@ export class LivePreviewStrategy extends LiveStrategy {
434
422
scrollState . restore ( this . _editor ) ;
435
423
}
436
424
437
- override getWidgetPosition ( initialRender : boolean , range : Range , response : SessionResponse ) : Position | undefined {
425
+ override getWidgetPosition ( initialRender : boolean , range : Range ) : Position | null {
438
426
if ( initialRender ) {
439
- const viewModel = this . _editor . _getViewModel ( ) ;
440
- assertType ( viewModel ) ;
441
- this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
442
427
return this . _initialPosition ;
443
428
} else {
444
- if ( range && response instanceof EditResponse ) {
429
+ const isEditResponse = this . _session . lastExchange ?. response instanceof EditResponse ;
430
+ if ( range && isEditResponse ) {
445
431
return range . getEndPosition ( ) ;
446
432
} else {
447
433
return this . _initialPosition ;
0 commit comments