Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update headings; jump in main editor; close if no changes
  • Loading branch information
colin-grant-work committed Mar 26, 2025
commit d809c933f07e5de64a0ee994f8807b14f3c9d171
57 changes: 40 additions & 17 deletions packages/scm/src/browser/dirty-diff/dirty-diff-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,25 @@ export class DirtyDiffWidget implements Disposable {
}

protected handleChangedChanges(updated: readonly Change[]): void {
if (!updated.length) {
return this.dispose();
}
if (this.currentChange) {
const {previousRange: {start, end}} = this.currentChange;
this.index = updated.findIndex(candidate => candidate.previousRange.start === start && candidate.previousRange.end === end);
const { previousRange: { start, end } } = this.currentChange;
// Same change or first after it.
const newIndex = updated.findIndex(candidate => (candidate.previousRange.start === start && candidate.previousRange.end === end)
|| candidate.previousRange.start > start);
if (newIndex !== -1) {
this.index = newIndex;
} else {
this.index = Math.min(this.index, updated.length - 1);
}
this.showCurrentChange();
} else {
this.index = -1;
}
this._changes = updated;
this.updateHeading();
}

async showChange(index: number): Promise<void> {
Expand All @@ -112,21 +124,24 @@ export class DirtyDiffWidget implements Disposable {
}
}

async showNextChange(): Promise<void> {
const editor = await this.checkCreated();
editor.diffNavigator.next();
this.updateIndex(editor);
}

async showPreviousChange(): Promise<void> {
const editor = await this.checkCreated();
editor.diffNavigator.previous();
this.updateIndex(editor);
showNextChange(): void {
this.checkCreated();
const index = this.index;
const length = this.changes.length;
if (length > 0 && (index < 0 || length > 1)) {
this.index = index < 0 ? 0 : cycle(index, 1, length);
this.showCurrentChange();
}
}

protected updateIndex(editor: MonacoDiffEditor): void {
const line = editor.cursor.line;
this.index = this.changes.findIndex(candidate => candidate.currentRange.start <= line && candidate.currentRange.end >= line);
showPreviousChange(): void {
this.checkCreated();
const index = this.index;
const length = this.changes.length;
if (length > 0 && (index < 0 || length > 1)) {
this.index = index < 0 ? length - 1 : cycle(index, -1, length);
this.showCurrentChange();
}
}

async getContentWithSelectedChanges(predicate: (change: Change, index: number, changes: readonly Change[]) => boolean): Promise<string> {
Expand All @@ -143,7 +158,7 @@ export class DirtyDiffWidget implements Disposable {
}

protected showCurrentChange(): void {
this.peekView!.setTitle(this.computePrimaryHeading(), this.computeSecondaryHeading());
this.updateHeading();
const { previousRange, currentRange } = this.changes[this.index];
this.peekView.show(Position.create(LineRange.getEndPosition(currentRange).line, 0),
this.computeHeightInLines());
Expand All @@ -162,6 +177,10 @@ export class DirtyDiffWidget implements Disposable {
this.editor.focus();
}

protected updateHeading(): void {
this.peekView.setTitle(this.computePrimaryHeading(), this.computeSecondaryHeading());
}

protected computePrimaryHeading(): string {
return this.uri.path.base;
}
Expand Down Expand Up @@ -190,6 +209,10 @@ export class DirtyDiffWidget implements Disposable {
}
}

function cycle(index: number, offset: -1 | 1, length: number): number {
return (index + offset + length) % length;
}

// adapted from https://github.com/microsoft/vscode/blob/823d54f86ee13eb357bc6e8e562e89d793f3c43b/extensions/git/src/staging.ts
function applyChanges(changes: readonly Change[], original: monaco.editor.ITextModel, modified: monaco.editor.ITextModel): string {
const result: string[] = [];
Expand Down Expand Up @@ -325,7 +348,7 @@ class DirtyDiffPeekView extends MonacoEditorPeekViewWidget {
// Close editor on successful contributed action.
// https://github.com/microsoft/vscode/blob/11b1500e0a2e8b5ba12e98a3905f9d120b8646a0/src/vs/workbench/contrib/scm/browser/quickDiffWidget.ts#L356-L361
this.addAction(id, label, icon, menuCommandExecutor.isEnabled(menuPath, command, this.widget), () => {
menuCommandExecutor.executeCommand(menuPath, command, this.widget).then(() => this.dispose());
menuCommandExecutor.executeCommand(menuPath, command, this.widget).then(() => this.dispose());
});
}
}
Expand Down
Loading