From 3c537f51c13ee9064a3e17f3aec62dc5283ae3e2 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 26 Nov 2024 14:20:35 -0800 Subject: [PATCH] feat: split diffs --- core/index.d.ts | 2 +- extensions/vscode/src/util/ideUtils.ts | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/core/index.d.ts b/core/index.d.ts index 5e1e31def..1f072112e 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -503,7 +503,7 @@ export interface IdeSettings { export interface IDE { getIdeInfo(): Promise; getIdeSettings(): Promise; - getDiff(includeUnstaged: boolean): Promise; + getDiff(includeUnstaged: boolean): Promise; getClipboardContent(): Promise<{ text: string; copiedAt: string }>; isTelemetryEnabled(): Promise; getUniqueId(): Promise; diff --git a/extensions/vscode/src/util/ideUtils.ts b/extensions/vscode/src/util/ideUtils.ts index 2ce332f02..cfd6898eb 100644 --- a/extensions/vscode/src/util/ideUtils.ts +++ b/extensions/vscode/src/util/ideUtils.ts @@ -570,8 +570,20 @@ export class VsCodeIdeUtils { return repo?.state?.HEAD?.name || "NONE"; } - async getDiff(includeUnstaged: boolean): Promise { - let diffs: string[] = []; + private splitDiff(diffString: string): string[] { + const fileDiffHeaderRegex = /(?=diff --git a\/.* b\/.*)/; + + const diffs = diffString.split(fileDiffHeaderRegex); + + if (diffs[0].trim() === "") { + diffs.shift(); + } + + return diffs; + } + + async getDiff(includeUnstaged: boolean): Promise { + const diffs: string[] = []; for (const dir of this.getWorkspaceDirectories()) { const repo = await this.getRepo(vscode.Uri.file(dir)); @@ -587,9 +599,7 @@ export class VsCodeIdeUtils { } } - const fullDiff = diffs.join("\n\n"); - - return fullDiff; + return diffs.flatMap((diff) => this.splitDiff(diff)); } getHighlightedCode(): RangeInFile[] {