Skip to content

set markdown to edit mode when navigating to its changes #245911

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

Merged
merged 3 commits into from
Apr 10, 2025
Merged
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 @@ -20,7 +20,7 @@ import { NotebookDeletedCellDecorator } from '../../../../notebook/browser/diff/
import { NotebookInsertedCellDecorator } from '../../../../notebook/browser/diff/inlineDiff/notebookInsertedCellDecorator.js';
import { NotebookModifiedCellDecorator } from '../../../../notebook/browser/diff/inlineDiff/notebookModifiedCellDecorator.js';
import { INotebookTextDiffEditor } from '../../../../notebook/browser/diff/notebookDiffEditorBrowser.js';
import { getNotebookEditorFromEditorPane, ICellViewModel, INotebookEditor } from '../../../../notebook/browser/notebookBrowser.js';
import { CellEditState, getNotebookEditorFromEditorPane, ICellViewModel, INotebookEditor } from '../../../../notebook/browser/notebookBrowser.js';
import { INotebookEditorService } from '../../../../notebook/browser/services/notebookEditorService.js';
import { NotebookCellTextModel } from '../../../../notebook/common/model/notebookCellTextModel.js';
import { NotebookTextModel } from '../../../../notebook/common/model/notebookTextModel.js';
Expand Down Expand Up @@ -108,6 +108,8 @@ class ChatEditingNotebookEditorWidgetIntegration extends Disposable implements I

private markupCellListeners = new Map<number, IDisposable>();

private editingPreview: ICellDiffInfo | undefined = undefined;

constructor(
private readonly _entry: ChatEditingModifiedNotebookEntry,
private readonly notebookEditor: INotebookEditor,
Expand Down Expand Up @@ -364,17 +366,12 @@ class ChatEditingNotebookEditorWidgetIntegration extends Disposable implements I
case 'insert':
case 'modified':
{
this.blur(this._currentChange.get()?.change);
const index = firstOrLast || change.type === 'insert' ? 0 : change.diff.get().changes.length - 1;
const cellIntegration = this.getCell(change.modifiedCellIndex);
if (cellIntegration) {
cellIntegration.reveal(firstOrLast);
this._currentChange.set({ change: change, index }, undefined);
return true;
} else {
return this._revealChange(change, index);
}
return this._revealChange(change, index);
}
case 'delete':
this.blur(this._currentChange.get()?.change);
// reveal the deleted cell decorator
this.deletedCellDecorator?.reveal(change.originalCellIndex);
this._currentChange.set({ change: change, index: 0 }, undefined);
Expand All @@ -394,7 +391,7 @@ class ChatEditingNotebookEditorWidgetIntegration extends Disposable implements I
const textChange = change.diff.get().changes[indexInCell];
const cellViewModel = this.getCellViewModel(change);
if (cellViewModel) {
this.revealChangeInView(cellViewModel, textChange?.modified);
this.revealChangeInView(cellViewModel, textChange?.modified, change);
this._currentChange.set({ change: change, index: indexInCell }, undefined);
}

Expand All @@ -421,12 +418,29 @@ class ChatEditingNotebookEditorWidgetIntegration extends Disposable implements I
return cellViewModel;
}

private async revealChangeInView(cell: ICellViewModel, lines: LineRange | undefined): Promise<void> {
private async revealChangeInView(cell: ICellViewModel, lines: LineRange | undefined, change: ICellDiffInfo): Promise<void> {
const targetLines = lines ?? new LineRange(0, 0);
await this.notebookEditor.focusNotebookCell(cell, 'container', { focusEditorLine: targetLines.startLineNumber });
if (cell.cellKind === CellKind.Markup && cell.getEditState() === CellEditState.Preview) {
this.editingPreview = change;
cell.updateEditState(CellEditState.Editing, 'chatEditNavigation');
} else {
this.editingPreview = undefined;
}

await this.notebookEditor.focusNotebookCell(cell, 'editor', { focusEditorLine: targetLines.startLineNumber });
await this.notebookEditor.revealRangeInCenterAsync(cell, new Range(targetLines.startLineNumber, 0, targetLines.endLineNumberExclusive, 0));
}

blur(change: ICellDiffInfo | undefined) {
if (!change) {
return;
}
const cellViewModel = this.getCellViewModel(change);
if (cellViewModel?.cellKind === CellKind.Markup && cellViewModel.getEditState() === CellEditState.Editing && this.editingPreview === change) {
cellViewModel.updateEditState(CellEditState.Preview, 'chatEditNavigation');
}
}

next(wrap: boolean): boolean {
const changes = sortCellChanges(this.cellChanges.get().filter(c => c.type !== 'unchanged'));
const currentChange = this.currentChange.get();
Expand Down Expand Up @@ -458,13 +472,18 @@ class ChatEditingNotebookEditorWidgetIntegration extends Disposable implements I
const change = isLastChangeInCell ? changes[changes.indexOf(currentChange.change) + 1] : currentChange.change;

if (change) {
if (isLastChangeInCell) {
this.blur(currentChange.change);
}

return this._revealChange(change, index);
}
}
break;
case 'insert':
case 'delete':
{
this.blur(currentChange.change);
// go to next change directly
const nextChange = changes[changes.indexOf(currentChange.change) + 1];
if (nextChange) {
Expand Down Expand Up @@ -513,13 +532,17 @@ class ChatEditingNotebookEditorWidgetIntegration extends Disposable implements I

if (change) {
const index = isFirstChangeInCell ? lastChangeIndex(change) : currentChange.index - 1;
if (isFirstChangeInCell) {
this.blur(currentChange.change);
}
return this._revealChange(change, index);
}
}
break;
case 'insert':
case 'delete':
{
this.blur(currentChange.change);
// go to previous change directly
const prevChange = changes[changes.indexOf(currentChange.change) - 1];
if (prevChange) {
Expand Down
Loading