diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-13 10:09:46 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-13 10:09:46 +0100 |
commit | 9594fe33fabeb2f97afad86c3a5d5ed79e3d54d8 (patch) | |
tree | 85c8c2cb648a8fafc4fe7f098527c9b728821bbc /xtask/src/codegen | |
parent | f4aec8a22e401fe8441470cf5c9ab37988486219 (diff) | |
parent | b22cf23ad1355af5b039f6da19aa69f57ed47b97 (diff) |
Merge #4083
4083: Smol documentation for ast nodes r=matklad a=Veetaha
There is a tremendous amount of TODOs to clarify the topics I am not certain about.
Please @matklad, @edwin0cheng review carefully, I even left some mentions of your names in todos to put your attention where you most probably can give comments.
In order to simplify the review, I separated the codegen (i.e. changes in `ast/generated/nodes.rs`) from `ast_src` changes (they in fact just duplicate one another) into two commits.
Also, I had to hack a little bit to let the docs be generated as doc comments and not as doc attributes because it's easier to read them this way and IIRC we don't support hints for `#[doc = ""]` attributes for now...
Closes #3682
Co-authored-by: veetaha <[email protected]>
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 8028575c5..19d5594f5 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs | |||
@@ -3,7 +3,7 @@ | |||
3 | //! Specifically, it generates the `SyntaxKind` enum and a number of newtype | 3 | //! Specifically, it generates the `SyntaxKind` enum and a number of newtype |
4 | //! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. | 4 | //! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. |
5 | 5 | ||
6 | use std::collections::HashSet; | 6 | use std::{collections::HashSet, fmt::Write}; |
7 | 7 | ||
8 | use proc_macro2::{Punct, Spacing}; | 8 | use proc_macro2::{Punct, Spacing}; |
9 | use quote::{format_ident, quote}; | 9 | use quote::{format_ident, quote}; |
@@ -102,6 +102,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
102 | }); | 102 | }); |
103 | ( | 103 | ( |
104 | quote! { | 104 | quote! { |
105 | #[pretty_doc_comment_placeholder_workaround] | ||
105 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 106 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
106 | pub struct #name { | 107 | pub struct #name { |
107 | pub(crate) syntax: SyntaxNode, | 108 | pub(crate) syntax: SyntaxNode, |
@@ -145,6 +146,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
145 | 146 | ||
146 | ( | 147 | ( |
147 | quote! { | 148 | quote! { |
149 | #[pretty_doc_comment_placeholder_workaround] | ||
148 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 150 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
149 | pub enum #name { | 151 | pub enum #name { |
150 | #(#variants(#variants),)* | 152 | #(#variants(#variants),)* |
@@ -230,10 +232,29 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> { | |||
230 | }; | 232 | }; |
231 | 233 | ||
232 | let ast = ast.to_string().replace("T ! [ ", "T![").replace(" ] )", "])"); | 234 | let ast = ast.to_string().replace("T ! [ ", "T![").replace(" ] )", "])"); |
233 | let pretty = crate::reformat(ast)?.replace("#[derive", "\n#[derive"); | 235 | |
236 | let mut res = String::with_capacity(ast.len() * 2); | ||
237 | |||
238 | let mut docs = | ||
239 | grammar.nodes.iter().map(|it| it.doc).chain(grammar.enums.iter().map(|it| it.doc)); | ||
240 | |||
241 | for chunk in ast.split("# [ pretty_doc_comment_placeholder_workaround ]") { | ||
242 | res.push_str(chunk); | ||
243 | if let Some(doc) = docs.next() { | ||
244 | write_doc_comment(doc, &mut res); | ||
245 | } | ||
246 | } | ||
247 | |||
248 | let pretty = crate::reformat(res)?; | ||
234 | Ok(pretty) | 249 | Ok(pretty) |
235 | } | 250 | } |
236 | 251 | ||
252 | fn write_doc_comment(contents: &[&str], dest: &mut String) { | ||
253 | for line in contents { | ||
254 | writeln!(dest, "///{}", line).unwrap(); | ||
255 | } | ||
256 | } | ||
257 | |||
237 | fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> { | 258 | fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> { |
238 | let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar | 259 | let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar |
239 | .punct | 260 | .punct |