@@ -21,7 +21,7 @@ import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/c
21
21
import { IInstantiationService , ServicesAccessor } from 'vs/platform/instantiation/common/instantiation' ;
22
22
import { IStorageService , StorageScope , StorageTarget } from 'vs/platform/storage/common/storage' ;
23
23
import { InlineChatFileCreatePreviewWidget , InlineChatLivePreviewWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatLivePreviewWidget' ;
24
- import { EditResponse , Session } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession' ;
24
+ import { EditResponse , Session , SessionResponse } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession' ;
25
25
import { InlineChatWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget' ;
26
26
import { CTX_INLINE_CHAT_SHOWING_DIFF , CTX_INLINE_CHAT_DOCUMENT_CHANGED } from 'vs/workbench/contrib/inlineChat/common/inlineChat' ;
27
27
import { IEditorService , SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService' ;
@@ -46,7 +46,7 @@ export abstract class EditModeStrategy {
46
46
47
47
abstract hasFocus ( ) : boolean ;
48
48
49
- abstract getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined ;
49
+ abstract getWidgetPosition ( initialRender : boolean , range ? : Range , response ?: SessionResponse ) : Position | undefined ;
50
50
}
51
51
52
52
export class PreviewStrategy extends EditModeStrategy {
@@ -135,15 +135,13 @@ export class PreviewStrategy extends EditModeStrategy {
135
135
// nothing to do
136
136
}
137
137
138
- getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
138
+ getWidgetPosition ( initialRender : boolean , _range : Range , _response : SessionResponse ) : Position | undefined {
139
139
const viewModel = this . _editor . _getViewModel ( ) ;
140
140
assertType ( viewModel ) ;
141
141
if ( initialRender ) {
142
142
this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
143
- return this . _initialPosition ;
144
- } else {
145
- return minimalJumpPosition ( this . _editor , this . _initialPosition , range , hasEditResponse ) ;
146
143
}
144
+ return this . _initialPosition ;
147
145
}
148
146
149
147
hasFocus ( ) : boolean {
@@ -342,14 +340,39 @@ export class LiveStrategy extends EditModeStrategy {
342
340
this . _widget . updateStatus ( message ) ;
343
341
}
344
342
345
- override getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
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
+ }
354
+ }
355
+ return editsMaxLineNumber ;
356
+ }
357
+ }
358
+
359
+ override getWidgetPosition ( initialRender : boolean , _range : Range , response : SessionResponse ) : Position | undefined {
346
360
const viewModel = this . _editor . _getViewModel ( ) ;
347
361
assertType ( viewModel ) ;
348
362
if ( initialRender ) {
349
363
this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
350
364
return this . _initialPosition ;
351
365
} else {
352
- return minimalJumpPosition ( this . _editor , this . _initialPosition , range , hasEditResponse ) ;
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
+ }
373
+ } else {
374
+ return this . _initialPosition ;
375
+ }
353
376
}
354
377
}
355
378
@@ -411,14 +434,14 @@ export class LivePreviewStrategy extends LiveStrategy {
411
434
scrollState . restore ( this . _editor ) ;
412
435
}
413
436
414
- override getWidgetPosition ( initialRender : boolean , range : Range , hasEditResponse : boolean | undefined ) : Position | undefined {
437
+ override getWidgetPosition ( initialRender : boolean , range : Range , response : SessionResponse ) : Position | undefined {
415
438
if ( initialRender ) {
416
439
const viewModel = this . _editor . _getViewModel ( ) ;
417
440
assertType ( viewModel ) ;
418
441
this . _initialPosition = viewModel . getPrimaryCursorState ( ) . viewState . position ;
419
442
return this . _initialPosition ;
420
443
} else {
421
- if ( hasEditResponse ) {
444
+ if ( range && response instanceof EditResponse ) {
422
445
return range . getEndPosition ( ) ;
423
446
} else {
424
447
return this . _initialPosition ;
@@ -437,19 +460,3 @@ function showSingleCreateFile(accessor: ServicesAccessor, edit: EditResponse) {
437
460
editorService . openEditor ( { resource : edit . singleCreateFileEdit . uri } , SIDE_GROUP ) ;
438
461
}
439
462
}
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 . getEndPosition ( ) ;
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