Skip to content

Commit

Permalink
Preserve format as much as possible.
Browse files Browse the repository at this point in the history
- Add a new line between the last table element and newelements when there were no previous elements.
- Preserve the decor prefix of previous elements.

Signed-off-by: David Calavera <david.calavera@gmail.com>
  • Loading branch information
calavera committed Oct 9, 2023
1 parent 78e75c1 commit d5ad2d2
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 2 deletions.
45 changes: 44 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,16 +997,59 @@ fn update_manifest_with_new_member(
return Ok(());
}
}
let decor = if members.is_empty() {
None
} else {
members.get(members.len() - 1).map(|m| m.decor()).cloned()
};

let Some(toml_members) = workspace_document["workspace"]["members"].as_array_mut() else {
anyhow::bail!("[workspace.members] is not an array");
};

toml_members.push(relpath);

if let Some(decor) = decor {
if let Some(last) = toml_members
.get_mut(toml_members.len() - 1)
.map(|m| m.decor_mut())
{
if let Some(prefix) = decor.prefix() {
last.set_prefix(prefix.to_owned());
}
if let Some(suffix) = decor.suffix() {
last.set_suffix(suffix.to_owned());
}
}
}
} else {
let ws = workspace_document["workspace"]
.as_table_mut()
.with_context(|| format!("[workspace] is not a table"))?;

let mut last = None;
for (_, value) in ws.get_values() {
if !value.is_inline_table() {
last = Some(value.clone());
}
}

let assume_new_line = last.is_some();
let mut prefix = None;
if let Some(value) = last {
prefix = value.decor().prefix().cloned();
}

let mut array = Array::new();
array.push(relpath);
workspace_document["workspace"]["members"] = toml_edit::value(array);
if !assume_new_line {
array.decor_mut().set_suffix("\n");
}
if let Some(prefix) = prefix {
array.decor_mut().set_prefix(prefix);
}

ws.insert("members", toml_edit::value(array));
}

paths::write(&root_manifest_path, workspace_document.to_string())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
resolver = "2"

[workspace.package]
authors = ["Rustaceans"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::curr_dir;
use cargo_test_support::CargoCommand;
use cargo_test_support::Project;

#[cargo_test]
fn case() {
let project = Project::from_template(curr_dir!().join("in"));
let project_root = project.root();
let cwd = &project_root;

snapbox::cmd::Command::cargo_ui()
.arg("new")
.args(["crates/foo"])
.current_dir(cwd)
.assert()
.success()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]
resolver = "2"
members = ["crates/foo"]

[workspace.package]
authors = ["Rustaceans"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "foo"
version = "0.1.0"
edition = "2021"
authors.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created binary (application) `crates/foo` package
Empty file.
3 changes: 2 additions & 1 deletion tests/testsuite/cargo_new/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod add_members_to_workspace_preserving_format;
mod add_members_to_workspace_format_new_line;
mod add_members_to_workspace_format_previous_items;
mod add_members_to_workspace_with_absolute_package_path;
mod add_members_to_workspace_with_empty_members;
mod add_members_to_workspace_with_exclude_list;
Expand Down

0 comments on commit d5ad2d2

Please sign in to comment.