Skip to content

Commit

Permalink
Fix handling of add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 23, 2024
1 parent 72185ba commit a373b79
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 5 deletions.
27 changes: 22 additions & 5 deletions crates/uv-workspace/src/pyproject_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ impl PyProjectTomlMut {

// Set the position to the minimum, if it's not already the first element.
if let Some(min) = existing.iter().filter_map(toml_edit::Table::position).min() {
// if !table.position().is_some_and(|position| position < min) {
table.set_position(min);

// Increment the position of all existing elements.
Expand All @@ -375,7 +374,6 @@ impl PyProjectTomlMut {
table.set_position(position + 1);
}
}
// }
}

// Push the item to the table.
Expand Down Expand Up @@ -805,11 +803,16 @@ pub fn add_dependency(
let index = index.unwrap_or(deps.len());

let mut value = Value::from(req_string.as_str());

let decor = value.decor_mut();
decor.set_prefix(deps.trailing().clone());
deps.set_trailing("");

if index == deps.len() {
decor.set_prefix(deps.trailing().clone());
deps.set_trailing("");
}

deps.insert_formatted(index, value);

// `reformat_array_multiline` uses the indentation of the first dependency entry.
// Therefore, we retrieve the indentation of the first dependency entry and apply it to
// the new entry. Note that it is only necessary if the newly added dependency is going
Expand Down Expand Up @@ -992,6 +995,11 @@ fn reformat_array_multiline(deps: &mut Array) {
.and_then(|s| s.lines().last())
.unwrap_or_default();

let decor_prefix = decor_prefix
.split_once('#')
.map(|(s, _)| s)
.unwrap_or(decor_prefix);

// If there is no indentation, use four-space.
indentation_prefix = Some(if decor_prefix.is_empty() {
" ".to_string()
Expand Down Expand Up @@ -1023,7 +1031,16 @@ fn reformat_array_multiline(deps: &mut Array) {
let mut rv = String::new();
if comments.peek().is_some() {
for comment in comments {
rv.push_str("\n ");
match comment.comment_type {
CommentType::OwnLine => {
let indentation_prefix_str =
format!("\n{}", indentation_prefix.as_ref().unwrap());
rv.push_str(&indentation_prefix_str);
}
CommentType::EndOfLine => {
rv.push(' ');
}
}
rv.push_str(&comment.text);
}
}
Expand Down
237 changes: 237 additions & 0 deletions crates/uv/tests/it/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6591,3 +6591,240 @@ fn add_preserves_open_bracket_comment() -> Result<()> {
});
Ok(())
}

#[test]
fn add_preserves_empty_comment() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae
# magna euismod, commodo eros at, aliquam lacus. Nulla facilisi.

Check warning on line 6607 in crates/uv/tests/it/edit.rs

View workflow job for this annotation

GitHub Actions / typos

"facilisi" should be "facilities".
]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

uv_snapshot!(context.filters(), context.add().arg("anyio==3.7.0"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 4 packages in [TIME]
Prepared 4 packages in [TIME]
Installed 4 packages in [TIME]
+ anyio==3.7.0
+ idna==3.6
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae
# magna euismod, commodo eros at, aliquam lacus. Nulla facilisi.

Check warning on line 6643 in crates/uv/tests/it/edit.rs

View workflow job for this annotation

GitHub Actions / typos

"facilisi" should be "facilities".
"anyio==3.7.0",
]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

Ok(())
}

#[test]
fn add_preserves_trailing_comment() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"idna",
"iniconfig", # Use iniconfig.
# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae
# magna euismod, commodo eros at, aliquam lacus. Nulla facilisi.

Check warning on line 6671 in crates/uv/tests/it/edit.rs

View workflow job for this annotation

GitHub Actions / typos

"facilisi" should be "facilities".
]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

uv_snapshot!(context.filters(), context.add().arg("anyio==3.7.0"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 5 packages in [TIME]
Prepared 5 packages in [TIME]
Installed 5 packages in [TIME]
+ anyio==3.7.0
+ idna==3.6
+ iniconfig==2.0.0
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"anyio==3.7.0",
"idna",
"iniconfig", # Use iniconfig.
# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae
# magna euismod, commodo eros at, aliquam lacus. Nulla facilisi.

Check warning on line 6711 in crates/uv/tests/it/edit.rs

View workflow job for this annotation

GitHub Actions / typos

"facilisi" should be "facilities".
]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

uv_snapshot!(context.filters(), context.add().arg("typing-extensions"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 6 packages in [TIME]
Prepared 2 packages in [TIME]
Uninstalled 1 package in [TIME]
Installed 2 packages in [TIME]
~ project==0.1.0 (from file://[TEMP_DIR]/)
+ typing-extensions==4.10.0
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"anyio==3.7.0",
"idna",
"iniconfig", # Use iniconfig.
# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae
# magna euismod, commodo eros at, aliquam lacus. Nulla facilisi.

Check warning on line 6751 in crates/uv/tests/it/edit.rs

View workflow job for this annotation

GitHub Actions / typos

"facilisi" should be "facilities".
"typing-extensions>=4.10.0",
]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

Ok(())
}

#[test]
fn add_preserves_trailing_depth() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! {r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"idna",
"iniconfig",# Use iniconfig.
# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae
# magna euismod, commodo eros at, aliquam lacus. Nulla facilisi.

Check warning on line 6779 in crates/uv/tests/it/edit.rs

View workflow job for this annotation

GitHub Actions / typos

"facilisi" should be "facilities".
]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"#})?;

uv_snapshot!(context.filters(), context.add().arg("anyio==3.7.0"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 5 packages in [TIME]
Prepared 5 packages in [TIME]
Installed 5 packages in [TIME]
+ anyio==3.7.0
+ idna==3.6
+ iniconfig==2.0.0
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ sniffio==1.3.1
"###);

let pyproject_toml = context.read("pyproject.toml");

insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject_toml, @r###"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"anyio==3.7.0",
"idna",
"iniconfig", # Use iniconfig.
# Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae
# magna euismod, commodo eros at, aliquam lacus. Nulla facilisi.

Check warning on line 6819 in crates/uv/tests/it/edit.rs

View workflow job for this annotation

GitHub Actions / typos

"facilisi" should be "facilities".
]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
"###
);
});

Ok(())
}

0 comments on commit a373b79

Please sign in to comment.