Rework reformatting in PyProjectTomlMut to respect original indentation (#5075)

## Summary

So this PR introduces change to how `Array` of dependencies
representation is reformatted while `PyProjectTomlMut` is manipulated.
These changes are here for it to respect the original indentation.
Closes https://github.com/astral-sh/uv/issues/5009

## Test Plan
Using `pyproject.toml` like
```
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
  "requests"
]
```
Executed 
```
$ uv add httpx
```
And expected in `pyproject.toml`
```
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
  "requests",
  "httpx",
]
```
Preserving original indentation
This commit is contained in:
Alexander Gherm 2024-07-16 01:13:22 +02:00 committed by GitHub
parent d1010228b3
commit 14a1ea460d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 138 additions and 2 deletions

View file

@ -424,14 +424,36 @@ fn reformat_array_multiline(deps: &mut Array) {
})
}
let mut indentation_prefix = None;
for item in deps.iter_mut() {
let decor = item.decor_mut();
let mut prefix = String::new();
// calculating the indentation prefix as the indentation of the first dependency entry
if indentation_prefix.is_none() {
let decor_prefix = decor
.prefix()
.and_then(|s| s.as_str())
.map(|s| s.split('#').next().unwrap_or("").to_string())
.unwrap_or(String::new())
.trim_start_matches('\n')
.to_string();
// if there is no indentation then apply a default one
indentation_prefix = Some(if decor_prefix.is_empty() {
" ".to_string()
} else {
decor_prefix
});
}
let indentation_prefix_str = format!("\n{}", indentation_prefix.as_ref().unwrap());
for comment in find_comments(decor.prefix()).chain(find_comments(decor.suffix())) {
prefix.push_str("\n ");
prefix.push_str(&indentation_prefix_str);
prefix.push_str(comment);
}
prefix.push_str("\n ");
prefix.push_str(&indentation_prefix_str);
decor.set_prefix(prefix);
decor.set_suffix("");
}