mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-03 21:25:25 +00:00
feat: support navigation on primitives
This commit is contained in:
parent
a53b44412d
commit
67a39886c3
3 changed files with 27 additions and 17 deletions
|
|
@ -363,7 +363,7 @@ impl Definition {
|
|||
}
|
||||
}
|
||||
|
||||
fn find_std_module(
|
||||
pub fn find_std_module(
|
||||
famous_defs: &FamousDefs<'_, '_>,
|
||||
name: &str,
|
||||
edition: Edition,
|
||||
|
|
|
|||
|
|
@ -6,12 +6,13 @@ use crate::{
|
|||
navigation_target::{self, ToNav},
|
||||
};
|
||||
use hir::{
|
||||
AsAssocItem, AssocItem, CallableKind, FileRange, HasCrate, InFile, ModuleDef, Semantics, sym,
|
||||
AsAssocItem, AssocItem, CallableKind, FileRange, HasCrate, InFile, ModuleDef, PathResolution,
|
||||
Semantics, sym,
|
||||
};
|
||||
use ide_db::{
|
||||
RootDatabase, SymbolKind,
|
||||
base_db::{AnchoredPath, SourceDatabase},
|
||||
defs::{Definition, IdentClass},
|
||||
defs::{Definition, IdentClass, find_std_module},
|
||||
famous_defs::FamousDefs,
|
||||
helpers::pick_best_token,
|
||||
};
|
||||
|
|
@ -90,6 +91,9 @@ pub(crate) fn goto_definition(
|
|||
if let Some(navs) = find_definition_for_known_blanket_dual_impls(sema, &token.value) {
|
||||
return Some(navs);
|
||||
}
|
||||
if let Some(navs) = find_definition_for_builtin_types(sema, &token.value, edition) {
|
||||
return Some(navs);
|
||||
}
|
||||
|
||||
let parent = token.value.parent()?;
|
||||
|
||||
|
|
@ -204,6 +208,25 @@ fn find_definition_for_known_blanket_dual_impls(
|
|||
Some(def_to_nav(sema.db, def))
|
||||
}
|
||||
|
||||
// If the token is a builtin type search the definition from the rustdoc module shims.
|
||||
fn find_definition_for_builtin_types(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
original_token: &SyntaxToken,
|
||||
edition: Edition,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let path = original_token.parent_ancestors().find_map(ast::Path::cast)?;
|
||||
let res = sema.resolve_path(&path)?;
|
||||
let PathResolution::Def(ModuleDef::BuiltinType(builtin)) = res else {
|
||||
return None;
|
||||
};
|
||||
|
||||
let fd = FamousDefs(sema, sema.scope(path.syntax())?.krate());
|
||||
let primitive_mod = format!("prim_{}", builtin.name().display(fd.0.db, edition));
|
||||
let doc_owner = find_std_module(&fd, &primitive_mod, edition)?;
|
||||
|
||||
Some(def_to_nav(sema.db, doc_owner.into()))
|
||||
}
|
||||
|
||||
fn try_lookup_include_path(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
token: InFile<ast::String>,
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use hir::{
|
|||
};
|
||||
use ide_db::{
|
||||
RootDatabase,
|
||||
defs::Definition,
|
||||
defs::{Definition, find_std_module},
|
||||
documentation::{DocsRangeMap, HasDocs},
|
||||
famous_defs::FamousDefs,
|
||||
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
|
||||
|
|
@ -1160,19 +1160,6 @@ fn markup(
|
|||
}
|
||||
}
|
||||
|
||||
fn find_std_module(
|
||||
famous_defs: &FamousDefs<'_, '_>,
|
||||
name: &str,
|
||||
edition: Edition,
|
||||
) -> Option<hir::Module> {
|
||||
let db = famous_defs.0.db;
|
||||
let std_crate = famous_defs.std()?;
|
||||
let std_root_module = std_crate.root_module();
|
||||
std_root_module.children(db).find(|module| {
|
||||
module.name(db).is_some_and(|module| module.display(db, edition).to_string() == name)
|
||||
})
|
||||
}
|
||||
|
||||
fn render_memory_layout(
|
||||
config: Option<MemoryLayoutHoverConfig>,
|
||||
layout: impl FnOnce() -> Result<Layout, LayoutError>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue