Skip to content

Commit e3074e4

Browse files
committed
refactoring
1 parent 0a02b55 commit e3074e4

File tree

3 files changed

+31
-44
lines changed

3 files changed

+31
-44
lines changed

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/c
2929
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
3030
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
3131
import { ILogService } from 'vs/platform/log/common/log';
32-
import { EditResponse, EmptyResponse, ErrorResponse, IInteractiveEditorSessionService, MarkdownResponse, MarkdownResponseCropState, Session, SessionExchange } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession';
32+
import { EditResponse, EmptyResponse, ErrorResponse, IInteractiveEditorSessionService, MarkdownResponse, Session, SessionExchange } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession';
3333
import { EditModeStrategy, LivePreviewStrategy, LiveStrategy, PreviewStrategy } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorStrategies';
3434
import { InteractiveEditorZoneWidget } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorWidget';
35-
import { CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST, CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK, IInteractiveEditorRequest, IInteractiveEditorResponse, INTERACTIVE_EDITOR_ID, EditMode, InteractiveEditorResponseFeedbackKind, CTX_INTERACTIVE_EDITOR_LAST_RESPONSE_TYPE, InteractiveEditorResponseType, CTX_INTERACTIVE_EDITOR_DID_EDIT, CTX_INTERACTIVE_EDITOR_MESSAGE_CROP_STATE } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
35+
import { CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST, CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK, IInteractiveEditorRequest, IInteractiveEditorResponse, INTERACTIVE_EDITOR_ID, EditMode, InteractiveEditorResponseFeedbackKind, CTX_INTERACTIVE_EDITOR_LAST_RESPONSE_TYPE, InteractiveEditorResponseType, CTX_INTERACTIVE_EDITOR_DID_EDIT } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
3636
import { IChatWidgetService } from 'vs/workbench/contrib/chat/browser/chat';
3737
import { IChatService } from 'vs/workbench/contrib/chat/common/chatService';
3838
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
@@ -85,7 +85,6 @@ export class InteractiveEditorController implements IEditorContribution {
8585
private readonly _store = new DisposableStore();
8686
private readonly _zone: InteractiveEditorZoneWidget;
8787
private readonly _ctxHasActiveRequest: IContextKey<boolean>;
88-
private readonly _ctxMessageCropState: IContextKey<'cropped' | 'not_cropped' | 'expanded'>;
8988
private readonly _ctxLastResponseType: IContextKey<undefined | InteractiveEditorResponseType>;
9089
private readonly _ctxDidEdit: IContextKey<boolean>;
9190
private readonly _ctxLastFeedbackKind: IContextKey<'helpful' | 'unhelpful' | ''>;
@@ -111,7 +110,6 @@ export class InteractiveEditorController implements IEditorContribution {
111110
@IAccessibilityService private readonly _accessibilityService: IAccessibilityService,
112111
@IKeybindingService private readonly _keybindingService: IKeybindingService,
113112
) {
114-
this._ctxMessageCropState = CTX_INTERACTIVE_EDITOR_MESSAGE_CROP_STATE.bindTo(contextKeyService);
115113
this._ctxHasActiveRequest = CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST.bindTo(contextKeyService);
116114
this._ctxDidEdit = CTX_INTERACTIVE_EDITOR_DID_EDIT.bindTo(contextKeyService);
117115
this._ctxLastResponseType = CTX_INTERACTIVE_EDITOR_LAST_RESPONSE_TYPE.bindTo(contextKeyService);
@@ -473,7 +471,7 @@ export class InteractiveEditorController implements IEditorContribution {
473471
const { response } = this._activeSession.lastExchange!;
474472
if (response instanceof EditResponse) {
475473
// edit response -> complex...
476-
this.updateMarkdownMessage(undefined);
474+
this._zone.widget.updateMarkdownMessage(undefined);
477475

478476
const canContinue = this._strategy.checkChanges(response);
479477
if (!canContinue) {
@@ -524,13 +522,15 @@ export class InteractiveEditorController implements IEditorContribution {
524522

525523
} else if (response instanceof MarkdownResponse) {
526524
// clear status, show MD message
525+
const renderedMarkdown = renderMarkdown(response.raw.message, { inline: true });
527526
this._zone.widget.updateStatus('');
528-
this.updateMarkdownMessage(response);
527+
this._zone.widget.updateMarkdownMessage(renderedMarkdown.element);
529528
this._zone.widget.updateToolbar(true);
529+
this._zone.widget.updateMarkdownMessageExpansionState(this._activeSession.lastExpansionState);
530530

531531
} else if (response instanceof EditResponse) {
532532
// edit response -> complex...
533-
this.updateMarkdownMessage(undefined);
533+
this._zone.widget.updateMarkdownMessage(undefined);
534534
this._zone.widget.updateToolbar(true);
535535

536536
const canContinue = this._strategy.checkChanges(response);
@@ -579,22 +579,6 @@ export class InteractiveEditorController implements IEditorContribution {
579579

580580
// ---- controller API
581581

582-
updateMarkdownMessage(response: MarkdownResponse | undefined): void {
583-
if (response) {
584-
const renderedMarkdown = renderMarkdown(response.raw.message, { inline: true });
585-
this._zone.widget.updateMarkdownMessage(renderedMarkdown.element);
586-
let cropState = response.cropState;
587-
if (!cropState) {
588-
cropState = this._zone.widget.isMarkdownMessageOverflowing() ? MarkdownResponseCropState.CROPPED : MarkdownResponseCropState.NOT_CROPPED;
589-
}
590-
this._ctxMessageCropState.set(cropState);
591-
response.cropState = cropState;
592-
this._zone.widget.updateToggleState(cropState === MarkdownResponseCropState.EXPANDED);
593-
} else {
594-
this._zone.widget.updateMarkdownMessage(undefined);
595-
this._ctxMessageCropState.reset();
596-
}
597-
}
598582
accept(): void {
599583
this._messages.fire(Message.ACCEPT_INPUT);
600584
}
@@ -642,12 +626,9 @@ export class InteractiveEditorController implements IEditorContribution {
642626
}
643627

644628
updateExpansionState(expand: boolean) {
645-
const response = this._activeSession?.lastExchange!.response;
646-
if (response instanceof MarkdownResponse) {
647-
const cropState = expand ? MarkdownResponseCropState.EXPANDED : MarkdownResponseCropState.CROPPED;
648-
response.cropState = cropState;
649-
this._ctxMessageCropState.set(cropState);
650-
this._zone.widget.updateToggleState(expand);
629+
if (this._activeSession) {
630+
this._zone.widget.updateMarkdownMessageExpansionState(expand);
631+
this._activeSession.lastExpansionState = expand;
651632
}
652633
}
653634

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type TelemetryDataClassification = {
5959
export class Session {
6060

6161
private _lastInput: string | undefined;
62+
private _lastExpansionState: boolean | undefined;
6263
private _lastTextModelChanges: LineRangeMapping[] | undefined;
6364
private _lastSnapshot: ITextSnapshot | undefined;
6465
private readonly _exchange: SessionExchange[] = [];
@@ -92,6 +93,14 @@ export class Session {
9293
return this._lastInput;
9394
}
9495

96+
get lastExpansionState() {
97+
return this._lastExpansionState ?? false;
98+
}
99+
100+
set lastExpansionState(state: boolean) {
101+
this._lastExpansionState = state;
102+
}
103+
95104
get lastSnapshot(): ITextSnapshot | undefined {
96105
return this._lastSnapshot;
97106
}
@@ -194,16 +203,7 @@ export class ErrorResponse {
194203
}
195204
}
196205

197-
export enum MarkdownResponseCropState {
198-
CROPPED = 'cropped',
199-
NOT_CROPPED = 'not_cropped',
200-
EXPANDED = 'expanded'
201-
}
202-
203206
export class MarkdownResponse {
204-
205-
cropState: MarkdownResponseCropState | undefined;
206-
207207
constructor(
208208
readonly localUri: URI,
209209
readonly raw: IInteractiveEditorMessageResponse

src/vs/workbench/contrib/interactiveEditor/browser/interactiveEditorWidget.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { localize } from 'vs/nls';
1212
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
1313
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1414
import { ZoneWidget } from 'vs/editor/contrib/zoneWidget/browser/zoneWidget';
15-
import { CTX_INTERACTIVE_EDITOR_FOCUSED, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_FIRST, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_LAST, CTX_INTERACTIVE_EDITOR_EMPTY, CTX_INTERACTIVE_EDITOR_OUTER_CURSOR_POSITION, CTX_INTERACTIVE_EDITOR_VISIBLE, MENU_INTERACTIVE_EDITOR_WIDGET, MENU_INTERACTIVE_EDITOR_WIDGET_STATUS, MENU_INTERACTIVE_EDITOR_WIDGET_MARKDOWN_MESSAGE, IInteractiveEditorSlashCommand } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
15+
import { CTX_INTERACTIVE_EDITOR_FOCUSED, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_FIRST, CTX_INTERACTIVE_EDITOR_INNER_CURSOR_LAST, CTX_INTERACTIVE_EDITOR_EMPTY, CTX_INTERACTIVE_EDITOR_OUTER_CURSOR_POSITION, CTX_INTERACTIVE_EDITOR_VISIBLE, MENU_INTERACTIVE_EDITOR_WIDGET, MENU_INTERACTIVE_EDITOR_WIDGET_STATUS, MENU_INTERACTIVE_EDITOR_WIDGET_MARKDOWN_MESSAGE, CTX_INTERACTIVE_EDITOR_MESSAGE_CROP_STATE, IInteractiveEditorSlashCommand } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
1616
import { IModelDeltaDecoration, ITextModel } from 'vs/editor/common/model';
1717
import { Dimension, addDisposableListener, getTotalHeight, getTotalWidth, h, reset } from 'vs/base/browser/dom';
1818
import { Emitter, Event, MicrotaskEmitter } from 'vs/base/common/event';
@@ -152,6 +152,7 @@ export class InteractiveEditorWidget {
152152
private readonly _inputEditor: IActiveCodeEditor;
153153
private readonly _inputModel: ITextModel;
154154
private readonly _ctxInputEmpty: IContextKey<boolean>;
155+
private readonly _ctxMessageCropState: IContextKey<'cropped' | 'not_cropped' | 'expanded'>;
155156

156157
private readonly _progressBar: ProgressBar;
157158

@@ -210,6 +211,7 @@ export class InteractiveEditorWidget {
210211

211212
// --- context keys
212213

214+
this._ctxMessageCropState = CTX_INTERACTIVE_EDITOR_MESSAGE_CROP_STATE.bindTo(this._contextKeyService);
213215
this._ctxInputEmpty = CTX_INTERACTIVE_EDITOR_EMPTY.bindTo(this._contextKeyService);
214216

215217
const ctxInnerCursorFirst = CTX_INTERACTIVE_EDITOR_INNER_CURSOR_FIRST.bindTo(this._contextKeyService);
@@ -318,6 +320,7 @@ export class InteractiveEditorWidget {
318320
dispose(): void {
319321
this._store.dispose();
320322
this._ctxInputEmpty.reset();
323+
this._ctxMessageCropState.reset();
321324
}
322325

323326
get domNode(): HTMLElement {
@@ -390,18 +393,20 @@ export class InteractiveEditorWidget {
390393
updateMarkdownMessage(message: Node | undefined) {
391394
this._elements.markdownMessage.classList.toggle('hidden', !message);
392395
if (!message) {
396+
this._ctxMessageCropState.reset();
393397
reset(this._elements.message);
394398

395399
} else {
396400
reset(this._elements.message, message);
401+
if (this._elements.message.scrollHeight > this._elements.message.clientHeight) {
402+
this._ctxMessageCropState.set('cropped');
403+
} else {
404+
this._ctxMessageCropState.set('not_cropped');
405+
}
397406
}
398407
this._onDidChangeHeight.fire();
399408
}
400409

401-
isMarkdownMessageOverflowing(): boolean {
402-
return this._elements.message.scrollHeight > this._elements.message.clientHeight;
403-
}
404-
405410
updateStatus(message: string, ops: { classes?: string[]; resetAfter?: number; keepMessage?: boolean } = {}) {
406411
const isTempMessage = typeof ops.resetAfter === 'number';
407412
if (isTempMessage && !this._elements.statusLabel.dataset['state']) {
@@ -439,7 +444,8 @@ export class InteractiveEditorWidget {
439444
this._inputEditor.focus();
440445
}
441446

442-
updateToggleState(expand: boolean) {
447+
updateMarkdownMessageExpansionState(expand: boolean) {
448+
this._ctxMessageCropState.set(expand ? 'expanded' : 'cropped');
443449
this._elements.message.style.webkitLineClamp = expand ? '10' : '3';
444450
this._onDidChangeHeight.fire();
445451
}

0 commit comments

Comments
 (0)