rust-analyzer/crates/hir-def/src/src.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.6 KiB
Rust
Raw Normal View History

//! Utilities for mapping between hir IDs and the surface syntax.
use hir_expand::InFile;
use la_arena::ArenaMap;
use syntax::ast;
use crate::{
db::DefDatabase, item_tree::ItemTreeNode, AssocItemLoc, ItemLoc, Macro2Loc, MacroRulesLoc,
ProcMacroLoc,
};
pub trait HasSource {
type Value;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
}
impl<N: ItemTreeNode> HasSource for AssocItemLoc<N> {
2020-06-22 13:07:06 +00:00
type Value = N::Source;
2020-06-22 13:07:06 +00:00
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
2021-03-12 23:34:01 +00:00
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
2023-04-16 17:20:48 +00:00
let root = db.parse_or_expand(self.id.file_id());
2020-06-22 13:07:06 +00:00
let node = &tree[self.id.value];
2021-03-12 23:34:01 +00:00
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl<N: ItemTreeNode> HasSource for ItemLoc<N> {
2020-06-22 13:07:06 +00:00
type Value = N::Source;
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
2021-03-12 23:34:01 +00:00
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
2023-04-16 17:20:48 +00:00
let root = db.parse_or_expand(self.id.file_id());
2020-06-22 13:07:06 +00:00
let node = &tree[self.id.value];
2021-03-12 23:34:01 +00:00
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
2019-12-12 14:11:57 +00:00
}
}
impl HasSource for Macro2Loc {
type Value = ast::MacroDef;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
2023-04-16 17:20:48 +00:00
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl HasSource for MacroRulesLoc {
type Value = ast::MacroRules;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
2023-04-16 17:20:48 +00:00
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl HasSource for ProcMacroLoc {
type Value = ast::Fn;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
2023-04-16 17:20:48 +00:00
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
}
}
pub trait HasChildSource<ChildId> {
type Value;
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<ChildId, Self::Value>>;
}