diff options
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r-- | crates/ra_hir_expand/src/diagnostics.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 38 |
2 files changed, 23 insertions, 19 deletions
diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs index 201884b95..3d37e9335 100644 --- a/crates/ra_hir_expand/src/diagnostics.rs +++ b/crates/ra_hir_expand/src/diagnostics.rs | |||
@@ -24,7 +24,7 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | |||
24 | fn message(&self) -> String; | 24 | fn message(&self) -> String; |
25 | fn source(&self) -> Source<SyntaxNodePtr>; | 25 | fn source(&self) -> Source<SyntaxNodePtr>; |
26 | fn highlight_range(&self) -> TextRange { | 26 | fn highlight_range(&self) -> TextRange { |
27 | self.source().ast.range() | 27 | self.source().value.range() |
28 | } | 28 | } |
29 | fn as_any(&self) -> &(dyn Any + Send + 'static); | 29 | fn as_any(&self) -> &(dyn Any + Send + 'static); |
30 | } | 30 | } |
@@ -37,7 +37,7 @@ pub trait AstDiagnostic { | |||
37 | impl dyn Diagnostic { | 37 | impl dyn Diagnostic { |
38 | pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode { | 38 | pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode { |
39 | let node = db.parse_or_expand(self.source().file_id).unwrap(); | 39 | let node = db.parse_or_expand(self.source().file_id).unwrap(); |
40 | self.source().ast.to_node(&node) | 40 | self.source().value.to_node(&node) |
41 | } | 41 | } |
42 | 42 | ||
43 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { | 43 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { |
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index cfe7e6d15..1389f64ce 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -166,19 +166,19 @@ impl ExpansionInfo { | |||
166 | pub fn map_token_down(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { | 166 | pub fn map_token_down(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { |
167 | assert_eq!(token.file_id, self.arg.file_id); | 167 | assert_eq!(token.file_id, self.arg.file_id); |
168 | let range = | 168 | let range = |
169 | token.ast.text_range().checked_sub(self.arg.ast.syntax().text_range().start())?; | 169 | token.value.text_range().checked_sub(self.arg.value.syntax().text_range().start())?; |
170 | let token_id = self.macro_arg.1.token_by_range(range)?; | 170 | let token_id = self.macro_arg.1.token_by_range(range)?; |
171 | let token_id = self.macro_def.0.map_id_down(token_id); | 171 | let token_id = self.macro_def.0.map_id_down(token_id); |
172 | 172 | ||
173 | let range = self.exp_map.range_by_token(token_id)?; | 173 | let range = self.exp_map.range_by_token(token_id)?; |
174 | 174 | ||
175 | let token = algo::find_covering_element(&self.expanded.ast, range).into_token()?; | 175 | let token = algo::find_covering_element(&self.expanded.value, range).into_token()?; |
176 | 176 | ||
177 | Some(self.expanded.with_ast(token)) | 177 | Some(self.expanded.with_value(token)) |
178 | } | 178 | } |
179 | 179 | ||
180 | pub fn map_token_up(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { | 180 | pub fn map_token_up(&self, token: Source<&SyntaxToken>) -> Option<Source<SyntaxToken>> { |
181 | let token_id = self.exp_map.token_by_range(token.ast.text_range())?; | 181 | let token_id = self.exp_map.token_by_range(token.value.text_range())?; |
182 | 182 | ||
183 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); | 183 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); |
184 | let (token_map, tt) = match origin { | 184 | let (token_map, tt) = match origin { |
@@ -188,11 +188,11 @@ impl ExpansionInfo { | |||
188 | 188 | ||
189 | let range = token_map.range_by_token(token_id)?; | 189 | let range = token_map.range_by_token(token_id)?; |
190 | let token = algo::find_covering_element( | 190 | let token = algo::find_covering_element( |
191 | tt.ast.syntax(), | 191 | tt.value.syntax(), |
192 | range + tt.ast.syntax().text_range().start(), | 192 | range + tt.value.syntax().text_range().start(), |
193 | ) | 193 | ) |
194 | .into_token()?; | 194 | .into_token()?; |
195 | Some(tt.with_ast(token)) | 195 | Some(tt.with_value(token)) |
196 | } | 196 | } |
197 | } | 197 | } |
198 | 198 | ||
@@ -240,30 +240,34 @@ impl<N: AstNode> AstId<N> { | |||
240 | } | 240 | } |
241 | } | 241 | } |
242 | 242 | ||
243 | /// FIXME: https://github.com/matklad/with ? | 243 | /// `Source<T>` stores a value of `T` inside a particular file/syntax tree. |
244 | /// | ||
245 | /// Typical usages are: | ||
246 | /// | ||
247 | /// * `Source<SyntaxNode>` -- syntax node in a file | ||
248 | /// * `Source<ast::FnDef>` -- ast node in a file | ||
249 | /// * `Source<TextUnit>` -- offset in a file | ||
244 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] | 250 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] |
245 | pub struct Source<T> { | 251 | pub struct Source<T> { |
246 | pub file_id: HirFileId, | 252 | pub file_id: HirFileId, |
247 | // FIXME: this stores all kind of things, not only `ast`. | 253 | pub value: T, |
248 | // There should be a better name... | ||
249 | pub ast: T, | ||
250 | } | 254 | } |
251 | 255 | ||
252 | impl<T> Source<T> { | 256 | impl<T> Source<T> { |
253 | pub fn new(file_id: HirFileId, ast: T) -> Source<T> { | 257 | pub fn new(file_id: HirFileId, value: T) -> Source<T> { |
254 | Source { file_id, ast } | 258 | Source { file_id, value } |
255 | } | 259 | } |
256 | 260 | ||
257 | // Similarly, naming here is stupid... | 261 | // Similarly, naming here is stupid... |
258 | pub fn with_ast<U>(&self, ast: U) -> Source<U> { | 262 | pub fn with_value<U>(&self, value: U) -> Source<U> { |
259 | Source::new(self.file_id, ast) | 263 | Source::new(self.file_id, value) |
260 | } | 264 | } |
261 | 265 | ||
262 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { | 266 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { |
263 | Source::new(self.file_id, f(self.ast)) | 267 | Source::new(self.file_id, f(self.value)) |
264 | } | 268 | } |
265 | pub fn as_ref(&self) -> Source<&T> { | 269 | pub fn as_ref(&self) -> Source<&T> { |
266 | self.with_ast(&self.ast) | 270 | self.with_value(&self.value) |
267 | } | 271 | } |
268 | pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode { | 272 | pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode { |
269 | db.parse_or_expand(self.file_id).expect("source created from invalid file") | 273 | db.parse_or_expand(self.file_id).expect("source created from invalid file") |