Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawning the interactive editor widget with left indentation #184480

Merged
merged 13 commits into from Jun 12, 2023
51 changes: 41 additions & 10 deletions src/vs/workbench/contrib/inlineChat/browser/inlineChatWidget.ts
Expand Up @@ -26,7 +26,7 @@ import { EmbeddedCodeEditorWidget, EmbeddedDiffEditorWidget } from 'vs/editor/br
import { HiddenItemStrategy, MenuWorkbenchToolBar } from 'vs/platform/actions/browser/toolbar';
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
import { SuggestController } from 'vs/editor/contrib/suggest/browser/suggestController';
import { IPosition, Position } from 'vs/editor/common/core/position';
import { Position } from 'vs/editor/common/core/position';
import { DEFAULT_FONT_FAMILY } from 'vs/workbench/browser/style';
import { DropdownWithDefaultActionViewItem, IMenuEntryActionViewItemOptions, MenuEntryActionViewItem, createActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { CompletionItem, CompletionItemInsertTextRule, CompletionItemKind, CompletionItemProvider, CompletionList, ProviderResult, TextEdit } from 'vs/editor/common/languages';
Expand Down Expand Up @@ -716,6 +716,7 @@ export class InteractiveEditorZoneWidget extends ZoneWidget {
private readonly _ctxVisible: IContextKey<boolean>;
private readonly _ctxCursorPosition: IContextKey<'above' | 'below' | ''>;
private _dimension?: Dimension;
private _indentationWidth: number = 0;
aiday-mar marked this conversation as resolved.
Show resolved Hide resolved

constructor(
editor: ICodeEditor,
Expand Down Expand Up @@ -762,19 +763,18 @@ export class InteractiveEditorZoneWidget extends ZoneWidget {

protected override _doLayout(heightInPixel: number): void {

const info = this.editor.getLayoutInfo();
const spaceLeft = info.lineNumbersWidth + info.glyphMarginWidth + info.decorationsWidth;
const spaceRight = info.minimap.minimapWidth + info.verticalScrollbarWidth;

const maxWidth = !this.widget.showsAnyPreview() ? 640 : Number.MAX_SAFE_INTEGER;
const width = Math.min(maxWidth, info.contentWidth - (info.glyphMarginWidth + info.decorationsWidth));
const width = Math.min(maxWidth, this._availableSpaceGivenIndentation());
this._dimension = new Dimension(width, heightInPixel);
this.widget.domNode.style.marginLeft = `${spaceLeft}px`;
this.widget.domNode.style.marginRight = `${spaceRight}px`;
this.widget.domNode.style.width = `${width}px`;
this.widget.layout(this._dimension);
}

private _availableSpaceGivenIndentation(): number {
const info = this.editor.getLayoutInfo();
return info.contentWidth - (info.glyphMarginWidth + info.decorationsWidth + this._indentationWidth);
}

private _computeHeightInLines(): number {
const lineHeight = this.editor.getOption(EditorOption.lineHeight);
return this.widget.getHeight() / lineHeight;
Expand All @@ -787,10 +787,41 @@ export class InteractiveEditorZoneWidget extends ZoneWidget {
super._relayout(this._computeHeightInLines());
}

override show(where: IPosition): void {
super.show(where, this._computeHeightInLines());
override show(position: Position): void {
super.show(position, this._computeHeightInLines());
this.widget.focus();
this._ctxVisible.set(true);
this._setMargins(position);
}

private _setMargins(position: Position): void {
const viewModel = this.editor._getViewModel();
if (!viewModel) {
return;
}
const visibleRange = viewModel.getCompletelyVisibleViewRange();
const startLineVisibleRange = visibleRange.startLineNumber;
const positionLine = position.lineNumber;
let indentationLineNumber: number | undefined;
let indentationLevel: number | undefined;
for (let lineNumber = positionLine; lineNumber >= startLineVisibleRange; lineNumber--) {
const currentIndentationLevel = viewModel.getLineFirstNonWhitespaceColumn(lineNumber);
if (currentIndentationLevel !== 0) {
indentationLineNumber = lineNumber;
indentationLevel = currentIndentationLevel;
break;
}
}
this._indentationWidth = this.editor.getOffsetForColumn(indentationLineNumber ?? positionLine, indentationLevel ?? viewModel.getLineFirstNonWhitespaceColumn(positionLine));
const info = this.editor.getLayoutInfo();
const marginWithoutIndentation = info.glyphMarginWidth + info.decorationsWidth + info.lineNumbersWidth;
const marginWithIndentation = marginWithoutIndentation + this._indentationWidth;
const isEnoughAvailableSpaceWithIndentation = this._availableSpaceGivenIndentation() > 400;
this._indentationWidth = isEnoughAvailableSpaceWithIndentation ? this._indentationWidth : 0;
const spaceLeft = isEnoughAvailableSpaceWithIndentation ? marginWithIndentation : marginWithoutIndentation;
const spaceRight = info.minimap.minimapWidth + info.verticalScrollbarWidth;
this.widget.domNode.style.marginLeft = `${spaceLeft}px`;
this.widget.domNode.style.marginRight = `${spaceRight}px`;
}

override hide(): void {
Expand Down