Skip to content

Commit 81410ab

Browse files
committed
Cleanup FileId stuff
1 parent 02a3a94 commit 81410ab

File tree

5 files changed

+64
-48
lines changed

5 files changed

+64
-48
lines changed

crates/hir-expand/src/files.rs

+43-36
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,41 @@ impl<FileKind, L, R> InFileWrapper<FileKind, Either<L, R>> {
8383

8484
// endregion:transpose impls
8585

86-
// region:specific impls
86+
trait FileIdToSyntax: Copy {
87+
fn file_syntax(self, db: &dyn db::ExpandDatabase) -> SyntaxNode;
88+
}
8789

88-
impl<T> InFile<T> {
89-
pub fn file_syntax(&self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
90-
db.parse_or_expand(self.file_id)
90+
impl FileIdToSyntax for FileId {
91+
fn file_syntax(self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
92+
db.parse(self).syntax_node()
93+
}
94+
}
95+
impl FileIdToSyntax for MacroFileId {
96+
fn file_syntax(self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
97+
db.parse_macro_expansion(self).value.0.syntax_node()
98+
}
99+
}
100+
impl FileIdToSyntax for HirFileId {
101+
fn file_syntax(self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
102+
db.parse_or_expand(self)
91103
}
92104
}
93105

94-
impl<T> InRealFile<T> {
106+
#[allow(private_bounds)]
107+
impl<FileId: FileIdToSyntax, T> InFileWrapper<FileId, T> {
95108
pub fn file_syntax(&self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
96-
db.parse(self.file_id).syntax_node()
109+
FileIdToSyntax::file_syntax(self.file_id, db)
97110
}
98111
}
99112

100-
impl<T> InMacroFile<T> {
101-
pub fn file_syntax(&self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
102-
db.parse_macro_expansion(self.file_id).value.0.syntax_node()
113+
impl<FileId: Copy, N: AstNode> InFileWrapper<FileId, N> {
114+
pub fn syntax(&self) -> InFileWrapper<FileId, &SyntaxNode> {
115+
self.with_value(self.value.syntax())
103116
}
104117
}
105118

119+
// region:specific impls
120+
106121
impl InFile<&SyntaxNode> {
107122
pub fn ancestors_with_macros(
108123
self,
@@ -241,9 +256,15 @@ impl InFile<SyntaxToken> {
241256
match self.file_id.repr() {
242257
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() },
243258
HirFileIdRepr::MacroFile(mac_file) => {
244-
if let Some(res) = self.original_file_range_opt(db) {
245-
return res;
259+
let (range, ctxt) = ExpansionInfo::new(db, mac_file)
260+
.map_token_range_up(db, self.value.text_range());
261+
262+
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
263+
// keep pre-token map rewrite behaviour.
264+
if ctxt.is_root() {
265+
return range;
246266
}
267+
247268
// Fall back to whole macro call.
248269
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
249270
loc.kind.original_call_range(db)
@@ -257,8 +278,9 @@ impl InFile<SyntaxToken> {
257278
HirFileIdRepr::FileId(file_id) => {
258279
Some(FileRange { file_id, range: self.value.text_range() })
259280
}
260-
HirFileIdRepr::MacroFile(_) => {
261-
let (range, ctxt) = ascend_range_up_macros(db, self.map(|it| it.text_range()));
281+
HirFileIdRepr::MacroFile(mac_file) => {
282+
let (range, ctxt) = ExpansionInfo::new(db, mac_file)
283+
.map_token_range_up(db, self.value.text_range());
262284

263285
// FIXME: Figure out an API that makes proper use of ctx, this only exists to
264286
// keep pre-token map rewrite behaviour.
@@ -275,16 +297,19 @@ impl InFile<SyntaxToken> {
275297
impl InFile<TextRange> {
276298
/// Attempts to map the syntax node back up its macro calls.
277299
pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> FileRange {
278-
let (range, _ctxt) = ascend_range_up_macros(db, self);
300+
let (range, _ctxt) = match self.file_id.repr() {
301+
HirFileIdRepr::FileId(file_id) => {
302+
(FileRange { file_id, range: self.value }, SyntaxContextId::ROOT)
303+
}
304+
HirFileIdRepr::MacroFile(m) => {
305+
ExpansionInfo::new(db, m).map_token_range_up(db, self.value)
306+
}
307+
};
279308
range
280309
}
281310
}
282311

283312
impl<N: AstNode> InFile<N> {
284-
pub fn descendants<T: AstNode>(self) -> impl Iterator<Item = InFile<T>> {
285-
self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n))
286-
}
287-
288313
pub fn original_ast_node(self, db: &dyn db::ExpandDatabase) -> Option<InRealFile<N>> {
289314
// This kind of upmapping can only be achieved in attribute expanded files,
290315
// as we don't have node inputs otherwise and therefore can't find an `N` node in the input
@@ -312,22 +337,4 @@ impl<N: AstNode> InFile<N> {
312337
let value = anc.ancestors().find_map(N::cast)?;
313338
Some(InRealFile::new(file_id, value))
314339
}
315-
316-
pub fn syntax(&self) -> InFile<&SyntaxNode> {
317-
self.with_value(self.value.syntax())
318-
}
319-
}
320-
321-
fn ascend_range_up_macros(
322-
db: &dyn db::ExpandDatabase,
323-
range: InFile<TextRange>,
324-
) -> (FileRange, SyntaxContextId) {
325-
match range.file_id.repr() {
326-
HirFileIdRepr::FileId(file_id) => {
327-
(FileRange { file_id, range: range.value }, SyntaxContextId::ROOT)
328-
}
329-
HirFileIdRepr::MacroFile(m) => {
330-
ExpansionInfo::new(db, m).map_token_range_up(db, range.value)
331-
}
332-
}
333340
}

crates/hir-expand/src/lib.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,18 @@ pub enum MacroCallKind {
172172
}
173173

174174
pub trait HirFileIdExt {
175-
/// For macro-expansion files, returns the file original source file the
176-
/// expansion originated from.
175+
/// Returns the original file of this macro call hierarchy.
177176
fn original_file(self, db: &dyn db::ExpandDatabase) -> FileId;
178177

178+
/// Returns the original file of this macro call hierarchy while going into the included file if
179+
/// one of the calls comes from an `include!``.
179180
fn original_file_respecting_includes(self, db: &dyn db::ExpandDatabase) -> FileId;
180181

181182
/// If this is a macro call, returns the syntax node of the call.
182183
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>>;
183184

184185
/// If this is a macro call, returns the syntax node of the very first macro call this file resides in.
185-
fn original_call_node(self, db: &dyn db::ExpandDatabase) -> Option<(FileId, SyntaxNode)>;
186+
fn original_call_node(self, db: &dyn db::ExpandDatabase) -> Option<InRealFile<SyntaxNode>>;
186187

187188
/// Return expansion information if it is a macro-expansion file
188189
fn expansion_info(self, db: &dyn db::ExpandDatabase) -> Option<ExpansionInfo>;
@@ -246,11 +247,13 @@ impl HirFileIdExt for HirFileId {
246247
Some(loc.to_node(db))
247248
}
248249

249-
fn original_call_node(self, db: &dyn db::ExpandDatabase) -> Option<(FileId, SyntaxNode)> {
250+
fn original_call_node(self, db: &dyn db::ExpandDatabase) -> Option<InRealFile<SyntaxNode>> {
250251
let mut call = db.lookup_intern_macro_call(self.macro_file()?.macro_call_id).to_node(db);
251252
loop {
252253
match call.file_id.repr() {
253-
HirFileIdRepr::FileId(file_id) => break Some((file_id, call.value)),
254+
HirFileIdRepr::FileId(file_id) => {
255+
break Some(InRealFile { file_id, value: call.value })
256+
}
254257
HirFileIdRepr::MacroFile(MacroFileId { macro_call_id }) => {
255258
call = db.lookup_intern_macro_call(macro_call_id).to_node(db);
256259
}

crates/ide-assists/src/handlers/remove_unused_imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::{hash_map::Entry, HashMap};
22

3-
use hir::{HirFileIdExt, InFile, Module, ModuleSource};
3+
use hir::{HirFileIdExt, InFile, InRealFile, Module, ModuleSource};
44
use ide_db::{
55
base_db::FileRange,
66
defs::Definition,
@@ -167,7 +167,7 @@ fn used_once_in_scope(ctx: &AssistContext<'_>, def: Definition, scopes: &Vec<Sea
167167
fn module_search_scope(db: &RootDatabase, module: hir::Module) -> Vec<SearchScope> {
168168
let (file_id, range) = {
169169
let InFile { file_id, value } = module.definition_source(db);
170-
if let Some((file_id, call_source)) = file_id.original_call_node(db) {
170+
if let Some(InRealFile { file_id, value: call_source }) = file_id.original_call_node(db) {
171171
(file_id, Some(call_source.text_range()))
172172
} else {
173173
(

crates/ide-db/src/search.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::mem;
88

99
use base_db::{salsa::Database, FileId, FileRange, SourceDatabase, SourceDatabaseExt};
1010
use hir::{
11-
AsAssocItem, DefWithBody, HasAttrs, HasSource, HirFileIdExt, InFile, ModuleSource, Semantics,
12-
Visibility,
11+
AsAssocItem, DefWithBody, HasAttrs, HasSource, HirFileIdExt, InFile, InRealFile, ModuleSource,
12+
Semantics, Visibility,
1313
};
1414
use memchr::memmem::Finder;
1515
use nohash_hasher::IntMap;
@@ -133,7 +133,8 @@ impl SearchScope {
133133

134134
let (file_id, range) = {
135135
let InFile { file_id, value } = module.definition_source(db);
136-
if let Some((file_id, call_source)) = file_id.original_call_node(db) {
136+
if let Some(InRealFile { file_id, value: call_source }) = file_id.original_call_node(db)
137+
{
137138
(file_id, Some(call_source.text_range()))
138139
} else {
139140
(

crates/ide/src/navigation_target.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,13 @@ impl TryToNav for FileSymbol {
169169
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
170170
let full_range = self.loc.original_range(db);
171171
let focus_range = self.loc.original_name_range(db);
172-
let focus_range =
173-
if focus_range.file_id == full_range.file_id { Some(focus_range.range) } else { None };
172+
let focus_range = if focus_range.file_id == full_range.file_id
173+
&& full_range.range.contains_range(focus_range.range)
174+
{
175+
Some(focus_range.range)
176+
} else {
177+
None
178+
};
174179

175180
Some(NavigationTarget {
176181
file_id: full_range.file_id,

0 commit comments

Comments
 (0)