2
0
Fork 0
mirror of https://github.com/rust-lang/rust-analyzer synced 2025-01-28 04:45:05 +00:00
rust-analyzer/crates/ra_hir_def/src/src.rs
Jonas Schievink f9a1a9cd3c Merge item tree traits
The Source trait isn't needed anymore since we no longer merge
extern crate items with use items.
2020-06-24 16:53:56 +02:00

43 lines
1.3 KiB
Rust

//! Utilities for mapping between hir IDs and the surface syntax.
use hir_expand::InFile;
use ra_arena::map::ArenaMap;
use crate::{db::DefDatabase, item_tree::ItemTreeNode, AssocItemLoc, ItemLoc};
pub trait HasSource {
type Value;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
}
impl<N: ItemTreeNode> HasSource for AssocItemLoc<N> {
type Value = N::Source;
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
let tree = db.item_tree(self.id.file_id);
let ast_id_map = db.ast_id_map(self.id.file_id);
let root = db.parse_or_expand(self.id.file_id).unwrap();
let node = &tree[self.id.value];
InFile::new(self.id.file_id, ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl<N: ItemTreeNode> HasSource for ItemLoc<N> {
type Value = N::Source;
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
let tree = db.item_tree(self.id.file_id);
let ast_id_map = db.ast_id_map(self.id.file_id);
let root = db.parse_or_expand(self.id.file_id).unwrap();
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 {
type ChildId;
type Value;
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>;
}