Skip to content

Commit aa4daa4

Browse files
committed
Change to the code after review
1 parent 631e85c commit aa4daa4

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface InteractiveEditorRunOptions {
6767
autoSend?: boolean;
6868
existingSession?: Session;
6969
isUnstashed?: boolean;
70+
initialRender?: boolean;
7071
}
7172

7273
export class InteractiveEditorController implements IEditorContribution {
@@ -99,6 +100,8 @@ export class InteractiveEditorController implements IEditorContribution {
99100
private _activeSession?: Session;
100101
private _strategy?: EditModeStrategy;
101102
private _ignoreModelContentChanged = false;
103+
private _initialRender?: boolean = true;
104+
private _updateEditMode?: boolean = false;
102105

103106
constructor(
104107
private readonly _editor: ICodeEditor,
@@ -232,26 +235,55 @@ export class InteractiveEditorController implements IEditorContribution {
232235
return State.CANCEL;
233236
}
234237

238+
this._initializeStratetgy(session);
239+
this._activeSession = session;
240+
this._store.add(this._configurationService.onDidChangeConfiguration((e) => {
241+
console.log('e : ', e);
242+
console.log('e.affectsConfiguration(interactiveEditor.editModes) : ', e.affectsConfiguration('interactiveEditor.editMode'));
243+
console.log('this._activeSession : ', this._activeSession);
244+
if (e.affectsConfiguration('interactiveEditor.editMode')) {
245+
console.log('entered into inner if loop of onDidChangeConfiguration');
246+
console.log('this._getMode() : ', this._getMode());
247+
this._updateEditMode = true;
248+
}
249+
}));
250+
251+
return State.INIT_UI;
252+
}
253+
254+
private _initializeStratetgy(session: Session): void {
255+
console.log('inside of initial strategy');
235256
switch (session.editMode) {
236257
case EditMode.Live:
237258
this._strategy = this._instaService.createInstance(LiveStrategy, session, this._editor, this._zone.value.widget);
238259
break;
239260
case EditMode.Preview:
240-
this._strategy = this._instaService.createInstance(PreviewStrategy, session, this._zone.value.widget);
261+
this._strategy = this._instaService.createInstance(PreviewStrategy, session, this._editor, this._zone.value.widget);
241262
break;
242263
case EditMode.LivePreview:
243264
default:
244265
this._strategy = this._instaService.createInstance(LivePreviewStrategy, session, this._editor, this._zone.value.widget);
245266
break;
246267
}
268+
}
247269

248-
this._activeSession = session;
249-
return State.INIT_UI;
270+
private _showWidget(initialRender: boolean = false) {
271+
console.log('inside of _showWidget');
272+
console.log('initialRender : ', initialRender);
273+
assertType(this._activeSession);
274+
const selectionRange = this._activeSession.wholeRange.value;
275+
this._zone.value.showWidget(selectionRange, this._strategy?.getWidgetPosition(initialRender, selectionRange));
250276
}
251277

252278
private async [State.INIT_UI](options: InteractiveEditorRunOptions | undefined): Promise<State.WAIT_FOR_INPUT | State.SHOW_RESPONSE | State.APPLY_RESPONSE> {
253279
assertType(this._activeSession);
254280

281+
if (this._updateEditMode) {
282+
this._activeSession.editMode = this._getMode();
283+
this._initializeStratetgy(this._activeSession);
284+
this._updateEditMode = false;
285+
}
286+
255287
// hide/cancel inline completions when invoking IE
256288
InlineCompletionsController.get(this._editor)?.hide();
257289

@@ -269,8 +301,10 @@ export class InteractiveEditorController implements IEditorContribution {
269301
this._zone.value.widget.placeholder = this._getPlaceholderText();
270302
this._zone.value.widget.value = this._activeSession.lastInput ?? '';
271303
this._zone.value.widget.updateInfo(this._activeSession.session.message ?? localize('welcome.1', "AI-generated code may be incorrect"));
272-
this._zone.value.show(this._activeSession.wholeRange.value);
273304
this._zone.value.widget.preferredExpansionState = this._activeSession.lastExpansionState;
305+
console.log('inside of init ui before show widget');
306+
this._initialRender = true;
307+
this._showWidget(this._initialRender);
274308

275309
this._sessionStore.add(this._editor.onDidChangeModel((e) => {
276310
const msg = this._activeSession?.lastExchange
@@ -358,8 +392,10 @@ export class InteractiveEditorController implements IEditorContribution {
358392
private async [State.WAIT_FOR_INPUT](options: InteractiveEditorRunOptions | undefined): Promise<State.ACCEPT | State.CANCEL | State.PAUSE | State.WAIT_FOR_INPUT | State.MAKE_REQUEST> {
359393
assertType(this._activeSession);
360394

395+
console.log('inside of wait for input');
361396
this._zone.value.widget.placeholder = this._getPlaceholderText();
362-
this._zone.value.show(this._activeSession.wholeRange.value);
397+
this._showWidget(this._initialRender);
398+
this._initialRender = false;
363399

364400
if (options?.message) {
365401
this._zone.value.widget.value = options?.message;
@@ -398,6 +434,7 @@ export class InteractiveEditorController implements IEditorContribution {
398434
}
399435

400436
if (!this._zone.value.widget.value) {
437+
console.log('before the case when we call WAIT_FOR_INPUT');
401438
return State.WAIT_FOR_INPUT;
402439
}
403440

@@ -427,6 +464,7 @@ export class InteractiveEditorController implements IEditorContribution {
427464
private async [State.MAKE_REQUEST](): Promise<State.APPLY_RESPONSE | State.PAUSE | State.CANCEL | State.ACCEPT> {
428465
assertType(this._editor.hasModel());
429466
assertType(this._activeSession);
467+
console.log('this._activeSession.lastInput : ', this._activeSession.lastInput);
430468
assertType(this._activeSession.lastInput);
431469

432470
const requestCts = new CancellationTokenSource();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class Session {
120120
private _textModelNSnapshotAltVersion: number | undefined;
121121

122122
constructor(
123-
readonly editMode: EditMode,
123+
public editMode: EditMode,
124124
readonly editor: ICodeEditor,
125125
readonly textModel0: ITextModel,
126126
readonly textModelN: ITextModel,

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,28 @@ export abstract class EditModeStrategy {
4141

4242
abstract toggleDiff(): void;
4343

44-
abstract getWidgetPosition(range: Range): Position;
44+
abstract getWidgetPosition(initialRender: boolean, range: Range): Position | undefined;
45+
}
46+
47+
function positionCalculation(editor: ICodeEditor, initialRender: boolean, range: Range): Position | undefined {
48+
const viewModel = editor._getViewModel();
49+
if (!viewModel) {
50+
return;
51+
}
52+
const primaryCursorPosition = viewModel.getPrimaryCursorState().viewState.position;
53+
if (initialRender) {
54+
return primaryCursorPosition;
55+
} else {
56+
const visibleRange = viewModel.getCompletelyVisibleViewRange();
57+
const endPosition = range.getEndPosition();
58+
const endLine = endPosition.lineNumber;
59+
60+
if (endLine >= visibleRange.startLineNumber && endLine <= visibleRange.endLineNumber) {
61+
return endPosition;
62+
} else {
63+
return primaryCursorPosition;
64+
}
65+
}
4566
}
4667

4768
export class PreviewStrategy extends EditModeStrategy {
@@ -51,6 +72,7 @@ export class PreviewStrategy extends EditModeStrategy {
5172

5273
constructor(
5374
private readonly _session: Session,
75+
private readonly _editor: ICodeEditor,
5476
private readonly _widget: InteractiveEditorWidget,
5577
@IContextKeyService contextKeyService: IContextKeyService,
5678
@IBulkEditService private readonly _bulkEditService: IBulkEditService,
@@ -129,8 +151,9 @@ export class PreviewStrategy extends EditModeStrategy {
129151
// nothing to do
130152
}
131153

132-
getWidgetPosition(range: Range): Position {
133-
throw new Error('not supported');
154+
getWidgetPosition(initialRender: boolean, range: Range): Position | undefined {
155+
console.log('inside of preview strategy, getWidgetPosition');
156+
return positionCalculation(this._editor, initialRender, range);
134157
}
135158
}
136159

@@ -325,8 +348,9 @@ export class LiveStrategy extends EditModeStrategy {
325348
this._widget.updateStatus(message);
326349
}
327350

328-
getWidgetPosition(range: Range): Position {
329-
throw new Error('not supported');
351+
getWidgetPosition(initialRender: boolean, range: Range): Position | undefined {
352+
console.log('inside of live strategy, getWidgetPosition');
353+
return positionCalculation(this._editor, initialRender, range);
330354
}
331355
}
332356

@@ -383,8 +407,13 @@ export class LivePreviewStrategy extends LiveStrategy {
383407
scrollState.restore(this._editor);
384408
}
385409

386-
override getWidgetPosition(range: Range): Position {
387-
throw new Error('not supported');
410+
override getWidgetPosition(initialRender: boolean, range: Range): Position | undefined {
411+
console.log('inside of live preview strategy, getWidgetPosition');
412+
if (initialRender) {
413+
return this._editor._getViewModel()?.getPrimaryCursorState().viewState.position;
414+
} else {
415+
return range.getEndPosition();
416+
}
388417
}
389418
}
390419

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -787,32 +787,33 @@ export class InteractiveEditorZoneWidget extends ZoneWidget {
787787
super._relayout(this._computeHeightInLines());
788788
}
789789

790-
override show(selectionRange: Range): void {
791-
super.show(selectionRange.getEndPosition(), this._computeHeightInLines());
790+
showWidget(selectionRange: Range, position: Position | undefined): void {
791+
const widgetPosition = position ?? selectionRange.getEndPosition();
792+
super.show(widgetPosition, this._computeHeightInLines());
792793
this.widget.focus();
793794
this._ctxVisible.set(true);
794-
this._setMargins(selectionRange);
795+
this._setMargins(selectionRange, widgetPosition);
795796
}
796797

797-
private _setMargins(selectionRange: Range): void {
798+
private _setMargins(selectionRange: Range, position: Position): void {
799+
const positionLineNumber = position.lineNumber;
798800
const info = this.editor.getLayoutInfo();
799801
const startLineNumber = selectionRange.getStartPosition().lineNumber;
800-
const endLineNumber = selectionRange.getEndPosition().lineNumber;
801802
const viewModel = this.editor._getViewModel();
802803
if (!viewModel) {
803804
return;
804805
}
805806
let indentationLineNumber;
806807
let indentationLevel;
807-
for (let lineNumber = endLineNumber; lineNumber >= startLineNumber; lineNumber--) {
808+
for (let lineNumber = positionLineNumber; lineNumber >= startLineNumber; lineNumber--) {
808809
const currentIndentationLevel = viewModel.getLineFirstNonWhitespaceColumn(lineNumber);
809810
if (currentIndentationLevel !== 0) {
810811
indentationLineNumber = lineNumber;
811812
indentationLevel = currentIndentationLevel;
812813
break;
813814
}
814815
}
815-
this._indentationWidth = this.editor.getOffsetForColumn(indentationLineNumber ?? endLineNumber, indentationLevel ?? viewModel.getLineFirstNonWhitespaceColumn(endLineNumber));
816+
this._indentationWidth = this.editor.getOffsetForColumn(indentationLineNumber ?? positionLineNumber, indentationLevel ?? viewModel.getLineFirstNonWhitespaceColumn(positionLineNumber));
816817
const marginWithoutIndentation = info.glyphMarginWidth + info.decorationsWidth + info.lineNumbersWidth;
817818
const marginWithIndentation = marginWithoutIndentation + this._indentationWidth;
818819
const isEnoughAvailableSpaceWithIndentation = this._availableSpaceGivenIndentation() > 400;

0 commit comments

Comments
 (0)