2019-11-28 15:05:28 +00:00
|
|
|
//! Utilities for mapping between hir IDs and the surface syntax.
|
|
|
|
|
|
|
|
use hir_expand::InFile;
|
2021-01-15 00:11:07 +00:00
|
|
|
use la_arena::ArenaMap;
|
2022-03-08 20:41:19 +00:00
|
|
|
use syntax::ast;
|
2019-11-28 15:05:28 +00:00
|
|
|
|
2022-03-08 20:41:19 +00:00
|
|
|
use crate::{
|
2024-01-15 11:03:31 +00:00
|
|
|
db::DefDatabase, item_tree::ItemTreeModItemNode, AssocItemLoc, EnumVariantLoc, ItemLoc, Lookup,
|
2024-01-15 09:24:14 +00:00
|
|
|
Macro2Loc, MacroRulesLoc, ProcMacroLoc, UseId,
|
2022-03-08 20:41:19 +00:00
|
|
|
};
|
2019-11-28 15:05:28 +00:00
|
|
|
|
|
|
|
pub trait HasSource {
|
|
|
|
type Value;
|
2020-03-13 15:05:46 +00:00
|
|
|
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
|
2019-11-28 15:05:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:03:31 +00:00
|
|
|
impl<N: ItemTreeModItemNode> HasSource for AssocItemLoc<N> {
|
2020-06-22 13:07:06 +00:00
|
|
|
type Value = N::Source;
|
2019-11-28 15:05:28 +00:00
|
|
|
|
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))
|
2019-11-28 15:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:03:31 +00:00
|
|
|
impl<N: ItemTreeModItemNode> 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];
|
2019-11-28 15:05:28 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 09:24:14 +00:00
|
|
|
impl HasSource for EnumVariantLoc {
|
|
|
|
type Value = ast::Variant;
|
|
|
|
|
|
|
|
fn source(&self, db: &dyn DefDatabase) -> InFile<ast::Variant> {
|
|
|
|
let tree = self.id.item_tree(db);
|
|
|
|
let ast_id_map = db.ast_id_map(self.id.file_id());
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 20:41:19 +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());
|
2022-03-08 20:41:19 +00:00
|
|
|
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());
|
2022-03-08 20:41:19 +00:00
|
|
|
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());
|
2022-03-08 20:41:19 +00:00
|
|
|
let node = &tree[self.id.value];
|
|
|
|
|
|
|
|
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 16:11:27 +00:00
|
|
|
pub trait HasChildSource<ChildId> {
|
2019-11-28 15:05:28 +00:00
|
|
|
type Value;
|
2020-12-14 16:11:27 +00:00
|
|
|
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<ChildId, Self::Value>>;
|
2019-11-28 15:05:28 +00:00
|
|
|
}
|
2023-08-18 09:46:35 +00:00
|
|
|
|
|
|
|
impl HasChildSource<la_arena::Idx<ast::UseTree>> for UseId {
|
|
|
|
type Value = ast::UseTree;
|
|
|
|
fn child_source(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
) -> InFile<ArenaMap<la_arena::Idx<ast::UseTree>, Self::Value>> {
|
|
|
|
let loc = &self.lookup(db);
|
|
|
|
let use_ = &loc.id.item_tree(db)[loc.id.value];
|
|
|
|
InFile::new(
|
|
|
|
loc.id.file_id(),
|
|
|
|
use_.use_tree_source_map(db, loc.id.file_id()).into_iter().collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|