Prepare SourceAnalyzer for macros

This commit is contained in:
Aleksey Kladov 2019-11-16 00:40:54 +03:00
parent bd8af6a413
commit 0404e647e6
2 changed files with 14 additions and 9 deletions

View file

@ -223,6 +223,7 @@ impl<N: AstNode> AstId<N> {
}
}
/// FIXME: https://github.com/matklad/with ?
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Source<T> {
pub file_id: HirFileId,
@ -236,11 +237,16 @@ impl<T> Source<T> {
Source { file_id, ast }
}
// Similarly, naming here is stupid...
pub fn with_ast<U>(&self, ast: U) -> Source<U> {
Source::new(self.file_id, ast)
}
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
Source { file_id: self.file_id, ast: f(self.ast) }
Source::new(self.file_id, f(self.ast))
}
pub fn as_ref(&self) -> Source<&T> {
Source { file_id: self.file_id, ast: &self.ast }
self.with_ast(&self.ast)
}
pub fn file_syntax(&self, db: &impl db::AstDatabase) -> SyntaxNode {
db.parse_or_expand(self.file_id).expect("source created from invalid file")