Skip to content

Commit ad3b07c

Browse files
committed
cleaned the code
1 parent 561854e commit ad3b07c

File tree

3 files changed

+71
-137
lines changed

3 files changed

+71
-137
lines changed

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

Lines changed: 12 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ export class InteractiveEditorController implements IEditorContribution {
9999
private _activeSession?: Session;
100100
private _strategy?: EditModeStrategy;
101101
private _ignoreModelContentChanged = false;
102-
private _initialRender?: boolean = true;
103-
private _updateEditMode?: boolean = false;
104102

105103
constructor(
106104
private readonly _editor: ICodeEditor,
@@ -195,6 +193,13 @@ export class InteractiveEditorController implements IEditorContribution {
195193

196194
// ---- state machine
197195

196+
private _showWidget(initialRender: boolean = false, hasEditResponse: boolean = false) {
197+
assertType(this._activeSession);
198+
const selectionRange = this._activeSession.wholeRange.value;
199+
const widgetPosition = this._strategy?.getWidgetPosition(initialRender, selectionRange, hasEditResponse);
200+
this._zone.value.showWidget(selectionRange, widgetPosition);
201+
}
202+
198203
protected async _nextState(state: State, options: InteractiveEditorRunOptions | undefined): Promise<void> {
199204
this._log('setState to ', state);
200205
const nextState = await this[state](options);
@@ -234,30 +239,7 @@ export class InteractiveEditorController implements IEditorContribution {
234239
return State.CANCEL;
235240
}
236241

237-
this._initializeStratetgy(session);
238-
this._activeSession = session;
239-
this._store.add(this._configurationService.onDidChangeConfiguration((e) => {
240-
241-
console.log('e : ', e);
242-
console.log('e.affectsConfiguration(interactiveEditor.editModes) : ', e.affectsConfiguration('interactiveEditor.editMode'));
243-
console.log('this._activeSession : ', this._activeSession);
244-
245-
if (e.affectsConfiguration('interactiveEditor.editMode')) {
246-
247-
console.log('entered into inner if loop of onDidChangeConfiguration');
248-
console.log('this._getMode() : ', this._getMode());
249-
250-
this._updateEditMode = true;
251-
}
252-
}));
253-
254-
return State.INIT_UI;
255-
}
256-
257-
private _initializeStratetgy(session: Session): void {
258-
259-
console.log('inside of initial strategy');
260-
242+
session.editMode = this._getMode();
261243
switch (session.editMode) {
262244
case EditMode.Live:
263245
this._strategy = this._instaService.createInstance(LiveStrategy, session, this._editor, this._zone.value.widget);
@@ -270,64 +252,14 @@ export class InteractiveEditorController implements IEditorContribution {
270252
this._strategy = this._instaService.createInstance(LivePreviewStrategy, session, this._editor, this._zone.value.widget);
271253
break;
272254
}
273-
}
274-
275-
private _showWidget(initialRender: boolean = false, updateEditMode: boolean = false) {
276-
277-
console.log('inside of _showWidget');
278-
console.log('initialRender : ', initialRender);
279-
console.log('updateEditMode : ', updateEditMode);
280255

281-
assertType(this._activeSession);
282-
const selectionRange = this._activeSession.wholeRange.value;
283-
284-
if (!initialRender) {
285-
console.log('initial render is false');
286-
console.log('this._activeSession.lastPosition : ', this._activeSession.lastPosition);
287-
if (this._activeSession.lastPosition) {
288-
console.log('inside if');
289-
if (updateEditMode) {
290-
console.log('inside if');
291-
this._zone.value.showWidget(selectionRange, this._strategy?.getWidgetPosition(initialRender, selectionRange));
292-
} else {
293-
console.log('inside else');
294-
this._zone.value.showWidget(selectionRange, this._activeSession.lastPosition);
295-
}
296-
} else {
297-
console.log('inside else');
298-
const widgetPosition = this._strategy?.getWidgetPosition(initialRender, selectionRange);
299-
this._activeSession.lastPosition = widgetPosition;
300-
this._zone.value.showWidget(selectionRange, widgetPosition);
301-
}
302-
} else {
303-
console.log('initial render is true');
304-
console.log('this._activeSession.lastPosition : ', this._activeSession.lastPosition);
305-
306-
if (this._activeSession.lastPosition) {
307-
console.log('inside if');
308-
if (updateEditMode) {
309-
console.log('inside if');
310-
this._zone.value.showWidget(selectionRange, this._strategy?.getWidgetPosition(initialRender, selectionRange));
311-
this._activeSession.lastPosition = undefined;
312-
} else {
313-
console.log('inside else');
314-
this._zone.value.showWidget(selectionRange, this._activeSession.lastPosition);
315-
}
316-
} else {
317-
console.log('inside else');
318-
this._zone.value.showWidget(selectionRange, this._strategy?.getWidgetPosition(initialRender, selectionRange));
319-
}
320-
}
256+
this._activeSession = session;
257+
return State.INIT_UI;
321258
}
322259

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

326-
if (this._updateEditMode) {
327-
this._activeSession.editMode = this._getMode();
328-
this._initializeStratetgy(this._activeSession);
329-
}
330-
331263
// hide/cancel inline completions when invoking IE
332264
InlineCompletionsController.get(this._editor)?.hide();
333265

@@ -346,11 +278,7 @@ export class InteractiveEditorController implements IEditorContribution {
346278
this._zone.value.widget.value = this._activeSession.lastInput ?? '';
347279
this._zone.value.widget.updateInfo(this._activeSession.session.message ?? localize('welcome.1', "AI-generated code may be incorrect"));
348280
this._zone.value.widget.preferredExpansionState = this._activeSession.lastExpansionState;
349-
350-
console.log('inside of init ui before show widget');
351-
352-
this._initialRender = true;
353-
this._showWidget(this._initialRender, this._updateEditMode);
281+
this._showWidget(true);
354282

355283
this._sessionStore.add(this._editor.onDidChangeModel((e) => {
356284
const msg = this._activeSession?.lastExchange
@@ -438,13 +366,7 @@ export class InteractiveEditorController implements IEditorContribution {
438366
private async [State.WAIT_FOR_INPUT](options: InteractiveEditorRunOptions | undefined): Promise<State.ACCEPT | State.CANCEL | State.PAUSE | State.WAIT_FOR_INPUT | State.MAKE_REQUEST> {
439367
assertType(this._activeSession);
440368

441-
442-
console.log('inside of wait for input');
443-
444369
this._zone.value.widget.placeholder = this._getPlaceholderText();
445-
this._showWidget(this._initialRender, this._updateEditMode);
446-
this._initialRender = false;
447-
this._updateEditMode = false;
448370

449371
if (options?.message) {
450372
this._zone.value.widget.value = options?.message;
@@ -483,9 +405,6 @@ export class InteractiveEditorController implements IEditorContribution {
483405
}
484406

485407
if (!this._zone.value.widget.value) {
486-
487-
console.log('before the case when we call WAIT_FOR_INPUT');
488-
489408
return State.WAIT_FOR_INPUT;
490409
}
491410

@@ -515,9 +434,6 @@ export class InteractiveEditorController implements IEditorContribution {
515434
private async [State.MAKE_REQUEST](): Promise<State.APPLY_RESPONSE | State.PAUSE | State.CANCEL | State.ACCEPT> {
516435
assertType(this._editor.hasModel());
517436
assertType(this._activeSession);
518-
519-
console.log('this._activeSession.lastInput : ', this._activeSession.lastInput);
520-
521437
assertType(this._activeSession.lastInput);
522438

523439
const requestCts = new CancellationTokenSource();
@@ -628,6 +544,7 @@ export class InteractiveEditorController implements IEditorContribution {
628544
assertType(this._strategy);
629545

630546
const { response } = this._activeSession.lastExchange!;
547+
this._showWidget(false, response instanceof EditResponse); // this._updateEditMode, r
631548

632549
this._ctxLastResponseType.set(response instanceof EditResponse || response instanceof MarkdownResponse
633550
? response.raw.type

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import { toErrorMessage } from 'vs/base/common/errorMessage';
2525
import { isCancellationError } from 'vs/base/common/errors';
2626
import { LineRangeMapping } from 'vs/editor/common/diff/linesDiffComputer';
2727
import { ISingleEditOperation } from 'vs/editor/common/core/editOperation';
28-
import { Position } from 'vs/editor/common/core/position';
2928

3029
export type Recording = {
3130
when: Date;
@@ -113,7 +112,6 @@ export class Session {
113112
private _lastExpansionState: ExpansionState | undefined;
114113
private _lastTextModelChanges: readonly LineRangeMapping[] | undefined;
115114
private _isUnstashed: boolean = false;
116-
private _lastPosition: Position | undefined;
117115
private readonly _exchange: SessionExchange[] = [];
118116
private readonly _startTime = new Date();
119117
private readonly _teldata: Partial<TelemetryData>;
@@ -157,14 +155,6 @@ export class Session {
157155
return this._lastInput;
158156
}
159157

160-
get lastPosition() {
161-
return this._lastPosition;
162-
}
163-
164-
set lastPosition(position: Position | undefined) {
165-
this._lastPosition = position;
166-
}
167-
168158
get lastExpansionState(): ExpansionState | undefined {
169159
return this._lastExpansionState;
170160
}

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

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,14 @@ export abstract class EditModeStrategy {
4141

4242
abstract toggleDiff(): void;
4343

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-
}
44+
abstract getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined;
6645
}
6746

6847
export class PreviewStrategy extends EditModeStrategy {
6948

7049
private readonly _ctxDocumentChanged: IContextKey<boolean>;
7150
private readonly _listener: IDisposable;
51+
private _initialPosition: Position | undefined;
7252

7353
constructor(
7454
private readonly _session: Session,
@@ -151,9 +131,30 @@ export class PreviewStrategy extends EditModeStrategy {
151131
// nothing to do
152132
}
153133

154-
getWidgetPosition(initialRender: boolean, range: Range): Position | undefined {
155-
console.log('inside of preview strategy, getWidgetPosition');
156-
return positionCalculation(this._editor, initialRender, range);
134+
getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined {
135+
console.log('inside of preview strategy');
136+
const viewModel = this._editor._getViewModel();
137+
if (!viewModel) {
138+
return;
139+
}
140+
const primaryCursorPosition = viewModel.getPrimaryCursorState().viewState.position;
141+
if (initialRender) {
142+
this._initialPosition = primaryCursorPosition;
143+
return this._initialPosition;
144+
} else {
145+
if (hasEditResponse) {
146+
const visibleRange = viewModel.getCompletelyVisibleViewRange();
147+
const endPosition = range.getEndPosition();
148+
const endLine = endPosition.lineNumber;
149+
if (endLine >= visibleRange.startLineNumber && endLine <= visibleRange.endLineNumber) {
150+
return endPosition;
151+
} else {
152+
return this._initialPosition;
153+
}
154+
} else {
155+
return this._initialPosition;
156+
}
157+
}
157158
}
158159
}
159160

@@ -233,6 +234,7 @@ export class LiveStrategy extends EditModeStrategy {
233234
private readonly _ctxShowingDiff: IContextKey<boolean>;
234235
private _lastResponse?: EditResponse;
235236
private _editCount: number = 0;
237+
protected _initialPosition: Position | undefined;
236238

237239
constructor(
238240
protected readonly _session: Session,
@@ -348,9 +350,28 @@ export class LiveStrategy extends EditModeStrategy {
348350
this._widget.updateStatus(message);
349351
}
350352

351-
getWidgetPosition(initialRender: boolean, range: Range): Position | undefined {
352-
console.log('inside of live strategy, getWidgetPosition');
353-
return positionCalculation(this._editor, initialRender, range);
353+
getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined {
354+
const viewModel = this._editor._getViewModel();
355+
if (!viewModel) {
356+
return;
357+
}
358+
if (initialRender) {
359+
this._initialPosition = viewModel.getPrimaryCursorState().viewState.position;
360+
return this._initialPosition;
361+
} else {
362+
if (hasEditResponse) {
363+
const visibleRange = viewModel.getCompletelyVisibleViewRange();
364+
const endPosition = range.getEndPosition();
365+
const endLine = endPosition.lineNumber;
366+
if (endLine >= visibleRange.startLineNumber && endLine <= visibleRange.endLineNumber) {
367+
return endPosition;
368+
} else {
369+
return this._initialPosition;
370+
}
371+
} else {
372+
return this._initialPosition;
373+
}
374+
}
354375
}
355376
}
356377

@@ -407,12 +428,18 @@ export class LivePreviewStrategy extends LiveStrategy {
407428
scrollState.restore(this._editor);
408429
}
409430

410-
override getWidgetPosition(initialRender: boolean, range: Range): Position | undefined {
411-
console.log('inside of live preview strategy, getWidgetPosition');
431+
override getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined {
432+
console.log('inside of live preview strategy');
433+
const primaryCursorPosition = this._editor._getViewModel()?.getPrimaryCursorState().viewState.position;
412434
if (initialRender) {
413-
return this._editor._getViewModel()?.getPrimaryCursorState().viewState.position;
435+
this._initialPosition = primaryCursorPosition;
436+
return this._initialPosition;
414437
} else {
415-
return range.getEndPosition();
438+
if (hasEditResponse) {
439+
return range.getEndPosition();
440+
} else {
441+
return this._initialPosition;
442+
}
416443
}
417444
}
418445
}

0 commit comments

Comments
 (0)