From 70d469ccd33c6c3e5f3719f58d5690776098cca4 Mon Sep 17 00:00:00 2001 From: Taylor Mullen Date: Sun, 25 May 2025 22:38:44 -0700 Subject: [PATCH] Fix(diff): Hide whitespace changes in diffs with content changes - Updated the diff generation in `edit.ts` and `write-file.ts` to include the `ignoreWhitespace: true` option. - This ensures that whitespace-only changes are not highlighted in the diff output when there are other content modifications, making the diffs cleaner and easier to review. - Extract default diffing options into single source of truth. Fixes https://github.com/google-gemini/gemini-cli/issues/548 --- packages/server/src/tools/diffOptions.ts | 12 ++++++++++++ packages/server/src/tools/edit.ts | 5 +++-- packages/server/src/tools/write-file.ts | 5 +++-- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 packages/server/src/tools/diffOptions.ts diff --git a/packages/server/src/tools/diffOptions.ts b/packages/server/src/tools/diffOptions.ts new file mode 100644 index 00000000..598b46f1 --- /dev/null +++ b/packages/server/src/tools/diffOptions.ts @@ -0,0 +1,12 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import * as Diff from 'diff'; + +export const DEFAULT_DIFF_OPTIONS: Diff.PatchOptions = { + context: 3, + ignoreWhitespace: true, +}; diff --git a/packages/server/src/tools/edit.ts b/packages/server/src/tools/edit.ts index bd070d65..781483ae 100644 --- a/packages/server/src/tools/edit.ts +++ b/packages/server/src/tools/edit.ts @@ -22,6 +22,7 @@ import { ReadFileTool } from './read-file.js'; import { GeminiClient } from '../core/client.js'; import { Config } from '../config/config.js'; import { ensureCorrectEdit } from '../utils/editCorrector.js'; +import { DEFAULT_DIFF_OPTIONS } from './diffOptions.js'; /** * Parameters for the Edit tool @@ -326,7 +327,7 @@ Expectation for parameters: newContent, 'Current', 'Proposed', - { context: 3 }, + DEFAULT_DIFF_OPTIONS, ); const confirmationDetails: ToolEditConfirmationDetails = { type: 'edit', @@ -408,7 +409,7 @@ Expectation for parameters: editData.newContent, 'Current', 'Proposed', - { context: 3 }, + DEFAULT_DIFF_OPTIONS, ); displayResult = { fileDiff, fileName }; } diff --git a/packages/server/src/tools/write-file.ts b/packages/server/src/tools/write-file.ts index 6c0b7d72..60646cc2 100644 --- a/packages/server/src/tools/write-file.ts +++ b/packages/server/src/tools/write-file.ts @@ -24,6 +24,7 @@ import { ensureCorrectFileContent, } from '../utils/editCorrector.js'; import { GeminiClient } from '../core/client.js'; +import { DEFAULT_DIFF_OPTIONS } from './diffOptions.js'; /** * Parameters for the WriteFile tool @@ -173,7 +174,7 @@ export class WriteFileTool extends BaseTool { correctedContent, // Content after potential correction 'Current', 'Proposed', - { context: 3 }, + DEFAULT_DIFF_OPTIONS, ); const confirmationDetails: ToolEditConfirmationDetails = { @@ -251,7 +252,7 @@ export class WriteFileTool extends BaseTool { fileContent, 'Original', 'Written', - { context: 3 }, + DEFAULT_DIFF_OPTIONS, ); const llmSuccessMessage = isNewFile