Skip to content

Commit 4144b0a

Browse files
authored
Merge pull request microsoft#184480 from microsoft/aiday/internalIssue4344
Spawning the interactive editor widget with left indentation
2 parents 323e6df + e2d9e43 commit 4144b0a

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { EmbeddedCodeEditorWidget, EmbeddedDiffEditorWidget } from 'vs/editor/br
2626
import { HiddenItemStrategy, MenuWorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
2727
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
2828
import { SuggestController } from 'vs/editor/contrib/suggest/browser/suggestController';
29-
import { IPosition, Position } from 'vs/editor/common/core/position';
29+
import { Position } from 'vs/editor/common/core/position';
3030
import { DEFAULT_FONT_FAMILY } from 'vs/workbench/browser/style';
3131
import { DropdownWithDefaultActionViewItem, IMenuEntryActionViewItemOptions, MenuEntryActionViewItem, createActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
3232
import { CompletionItem, CompletionItemInsertTextRule, CompletionItemKind, CompletionItemProvider, CompletionList, ProviderResult, TextEdit } from 'vs/editor/common/languages';
@@ -720,6 +720,7 @@ export class InlineChatZoneWidget extends ZoneWidget {
720720
private readonly _ctxVisible: IContextKey<boolean>;
721721
private readonly _ctxCursorPosition: IContextKey<'above' | 'below' | ''>;
722722
private _dimension?: Dimension;
723+
private _indentationWidth: number = 0;
723724

724725
constructor(
725726
editor: ICodeEditor,
@@ -766,19 +767,18 @@ export class InlineChatZoneWidget extends ZoneWidget {
766767

767768
protected override _doLayout(heightInPixel: number): void {
768769

769-
const info = this.editor.getLayoutInfo();
770-
const spaceLeft = info.lineNumbersWidth + info.glyphMarginWidth + info.decorationsWidth;
771-
const spaceRight = info.minimap.minimapWidth + info.verticalScrollbarWidth;
772-
773770
const maxWidth = !this.widget.showsAnyPreview() ? 640 : Number.MAX_SAFE_INTEGER;
774-
const width = Math.min(maxWidth, info.contentWidth - (info.glyphMarginWidth + info.decorationsWidth));
771+
const width = Math.min(maxWidth, this._availableSpaceGivenIndentation());
775772
this._dimension = new Dimension(width, heightInPixel);
776-
this.widget.domNode.style.marginLeft = `${spaceLeft}px`;
777-
this.widget.domNode.style.marginRight = `${spaceRight}px`;
778773
this.widget.domNode.style.width = `${width}px`;
779774
this.widget.layout(this._dimension);
780775
}
781776

777+
private _availableSpaceGivenIndentation(): number {
778+
const info = this.editor.getLayoutInfo();
779+
return info.contentWidth - (info.glyphMarginWidth + info.decorationsWidth + this._indentationWidth);
780+
}
781+
782782
private _computeHeightInLines(): number {
783783
const lineHeight = this.editor.getOption(EditorOption.lineHeight);
784784
return this.widget.getHeight() / lineHeight;
@@ -791,10 +791,41 @@ export class InlineChatZoneWidget extends ZoneWidget {
791791
super._relayout(this._computeHeightInLines());
792792
}
793793

794-
override show(where: IPosition): void {
795-
super.show(where, this._computeHeightInLines());
794+
override show(position: Position): void {
795+
super.show(position, this._computeHeightInLines());
796796
this.widget.focus();
797797
this._ctxVisible.set(true);
798+
this._setMargins(position);
799+
}
800+
801+
private _setMargins(position: Position): void {
802+
const viewModel = this.editor._getViewModel();
803+
if (!viewModel) {
804+
return;
805+
}
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--) {
812+
const currentIndentationLevel = viewModel.getLineFirstNonWhitespaceColumn(lineNumber);
813+
if (currentIndentationLevel !== 0) {
814+
indentationLineNumber = lineNumber;
815+
indentationLevel = currentIndentationLevel;
816+
break;
817+
}
818+
}
819+
this._indentationWidth = this.editor.getOffsetForColumn(indentationLineNumber ?? positionLine, indentationLevel ?? viewModel.getLineFirstNonWhitespaceColumn(positionLine));
820+
const info = this.editor.getLayoutInfo();
821+
const marginWithoutIndentation = info.glyphMarginWidth + info.decorationsWidth + info.lineNumbersWidth;
822+
const marginWithIndentation = marginWithoutIndentation + this._indentationWidth;
823+
const isEnoughAvailableSpaceWithIndentation = this._availableSpaceGivenIndentation() > 400;
824+
this._indentationWidth = isEnoughAvailableSpaceWithIndentation ? this._indentationWidth : 0;
825+
const spaceLeft = isEnoughAvailableSpaceWithIndentation ? marginWithIndentation : marginWithoutIndentation;
826+
const spaceRight = info.minimap.minimapWidth + info.verticalScrollbarWidth;
827+
this.widget.domNode.style.marginLeft = `${spaceLeft}px`;
828+
this.widget.domNode.style.marginRight = `${spaceRight}px`;
798829
}
799830

800831
override hide(): void {

0 commit comments

Comments
 (0)