Skip to content

Commit b10d6b0

Browse files
committed
cleaning the code
1 parent 5e921de commit b10d6b0

File tree

3 files changed

+45
-56
lines changed

3 files changed

+45
-56
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export class InlineChatController implements IEditorContribution {
197197
assertType(this._activeSession);
198198
const selectionRange = this._activeSession.wholeRange.value;
199199
const widgetPosition = this._strategy?.getWidgetPosition(initialRender, selectionRange, hasEditResponse);
200-
this._zone.value.showWidget(widgetPosition ?? selectionRange.getStartPosition());
200+
this._zone.value.show(widgetPosition ?? selectionRange.getStartPosition());
201201
}
202202

203203
protected async _nextState(state: State, options: InlineChatRunOptions | undefined): Promise<void> {
@@ -544,7 +544,7 @@ export class InlineChatController implements IEditorContribution {
544544
assertType(this._strategy);
545545

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

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

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

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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';
89
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
910
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
1011
import { StableEditorScrollState } from 'vs/editor/browser/stableEditorScroll';
@@ -27,6 +28,8 @@ import { IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/
2728

2829
export abstract class EditModeStrategy {
2930

31+
protected _initialPosition: Position | undefined;
32+
3033
abstract dispose(): void;
3134

3235
abstract checkChanges(response: EditResponse): boolean;
@@ -41,16 +44,15 @@ export abstract class EditModeStrategy {
4144

4245
abstract toggleDiff(): void;
4346

44-
abstract getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined;
45-
4647
abstract hasFocus(): boolean;
48+
49+
abstract getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined;
4750
}
4851

4952
export class PreviewStrategy extends EditModeStrategy {
5053

5154
private readonly _ctxDocumentChanged: IContextKey<boolean>;
5255
private readonly _listener: IDisposable;
53-
private _initialPosition: Position | undefined;
5456

5557
constructor(
5658
private readonly _session: Session,
@@ -134,28 +136,13 @@ export class PreviewStrategy extends EditModeStrategy {
134136
}
135137

136138
getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined {
137-
console.log('inside of preview strategy');
138139
const viewModel = this._editor._getViewModel();
139-
if (!viewModel) {
140-
return;
141-
}
142-
const primaryCursorPosition = viewModel.getPrimaryCursorState().viewState.position;
140+
assertType(viewModel);
143141
if (initialRender) {
144-
this._initialPosition = primaryCursorPosition;
142+
this._initialPosition = viewModel.getPrimaryCursorState().viewState.position;
145143
return this._initialPosition;
146144
} else {
147-
if (hasEditResponse) {
148-
const visibleRange = viewModel.getCompletelyVisibleViewRange();
149-
const endPosition = range.getEndPosition();
150-
const endLine = endPosition.lineNumber;
151-
if (endLine >= visibleRange.startLineNumber && endLine <= visibleRange.endLineNumber) {
152-
return endPosition;
153-
} else {
154-
return this._initialPosition;
155-
}
156-
} else {
157-
return this._initialPosition;
158-
}
145+
return minimalJumpPosition(this._editor, this._initialPosition, range, hasEditResponse);
159146
}
160147
}
161148

@@ -240,7 +227,6 @@ export class LiveStrategy extends EditModeStrategy {
240227
private readonly _ctxShowingDiff: IContextKey<boolean>;
241228
private _lastResponse?: EditResponse;
242229
private _editCount: number = 0;
243-
protected _initialPosition: Position | undefined;
244230

245231
constructor(
246232
protected readonly _session: Session,
@@ -315,7 +301,7 @@ export class LiveStrategy extends EditModeStrategy {
315301
const cursorStateComputerAndInlineDiffCollection: ICursorStateComputer = (undoEdits) => {
316302
let last: Position | null = null;
317303
for (const edit of undoEdits) {
318-
last = !last || last.isBefore(edit.range.getEndPosition()) ? edit.range.getEndPosition() : last;
304+
last = !last || last.isBefore(edit.range.getStartPosition()) ? edit.range.getStartPosition() : last;
319305
this._inlineDiffDecorations.collectEditOperation(edit);
320306
}
321307
return last && [Selection.fromPositions(last)];
@@ -356,27 +342,14 @@ export class LiveStrategy extends EditModeStrategy {
356342
this._widget.updateStatus(message);
357343
}
358344

359-
getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined {
345+
override getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined {
360346
const viewModel = this._editor._getViewModel();
361-
if (!viewModel) {
362-
return;
363-
}
347+
assertType(viewModel);
364348
if (initialRender) {
365349
this._initialPosition = viewModel.getPrimaryCursorState().viewState.position;
366350
return this._initialPosition;
367351
} else {
368-
if (hasEditResponse) {
369-
const visibleRange = viewModel.getCompletelyVisibleViewRange();
370-
const endPosition = range.getEndPosition();
371-
const endLine = endPosition.lineNumber;
372-
if (endLine >= visibleRange.startLineNumber && endLine <= visibleRange.endLineNumber) {
373-
return endPosition;
374-
} else {
375-
return this._initialPosition;
376-
}
377-
} else {
378-
return this._initialPosition;
379-
}
352+
return minimalJumpPosition(this._editor, this._initialPosition, range, hasEditResponse);
380353
}
381354
}
382355

@@ -439,14 +412,14 @@ export class LivePreviewStrategy extends LiveStrategy {
439412
}
440413

441414
override getWidgetPosition(initialRender: boolean, range: Range, hasEditResponse: boolean | undefined): Position | undefined {
442-
console.log('inside of live preview strategy');
443-
const primaryCursorPosition = this._editor._getViewModel()?.getPrimaryCursorState().viewState.position;
444415
if (initialRender) {
445-
this._initialPosition = primaryCursorPosition;
416+
const viewModel = this._editor._getViewModel();
417+
assertType(viewModel);
418+
this._initialPosition = viewModel.getPrimaryCursorState().viewState.position;
446419
return this._initialPosition;
447420
} else {
448421
if (hasEditResponse) {
449-
return range.getEndPosition();
422+
return range.getStartPosition();
450423
} else {
451424
return this._initialPosition;
452425
}
@@ -464,3 +437,19 @@ function showSingleCreateFile(accessor: ServicesAccessor, edit: EditResponse) {
464437
editorService.openEditor({ resource: edit.singleCreateFileEdit.uri }, SIDE_GROUP);
465438
}
466439
}
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.getStartPosition();
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+
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ export class InlineChatWidget {
430430

431431
set value(value: string) {
432432
this._inputModel.setValue(value);
433-
this._inputEditor.setPosition(this._inputModel.getFullModelRange().getEndPosition());
433+
this._inputEditor.setPosition(this._inputModel.getFullModelRange().getStartPosition());
434434
}
435435

436436
selectAll() {
@@ -791,32 +791,32 @@ export class InlineChatZoneWidget extends ZoneWidget {
791791
super._relayout(this._computeHeightInLines());
792792
}
793793

794-
showWidget(position: Position): void {
795-
const widgetPosition = position;
796-
super.show(widgetPosition, this._computeHeightInLines());
794+
override show(position: Position): void {
795+
super.show(position, this._computeHeightInLines());
797796
this.widget.focus();
798797
this._ctxVisible.set(true);
799-
this._setMargins(widgetPosition);
798+
this._setMargins(position);
800799
}
801800

802801
private _setMargins(position: Position): void {
803802
const viewModel = this.editor._getViewModel();
804803
if (!viewModel) {
805804
return;
806805
}
807-
const positionLineNumber = position.lineNumber;
808-
const startLineNumber = viewModel.getCompletelyVisibleViewRange().startLineNumber;
809-
let indentationLineNumber;
810-
let indentationLevel;
811-
for (let lineNumber = positionLineNumber; lineNumber >= startLineNumber; lineNumber--) {
806+
const visibleRange = viewModel.getCompletelyVisibleViewRange();
807+
const startLineVisibleRange = visibleRange.startLineNumber;
808+
const positionLine = position.lineNumber;
809+
let indentationLineNumber: number | undefined;
810+
let indentationLevel: number | undefined;
811+
for (let lineNumber = positionLine; lineNumber >= startLineVisibleRange; lineNumber--) {
812812
const currentIndentationLevel = viewModel.getLineFirstNonWhitespaceColumn(lineNumber);
813813
if (currentIndentationLevel !== 0) {
814814
indentationLineNumber = lineNumber;
815815
indentationLevel = currentIndentationLevel;
816816
break;
817817
}
818818
}
819-
this._indentationWidth = this.editor.getOffsetForColumn(indentationLineNumber ?? positionLineNumber, indentationLevel ?? viewModel.getLineFirstNonWhitespaceColumn(positionLineNumber));
819+
this._indentationWidth = this.editor.getOffsetForColumn(indentationLineNumber ?? positionLine, indentationLevel ?? viewModel.getLineFirstNonWhitespaceColumn(positionLine));
820820
const info = this.editor.getLayoutInfo();
821821
const marginWithoutIndentation = info.glyphMarginWidth + info.decorationsWidth + info.lineNumbersWidth;
822822
const marginWithIndentation = marginWithoutIndentation + this._indentationWidth;

0 commit comments

Comments
 (0)