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

Normalize indentation of code blocks after 'help:' #238

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 16 additions & 10 deletions src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ normalizations! {
Unindent,
AndOthers,
StripLongTypeNameFiles,
UnindentAfterHelp,
// New normalization steps are to be inserted here at the end so that any
// snapshots saved before your normalization change remain passing.
}
Expand Down Expand Up @@ -139,9 +140,7 @@ fn apply(original: &str, normalization: Normalization, context: Context) -> Stri
}
}

if normalization >= Unindent {
normalized = unindent(normalized);
}
normalized = unindent(normalized, normalization);

trim(normalized)
}
Expand Down Expand Up @@ -494,15 +493,19 @@ enum IndentedLineKind {
Other(usize),
}

fn unindent(diag: String) -> String {
fn unindent(diag: String, normalization: Normalization) -> String {
if normalization < Unindent {
return diag;
}

let mut normalized = String::new();
let mut lines = diag.lines();

while let Some(line) = lines.next() {
normalized.push_str(line);
normalized.push('\n');

if indented_line_kind(line) != IndentedLineKind::Heading {
if indented_line_kind(line, normalization) != IndentedLineKind::Heading {
continue;
}

Expand All @@ -512,12 +515,12 @@ fn unindent(diag: String) -> String {
None => continue,
};

if let IndentedLineKind::Code(indent) = indented_line_kind(next_line) {
if let IndentedLineKind::Code(indent) = indented_line_kind(next_line, normalization) {
if next_line[indent + 1..].starts_with("--> ") {
let mut lines_in_block = 1;
let mut least_indent = indent;
while let Some(line) = ahead.next() {
match indented_line_kind(line) {
match indented_line_kind(line, normalization) {
IndentedLineKind::Heading => break,
IndentedLineKind::Code(indent) => {
lines_in_block += 1;
Expand All @@ -536,7 +539,7 @@ fn unindent(diag: String) -> String {
for _ in 0..lines_in_block {
let line = lines.next().unwrap();
if let IndentedLineKind::Code(_) | IndentedLineKind::Other(_) =
indented_line_kind(line)
indented_line_kind(line, normalization)
{
let space = line.find(' ').unwrap();
normalized.push_str(&line[..space]);
Expand All @@ -553,7 +556,7 @@ fn unindent(diag: String) -> String {
normalized
}

fn indented_line_kind(line: &str) -> IndentedLineKind {
fn indented_line_kind(line: &str, normalization: Normalization) -> IndentedLineKind {
if let Some(heading_len) = if line.starts_with("error") {
Some("error".len())
} else if line.starts_with("warning") {
Expand All @@ -566,7 +569,10 @@ fn indented_line_kind(line: &str) -> IndentedLineKind {
}
}

if line.starts_with("note:") || line == "..." {
if line.starts_with("note:")
|| line == "..."
|| normalization >= UnindentAfterHelp && line.starts_with("help:")
{
return IndentedLineKind::Note;
}

Expand Down
43 changes: 43 additions & 0 deletions src/tests/erased-serde-trait-bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
test_normalize! {"
error[E0277]: the trait bound `__T: serde::ser::Serialize` is not satisfied
--> src/main.rs:5:1
|
5 | serialize_trait_object!(MyTrait);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `__T`
|
= note: required for `__T` to implement `erased_serde::Serialize`
note: required by a bound in `require_erased_serialize_impl`
--> /home/david/.cargo/registry/src/index.crates.io-6f17d22bba15001f/erased-serde-0.3.28/src/private.rs:14:17
|
12 | pub fn require_erased_serialize_impl<T>()
| ----------------------------- required by a bound in this function
13 | where
14 | T: ?Sized + crate::Serialize,
| ^^^^^^^^^^^^^^^^ required by this bound in `require_erased_serialize_impl`
= note: this error originates in the macro `$crate::__internal_serialize_trait_object` which comes from the expansion of the macro `serialize_trait_object` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
|
5 | serialize_trait_object!(MyTrait + serde::ser::Serialize);
| +++++++++++++++++++++++
" "
error[E0277]: the trait bound `__T: serde::ser::Serialize` is not satisfied
--> src/main.rs:5:1
|
5 | serialize_trait_object!(MyTrait);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `__T`
|
= note: required for `__T` to implement `erased_serde::Serialize`
note: required by a bound in `require_erased_serialize_impl`
--> $CARGO/erased-serde-0.3.28/src/private.rs
|
| pub fn require_erased_serialize_impl<T>()
| ----------------------------- required by a bound in this function
| where
| T: ?Sized + crate::Serialize,
| ^^^^^^^^^^^^^^^^ required by this bound in `require_erased_serialize_impl`
= note: this error originates in the macro `$crate::__internal_serialize_trait_object` which comes from the expansion of the macro `serialize_trait_object` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
|
5 | serialize_trait_object!(MyTrait + serde::ser::Serialize);
| +++++++++++++++++++++++
"}