From e0e384a34ab06b18c76f8020df149bef392c04c7 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 22 Apr 2020 04:15:55 +0300 Subject: Add ast docs to codegen script --- xtask/src/codegen/gen_syntax.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'xtask/src/codegen') 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 @@ //! Specifically, it generates the `SyntaxKind` enum and a number of newtype //! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. -use std::collections::HashSet; +use std::{collections::HashSet, fmt::Write}; use proc_macro2::{Punct, Spacing}; use quote::{format_ident, quote}; @@ -102,6 +102,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { }); ( quote! { + #[pretty_doc_comment_placeholder_workaround] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct #name { pub(crate) syntax: SyntaxNode, @@ -145,6 +146,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { ( quote! { + #[pretty_doc_comment_placeholder_workaround] #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum #name { #(#variants(#variants),)* @@ -230,10 +232,29 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { }; let ast = ast.to_string().replace("T ! [ ", "T![").replace(" ] )", "])"); - let pretty = crate::reformat(ast)?.replace("#[derive", "\n#[derive"); + + let mut res = String::with_capacity(ast.len() * 2); + + let mut docs = + grammar.nodes.iter().map(|it| it.doc).chain(grammar.enums.iter().map(|it| it.doc)); + + for chunk in ast.split("# [ pretty_doc_comment_placeholder_workaround ]") { + res.push_str(chunk); + if let Some(doc) = docs.next() { + write_doc_comment(doc, &mut res); + } + } + + let pretty = crate::reformat(res)?; Ok(pretty) } +fn write_doc_comment(contents: &[&str], dest: &mut String) { + for line in contents { + writeln!(dest, "///{}", line).unwrap(); + } +} + fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result { let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar .punct -- cgit v1.2.3