Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly support nested identifiers #27

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use self::docbook::*;
use rnix::parser::{Arena, ASTNode, ASTKind, Data};
use rnix::tokenizer::Meta;
use rnix::tokenizer::Trivia;
use rnix::tokenizer::TokenKind;
use std::fs;
use std::io;
use std::path::PathBuf;
Expand Down Expand Up @@ -90,13 +91,40 @@ fn retrieve_doc_comment(allow_single_line: bool, meta: &Meta) -> Option<String>

/// Transforms an AST node into a `DocItem` if it has a leading
/// documentation comment.
fn retrieve_doc_item(node: &ASTNode) -> Option<DocItem> {
fn retrieve_doc_item(arena: &Arena, node: &ASTNode) -> Option<DocItem> {
// We are only interested in identifiers.
if let Data::Ident(meta, name) = &node.data {
let comment = retrieve_doc_comment(false, meta)?;

// Loop through additional identifier sibling nodes
// Makes it detect comments above definitions like `foo.bar.baz = 10`
// work correctly
let mut current_ident = node;
let mut item_name = name.to_string();
// If there is a sibling to the current identifier node
while let Some(next) = current_ident.node.sibling {
// Check if it's a `.` node (just to be sure)
let dot_node = &arena[next];
if let Data::Token(_, TokenKind::Dot) = &dot_node.data {
// Append a dot
item_name += ".";
} else {
// If it's not, exit, not sure when this could happen
break;
}

// We expect the next sibling to be the identifier node
let next_ident = &arena[dot_node.node.sibling?];
if let Data::Ident(_, name) = &next_ident.data {
// Append the name of the new identifier
item_name += name;
} else {
break;
}
current_ident = next_ident;
}
return Some(DocItem {
name: name.to_string(),
name: item_name,
comment: parse_doc_comment(&comment),
args: vec![],
})
Expand Down Expand Up @@ -239,7 +267,7 @@ fn collect_entry_information<'a>(arena: &Arena<'a>, entry_node: &ASTNode) -> Opt
// At this point we can retrieve the `DocItem` from the identifier
// node - this already contains most of the information we are
// interested in.
let doc_item = retrieve_doc_item(ident_node)?;
let doc_item = retrieve_doc_item(arena, ident_node)?;

// From our entry we can walk two nodes to the right and check
// whether we are dealing with a lambda. If so, we can start
Expand Down