mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Add ast docs to codegen script
This commit is contained in:
parent
225f353aa2
commit
e0e384a34a
2 changed files with 1516 additions and 18 deletions
1507
xtask/src/ast_src.rs
1507
xtask/src/ast_src.rs
File diff suppressed because it is too large
Load diff
|
@ -3,7 +3,7 @@
|
||||||
//! Specifically, it generates the `SyntaxKind` enum and a number of newtype
|
//! Specifically, it generates the `SyntaxKind` enum and a number of newtype
|
||||||
//! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`.
|
//! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`.
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::{collections::HashSet, fmt::Write};
|
||||||
|
|
||||||
use proc_macro2::{Punct, Spacing};
|
use proc_macro2::{Punct, Spacing};
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
|
@ -102,6 +102,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
|
||||||
});
|
});
|
||||||
(
|
(
|
||||||
quote! {
|
quote! {
|
||||||
|
#[pretty_doc_comment_placeholder_workaround]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct #name {
|
pub struct #name {
|
||||||
pub(crate) syntax: SyntaxNode,
|
pub(crate) syntax: SyntaxNode,
|
||||||
|
@ -145,6 +146,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
|
||||||
|
|
||||||
(
|
(
|
||||||
quote! {
|
quote! {
|
||||||
|
#[pretty_doc_comment_placeholder_workaround]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum #name {
|
pub enum #name {
|
||||||
#(#variants(#variants),)*
|
#(#variants(#variants),)*
|
||||||
|
@ -230,10 +232,29 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let ast = ast.to_string().replace("T ! [ ", "T![").replace(" ] )", "])");
|
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)
|
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<String> {
|
fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> {
|
||||||
let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar
|
let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar
|
||||||
.punct
|
.punct
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue