Skip to content

Commit

Permalink
Use multipart suggestion for code wrapping
Browse files Browse the repository at this point in the history
Another one of those "good grief, I just submitted it and NOW I think of it" moments.
  • Loading branch information
notriddle committed Feb 2, 2022
1 parent 0db9e40 commit 76b5b27
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 52 deletions.
27 changes: 10 additions & 17 deletions src/librustdoc/passes/html_tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
// and we don't try to detect stuff `<like this>` because that's not valid Rust.
if let Some(Some(generics_start)) = (is_open_tag
&& dox[..range.end].ends_with(">"))
.then(|| extract_path_backwards(&dox, range.start))
.then(|| extract_path_backwards(&dox, range.start))
{
let generics_sp = match super::source_span_for_markdown_range(
tcx,
Expand All @@ -231,22 +231,15 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
Some(sp) => sp,
None => item.attr_span(tcx),
};
if let Ok(generics_snippet) =
tcx.sess.source_map().span_to_snippet(generics_sp)
{
// short form is chosen here because ``Vec<i32>`` would be confusing.
diag.span_suggestion_short(
generics_sp,
"try marking as source code with `backticks`",
format!("`{}`", generics_snippet),
Applicability::MaybeIncorrect,
);
} else {
diag.span_help(
generics_sp,
"try marking as source code with `backticks`",
);
}
// multipart form is chosen here because ``Vec<i32>`` would be confusing.
diag.multipart_suggestion(
"try marking as source code",
vec![
(generics_sp.shrink_to_lo(), String::from("`")),
(generics_sp.shrink_to_hi(), String::from("`")),
],
Applicability::MaybeIncorrect,
);
}
diag.emit()
});
Expand Down
6 changes: 0 additions & 6 deletions src/test/rustdoc-ui/suggestions/html-as-generics.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@
/// This `Vec<i32>` thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `Vec<i32>`
pub struct Generic;

/// This `vec::Vec<i32>` thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `vec::Vec<i32>`
pub struct GenericPath;

/// This `i32<i32>` thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `i32<i32>`
pub struct PathsCanContainTrailingNumbers;

/// This `Vec::<i32>` thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `Vec::<i32>`
pub struct Turbofish;

/// This [link](https://rust-lang.org)`::<i32>` thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `::<i32>`
pub struct BareTurbofish;

/// This <span>`Vec::<i32>`</span> thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `Vec::<i32>`
pub struct Nested;
6 changes: 0 additions & 6 deletions src/test/rustdoc-ui/suggestions/html-as-generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,29 @@
/// This Vec<i32> thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `Vec<i32>`
pub struct Generic;

/// This vec::Vec<i32> thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `vec::Vec<i32>`
pub struct GenericPath;

/// This i32<i32> thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `i32<i32>`
pub struct PathsCanContainTrailingNumbers;

/// This Vec::<i32> thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `Vec::<i32>`
pub struct Turbofish;

/// This [link](https://rust-lang.org)::<i32> thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `::<i32>`
pub struct BareTurbofish;

/// This <span>Vec::<i32></span> thing!
//~^ERROR unclosed HTML tag `i32`
//~|HELP try marking as source
//~|SUGGESTION `Vec::<i32>`
pub struct Nested;
63 changes: 40 additions & 23 deletions src/test/rustdoc-ui/suggestions/html-as-generics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,72 @@ error: unclosed HTML tag `i32`
--> $DIR/html-as-generics.rs:4:13
|
LL | /// This Vec<i32> thing!
| ---^^^^^
| |
| help: try marking as source code with `backticks`
| ^^^^^
|
note: the lint level is defined here
--> $DIR/html-as-generics.rs:2:9
|
LL | #![deny(rustdoc::invalid_html_tags)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try marking as source code
|
LL | /// This `Vec<i32>` thing!
| + +

error: unclosed HTML tag `i32`
--> $DIR/html-as-generics.rs:10:18
--> $DIR/html-as-generics.rs:9:18
|
LL | /// This vec::Vec<i32> thing!
| --------^^^^^
| |
| help: try marking as source code with `backticks`
| ^^^^^
|
help: try marking as source code
|
LL | /// This `vec::Vec<i32>` thing!
| + +

error: unclosed HTML tag `i32`
--> $DIR/html-as-generics.rs:16:13
--> $DIR/html-as-generics.rs:14:13
|
LL | /// This i32<i32> thing!
| ---^^^^^
| |
| help: try marking as source code with `backticks`
| ^^^^^
|
help: try marking as source code
|
LL | /// This `i32<i32>` thing!
| + +

error: unclosed HTML tag `i32`
--> $DIR/html-as-generics.rs:22:15
--> $DIR/html-as-generics.rs:19:15
|
LL | /// This Vec::<i32> thing!
| -----^^^^^
| |
| help: try marking as source code with `backticks`
| ^^^^^
|
help: try marking as source code
|
LL | /// This `Vec::<i32>` thing!
| + +

error: unclosed HTML tag `i32`
--> $DIR/html-as-generics.rs:28:41
--> $DIR/html-as-generics.rs:24:41
|
LL | /// This [link](https://rust-lang.org)::<i32> thing!
| --^^^^^
| |
| help: try marking as source code with `backticks`
| ^^^^^
|
help: try marking as source code
|
LL | /// This [link](https://rust-lang.org)`::<i32>` thing!
| + +

error: unclosed HTML tag `i32`
--> $DIR/html-as-generics.rs:34:21
--> $DIR/html-as-generics.rs:29:21
|
LL | /// This <span>Vec::<i32></span> thing!
| -----^^^^^
| |
| help: try marking as source code with `backticks`
| ^^^^^
|
help: try marking as source code
|
LL | /// This <span>`Vec::<i32>`</span> thing!
| + +

error: aborting due to 6 previous errors

0 comments on commit 76b5b27

Please sign in to comment.