aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/lib.rs')
-rw-r--r--crates/ra_syntax/src/lib.rs38
1 files changed, 14 insertions, 24 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 6753c513f..a75e641ea 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -42,52 +42,42 @@ pub use crate::{
42 ast::AstNode, 42 ast::AstNode,
43 lexer::{tokenize, Token}, 43 lexer::{tokenize, Token},
44 syntax_kinds::SyntaxKind, 44 syntax_kinds::SyntaxKind,
45 yellow::{ 45 yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreePtr},
46 Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location,
47 },
48}; 46};
49 47
50use ra_text_edit::AtomTextEdit; 48use ra_text_edit::AtomTextEdit;
51use crate::yellow::GreenNode; 49use crate::yellow::GreenNode;
52 50
53/// `SourceFileNode` represents a parse tree for a single Rust file. 51/// `SourceFile` represents a parse tree for a single Rust file.
54pub use crate::ast::{SourceFile, SourceFileNode}; 52pub use crate::ast::SourceFile;
55 53
56impl SourceFileNode { 54impl SourceFile {
57 fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SourceFileNode { 55 fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SourceFile> {
58 let root = SyntaxNode::new(green, errors); 56 let root = SyntaxNode::new(green, errors);
59 if cfg!(debug_assertions) { 57 if cfg!(debug_assertions) {
60 utils::validate_block_structure(root.borrowed()); 58 utils::validate_block_structure(&root);
61 } 59 }
62 assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE); 60 assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
63 ast::SourceFileNode { syntax: root } 61 TreePtr::cast(root)
64 } 62 }
65 pub fn parse(text: &str) -> SourceFileNode { 63 pub fn parse(text: &str) -> TreePtr<SourceFile> {
66 let tokens = tokenize(&text); 64 let tokens = tokenize(&text);
67 let (green, errors) = 65 let (green, errors) =
68 parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root); 66 parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
69 SourceFileNode::new(green, errors) 67 SourceFile::new(green, errors)
70 } 68 }
71 pub fn reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { 69 pub fn reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> {
72 self.incremental_reparse(edit) 70 self.incremental_reparse(edit)
73 .unwrap_or_else(|| self.full_reparse(edit)) 71 .unwrap_or_else(|| self.full_reparse(edit))
74 } 72 }
75 pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<SourceFileNode> { 73 pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreePtr<SourceFile>> {
76 reparsing::incremental_reparse(self.syntax(), edit, self.errors()) 74 reparsing::incremental_reparse(self.syntax(), edit, self.errors())
77 .map(|(green_node, errors)| SourceFileNode::new(green_node, errors)) 75 .map(|(green_node, errors)| SourceFile::new(green_node, errors))
78 } 76 }
79 fn full_reparse(&self, edit: &AtomTextEdit) -> SourceFileNode { 77 fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> {
80 let text = 78 let text =
81 text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert); 79 text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
82 SourceFileNode::parse(&text) 80 SourceFile::parse(&text)
83 }
84 /// Typed AST representation of the parse tree.
85 pub fn ast(&self) -> ast::SourceFile {
86 self.borrowed()
87 }
88 /// Untyped homogeneous representation of the parse tree.
89 pub fn syntax(&self) -> SyntaxNodeRef {
90 self.syntax.borrowed()
91 } 81 }
92 pub fn errors(&self) -> Vec<SyntaxError> { 82 pub fn errors(&self) -> Vec<SyntaxError> {
93 let mut errors = self.syntax.root_data().clone(); 83 let mut errors = self.syntax.root_data().clone();