Skip to content

feat(core): enhance EditTool with intelligent indentation rebasing#19178

Open
Thomas-Shephard wants to merge 5 commits intogoogle-gemini:mainfrom
Thomas-Shephard:feat/edit-tool-indentation
Open

feat(core): enhance EditTool with intelligent indentation rebasing#19178
Thomas-Shephard wants to merge 5 commits intogoogle-gemini:mainfrom
Thomas-Shephard:feat/edit-tool-indentation

Conversation

@Thomas-Shephard
Copy link
Contributor

Summary

This PR enhances the EditTool by implementing intelligent indentation rebasing and trailing whitespace handling. These improvements ensure that code replacements integrate seamlessly with the existing file's style, even when the AI-proposed code has different indentation levels.

Details

  • Added rebaseIndentation to adjust proposed code blocks to match target indentation.
  • Added handleTrailingSpaces to prevent double-trailing spaces when the source file already contains them.
  • Integrated these utilities into calculateExactReplacement, calculateFlexibleReplacement, and calculateRegexReplacement.
  • Added logic to preserve inline suffixes (like comments) when shifting or replacing code blocks.
  • Comprehensive test coverage added in edit.test.ts.

Related Issues

Closes #19177

How to Validate

Run the tests:
bash
npm test -w @google/gemini-cli-core -- src/tools/edit.test.ts

All 52 tests should pass.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • Windows
      • npm run

- Implement rebaseIndentation and handleTrailingSpaces utilities.
- Integrate indentation rebasing into exact, flexible, and regex matching.
- Preserve inline suffixes (comments, etc.) during multi-line replacements.
- Add comprehensive tests for whitespace-only and multi-line rebasing.

Relates to google-gemini#19177
@Thomas-Shephard Thomas-Shephard requested a review from a team as a code owner February 15, 2026 23:31
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Thomas-Shephard, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the EditTool's ability to apply code modifications gracefully. It introduces intelligent indentation rebasing and trailing whitespace management, ensuring that AI-generated code snippets or replacements seamlessly conform to the existing file's formatting and style, thereby improving the overall quality and consistency of automated code changes.

Highlights

  • Intelligent Indentation Rebasing: Implemented a new utility function, rebaseIndentation, to dynamically adjust the indentation of replacement code blocks to match the target file's existing style.
  • Trailing Whitespace Handling: Added handleTrailingSpaces to prevent the duplication of trailing spaces when a replacement is applied, ensuring cleaner code integration.
  • Enhanced Replacement Calculations: Integrated the new indentation and whitespace handling logic into calculateExactReplacement, calculateFlexibleReplacement, and calculateRegexReplacement functions.
  • Suffix Preservation: Improved logic to preserve inline suffixes, such as comments, when code blocks are shifted or replaced, maintaining context and readability.
  • Comprehensive Test Coverage: Significantly expanded the test suite with new cases to validate the intelligent indentation rebasing, trailing space handling, and multi-line replacement functionalities.
Changelog
  • packages/core/src/tools/edit.test.ts
    • Added numerous test cases to validate intelligent indentation rebasing, trailing whitespace handling, multi-line replacements, and suffix preservation across exact, flexible, and regex matching scenarios.
  • packages/core/src/tools/edit.ts
    • Introduced rebaseIndentation function to dynamically adjust the indentation of text blocks based on a target.
    • Implemented handleTrailingSpaces to prevent redundant trailing spaces in replacements.
    • Refactored calculateExactReplacement to apply intelligent indentation rebasing and preserve line suffixes.
    • Updated calculateFlexibleReplacement to use rebaseIndentation for new code blocks.
    • Modified calculateRegexReplacement to correctly capture and utilize trailing whitespace and apply the new indentation and trailing space logic.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces intelligent indentation rebasing and trailing whitespace handling to the EditTool, which is a great enhancement for ensuring AI-generated code edits match the existing file's style. The changes are extensive, touching all three replacement strategies (exact, flexible, and regex) and are backed by a comprehensive new set of tests. However, I found a critical issue in the new implementation of calculateExactReplacement that could lead to incorrect results when performing multiple replacements. My review includes a detailed explanation and a suggested fix for this issue.

@Thomas-Shephard Thomas-Shephard force-pushed the feat/edit-tool-indentation branch from 6f8cea7 to 90bf646 Compare February 15, 2026 23:43
…d non-overlapping matching

- Implement rebaseIndentation and handleTrailingSpaces utilities.
- Refactor calculateExactReplacement for non-overlapping 'replaceAll' behavior using immutable original content.
- Integrate indentation rebasing into all matching strategies (exact, flexible, regex).
- Preserve inline suffixes and respect multi-line indentation shifts.
- Add comprehensive test suite covering rebasing edge cases and overlapping matches.

Closes google-gemini#19177
@gemini-cli gemini-cli bot added the area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality label Feb 15, 2026
@Thomas-Shephard
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces intelligent indentation rebasing and trailing whitespace handling to the EditTool. While these are valuable enhancements, a critical issue has been identified in the calculateExactReplacement function where the new indentation rebasing logic fails for multiple occurrences of a multi-line search string, potentially leading to inconsistent formatting.

- Fix indentation rebasing consistency for multiple multi-line occurrences.
- Ensure line prefix safety using lastOffset validation.
- Add regression test for consistent multi-line rebasing.
- Clean up code formatting and unused expressions.
@Thomas-Shephard
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly enhances the EditTool by introducing intelligent indentation rebasing and improving whitespace handling through new rebaseIndentation and handleTrailingSpaces utilities, making the tool more robust in preserving file styling. While the extensive test suite is excellent, a potential issue was identified in the suffix handling logic within calculateExactReplacement that could lead to incorrect indentation. Overall, this is a great improvement to the tool's capabilities.

@Thomas-Shephard
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly enhances the EditTool by introducing intelligent indentation rebasing and sophisticated handling of trailing whitespace and inline suffixes. The new rebaseIndentation and handleTrailingSpaces utilities are well-designed and have been thoughtfully integrated into all three replacement strategies (exact, flexible, and regex), making code replacements much more robust and style-aware. The logic for preserving suffixes is a great addition that will prevent data loss in common editing scenarios. The accompanying tests are comprehensive and cover a wide range of edge cases, which gives high confidence in the correctness of these complex changes. Overall, this is an excellent improvement to the tool's capabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance EditTool with robust indentation rebasing

1 participant