Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(remove): Preserve feature comments #12837

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/cargo/util/toml_mut/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,5 +540,42 @@ fn non_existent_dependency_err(
}

fn remove_array_index(array: &mut toml_edit::Array, index: usize) {
array.remove(index);
let value = array.remove(index);

// Captures all lines before leading whitespace
let prefix_lines = value
.decor()
.prefix()
.and_then(|p| p.as_str().expect("spans removed").rsplit_once('\n'))
.map(|(lines, _current)| lines);
// Captures all lines after trailing whitespace, before the next comma
let suffix_lines = value
.decor()
.suffix()
.and_then(|p| p.as_str().expect("spans removed").split_once('\n'))
.map(|(_current, lines)| lines);
let mut merged_lines = String::new();
if let Some(prefix_lines) = prefix_lines {
merged_lines.push_str(prefix_lines);
merged_lines.push('\n');
}
if let Some(suffix_lines) = suffix_lines {
merged_lines.push_str(suffix_lines);
merged_lines.push('\n');
}

let next_index = index; // Since `index` was removed, that effectively auto-advances us
if let Some(next) = array.get_mut(next_index) {
let next_decor = next.decor_mut();
let next_prefix = next_decor
.prefix()
.map(|s| s.as_str().expect("spans removed"))
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or_default();
merged_lines.push_str(next_prefix);
next_decor.set_prefix(merged_lines);
} else {
let trailing = array.trailing().as_str().expect("spans removed");
merged_lines.push_str(trailing);
array.set_trailing(merged_lines);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ serde = "1.0.90"
[features]
std = [
# Leading clippy
"dep:clippy", # trailing docopt
"dep:clippy", # trailing clippy

# Leading docopt
# trailing docopt

# Leading rustc-serialize
"dep:rustc-serialize", # trailing rustc-serialize
Expand All @@ -30,5 +33,8 @@ std = [
"serde/std", # trailing serde/std

# Leading semver/std
"semver/std", # trailing toml
"semver/std", # trailing semver/std

# Leading toml
# trailing toml
]