Skip to content

Commit 59ef7fa

Browse files
committed
adding the changes from the review
1 parent 7503938 commit 59ef7fa

File tree

3 files changed

+38
-53
lines changed

3 files changed

+38
-53
lines changed

src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/c
2828
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
2929
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
3030
import { ILogService } from 'vs/platform/log/common/log';
31-
import { EditResponse, EmptyResponse, ErrorResponse, ExpansionState, IInlineChatSessionService, MarkdownResponse, Session, SessionExchange, SessionResponse } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
31+
import { EditResponse, EmptyResponse, ErrorResponse, ExpansionState, IInlineChatSessionService, MarkdownResponse, Session, SessionExchange } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
3232
import { EditModeStrategy, LivePreviewStrategy, LiveStrategy, PreviewStrategy } from 'vs/workbench/contrib/inlineChat/browser/inlineChatStrategies';
3333
import { InlineChatZoneWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget';
3434
import { CTX_INLINE_CHAT_HAS_ACTIVE_REQUEST, CTX_INLINE_CHAT_LAST_FEEDBACK, IInlineChatRequest, IInlineChatResponse, INLINE_CHAT_ID, EditMode, InlineChatResponseFeedbackKind, CTX_INLINE_CHAT_LAST_RESPONSE_TYPE, InlineChatResponseType, CTX_INLINE_CHAT_DID_EDIT, CTX_INLINE_CHAT_HAS_STASHED_SESSION } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
@@ -193,10 +193,10 @@ export class InlineChatController implements IEditorContribution {
193193

194194
// ---- state machine
195195

196-
private _showWidget(initialRender: boolean = false, response?: SessionResponse) {
196+
private _showWidget(initialRender: boolean = false) {
197197
assertType(this._activeSession);
198198
const selectionRange = this._activeSession.wholeRange.value;
199-
const widgetPosition = this._strategy?.getWidgetPosition(initialRender, selectionRange, response);
199+
const widgetPosition = this._strategy?.getWidgetPosition(initialRender, selectionRange);
200200
this._zone.value.show(widgetPosition ?? selectionRange.getEndPosition());
201201
}
202202

@@ -241,14 +241,14 @@ export class InlineChatController implements IEditorContribution {
241241

242242
switch (session.editMode) {
243243
case EditMode.Live:
244-
this._strategy = this._instaService.createInstance(LiveStrategy, session, this._editor, this._zone.value.widget);
244+
this._strategy = this._instaService.createInstance(LiveStrategy, this._editor, session, this._zone.value.widget);
245245
break;
246246
case EditMode.Preview:
247-
this._strategy = this._instaService.createInstance(PreviewStrategy, session, this._editor, this._zone.value.widget);
247+
this._strategy = this._instaService.createInstance(PreviewStrategy, this._editor, session, this._zone.value.widget);
248248
break;
249249
case EditMode.LivePreview:
250250
default:
251-
this._strategy = this._instaService.createInstance(LivePreviewStrategy, session, this._editor, this._zone.value.widget);
251+
this._strategy = this._instaService.createInstance(LivePreviewStrategy, this._editor, session, this._zone.value.widget);
252252
break;
253253
}
254254

@@ -543,7 +543,7 @@ export class InlineChatController implements IEditorContribution {
543543
assertType(this._strategy);
544544

545545
const { response } = this._activeSession.lastExchange!;
546-
this._showWidget(false, response);
546+
this._showWidget(false);
547547

548548
this._ctxLastResponseType.set(response instanceof EditResponse || response instanceof MarkdownResponse
549549
? response.raw.type

src/vs/workbench/contrib/inlineChat/browser/inlineChatSession.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,11 @@ export class Session {
236236
}
237237
}
238238

239-
export type SessionResponse = EditResponse | MarkdownResponse | ErrorResponse | EmptyResponse;
240239

241240
export class SessionExchange {
242241
constructor(
243242
readonly prompt: string,
244-
readonly response: SessionResponse
243+
readonly response: MarkdownResponse | EditResponse | EmptyResponse | ErrorResponse
245244
) { }
246245
}
247246

src/vs/workbench/contrib/inlineChat/browser/inlineChatStrategies.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { Event } from 'vs/base/common/event';
77
import { IDisposable } from 'vs/base/common/lifecycle';
8-
import { assertType } from 'vs/base/common/types';
98
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
109
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
1110
import { StableEditorScrollState } from 'vs/editor/browser/stableEditorScroll';
@@ -21,14 +20,18 @@ import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/c
2120
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
2221
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
2322
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';
2524
import { InlineChatWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget';
2625
import { CTX_INLINE_CHAT_SHOWING_DIFF, CTX_INLINE_CHAT_DOCUMENT_CHANGED } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
2726
import { IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
2827

2928
export abstract class EditModeStrategy {
3029

31-
protected _initialPosition: Position | undefined;
30+
constructor(protected readonly _editor: ICodeEditor) {
31+
this._initialPosition = this._editor.getPosition();
32+
}
33+
34+
protected _initialPosition: Position | null;
3235

3336
abstract dispose(): void;
3437

@@ -46,7 +49,7 @@ export abstract class EditModeStrategy {
4649

4750
abstract hasFocus(): boolean;
4851

49-
abstract getWidgetPosition(initialRender: boolean, range?: Range, response?: SessionResponse): Position | undefined;
52+
abstract getWidgetPosition(initialRender: boolean, range: Range): Position | null;
5053
}
5154

5255
export class PreviewStrategy extends EditModeStrategy {
@@ -55,14 +58,14 @@ export class PreviewStrategy extends EditModeStrategy {
5558
private readonly _listener: IDisposable;
5659

5760
constructor(
61+
_editor: ICodeEditor,
5862
private readonly _session: Session,
59-
private readonly _editor: ICodeEditor,
6063
private readonly _widget: InlineChatWidget,
6164
@IContextKeyService contextKeyService: IContextKeyService,
6265
@IBulkEditService private readonly _bulkEditService: IBulkEditService,
6366
@IInstantiationService private readonly _instaService: IInstantiationService,
6467
) {
65-
super();
68+
super(_editor);
6669

6770
this._ctxDocumentChanged = CTX_INLINE_CHAT_DOCUMENT_CHANGED.bindTo(contextKeyService);
6871
this._listener = Event.debounce(_session.textModelN.onDidChangeContent.bind(_session.textModelN), () => { }, 350)(_ => {
@@ -135,12 +138,7 @@ export class PreviewStrategy extends EditModeStrategy {
135138
// nothing to do
136139
}
137140

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 {
144142
return this._initialPosition;
145143
}
146144

@@ -227,16 +225,16 @@ export class LiveStrategy extends EditModeStrategy {
227225
private _editCount: number = 0;
228226

229227
constructor(
228+
_editor: ICodeEditor,
230229
protected readonly _session: Session,
231-
protected readonly _editor: ICodeEditor,
232230
protected readonly _widget: InlineChatWidget,
233231
@IContextKeyService contextKeyService: IContextKeyService,
234232
@IStorageService protected _storageService: IStorageService,
235233
@IBulkEditService protected readonly _bulkEditService: IBulkEditService,
236234
@IEditorWorkerService protected readonly _editorWorkerService: IEditorWorkerService,
237235
@IInstantiationService private readonly _instaService: IInstantiationService,
238236
) {
239-
super();
237+
super(_editor);
240238
this._diffEnabled = _storageService.getBoolean(LiveStrategy._inlineDiffStorageKey, StorageScope.PROFILE, true);
241239

242240
this._inlineDiffDecorations = new InlineDiffDecorations(this._editor, this._diffEnabled);
@@ -340,36 +338,26 @@ export class LiveStrategy extends EditModeStrategy {
340338
this._widget.updateStatus(message);
341339
}
342340

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;
354348
}
355-
return editsMaxLineNumber;
356349
}
350+
return lastLineOfLocalEdits;
357351
}
358352

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 {
362354
if (initialRender) {
363-
this._initialPosition = viewModel.getPrimaryCursorState().viewState.position;
364355
return this._initialPosition;
365356
} 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;
373361
} else {
374362
return this._initialPosition;
375363
}
@@ -387,16 +375,16 @@ export class LivePreviewStrategy extends LiveStrategy {
387375
private readonly _previewZone: InlineChatFileCreatePreviewWidget;
388376

389377
constructor(
390-
session: Session,
391378
editor: ICodeEditor,
379+
session: Session,
392380
widget: InlineChatWidget,
393381
@IContextKeyService contextKeyService: IContextKeyService,
394382
@IStorageService storageService: IStorageService,
395383
@IBulkEditService bulkEditService: IBulkEditService,
396384
@IEditorWorkerService editorWorkerService: IEditorWorkerService,
397385
@IInstantiationService instaService: IInstantiationService,
398386
) {
399-
super(session, editor, widget, contextKeyService, storageService, bulkEditService, editorWorkerService, instaService);
387+
super(editor, session, widget, contextKeyService, storageService, bulkEditService, editorWorkerService, instaService);
400388

401389
this._diffZone = instaService.createInstance(InlineChatLivePreviewWidget, editor, session);
402390
this._previewZone = instaService.createInstance(InlineChatFileCreatePreviewWidget, editor);
@@ -434,14 +422,12 @@ export class LivePreviewStrategy extends LiveStrategy {
434422
scrollState.restore(this._editor);
435423
}
436424

437-
override getWidgetPosition(initialRender: boolean, range: Range, response: SessionResponse): Position | undefined {
425+
override getWidgetPosition(initialRender: boolean, range: Range): Position | null {
438426
if (initialRender) {
439-
const viewModel = this._editor._getViewModel();
440-
assertType(viewModel);
441-
this._initialPosition = viewModel.getPrimaryCursorState().viewState.position;
442427
return this._initialPosition;
443428
} else {
444-
if (range && response instanceof EditResponse) {
429+
const isEditResponse = this._session.lastExchange?.response instanceof EditResponse;
430+
if (range && isEditResponse) {
445431
return range.getEndPosition();
446432
} else {
447433
return this._initialPosition;

0 commit comments

Comments
 (0)