refactor(xatu): collect syntax elements in one pass
This commit is contained in:
@@ -6,18 +6,12 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
|
||||
let parse = rnix::Root::parse(source);
|
||||
let source_len = source.len();
|
||||
|
||||
let (nodes, tokens) = convert_syntax(parse.syntax());
|
||||
|
||||
ast::ParsedFile {
|
||||
file_id,
|
||||
nodes: parse
|
||||
.syntax()
|
||||
.descendants()
|
||||
.filter_map(convert_node)
|
||||
.collect(),
|
||||
tokens: parse
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(convert_token)
|
||||
.collect(),
|
||||
nodes,
|
||||
tokens,
|
||||
errors: parse
|
||||
.errors()
|
||||
.iter()
|
||||
@@ -26,6 +20,20 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_syntax(syntax: rnix::SyntaxNode) -> (Vec<ast::Node>, Vec<ast::Token>) {
|
||||
let mut nodes = Vec::new();
|
||||
let mut tokens = Vec::new();
|
||||
|
||||
for element in syntax.descendants_with_tokens() {
|
||||
match element {
|
||||
rnix::NodeOrToken::Node(node) => nodes.extend(convert_node(node)),
|
||||
rnix::NodeOrToken::Token(token) => tokens.extend(convert_token(token)),
|
||||
}
|
||||
}
|
||||
|
||||
(nodes, tokens)
|
||||
}
|
||||
|
||||
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
match node.kind() {
|
||||
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
|
||||
@@ -111,12 +119,7 @@ impl From<rnix::ast::Inherit> for ast::Inherit {
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_token(element: rnix::SyntaxElement) -> Option<ast::Token> {
|
||||
let token = match element {
|
||||
rnix::NodeOrToken::Token(token) => token,
|
||||
rnix::NodeOrToken::Node(_) => return None,
|
||||
};
|
||||
|
||||
fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
|
||||
match token.kind() {
|
||||
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {
|
||||
range: token.text_range().into(),
|
||||
|
||||
Reference in New Issue
Block a user