mirror of
https://github.com/zed-industries/zed.git
synced 2026-07-10 00:13:29 +00:00
## Summary Introduces `format_on_save` variants that format only modified lines, limiting formatting to lines changed since the last commit. This prevents massive diffs when editing legacy codebases. Aligns with VS Code's `editor.formatOnSaveMode` naming: - `"modifications"` — formats only git-diffed lines. Requires source control; skips formatting if no diff is available. - `"modifications_if_available"` — formats only git-diffed lines, falling back to full-file formatting for untracked files, when source control is unavailable, or when the LSP does not support range formatting. Also supports importing equivalent VS Code settings (`formatOnSaveMode`). This PR uses the range-based whitespace/newline infrastructure from the dependency PR above. ## Changes <details> <summary><b>New settings, modified ranges computation, VS Code import</b></summary> ### New settings (`language.rs`, `default.json`, `all-settings.md`) Two new `FormatOnSave` variants: `Modifications` and `ModificationsIfAvailable`. ### Modified ranges computation (`items.rs`) - `compute_format_decision()` — reads `format_on_save` setting across all buffers, determines whether to use ranged formatting (`Ranges`), full formatting (`Full`), or skip (`Skip`). - `compute_modified_ranges()` — extracts modified line ranges from git diff hunks via `BufferDiffSnapshot`. - `is_empty_range()` — helper to detect deletion-only hunks that produce no formatable content. The `save()` method calls `compute_format_decision()` and dispatches accordingly. ### Modifications mode in `lsp_store.rs` - Adds `FormatOnSave::Modifications | FormatOnSave::ModificationsIfAvailable` to the formatter selection match arm. - `ModificationsIfAvailable` falls back to full-file formatting when ranged formatting produces no results. ### VS Code settings import (`vscode_import.rs`, `settings_store.rs`) Imports `editor.formatOnSaveMode` mapping: - `"modifications"` → `FormatOnSave::Modifications` - `"modificationsIfAvailable"` → `FormatOnSave::ModificationsIfAvailable` - `"file"` → `FormatOnSave::On` </details> ## Tests - `test_modifications_format_on_save` — basic modifications mode formatting with dirty buffer - `test_modifications_format_no_changes` — clean buffer triggers no formatting - `test_modifications_format_lsp_no_range_support` — LSP without range formatting skips entirely for `Modifications` - `test_modifications_format_lsp_returns_empty_edits` — empty edits handled gracefully - `test_modifications_format_multiple_hunks` — two non-adjacent edits produce two separate range formatting requests --- Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Closes #16509 Depends on #53942 Release Notes: - Added `modifications` and `modifications_if_available` options to `format_on_save`, which format only git-changed lines instead of the entire file - When using modifications mode, `remove_trailing_whitespace_on_save` and `ensure_final_newline_on_save` are also scoped to changed lines, preventing unwanted diff jitter in legacy codebases - Added support for importing VS Code's `editor.formatOnSaveMode` setting --------- Co-authored-by: Kirill Bulatov <kirill@zed.dev> |
||
|---|---|---|
| .. | ||
| account | ||
| ai | ||
| business | ||
| collaboration | ||
| development | ||
| extensions | ||
| languages | ||
| migrate | ||
| reference | ||
| all-actions.md | ||
| appearance.md | ||
| authentication.md | ||
| command-palette.md | ||
| completions.md | ||
| configuring-languages.md | ||
| configuring-zed.md | ||
| debugger.md | ||
| dev-containers.md | ||
| development.md | ||
| diagnostics.md | ||
| editing-code.md | ||
| environment.md | ||
| extensions.md | ||
| finding-navigating.md | ||
| getting-started.md | ||
| git.md | ||
| globs.md | ||
| helix.md | ||
| icon-themes.md | ||
| installation.md | ||
| key-bindings.md | ||
| languages.md | ||
| linux.md | ||
| macos.md | ||
| modelines.md | ||
| multibuffers.md | ||
| outline-panel.md | ||
| performance.md | ||
| project-panel.md | ||
| quick-start.md | ||
| remote-development.md | ||
| repl.md | ||
| roles.md | ||
| running-testing.md | ||
| semantic-tokens.md | ||
| snippets.md | ||
| soc2.md | ||
| SUMMARY.md | ||
| tab-switcher.md | ||
| tasks.md | ||
| telemetry.md | ||
| terminal.md | ||
| themes.md | ||
| toolchains.md | ||
| troubleshooting.md | ||
| uninstall.md | ||
| update.md | ||
| vim.md | ||
| visual-customization.md | ||
| windows-and-projects.md | ||
| windows.md | ||
| worktree-trust.md | ||