Skip to content

Commit 67a3988

Browse files
committed
feat: support navigation on primitives
1 parent a53b444 commit 67a3988

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

crates/ide-db/src/defs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl Definition {
363363
}
364364
}
365365

366-
fn find_std_module(
366+
pub fn find_std_module(
367367
famous_defs: &FamousDefs<'_, '_>,
368368
name: &str,
369369
edition: Edition,

crates/ide/src/goto_definition.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ use crate::{
66
navigation_target::{self, ToNav},
77
};
88
use hir::{
9-
AsAssocItem, AssocItem, CallableKind, FileRange, HasCrate, InFile, ModuleDef, Semantics, sym,
9+
AsAssocItem, AssocItem, CallableKind, FileRange, HasCrate, InFile, ModuleDef, PathResolution,
10+
Semantics, sym,
1011
};
1112
use ide_db::{
1213
RootDatabase, SymbolKind,
1314
base_db::{AnchoredPath, SourceDatabase},
14-
defs::{Definition, IdentClass},
15+
defs::{Definition, IdentClass, find_std_module},
1516
famous_defs::FamousDefs,
1617
helpers::pick_best_token,
1718
};
@@ -90,6 +91,9 @@ pub(crate) fn goto_definition(
9091
if let Some(navs) = find_definition_for_known_blanket_dual_impls(sema, &token.value) {
9192
return Some(navs);
9293
}
94+
if let Some(navs) = find_definition_for_builtin_types(sema, &token.value, edition) {
95+
return Some(navs);
96+
}
9397

9498
let parent = token.value.parent()?;
9599

@@ -204,6 +208,25 @@ fn find_definition_for_known_blanket_dual_impls(
204208
Some(def_to_nav(sema.db, def))
205209
}
206210

211+
// If the token is a builtin type search the definition from the rustdoc module shims.
212+
fn find_definition_for_builtin_types(
213+
sema: &Semantics<'_, RootDatabase>,
214+
original_token: &SyntaxToken,
215+
edition: Edition,
216+
) -> Option<Vec<NavigationTarget>> {
217+
let path = original_token.parent_ancestors().find_map(ast::Path::cast)?;
218+
let res = sema.resolve_path(&path)?;
219+
let PathResolution::Def(ModuleDef::BuiltinType(builtin)) = res else {
220+
return None;
221+
};
222+
223+
let fd = FamousDefs(sema, sema.scope(path.syntax())?.krate());
224+
let primitive_mod = format!("prim_{}", builtin.name().display(fd.0.db, edition));
225+
let doc_owner = find_std_module(&fd, &primitive_mod, edition)?;
226+
227+
Some(def_to_nav(sema.db, doc_owner.into()))
228+
}
229+
207230
fn try_lookup_include_path(
208231
sema: &Semantics<'_, RootDatabase>,
209232
token: InFile<ast::String>,

crates/ide/src/hover/render.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir::{
1010
};
1111
use ide_db::{
1212
RootDatabase,
13-
defs::Definition,
13+
defs::{Definition, find_std_module},
1414
documentation::{DocsRangeMap, HasDocs},
1515
famous_defs::FamousDefs,
1616
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
@@ -1160,19 +1160,6 @@ fn markup(
11601160
}
11611161
}
11621162

1163-
fn find_std_module(
1164-
famous_defs: &FamousDefs<'_, '_>,
1165-
name: &str,
1166-
edition: Edition,
1167-
) -> Option<hir::Module> {
1168-
let db = famous_defs.0.db;
1169-
let std_crate = famous_defs.std()?;
1170-
let std_root_module = std_crate.root_module();
1171-
std_root_module.children(db).find(|module| {
1172-
module.name(db).is_some_and(|module| module.display(db, edition).to_string() == name)
1173-
})
1174-
}
1175-
11761163
fn render_memory_layout(
11771164
config: Option<MemoryLayoutHoverConfig>,
11781165
layout: impl FnOnce() -> Result<Layout, LayoutError>,

0 commit comments

Comments
 (0)