-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧚♀️Normalize doc attributes into
///
(#17)
Stable `rustfmt` does not support normalizing doc attributes: rust-lang/rustfmt#3351 So, we "normalize" them ourselves by rendering `///`.
- Loading branch information
Ivan Dubrov
authored
Aug 21, 2019
1 parent
e6c6aec
commit 4ac283f
Showing
7 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,82 @@ | ||
use proc_macro2::{Delimiter, Spacing, TokenStream, TokenTree}; | ||
use syn::Lit; | ||
|
||
/// Write tokens same way as `TokenStream::to_string` would do, but with normalization of doc | ||
/// attributes into `///`. | ||
pub fn write_tokens_normalized( | ||
f: &mut std::fmt::Formatter, | ||
tokens: TokenStream, | ||
) -> std::fmt::Result { | ||
let mut tokens = tokens.into_iter().peekable(); | ||
let mut joint = false; | ||
let mut first = true; | ||
while let Some(tt) = tokens.next() { | ||
if !first && !joint { | ||
write!(f, " ")?; | ||
} | ||
first = false; | ||
joint = false; | ||
|
||
if let Some(comment) = tokens | ||
.peek() | ||
.and_then(|lookahead| as_doc_comment(&tt, lookahead)) | ||
{ | ||
let _ignore = tokens.next(); | ||
writeln!(f, "///{}", comment)?; | ||
continue; | ||
} | ||
match tt { | ||
TokenTree::Group(ref tt) => { | ||
let (start, end) = match tt.delimiter() { | ||
Delimiter::Parenthesis => ("(", ")"), | ||
Delimiter::Brace => ("{", "}"), | ||
Delimiter::Bracket => ("[", "]"), | ||
Delimiter::None => ("", ""), | ||
}; | ||
if tt.stream().into_iter().next().is_none() { | ||
write!(f, "{} {}", start, end)? | ||
} else { | ||
write!(f, "{} ", start)?; | ||
write_tokens_normalized(f, tt.stream())?; | ||
write!(f, " {}", end)? | ||
} | ||
} | ||
TokenTree::Ident(ref tt) => write!(f, "{}", tt)?, | ||
TokenTree::Punct(ref tt) => { | ||
write!(f, "{}", tt.as_char())?; | ||
match tt.spacing() { | ||
Spacing::Alone => {} | ||
Spacing::Joint => joint = true, | ||
} | ||
} | ||
TokenTree::Literal(ref tt) => write!(f, "{}", tt)?, | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn as_doc_comment(first: &TokenTree, second: &TokenTree) -> Option<String> { | ||
match (first, second) { | ||
(TokenTree::Punct(first), TokenTree::Group(group)) | ||
if first.as_char() == '#' && group.delimiter() == Delimiter::Bracket => | ||
{ | ||
let mut it = group.stream().into_iter(); | ||
match (it.next(), it.next(), it.next()) { | ||
( | ||
Some(TokenTree::Ident(ident)), | ||
Some(TokenTree::Punct(punct)), | ||
Some(TokenTree::Literal(lit)), | ||
) => { | ||
if ident == "doc" && punct.as_char() == '=' { | ||
if let Lit::Str(lit) = Lit::new(lit) { | ||
return Some(lit.value()); | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
_ => {} | ||
} | ||
None | ||
} |
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#[sourcegen::sourcegen(generator = "generate-doc-comments")] | ||
// Generated. All manual edits to the block annotated with #[sourcegen...] will be discarded. | ||
#[doc = r" Some generated comment here"] | ||
/// Some generated comment here | ||
struct Hello { | ||
pub hello: String, | ||
} |
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#![sourcegen::sourcegen(generator = "generate-file")] | ||
// Generated. All manual edits below this line will be discarded. | ||
#[doc = r" Some generated comment here"] | ||
/// Some generated comment here | ||
struct Hello { | ||
pub hello: String, | ||
} |
This file contains 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