Skip to content

Commit

Permalink
fix: improve live inline diff prioritization and fix discard function…
Browse files Browse the repository at this point in the history
…ality (#3837)

* fix: discard

* fix: content widget handle

* chore: improve prioritize those closest to the head

* chore: improve layout code

* fix: removed widget position

* feat: support setValue

* fix: inlineContentWidget
  • Loading branch information
Ricbet authored Jul 5, 2024
1 parent 4b9caae commit ae0cfd5
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,23 +336,13 @@ export class InlineChatHandler extends Disposable {
this.diffPreviewer = this.injector.get(LiveInlineDiffPreviewer, [monacoEditor, crossSelection]);
}

this.diffPreviewer.mount(this.aiInlineContentWidget);

this.diffPreviewer.show(
crossSelection.startLineNumber - 1,
crossSelection.endLineNumber - crossSelection.startLineNumber + 2,
);

const doLayoutContentWidget = () => {
if (inlineDiffMode === EInlineDifPreviewMode.sideBySide) {
this.aiInlineContentWidget.setPositionPreference([ContentWidgetPositionPreference.BELOW]);
} else {
this.aiInlineContentWidget.setPositionPreference([ContentWidgetPositionPreference.EXACT]);
}
this.aiInlineContentWidget?.setOptions({
position: this.diffPreviewer.getPosition(),
});
this.aiInlineContentWidget?.layoutContentWidget();
};

if (InlineChatController.is(chatResponse)) {
const controller = chatResponse as InlineChatController;

Expand All @@ -374,7 +364,7 @@ export class InlineChatHandler extends Disposable {
isRetry,
});
this.diffPreviewer.onError(error);
doLayoutContentWidget();
this.diffPreviewer.layout();
}),
controller.onAbort(() => {
this.convertInlineChatStatus(EInlineChatStatus.READY, {
Expand All @@ -385,7 +375,7 @@ export class InlineChatHandler extends Disposable {
isStop: true,
});
this.diffPreviewer.onAbort();
doLayoutContentWidget();
this.diffPreviewer.layout();
}),
controller.onEnd(() => {
this.convertInlineChatStatus(EInlineChatStatus.DONE, {
Expand All @@ -395,7 +385,7 @@ export class InlineChatHandler extends Disposable {
isRetry,
});
this.diffPreviewer.onEnd();
doLayoutContentWidget();
this.diffPreviewer.layout();
}),
]);
}),
Expand Down Expand Up @@ -442,11 +432,7 @@ export class InlineChatHandler extends Disposable {
);
}

this.aiInlineChatOperationDisposed.addDispose(
this.diffPreviewer.onLayout(() => {
doLayoutContentWidget();
}),
);
this.diffPreviewer.layout();

this.aiInlineChatOperationDisposed.addDispose(
this.diffPreviewer.onDispose(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Autowired, INJECTOR_TOKEN, Injectable, Injector } from '@opensumi/di';
import { Disposable, ErrorResponse, ReplyResponse } from '@opensumi/ide-core-common';
import { EOL, ICodeEditor, IPosition, ITextModel, Position, Selection } from '@opensumi/ide-monaco';
import { LineRange } from '@opensumi/monaco-editor-core/esm/vs/editor/common/core/lineRange';
import { ContentWidgetPositionPreference } from '@opensumi/ide-monaco/lib/browser/monaco-exports/editor';
import { DefaultEndOfLine } from '@opensumi/monaco-editor-core/esm/vs/editor/common/model';
import { createTextBuffer } from '@opensumi/monaco-editor-core/esm/vs/editor/common/model/textModel';
import { ModelService } from '@opensumi/monaco-editor-core/esm/vs/editor/common/services/modelService';

import { EResultKind } from '../inline-chat/inline-chat.service';
import { AIInlineContentWidget } from '../inline-chat/inline-content-widget';
import { EComputerMode, InlineStreamDiffHandler } from '../inline-stream-diff/inline-stream-diff.handler';

import { InlineDiffWidget } from './inline-diff-widget';
Expand All @@ -16,6 +17,8 @@ export abstract class BaseInlineDiffPreviewer<N> extends Disposable {
@Autowired(INJECTOR_TOKEN)
protected readonly injector: Injector;

protected inlineContentWidget: AIInlineContentWidget | null = null;

constructor(protected readonly monacoEditor: ICodeEditor, protected readonly selection: Selection) {
super();
this.node = this.createNode();
Expand All @@ -30,15 +33,20 @@ export abstract class BaseInlineDiffPreviewer<N> extends Disposable {
return this.node;
}

public mount(contentWidget: AIInlineContentWidget): void {
this.inlineContentWidget = contentWidget;
}

public layout(): void {
this.inlineContentWidget?.setOptions({ position: this.getPosition() });
this.inlineContentWidget?.layoutContentWidget();
}

abstract onReady(exec: () => void): Disposable;
abstract onLayout(exec: () => void): Disposable;
abstract createNode(): N;
abstract onData(data: ReplyResponse): void;
abstract handleAction(action: EResultKind): void;

getPosition(): IPosition | undefined {
return undefined;
}
abstract getPosition(): IPosition;

show(line: number, heightInLines: number): void {
// do nothing
Expand Down Expand Up @@ -80,19 +88,15 @@ export class SideBySideInlineDiffWidget extends BaseInlineDiffPreviewer<InlineDi
},
]);
widget.create();
this.addDispose(
Disposable.create(() => {
widget.dispose();
}),
);
this.addDispose(widget);
return widget;
}
getPosition(): IPosition | undefined {
getPosition(): IPosition {
return Position.lift({ lineNumber: this.selection.endLineNumber + 1, column: 1 });
}
onLayout(exec: () => void): Disposable {
requestAnimationFrame(() => exec());
return this;
layout(): void {
this.inlineContentWidget?.setPositionPreference([ContentWidgetPositionPreference.BELOW]);
super.layout();
}
onReady(exec: () => void): Disposable {
this.addDispose(this.node.onReady(exec.bind(this)));
Expand Down Expand Up @@ -150,14 +154,27 @@ export class LiveInlineDiffPreviewer extends BaseInlineDiffPreviewer<InlineStrea
}
createNode(): InlineStreamDiffHandler {
const node = this.injector.get(InlineStreamDiffHandler, [this.monacoEditor, this.selection]);

this.addDispose(node.onDidEditChange(() => this.layout()));
this.addDispose(node.onDispose(() => this.dispose()));
this.addDispose(node);

node.registerPartialEditWidgetHandle((widgets) => {
if (widgets.every((widget) => widget.isHidden)) {
this.dispose();
this.inlineContentWidget?.dispose();
}
});
return node;
}
getPosition(): IPosition | undefined {
getPosition(): IPosition {
const zone = this.node.getZone();
return Position.lift({ lineNumber: Math.max(0, zone.startLineNumber - 1), column: 1 });
}
setValue(content: string): void {
const diffModel = this.node.recompute(EComputerMode.legacy, content);
this.node.readyRender(diffModel);
}
handleAction(action: EResultKind): void {
switch (action) {
case EResultKind.ACCEPT:
Expand All @@ -167,29 +184,23 @@ export class LiveInlineDiffPreviewer extends BaseInlineDiffPreviewer<InlineStrea
case EResultKind.DISCARD:
case EResultKind.REGENERATE:
this.node.discard();
this.node.dispose();
break;

default:
break;
}
}
onLayout(exec: () => void): Disposable {
this.node.onDidEditChange(exec.bind(this));
return this;
layout(): void {
this.inlineContentWidget?.setPositionPreference([ContentWidgetPositionPreference.EXACT]);
super.layout();
}
onData(data: ReplyResponse): void {
const { message } = data;
this.node.addLinesToDiff(message);
}
onEnd(): void {
const { changes } = this.node.recompute(EComputerMode.legacy);
const zone = this.node.getZone();
const allAddRanges = changes.map((c) => {
const lineNumber = zone.startLineNumber + c.addedRange.startLineNumber - 1;
return new LineRange(lineNumber, lineNumber + 1);
});
this.node.renderPartialEditWidgets(allAddRanges);
this.node.pushStackElement();
this.monacoEditor.focus();
const diffModel = this.node.recompute(EComputerMode.legacy);
this.node.readyRender(diffModel);
}
}
Loading

0 comments on commit ae0cfd5

Please sign in to comment.