mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 22:31:43 +00:00
Check Fileid in SourceFileMap
This commit is contained in:
parent
13100da7a2
commit
159525b120
5 changed files with 23 additions and 7 deletions
|
@ -110,16 +110,32 @@ pub struct SourceItemId {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
|
/// Maps item's `SyntaxNode`s to `SourceFileItemId` and back.
|
||||||
#[derive(Debug, PartialEq, Eq, Default)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct SourceFileItems {
|
pub struct SourceFileItems {
|
||||||
|
file_id: FileId,
|
||||||
arena: Arena<SyntaxNode>,
|
arena: Arena<SyntaxNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SourceFileItems {
|
impl SourceFileItems {
|
||||||
|
fn new(file_id: FileId) -> SourceFileItems {
|
||||||
|
SourceFileItems {
|
||||||
|
file_id,
|
||||||
|
arena: Arena::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
|
fn alloc(&mut self, item: SyntaxNode) -> SourceFileItemId {
|
||||||
self.arena.alloc(item)
|
self.arena.alloc(item)
|
||||||
}
|
}
|
||||||
pub fn id_of(&self, item: SyntaxNodeRef) -> SourceFileItemId {
|
pub fn id_of(&self, file_id: FileId, item: SyntaxNodeRef) -> SourceFileItemId {
|
||||||
|
assert_eq!(
|
||||||
|
self.file_id, file_id,
|
||||||
|
"SourceFileItems: wrong file, expected {:?}, got {:?}",
|
||||||
|
self.file_id, file_id
|
||||||
|
);
|
||||||
|
self.id_of_unchecked(item)
|
||||||
|
}
|
||||||
|
fn id_of_unchecked(&self, item: SyntaxNodeRef) -> SourceFileItemId {
|
||||||
let (id, _item) = self
|
let (id, _item) = self
|
||||||
.arena
|
.arena
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -280,7 +280,7 @@ impl ModuleSource {
|
||||||
) -> ModuleSource {
|
) -> ModuleSource {
|
||||||
assert!(!m.has_semi());
|
assert!(!m.has_semi());
|
||||||
let file_items = db.file_items(file_id);
|
let file_items = db.file_items(file_id);
|
||||||
let item_id = file_items.id_of(m.syntax());
|
let item_id = file_items.id_of(file_id, m.syntax());
|
||||||
ModuleSource::new(file_id, item_id)
|
ModuleSource::new(file_id, item_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -168,7 +168,7 @@ impl InputModuleItems {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) {
|
fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) {
|
||||||
let file_item_id = file_items.id_of(item.syntax());
|
let file_item_id = file_items.id_of_unchecked(item.syntax());
|
||||||
let start_offset = item.syntax().range().start();
|
let start_offset = item.syntax().range().start();
|
||||||
Path::expand_use_item(item, |path, range| {
|
Path::expand_use_item(item, |path, range| {
|
||||||
let kind = match range {
|
let kind = match range {
|
||||||
|
@ -188,7 +188,7 @@ impl ModuleItem {
|
||||||
let name = item.name()?.text();
|
let name = item.name()?.text();
|
||||||
let kind = item.syntax().kind();
|
let kind = item.syntax().kind();
|
||||||
let vis = Vis::Other;
|
let vis = Vis::Other;
|
||||||
let id = file_items.id_of(item.syntax());
|
let id = file_items.id_of_unchecked(item.syntax());
|
||||||
let res = ModuleItem {
|
let res = ModuleItem {
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
|
|
|
@ -36,7 +36,7 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc<FnScopes> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> {
|
pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> {
|
||||||
let mut res = SourceFileItems::default();
|
let mut res = SourceFileItems::new(file_id);
|
||||||
let source_file = db.source_file(file_id);
|
let source_file = db.source_file(file_id);
|
||||||
res.alloc(source_file.syntax().owned());
|
res.alloc(source_file.syntax().owned());
|
||||||
let source_file = source_file.borrowed();
|
let source_file = source_file.borrowed();
|
||||||
|
|
|
@ -75,7 +75,7 @@ pub fn function_from_source(
|
||||||
) -> Cancelable<Option<Function>> {
|
) -> Cancelable<Option<Function>> {
|
||||||
let module = ctry!(module_from_child_node(db, file_id, fn_def.syntax())?);
|
let module = ctry!(module_from_child_node(db, file_id, fn_def.syntax())?);
|
||||||
let file_items = db.file_items(file_id);
|
let file_items = db.file_items(file_id);
|
||||||
let item_id = file_items.id_of(fn_def.syntax());
|
let item_id = file_items.id_of(file_id, fn_def.syntax());
|
||||||
let source_item_id = SourceItemId { file_id, item_id };
|
let source_item_id = SourceItemId { file_id, item_id };
|
||||||
let def_loc = DefLoc {
|
let def_loc = DefLoc {
|
||||||
kind: DefKind::Function,
|
kind: DefKind::Function,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue