aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs4
-rw-r--r--xtask/src/codegen/gen_syntax.rs26
2 files changed, 26 insertions, 4 deletions
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
index 31d606535..4bd6b5f0c 100644
--- a/xtask/src/codegen/gen_assists_docs.rs
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -101,14 +101,14 @@ fn collect_assists() -> Result<Vec<Assist>> {
101} 101}
102 102
103fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> { 103fn generate_tests(assists: &[Assist], mode: Mode) -> Result<()> {
104 let mut buf = String::from("use super::check;\n"); 104 let mut buf = String::from("use super::check_doc_test;\n");
105 105
106 for assist in assists.iter() { 106 for assist in assists.iter() {
107 let test = format!( 107 let test = format!(
108 r######" 108 r######"
109#[test] 109#[test]
110fn doctest_{}() {{ 110fn doctest_{}() {{
111 check( 111 check_doc_test(
112 "{}", 112 "{}",
113r#####" 113r#####"
114{}"#####, r#####" 114{}"#####, r#####"
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index e9dc09552..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
@@ -432,6 +453,7 @@ impl Field<'_> {
432 ":" => "colon", 453 ":" => "colon",
433 "::" => "coloncolon", 454 "::" => "coloncolon",
434 "#" => "pound", 455 "#" => "pound",
456 "?" => "question_mark",
435 _ => name, 457 _ => name,
436 }; 458 };
437 format_ident!("{}_token", name) 459 format_ident!("{}_token", name)