-
Notifications
You must be signed in to change notification settings - Fork 931
Fix normalisation of multiline doc attributes #3548
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
Merged
topecongiro
merged 8 commits into
rust-lang:master
from
bash:normalize-multiline-doc-attributes
May 20, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7fc181d
Add regression test
47a0f78
Fix normalisation of multi-line doc attributes
4ac0d35
Only run test on nightly
3eb8e4d
Ignore unstable key when overriding config
9b98dd7
Merge branch 'master' into normalize-multiline-doc-attributes
8d4cb6e
Use peek() instead of checking indexes
11a68d3
Pass string instead of Symbol to DocCommentFormatter
1ba35fc
Document DocCommentFormatter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::comment::CommentStyle; | ||
use std::fmt::{self, Display}; | ||
|
||
/// Formats a string as a doc comment using the given [`CommentStyle`]. | ||
#[derive(new)] | ||
pub(super) struct DocCommentFormatter<'a> { | ||
literal: &'a str, | ||
style: CommentStyle<'a>, | ||
} | ||
|
||
impl Display for DocCommentFormatter<'_> { | ||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let opener = self.style.opener().trim_end(); | ||
let mut lines = self.literal.lines().peekable(); | ||
while let Some(line) = lines.next() { | ||
let is_last_line = lines.peek().is_none(); | ||
if is_last_line { | ||
write!(formatter, "{}{}", opener, line)?; | ||
} else { | ||
writeln!(formatter, "{}{}", opener, line)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn literal_controls_leading_spaces() { | ||
test_doc_comment_is_formatted_correctly( | ||
" Lorem ipsum", | ||
"/// Lorem ipsum", | ||
CommentStyle::TripleSlash, | ||
); | ||
} | ||
|
||
#[test] | ||
fn single_line_doc_comment_is_formatted_correctly() { | ||
test_doc_comment_is_formatted_correctly( | ||
"Lorem ipsum", | ||
"///Lorem ipsum", | ||
CommentStyle::TripleSlash, | ||
); | ||
} | ||
|
||
#[test] | ||
fn multi_line_doc_comment_is_formatted_correctly() { | ||
test_doc_comment_is_formatted_correctly( | ||
"Lorem ipsum\nDolor sit amet", | ||
"///Lorem ipsum\n///Dolor sit amet", | ||
CommentStyle::TripleSlash, | ||
); | ||
} | ||
|
||
#[test] | ||
fn whitespace_within_lines_is_preserved() { | ||
test_doc_comment_is_formatted_correctly( | ||
" Lorem ipsum \n Dolor sit amet ", | ||
"/// Lorem ipsum \n/// Dolor sit amet ", | ||
CommentStyle::TripleSlash, | ||
); | ||
} | ||
|
||
fn test_doc_comment_is_formatted_correctly( | ||
literal: &str, | ||
expected_comment: &str, | ||
style: CommentStyle<'_>, | ||
) { | ||
assert_eq!( | ||
expected_comment, | ||
format!("{}", DocCommentFormatter::new(&literal, style)) | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// rustfmt-unstable: true | ||
// rustfmt-normalize_doc_attributes: true | ||
|
||
#[doc = "This comment | ||
is split | ||
on multiple lines"] | ||
fn foo() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// rustfmt-unstable: true | ||
// rustfmt-normalize_doc_attributes: true | ||
|
||
///This comment | ||
topecongiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
///is split | ||
///on multiple lines | ||
fn foo() {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.