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 <veetaha2@gmail.com>
This commit is contained in:
bors[bot] 2020-05-13 09:09:46 +00:00 committed by GitHub
commit 9594fe33fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 2878 additions and 154 deletions

File diff suppressed because it is too large Load diff

View file

@ -206,7 +206,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D
}
private parseRustTextRange(doc: vscode.TextDocument, astLine: string): undefined | vscode.Range {
const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine);
const parsedRange = /(\d+)\.\.(\d+)/.exec(astLine);
if (!parsedRange) return;
const [begin, end] = parsedRange

File diff suppressed because it is too large Load diff

View file

@ -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<String> {
});
(
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<String> {
(
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<String> {
};
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<String> {
let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar
.punct