mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
rename
This commit is contained in:
parent
b17217b34a
commit
e28db444df
7 changed files with 44 additions and 56 deletions
|
@ -4,7 +4,7 @@ use ra_syntax::{SyntaxNode, TreeArc, SourceFile};
|
||||||
use ra_db::{SourceDatabase, salsa};
|
use ra_db::{SourceDatabase, salsa};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
HirFileId, MacroDefId, SourceFileItems, SourceItemId, Crate, Module, HirInterner,
|
HirFileId, MacroDefId, AstIdMap, ErasedFileAstId, Crate, Module, HirInterner,
|
||||||
Function, FnSignature, ExprScopes, TypeAlias,
|
Function, FnSignature, ExprScopes, TypeAlias,
|
||||||
Struct, Enum, StructField,
|
Struct, Enum, StructField,
|
||||||
Const, ConstSignature, Static,
|
Const, ConstSignature, Static,
|
||||||
|
@ -34,11 +34,11 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> {
|
||||||
#[salsa::invoke(crate::traits::TraitData::trait_data_query)]
|
#[salsa::invoke(crate::traits::TraitData::trait_data_query)]
|
||||||
fn trait_data(&self, t: Trait) -> Arc<TraitData>;
|
fn trait_data(&self, t: Trait) -> Arc<TraitData>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::source_id::SourceFileItems::file_items_query)]
|
#[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)]
|
||||||
fn file_items(&self, file_id: HirFileId) -> Arc<SourceFileItems>;
|
fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::source_id::SourceFileItems::file_item_query)]
|
#[salsa::invoke(crate::source_id::AstIdMap::file_item_query)]
|
||||||
fn file_item(&self, source_item_id: SourceItemId) -> TreeArc<SyntaxNode>;
|
fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc<SyntaxNode>;
|
||||||
|
|
||||||
#[salsa::invoke(RawItems::raw_items_query)]
|
#[salsa::invoke(RawItems::raw_items_query)]
|
||||||
fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;
|
fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;
|
||||||
|
|
|
@ -207,7 +207,7 @@ impl<'a, DB: DefDatabase> LocationCtx<&'a DB> {
|
||||||
pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
|
pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
|
||||||
fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>;
|
fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>;
|
||||||
fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self {
|
fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self {
|
||||||
let items = ctx.db.file_items(ctx.file_id);
|
let items = ctx.db.ast_id_map(ctx.file_id);
|
||||||
let item_id = items.ast_id(ast);
|
let item_id = items.ast_id(ast);
|
||||||
Self::from_ast_id(ctx, item_id)
|
Self::from_ast_id(ctx, item_id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ use crate::{
|
||||||
pub use self::{
|
pub use self::{
|
||||||
path::{Path, PathKind},
|
path::{Path, PathKind},
|
||||||
name::Name,
|
name::Name,
|
||||||
source_id::{SourceFileItems, SourceItemId},
|
source_id::{AstIdMap, ErasedFileAstId},
|
||||||
ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc, HirInterner},
|
ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc, HirInterner},
|
||||||
nameres::{PerNs, Namespace},
|
nameres::{PerNs, Namespace},
|
||||||
ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay},
|
ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay},
|
||||||
|
|
|
@ -12,7 +12,7 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
DefDatabase, Name, AsName, Path, HirFileId, ModuleSource,
|
DefDatabase, Name, AsName, Path, HirFileId, ModuleSource,
|
||||||
SourceFileItems, FileAstId,
|
AstIdMap, FileAstId,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `RawItems` is a set of top-level items in a file (except for impls).
|
/// `RawItems` is a set of top-level items in a file (except for impls).
|
||||||
|
@ -60,7 +60,7 @@ impl RawItems {
|
||||||
) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
|
) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
|
||||||
let mut collector = RawItemsCollector {
|
let mut collector = RawItemsCollector {
|
||||||
raw_items: RawItems::default(),
|
raw_items: RawItems::default(),
|
||||||
source_file_items: db.file_items(file_id.into()),
|
source_ast_id_map: db.ast_id_map(file_id.into()),
|
||||||
source_map: ImportSourceMap::default(),
|
source_map: ImportSourceMap::default(),
|
||||||
};
|
};
|
||||||
let source_file = db.hir_parse(file_id);
|
let source_file = db.hir_parse(file_id);
|
||||||
|
@ -167,7 +167,7 @@ pub(super) struct MacroData {
|
||||||
|
|
||||||
struct RawItemsCollector {
|
struct RawItemsCollector {
|
||||||
raw_items: RawItems,
|
raw_items: RawItems,
|
||||||
source_file_items: Arc<SourceFileItems>,
|
source_ast_id_map: Arc<AstIdMap>,
|
||||||
source_map: ImportSourceMap,
|
source_map: ImportSourceMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,25 +200,25 @@ impl RawItemsCollector {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::StructDef(it) => {
|
ast::ModuleItemKind::StructDef(it) => {
|
||||||
(DefKind::Struct(self.source_file_items.ast_id(it)), it.name())
|
(DefKind::Struct(self.source_ast_id_map.ast_id(it)), it.name())
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::EnumDef(it) => {
|
ast::ModuleItemKind::EnumDef(it) => {
|
||||||
(DefKind::Enum(self.source_file_items.ast_id(it)), it.name())
|
(DefKind::Enum(self.source_ast_id_map.ast_id(it)), it.name())
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::FnDef(it) => {
|
ast::ModuleItemKind::FnDef(it) => {
|
||||||
(DefKind::Function(self.source_file_items.ast_id(it)), it.name())
|
(DefKind::Function(self.source_ast_id_map.ast_id(it)), it.name())
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::TraitDef(it) => {
|
ast::ModuleItemKind::TraitDef(it) => {
|
||||||
(DefKind::Trait(self.source_file_items.ast_id(it)), it.name())
|
(DefKind::Trait(self.source_ast_id_map.ast_id(it)), it.name())
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::TypeAliasDef(it) => {
|
ast::ModuleItemKind::TypeAliasDef(it) => {
|
||||||
(DefKind::TypeAlias(self.source_file_items.ast_id(it)), it.name())
|
(DefKind::TypeAlias(self.source_ast_id_map.ast_id(it)), it.name())
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::ConstDef(it) => {
|
ast::ModuleItemKind::ConstDef(it) => {
|
||||||
(DefKind::Const(self.source_file_items.ast_id(it)), it.name())
|
(DefKind::Const(self.source_ast_id_map.ast_id(it)), it.name())
|
||||||
}
|
}
|
||||||
ast::ModuleItemKind::StaticDef(it) => {
|
ast::ModuleItemKind::StaticDef(it) => {
|
||||||
(DefKind::Static(self.source_file_items.ast_id(it)), it.name())
|
(DefKind::Static(self.source_ast_id_map.ast_id(it)), it.name())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Some(name) = name {
|
if let Some(name) = name {
|
||||||
|
@ -233,7 +233,7 @@ impl RawItemsCollector {
|
||||||
Some(it) => it.as_name(),
|
Some(it) => it.as_name(),
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
let ast_id = self.source_file_items.ast_id(module);
|
let ast_id = self.source_ast_id_map.ast_id(module);
|
||||||
if module.has_semi() {
|
if module.has_semi() {
|
||||||
let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id });
|
let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id });
|
||||||
self.push_item(current_module, RawItem::Module(item));
|
self.push_item(current_module, RawItem::Module(item));
|
||||||
|
@ -297,7 +297,7 @@ impl RawItemsCollector {
|
||||||
};
|
};
|
||||||
|
|
||||||
let name = m.name().map(|it| it.as_name());
|
let name = m.name().map(|it| it.as_name());
|
||||||
let ast_id = self.source_file_items.ast_id(m);
|
let ast_id = self.source_ast_id_map.ast_id(m);
|
||||||
let export = m.has_atom_attr("macro_export");
|
let export = m.has_atom_attr("macro_export");
|
||||||
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export });
|
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export });
|
||||||
self.push_item(current_module, RawItem::Macro(m));
|
self.push_item(current_module, RawItem::Macro(m));
|
||||||
|
|
|
@ -54,8 +54,8 @@ fn module_from_inline(
|
||||||
) -> Option<Module> {
|
) -> Option<Module> {
|
||||||
assert!(!module.has_semi());
|
assert!(!module.has_semi());
|
||||||
let file_id = file_id.into();
|
let file_id = file_id.into();
|
||||||
let file_items = db.file_items(file_id);
|
let ast_id_map = db.ast_id_map(file_id);
|
||||||
let item_id = file_items.ast_id(module).with_file_id(file_id);
|
let item_id = ast_id_map.ast_id(module).with_file_id(file_id);
|
||||||
module_from_source(db, file_id, Some(item_id))
|
module_from_source(db, file_id, Some(item_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,9 @@ use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast};
|
||||||
|
|
||||||
use crate::{HirFileId, DefDatabase};
|
use crate::{HirFileId, DefDatabase};
|
||||||
|
|
||||||
/// `AstId` points to an AST node in any file
|
/// `AstId` points to an AST node in any file.
|
||||||
|
///
|
||||||
|
/// It is stable across reparses, and can be used as salsa key/value.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct AstId<N: AstNode> {
|
pub(crate) struct AstId<N: AstNode> {
|
||||||
file_id: HirFileId,
|
file_id: HirFileId,
|
||||||
|
@ -37,9 +39,7 @@ impl<N: AstNode> AstId<N> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc<N> {
|
pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc<N> {
|
||||||
let source_item_id =
|
let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.raw);
|
||||||
SourceItemId { file_id: self.file_id(), item_id: self.file_ast_id.raw };
|
|
||||||
let syntax_node = db.file_item(source_item_id);
|
|
||||||
N::cast(&syntax_node).unwrap().to_owned()
|
N::cast(&syntax_node).unwrap().to_owned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ impl<N: AstNode> AstId<N> {
|
||||||
/// `AstId` points to an AST node in a specific file.
|
/// `AstId` points to an AST node in a specific file.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct FileAstId<N: AstNode> {
|
pub(crate) struct FileAstId<N: AstNode> {
|
||||||
raw: SourceFileItemId,
|
raw: ErasedFileAstId,
|
||||||
_ty: PhantomData<N>,
|
_ty: PhantomData<N>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,41 +76,29 @@ impl<N: AstNode> FileAstId<N> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Identifier of item within a specific file. This is stable over reparses, so
|
|
||||||
/// it's OK to use it as a salsa key/value.
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
struct SourceFileItemId(RawId);
|
pub struct ErasedFileAstId(RawId);
|
||||||
impl_arena_id!(SourceFileItemId);
|
impl_arena_id!(ErasedFileAstId);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
|
||||||
pub struct SourceItemId {
|
|
||||||
file_id: HirFileId,
|
|
||||||
item_id: SourceFileItemId,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back.
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct SourceFileItems {
|
pub struct AstIdMap {
|
||||||
arena: Arena<SourceFileItemId, SyntaxNodePtr>,
|
arena: Arena<ErasedFileAstId, SyntaxNodePtr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceFileItems {
|
impl AstIdMap {
|
||||||
pub(crate) fn file_items_query(
|
pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
|
||||||
db: &impl DefDatabase,
|
|
||||||
file_id: HirFileId,
|
|
||||||
) -> Arc<SourceFileItems> {
|
|
||||||
let source_file = db.hir_parse(file_id);
|
let source_file = db.hir_parse(file_id);
|
||||||
Arc::new(SourceFileItems::from_source_file(&source_file))
|
Arc::new(AstIdMap::from_source_file(&source_file))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn file_item_query(
|
pub(crate) fn file_item_query(
|
||||||
db: &impl DefDatabase,
|
db: &impl DefDatabase,
|
||||||
source_item_id: SourceItemId,
|
file_id: HirFileId,
|
||||||
|
ast_id: ErasedFileAstId,
|
||||||
) -> TreeArc<SyntaxNode> {
|
) -> TreeArc<SyntaxNode> {
|
||||||
let source_file = db.hir_parse(source_item_id.file_id);
|
let source_file = db.hir_parse(file_id);
|
||||||
db.file_items(source_item_id.file_id).arena[source_item_id.item_id]
|
db.ast_id_map(file_id).arena[ast_id].to_node(&source_file).to_owned()
|
||||||
.to_node(&source_file)
|
|
||||||
.to_owned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
|
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
|
||||||
|
@ -118,7 +106,7 @@ impl SourceFileItems {
|
||||||
let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) {
|
let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) {
|
||||||
Some((it, _)) => it,
|
Some((it, _)) => it,
|
||||||
None => panic!(
|
None => panic!(
|
||||||
"Can't find {:?} in SourceFileItems:\n{:?}",
|
"Can't find {:?} in AstIdMap:\n{:?}",
|
||||||
item.syntax(),
|
item.syntax(),
|
||||||
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
|
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
|
||||||
),
|
),
|
||||||
|
@ -127,8 +115,8 @@ impl SourceFileItems {
|
||||||
FileAstId { raw, _ty: PhantomData }
|
FileAstId { raw, _ty: PhantomData }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_source_file(source_file: &SourceFile) -> SourceFileItems {
|
fn from_source_file(source_file: &SourceFile) -> AstIdMap {
|
||||||
let mut res = SourceFileItems { arena: Arena::default() };
|
let mut res = AstIdMap { arena: Arena::default() };
|
||||||
// By walking the tree in bread-first order we make sure that parents
|
// By walking the tree in bread-first order we make sure that parents
|
||||||
// get lower ids then children. That is, adding a new child does not
|
// get lower ids then children. That is, adding a new child does not
|
||||||
// change parent's id. This means that, say, adding a new function to a
|
// change parent's id. This means that, say, adding a new function to a
|
||||||
|
@ -143,7 +131,7 @@ impl SourceFileItems {
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId {
|
fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId {
|
||||||
self.arena.alloc(SyntaxNodePtr::new(item))
|
self.arena.alloc(SyntaxNodePtr::new(item))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,8 +220,8 @@ impl RootDatabase {
|
||||||
self.query(ra_db::ParseQuery).sweep(sweep);
|
self.query(ra_db::ParseQuery).sweep(sweep);
|
||||||
|
|
||||||
self.query(hir::db::HirParseQuery).sweep(sweep);
|
self.query(hir::db::HirParseQuery).sweep(sweep);
|
||||||
self.query(hir::db::FileItemsQuery).sweep(sweep);
|
self.query(hir::db::AstIdMapQuery).sweep(sweep);
|
||||||
self.query(hir::db::FileItemQuery).sweep(sweep);
|
self.query(hir::db::AstIdToNodeQuery).sweep(sweep);
|
||||||
|
|
||||||
self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep);
|
self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep);
|
||||||
self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
|
self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue