Skip to content

Comments

Preserve end-of-line comment whitespace when editing pyproject.toml#16734

Merged
konstin merged 4 commits intoastral-sh:mainfrom
terror:comment-formatting
Nov 20, 2025
Merged

Preserve end-of-line comment whitespace when editing pyproject.toml#16734
konstin merged 4 commits intoastral-sh:mainfrom
terror:comment-formatting

Conversation

@terror
Copy link
Contributor

@terror terror commented Nov 14, 2025

Resolves #16719

uv add collapses multiple spaces before inline comments in [project.dependencies], causing unrelated diffs and moving comments onto the wrong columns. This diff captures the exact whitespace padding that preceded each end-of-line comment when parsing the array and reuses it when formatting.

@terror terror force-pushed the comment-formatting branch from 1093a2d to f28a03b Compare November 14, 2025 09:06
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 14, 2025

CodSpeed Performance Report

Merging #16734 will not alter performance

Comparing terror:comment-formatting (65818fe) with main (7d8634b)

Summary

✅ 6 untouched

@terror
Copy link
Contributor Author

terror commented Nov 15, 2025

CodSpeed Performance Report

Merging #16734 will not alter performance

Comparing terror:comment-formatting (e868edb) with main (f5ce5b4)

Summary

✅ 6 untouched

This is cool 👀

if let Some(padding) = comment.padding.as_deref() {
prefix.push_str(padding);
} else {
prefix.push(' ');
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean we're inserting a whitespace when there was non before?

If we want to store padding specifically for end-of-line comments, we can store the data in the CommentType::EndOfLine variant and avoid the Option<T>.

Copy link
Contributor Author

@terror terror Nov 19, 2025

Choose a reason for hiding this comment

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

Ah good catch, I've refactored this to carry padding on the variant instead (and thus, no longer default if there's no padding).

@terror terror force-pushed the comment-formatting branch from 571cf3a to 78332f8 Compare November 19, 2025 19:25

let serialized = doc.to_string();

assert!(
Copy link
Member

Choose a reason for hiding this comment

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

For these it doesn't matter, but anything more complex I recommend snapshots, they are easier to maintain if we ever want to change the style

let comment_type = if (*prev_line_was_empty) || (*prev_line_was_comment) {

if let Some(index) = line.find('#') {
let comment_text = line[index..].trim().to_string();
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for coming up with this only now, but can we make those safer by using e.g. split_once? Indexing can panic, and it's easy to get logic such as this wrong, so we try to use non-indexing versions where they aren't too inconvenient or too slow.

Copy link
Member

@konstin konstin left a comment

Choose a reason for hiding this comment

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

Thanks!

@konstin konstin added the bug Something isn't working label Nov 20, 2025
@konstin konstin changed the title Preserve inline comment spacing when editing dependency arrays Preserve end-of-line comment spacing when editing dependency arrays Nov 20, 2025
@konstin konstin changed the title Preserve end-of-line comment spacing when editing dependency arrays Preserve end-of-line comment whitespace when editing pyproject.toml Nov 20, 2025
@konstin konstin merged commit 79bfa2b into astral-sh:main Nov 20, 2025
99 of 100 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Nov 21, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [astral-sh/uv](https://github.com/astral-sh/uv) | patch | `0.9.10` -> `0.9.11` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>astral-sh/uv (astral-sh/uv)</summary>

### [`v0.9.11`](https://github.com/astral-sh/uv/blob/HEAD/CHANGELOG.md#0911)

[Compare Source](astral-sh/uv@0.9.10...0.9.11)

Released on 2025-11-20.

##### Python

- Add CPython 3.15.0a2

See the [`python-build-standalone` release notes](https://github.com/astral-sh/python-build-standalone/releases/tag/20251120) for details.

##### Enhancements

- Add SBOM support to `uv export` ([#&#8203;16523](astral-sh/uv#16523))
- Publish to `crates.io` ([#&#8203;16770](astral-sh/uv#16770))

##### Preview features

- Add `uv workspace list --paths` ([#&#8203;16776](astral-sh/uv#16776))
- Fix the preview warning on `uv workspace dir` ([#&#8203;16775](astral-sh/uv#16775))

##### Bug fixes

- Fix `uv init` author serialization via `toml_edit` inline tables ([#&#8203;16778](astral-sh/uv#16778))
- Fix status messages without TTY ([#&#8203;16785](astral-sh/uv#16785))
- Preserve end-of-line comment whitespace when editing `pyproject.toml` ([#&#8203;16734](astral-sh/uv#16734))
- Disable `always-authenticate` when running under Dependabot ([#&#8203;16773](astral-sh/uv#16773))

##### Documentation

- Document the new behavior for free-threaded python versions ([#&#8203;16781](astral-sh/uv#16781))
- Improve note about build system in publish guide ([#&#8203;16788](astral-sh/uv#16788))
- Move do not upload publish note out of the guide into concepts ([#&#8203;16789](astral-sh/uv#16789))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNzMuMSIsInVwZGF0ZWRJblZlciI6IjQxLjE3My4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uv add also performs unprompted (and unrelated) toml formatting

2 participants