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

in notebooks cancel inline chat sessions from sibling editors when starting a new session #180509

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { InteractiveEditorZoneWidget } from 'vs/workbench/contrib/interactiveEdi
import { CTX_INTERACTIVE_EDITOR_HAS_ACTIVE_REQUEST, CTX_INTERACTIVE_EDITOR_HAS_RESPONSE, CTX_INTERACTIVE_EDITOR_INLNE_DIFF, CTX_INTERACTIVE_EDITOR_LAST_EDIT_TYPE as CTX_INTERACTIVE_EDITOR_LAST_EDIT_KIND, CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK as CTX_INTERACTIVE_EDITOR_LAST_FEEDBACK_KIND, IInteractiveEditorBulkEditResponse, IInteractiveEditorEditResponse, IInteractiveEditorRequest, IInteractiveEditorResponse, IInteractiveEditorService, IInteractiveEditorSession, IInteractiveEditorSessionProvider, IInteractiveEditorSlashCommand, INTERACTIVE_EDITOR_ID, EditMode, InteractiveEditorResponseFeedbackKind } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor';
import { IInteractiveSessionWidgetService } from 'vs/workbench/contrib/interactiveSession/browser/interactiveSessionWidget';
import { IInteractiveSessionService } from 'vs/workbench/contrib/interactiveSession/common/interactiveSessionService';
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon';


type Exchange = { req: IInteractiveEditorRequest; res: IInteractiveEditorResponse };
Expand Down Expand Up @@ -304,6 +306,7 @@ export class InteractiveEditorController implements IEditorContribution {
@IStorageService private readonly _storageService: IStorageService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IModelService private readonly _modelService: IModelService,
@INotebookEditorService private readonly _notebookEditorService: INotebookEditorService,
@IContextKeyService contextKeyService: IContextKeyService,

) {
Expand Down Expand Up @@ -334,6 +337,7 @@ export class InteractiveEditorController implements IEditorContribution {
const editMode = this._getMode();

this._ctsSession.dispose(true);
this._cancelNotebookSiblingEditors();

if (!this._editor.hasModel()) {
return;
Expand Down Expand Up @@ -691,6 +695,37 @@ export class InteractiveEditorController implements IEditorContribution {
this._editor.focus();
}

private _cancelNotebookSiblingEditors() {
if (!this._editor.hasModel()) {
return;
}
const candidate = CellUri.parse(this._editor.getModel().uri);
if (!candidate) {
return;
}
for (const editor of this._notebookEditorService.listNotebookEditors()) {
if (isEqual(editor.textModel?.uri, candidate.notebook)) {
let found = false;
const editors: ICodeEditor[] = [];
for (const [, codeEditor] of editor.codeEditors) {
editors.push(codeEditor);
found = codeEditor === this._editor || found;
}
if (found) {
// found the this editor in the outer notebook editor -> make sure to
// cancel all sibling sessions
for (const editor of editors) {
if (editor !== this._editor) {
InteractiveEditorController.get(editor)?.cancelSession();

}
}
break;
}
}
}
}

accept(): void {
this._zone.widget.acceptInput();
}
Expand Down Expand Up @@ -855,6 +890,9 @@ class LiveStrategy extends EditModeStrategy {

async cancel() {
const { modelN, model0 } = this._session;
if (modelN.isDisposed() || model0.isDisposed()) {
return;
}
const edits = await this._editorWorkerService.computeMoreMinimalEdits(modelN.uri, [{ range: modelN.getFullModelRange(), text: model0.getValue() }]);
if (edits) {
const operations = edits.map(e => EditOperation.replace(Range.lift(e.range), e.text));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function registerInteractiveSessionCodeBlockActions() {
}

if (notebookEditor.activeCodeEditor?.hasTextFocus()) {
const codeEditor = notebookEditor.activeCodeEditor as ICodeEditor;
const codeEditor = notebookEditor.activeCodeEditor;
const textModel = codeEditor.getModel();

if (textModel) {
Expand Down
4 changes: 3 additions & 1 deletion src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { cellRangesToIndexes, ICellRange, reduceCellRanges } from 'vs/workbench/
import { IWebviewElement } from 'vs/workbench/contrib/webview/browser/webview';
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';

//#region Shared commands
export const EXPAND_CELL_INPUT_COMMAND_ID = 'notebook.cell.expandCellInput';
Expand Down Expand Up @@ -463,7 +464,8 @@ export interface INotebookEditor {
readonly activeKernel: INotebookKernel | undefined;
readonly scrollTop: number;
readonly scopedContextKeyService: IContextKeyService;
readonly activeCodeEditor: editorCommon.IEditor | undefined;
readonly activeCodeEditor: ICodeEditor | undefined;
readonly codeEditors: [ICellViewModel, ICodeEditor][];
//#endregion

getLength(): number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { BareFontInfo, FontInfo } from 'vs/editor/common/config/fontInfo';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
import { IEditor } from 'vs/editor/common/editorCommon';
import { SuggestController } from 'vs/editor/contrib/suggest/browser/suggestController';
import * as nls from 'vs/nls';
import { MenuId } from 'vs/platform/actions/common/actions';
Expand Down Expand Up @@ -234,7 +233,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
return this._notebookViewModel?.options.isReadOnly ?? false;
}

get activeCodeEditor(): IEditor | undefined {
get activeCodeEditor(): ICodeEditor | undefined {
if (this._isDisposed) {
return;
}
Expand All @@ -243,6 +242,10 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
return this._renderedEditors.get(focused);
}

get codeEditors(): [ICellViewModel, ICodeEditor][] {
return [...this._renderedEditors];
}

get visibleRanges() {
return this._list.visibleRanges || [];
}
Expand Down