aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen/gen_syntax.rs
diff options
context:
space:
mode:
authorveetaha <[email protected]>2020-04-22 02:15:55 +0100
committerveetaha <[email protected]>2020-05-10 17:05:51 +0100
commite0e384a34ab06b18c76f8020df149bef392c04c7 (patch)
treedac26faaf5a1c46144cd27d4f0dd246d3a38c15a /xtask/src/codegen/gen_syntax.rs
parent225f353aa26329260b8c7f69305f616a9edaad70 (diff)
Add ast docs to codegen script
Diffstat (limited to 'xtask/src/codegen/gen_syntax.rs')
-rw-r--r--xtask/src/codegen/gen_syntax.rs25
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
6use std::collections::HashSet; 6use std::{collections::HashSet, fmt::Write};
7 7
8use proc_macro2::{Punct, Spacing}; 8use proc_macro2::{Punct, Spacing};
9use quote::{format_ident, quote}; 9use 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
252fn write_doc_comment(contents: &[&str], dest: &mut String) {
253 for line in contents {
254 writeln!(dest, "///{}", line).unwrap();
255 }
256}
257
237fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> { 258fn 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