diff options
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 930789b0f..26531cb05 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -223,18 +223,30 @@ impl<N: AstNode> AstId<N> { | |||
223 | } | 223 | } |
224 | } | 224 | } |
225 | 225 | ||
226 | /// FIXME: https://github.com/matklad/with ? | ||
226 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] | 227 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] |
227 | pub struct Source<T> { | 228 | pub struct Source<T> { |
228 | pub file_id: HirFileId, | 229 | pub file_id: HirFileId, |
230 | // FIXME: this stores all kind of things, not only `ast`. | ||
231 | // There should be a better name... | ||
229 | pub ast: T, | 232 | pub ast: T, |
230 | } | 233 | } |
231 | 234 | ||
232 | impl<T> Source<T> { | 235 | impl<T> Source<T> { |
236 | pub fn new(file_id: HirFileId, ast: T) -> Source<T> { | ||
237 | Source { file_id, ast } | ||
238 | } | ||
239 | |||
240 | // Similarly, naming here is stupid... | ||
241 | pub fn with_ast<U>(&self, ast: U) -> Source<U> { | ||
242 | Source::new(self.file_id, ast) | ||
243 | } | ||
244 | |||
233 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { | 245 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { |
234 | Source { file_id: self.file_id, ast: f(self.ast) } | 246 | Source::new(self.file_id, f(self.ast)) |
235 | } | 247 | } |
236 | pub fn as_ref(&self) -> Source<&T> { | 248 | pub fn as_ref(&self) -> Source<&T> { |
237 | Source { file_id: self.file_id, ast: &self.ast } | 249 | self.with_ast(&self.ast) |
238 | } | 250 | } |
239 | pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode { | 251 | pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode { |
240 | db.parse_or_expand(self.file_id).expect("source created from invalid file") | 252 | db.parse_or_expand(self.file_id).expect("source created from invalid file") |