diff options
Diffstat (limited to 'crates/ra_syntax/src/lib.rs')
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 55 |
1 files changed, 25 insertions, 30 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 6574eeed1..930a643b7 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -69,6 +69,10 @@ impl Parse { | |||
69 | } | 69 | } |
70 | } | 70 | } |
71 | 71 | ||
72 | pub fn reparse(&self, edit: &AtomTextEdit) -> Parse { | ||
73 | self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) | ||
74 | } | ||
75 | |||
72 | pub fn debug_dump(&self) -> String { | 76 | pub fn debug_dump(&self) -> String { |
73 | let mut buf = self.tree.syntax().debug_dump(); | 77 | let mut buf = self.tree.syntax().debug_dump(); |
74 | for err in self.errors.iter() { | 78 | for err in self.errors.iter() { |
@@ -76,6 +80,21 @@ impl Parse { | |||
76 | } | 80 | } |
77 | buf | 81 | buf |
78 | } | 82 | } |
83 | |||
84 | fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<Parse> { | ||
85 | // FIXME: validation errors are not handled here | ||
86 | parsing::incremental_reparse(self.tree.syntax(), edit, self.errors.to_vec()).map( | ||
87 | |(green_node, errors, _reparsed_range)| Parse { | ||
88 | tree: SourceFile::new(green_node), | ||
89 | errors: Arc::new(errors), | ||
90 | }, | ||
91 | ) | ||
92 | } | ||
93 | |||
94 | fn full_reparse(&self, edit: &AtomTextEdit) -> Parse { | ||
95 | let text = edit.apply(self.tree.syntax().text().to_string()); | ||
96 | SourceFile::parse(&text) | ||
97 | } | ||
79 | } | 98 | } |
80 | 99 | ||
81 | /// `SourceFile` represents a parse tree for a single Rust file. | 100 | /// `SourceFile` represents a parse tree for a single Rust file. |
@@ -91,37 +110,12 @@ impl SourceFile { | |||
91 | TreeArc::cast(root) | 110 | TreeArc::cast(root) |
92 | } | 111 | } |
93 | 112 | ||
94 | pub fn parse2(text: &str) -> Parse { | 113 | pub fn parse(text: &str) -> Parse { |
95 | let (green, mut errors) = parsing::parse_text(text); | 114 | let (green, mut errors) = parsing::parse_text(text); |
96 | let tree = SourceFile::new(green); | 115 | let tree = SourceFile::new(green); |
97 | errors.extend(validation::validate(&tree)); | 116 | errors.extend(validation::validate(&tree)); |
98 | Parse { tree, errors: Arc::new(errors) } | 117 | Parse { tree, errors: Arc::new(errors) } |
99 | } | 118 | } |
100 | |||
101 | pub fn parse(text: &str) -> TreeArc<SourceFile> { | ||
102 | let (green, _errors) = parsing::parse_text(text); | ||
103 | SourceFile::new(green) | ||
104 | } | ||
105 | |||
106 | pub fn reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> { | ||
107 | self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) | ||
108 | } | ||
109 | |||
110 | pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreeArc<SourceFile>> { | ||
111 | parsing::incremental_reparse(self.syntax(), edit, self.errors()) | ||
112 | .map(|(green_node, _errors, _reparsed_range)| SourceFile::new(green_node)) | ||
113 | } | ||
114 | |||
115 | fn full_reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> { | ||
116 | let text = edit.apply(self.syntax().text().to_string()); | ||
117 | SourceFile::parse(&text) | ||
118 | } | ||
119 | |||
120 | pub fn errors(&self) -> Vec<SyntaxError> { | ||
121 | let mut errors = self.syntax.root_data().to_vec(); | ||
122 | errors.extend(validation::validate(self)); | ||
123 | errors | ||
124 | } | ||
125 | } | 119 | } |
126 | 120 | ||
127 | /// This test does not assert anything and instead just shows off the crate's | 121 | /// This test does not assert anything and instead just shows off the crate's |
@@ -137,14 +131,15 @@ fn api_walkthrough() { | |||
137 | "; | 131 | "; |
138 | // `SourceFile` is the main entry point. | 132 | // `SourceFile` is the main entry point. |
139 | // | 133 | // |
140 | // Note how `parse` does not return a `Result`: even completely invalid | 134 | // The `parse` method returns a `Parse` -- a pair of syntax tree and a list |
141 | // source code might be parsed. | 135 | // of errors. That is, syntax tree is constructed even in presence of errors. |
142 | let file = SourceFile::parse(source_code); | 136 | let parse = SourceFile::parse(source_code); |
137 | assert!(parse.errors.is_empty()); | ||
143 | 138 | ||
144 | // Due to the way ownership is set up, owned syntax Nodes always live behind | 139 | // Due to the way ownership is set up, owned syntax Nodes always live behind |
145 | // a `TreeArc` smart pointer. `TreeArc` is roughly an `std::sync::Arc` which | 140 | // a `TreeArc` smart pointer. `TreeArc` is roughly an `std::sync::Arc` which |
146 | // points to the whole file instead of an individual node. | 141 | // points to the whole file instead of an individual node. |
147 | let file: TreeArc<SourceFile> = file; | 142 | let file: TreeArc<SourceFile> = parse.tree; |
148 | 143 | ||
149 | // `SourceFile` is the root of the syntax tree. We can iterate file's items: | 144 | // `SourceFile` is the root of the syntax tree. We can iterate file's items: |
150 | let mut func = None; | 145 | let mut func = None; |