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 parse = rnix::Root::parse(source);
|
||||||
let source_len = source.len();
|
let source_len = source.len();
|
||||||
|
|
||||||
|
let (nodes, tokens) = convert_syntax(parse.syntax());
|
||||||
|
|
||||||
ast::ParsedFile {
|
ast::ParsedFile {
|
||||||
file_id,
|
file_id,
|
||||||
nodes: parse
|
nodes,
|
||||||
.syntax()
|
tokens,
|
||||||
.descendants()
|
|
||||||
.filter_map(convert_node)
|
|
||||||
.collect(),
|
|
||||||
tokens: parse
|
|
||||||
.syntax()
|
|
||||||
.descendants_with_tokens()
|
|
||||||
.filter_map(convert_token)
|
|
||||||
.collect(),
|
|
||||||
errors: parse
|
errors: parse
|
||||||
.errors()
|
.errors()
|
||||||
.iter()
|
.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> {
|
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||||
match node.kind() {
|
match node.kind() {
|
||||||
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
|
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> {
|
fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
|
||||||
let token = match element {
|
|
||||||
rnix::NodeOrToken::Token(token) => token,
|
|
||||||
rnix::NodeOrToken::Node(_) => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
match token.kind() {
|
match token.kind() {
|
||||||
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {
|
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {
|
||||||
range: token.text_range().into(),
|
range: token.text_range().into(),
|
||||||
|
|||||||
Reference in New Issue
Block a user