Allow navigation targets to be duplicated when the focus range lies in the macro definition site

This commit is contained in:
Lukas Wirth 2023-12-06 11:53:28 +01:00
parent 9b7ec5e31b
commit 9cb13b6efb
38 changed files with 851 additions and 488 deletions

1
Cargo.lock generated
View File

@ -608,6 +608,7 @@ dependencies = [
name = "ide"
version = "0.0.0"
dependencies = [
"arrayvec",
"cfg",
"cov-mark",
"crossbeam-channel",

View File

@ -135,7 +135,7 @@ pub fn parse_with_proc_macros(
let mut file_set = FileSet::default();
let mut current_source_root_kind = SourceRootKind::Local;
let mut file_id = FileId(0);
let mut file_id = FileId::from_raw(0);
let mut roots = Vec::new();
let mut file_position = None;
@ -210,7 +210,7 @@ pub fn parse_with_proc_macros(
let path = VfsPath::new_virtual_path(meta.path);
file_set.insert(file_id, path);
files.push(file_id);
file_id.0 += 1;
file_id = FileId::from_raw(file_id.index() + 1);
}
if crates.is_empty() {
@ -255,7 +255,7 @@ pub fn parse_with_proc_macros(
if let Some(mini_core) = mini_core {
let core_file = file_id;
file_id.0 += 1;
file_id = FileId::from_raw(file_id.index() + 1);
let mut fs = FileSet::default();
fs.insert(core_file, VfsPath::new_virtual_path("/sysroot/core/lib.rs".to_string()));
@ -296,7 +296,6 @@ pub fn parse_with_proc_macros(
let mut proc_macros = ProcMacros::default();
if !proc_macro_names.is_empty() {
let proc_lib_file = file_id;
file_id.0 += 1;
proc_macro_defs.extend(default_test_proc_macros());
let (proc_macro, source) = filter_test_proc_macros(&proc_macro_names, proc_macro_defs);

View File

@ -880,7 +880,7 @@ mod tests {
fn detect_cyclic_dependency_indirect() {
let mut graph = CrateGraph::default();
let crate1 = graph.add_crate_root(
FileId(1u32),
FileId::from_raw(1u32),
Edition2018,
None,
None,
@ -893,7 +893,7 @@ fn detect_cyclic_dependency_indirect() {
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
FileId::from_raw(2u32),
Edition2018,
None,
None,
@ -906,7 +906,7 @@ fn detect_cyclic_dependency_indirect() {
None,
);
let crate3 = graph.add_crate_root(
FileId(3u32),
FileId::from_raw(3u32),
Edition2018,
None,
None,
@ -942,7 +942,7 @@ fn detect_cyclic_dependency_indirect() {
fn detect_cyclic_dependency_direct() {
let mut graph = CrateGraph::default();
let crate1 = graph.add_crate_root(
FileId(1u32),
FileId::from_raw(1u32),
Edition2018,
None,
None,
@ -955,7 +955,7 @@ fn detect_cyclic_dependency_direct() {
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
FileId::from_raw(2u32),
Edition2018,
None,
None,
@ -985,7 +985,7 @@ fn detect_cyclic_dependency_direct() {
fn it_works() {
let mut graph = CrateGraph::default();
let crate1 = graph.add_crate_root(
FileId(1u32),
FileId::from_raw(1u32),
Edition2018,
None,
None,
@ -998,7 +998,7 @@ fn it_works() {
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
FileId::from_raw(2u32),
Edition2018,
None,
None,
@ -1011,7 +1011,7 @@ fn it_works() {
None,
);
let crate3 = graph.add_crate_root(
FileId(3u32),
FileId::from_raw(3u32),
Edition2018,
None,
None,
@ -1041,7 +1041,7 @@ fn it_works() {
fn dashes_are_normalized() {
let mut graph = CrateGraph::default();
let crate1 = graph.add_crate_root(
FileId(1u32),
FileId::from_raw(1u32),
Edition2018,
None,
None,
@ -1054,7 +1054,7 @@ fn dashes_are_normalized() {
None,
);
let crate2 = graph.add_crate_root(
FileId(2u32),
FileId::from_raw(2u32),
Edition2018,
None,
None,

View File

@ -70,7 +70,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
impl tt::SpanAnchor for SpanAnchor {
const DUMMY: Self = SpanAnchor { file_id: FileId(0), ast_id: ROOT_ERASED_FILE_AST_ID };
const DUMMY: Self = SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID };
}
/// Input to the analyzer is a set of files, where each file is identified by
@ -99,12 +99,6 @@ fn from(value: HirFileId) -> Self {
}
}
impl From<u32> for HirFileId {
fn from(value: u32) -> Self {
HirFileId(value)
}
}
impl From<MacroCallId> for HirFileId {
fn from(value: MacroCallId) -> Self {
value.as_file()
@ -147,7 +141,7 @@ pub enum HirFileIdRepr {
impl fmt::Debug for HirFileIdRepr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::FileId(arg0) => f.debug_tuple("FileId").field(&arg0.0).finish(),
Self::FileId(arg0) => f.debug_tuple("FileId").field(&arg0.index()).finish(),
Self::MacroFile(arg0) => {
f.debug_tuple("MacroFile").field(&arg0.macro_call_id.0).finish()
}
@ -156,9 +150,9 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
impl From<FileId> for HirFileId {
fn from(FileId(id): FileId) -> Self {
assert!(id < Self::MAX_FILE_ID);
HirFileId(id)
fn from(id: FileId) -> Self {
assert!(id.index() < Self::MAX_FILE_ID);
HirFileId(id.index())
}
}
@ -192,7 +186,7 @@ pub fn macro_file(self) -> Option<MacroFileId> {
#[inline]
pub fn file_id(self) -> Option<FileId> {
match self.0 & Self::MACRO_FILE_TAG_MASK {
0 => Some(FileId(self.0)),
0 => Some(FileId::from_raw(self.0)),
_ => None,
}
}
@ -200,7 +194,7 @@ pub fn file_id(self) -> Option<FileId> {
#[inline]
pub fn repr(self) -> HirFileIdRepr {
match self.0 & Self::MACRO_FILE_TAG_MASK {
0 => HirFileIdRepr::FileId(FileId(self.0)),
0 => HirFileIdRepr::FileId(FileId::from_raw(self.0)),
_ => HirFileIdRepr::MacroFile(MacroFileId {
macro_call_id: MacroCallId(InternId::from(self.0 ^ Self::MACRO_FILE_TAG_MASK)),
}),

View File

@ -13,7 +13,7 @@ fn assert_parse_result(input: &str, expected: DocExpr) {
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
let tt = syntax_node_to_token_tree(
tt.syntax(),
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId(0))),
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::from_raw(0))),
);
let cfg = DocExpr::parse(&tt);
assert_eq!(cfg, expected);

View File

@ -524,7 +524,7 @@ fn file_id_and_params_of(
(src.file_id, src.value.generic_param_list())
}
// We won't be using this ID anyway
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => (FileId(!0).into(), None),
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => (FileId::BOGUS.into(), None),
}
}

View File

@ -307,6 +307,40 @@ pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> FileRange {
};
range
}
pub fn original_node_file_range(
self,
db: &dyn db::ExpandDatabase,
) -> (FileRange, SyntaxContextId) {
match self.file_id.repr() {
HirFileIdRepr::FileId(file_id) => {
(FileRange { file_id, range: self.value }, SyntaxContextId::ROOT)
}
HirFileIdRepr::MacroFile(mac_file) => {
match ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value) {
Some(it) => it,
None => {
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
(loc.kind.original_call_range(db), SyntaxContextId::ROOT)
}
}
}
}
}
pub fn original_node_file_range_opt(
self,
db: &dyn db::ExpandDatabase,
) -> Option<(FileRange, SyntaxContextId)> {
match self.file_id.repr() {
HirFileIdRepr::FileId(file_id) => {
Some((FileRange { file_id, range: self.value }, SyntaxContextId::ROOT))
}
HirFileIdRepr::MacroFile(mac_file) => {
ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value)
}
}
}
}
impl<N: AstNode> InFile<N> {

View File

@ -54,8 +54,10 @@ pub(crate) fn fixup_syntax(span_map: SpanMapRef<'_>, node: &SyntaxNode) -> Synta
let dummy_range = TextRange::empty(TextSize::new(0));
// we use a file id of `FileId(!0)` to signal a fake node, and the text range's start offset as
// the index into the replacement vec but only if the end points to !0
let dummy_anchor =
SpanAnchor { file_id: FileId(!0), ast_id: ErasedFileAstId::from_raw(RawIdx::from(!0)) };
let dummy_anchor = SpanAnchor {
file_id: FileId::from_raw(!0),
ast_id: ErasedFileAstId::from_raw(RawIdx::from(!0)),
};
let fake_span = |range| SpanData {
range: dummy_range,
anchor: dummy_anchor,
@ -308,7 +310,7 @@ fn reverse_fixups_(tt: &mut Subtree, undo_info: &[Subtree]) {
.filter(|tt| match tt {
tt::TokenTree::Leaf(leaf) => {
let span = leaf.span();
span.anchor.file_id != FileId(!0) || span.range.end() == TextSize::new(!0)
span.anchor.file_id != FileId::from_raw(!0) || span.range.end() == TextSize::new(!0)
}
tt::TokenTree::Subtree(_) => true,
})
@ -318,7 +320,7 @@ fn reverse_fixups_(tt: &mut Subtree, undo_info: &[Subtree]) {
SmallVec::from_const([tt.into()])
}
tt::TokenTree::Leaf(leaf) => {
if leaf.span().anchor.file_id == FileId(!0) {
if leaf.span().anchor.file_id == FileId::from_raw(!0) {
let original = undo_info[u32::from(leaf.span().range.start()) as usize].clone();
if original.delimiter.kind == tt::DelimiterKind::Invisible {
original.token_trees.into()
@ -373,7 +375,7 @@ fn check_tt_eq(a: &tt::TokenTree, b: &tt::TokenTree) -> bool {
#[track_caller]
fn check(ra_fixture: &str, mut expect: Expect) {
let parsed = syntax::SourceFile::parse(ra_fixture);
let span_map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId(0))));
let span_map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
let fixups = super::fixup_syntax(span_map.as_ref(), &parsed.syntax_node());
let mut tt = mbe::syntax_node_to_token_tree_modified(
&parsed.syntax_node(),

View File

@ -197,6 +197,7 @@ pub trait SyntaxContextExt {
fn normalize_to_macro_rules(self, db: &dyn ExpandDatabase) -> Self;
fn normalize_to_macros_2_0(self, db: &dyn ExpandDatabase) -> Self;
fn parent_ctxt(self, db: &dyn ExpandDatabase) -> Self;
fn remove_mark(&mut self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency);
fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency);
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)>;
}
@ -223,6 +224,11 @@ fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparen
let data = db.lookup_intern_syntax_context(self);
(data.outer_expn, data.outer_transparency)
}
fn remove_mark(&mut self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency) {
let data = db.lookup_intern_syntax_context(*self);
*self = data.parent;
(data.outer_expn, data.outer_transparency)
}
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)> {
let mut marks = marks_rev(self, db).collect::<Vec<_>>();
marks.reverse();

View File

@ -380,6 +380,21 @@ pub fn as_lazy_macro(
db.intern_macro_call(MacroCallLoc { def: self, krate, eager: None, kind, call_site })
}
pub fn definition_range(&self, db: &dyn db::ExpandDatabase) -> InFile<TextRange> {
match self.kind {
MacroDefKind::Declarative(id)
| MacroDefKind::BuiltIn(_, id)
| MacroDefKind::BuiltInAttr(_, id)
| MacroDefKind::BuiltInDerive(_, id)
| MacroDefKind::BuiltInEager(_, id) => {
id.with_value(db.ast_id_map(id.file_id).get(id.value).text_range())
}
MacroDefKind::ProcMacro(_, _, id) => {
id.with_value(db.ast_id_map(id.file_id).get(id.value).text_range())
}
}
}
pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
match self.kind {
MacroDefKind::ProcMacro(.., id) => return Either::Right(id),

View File

@ -261,8 +261,8 @@ fn test_quote_hash_token_tree() {
assert_eq!(quoted.to_string(), "hello");
let t = format!("{quoted:?}");
expect![[r#"
SUBTREE $$ SpanData { range: 0..0, anchor: SpanAnchor(FileId(4294967295), 0), ctx: SyntaxContextId(0) } SpanData { range: 0..0, anchor: SpanAnchor(FileId(4294967295), 0), ctx: SyntaxContextId(0) }
IDENT hello SpanData { range: 0..0, anchor: SpanAnchor(FileId(4294967295), 0), ctx: SyntaxContextId(0) }"#]].assert_eq(&t);
SUBTREE $$ SpanData { range: 0..0, anchor: SpanAnchor(FileId(937550), 0), ctx: SyntaxContextId(0) } SpanData { range: 0..0, anchor: SpanAnchor(FileId(937550), 0), ctx: SyntaxContextId(0) }
IDENT hello SpanData { range: 0..0, anchor: SpanAnchor(FileId(937550), 0), ctx: SyntaxContextId(0) }"#]].assert_eq(&t);
}
#[test]

View File

@ -241,7 +241,7 @@ fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
ModPath::from_src(
db.upcast(),
ast_path,
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId(0))),
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::BOGUS)),
)
};

View File

@ -125,6 +125,7 @@
},
hir_expand::{
attrs::{Attr, AttrId},
hygiene::{marks_rev, SyntaxContextExt},
name::{known, Name},
tt, ExpandResult, HirFileId, HirFileIdExt, InFile, InMacroFile, InRealFile, MacroFileId,
},

View File

@ -9,7 +9,7 @@
};
use hir_expand::{HirFileId, InFile};
use hir_ty::db::HirDatabase;
use syntax::{ast::HasName, AstNode, SmolStr, SyntaxNode, SyntaxNodePtr};
use syntax::{ast::HasName, AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr};
use crate::{Module, ModuleDef, Semantics};
@ -32,7 +32,7 @@ pub struct DeclarationLocation {
/// This points to the whole syntax node of the declaration.
pub ptr: SyntaxNodePtr,
/// This points to the [`syntax::ast::Name`] identifier of the declaration.
pub name_ptr: SyntaxNodePtr,
pub name_ptr: AstPtr<syntax::ast::Name>,
}
impl DeclarationLocation {
@ -185,7 +185,7 @@ fn collect_from_module(&mut self, module_id: ModuleId) {
let dec_loc = DeclarationLocation {
hir_file_id: source.file_id,
ptr: SyntaxNodePtr::new(use_tree_src.syntax()),
name_ptr: SyntaxNodePtr::new(name.syntax()),
name_ptr: AstPtr::new(&name),
};
self.symbols.push(FileSymbol {
@ -289,7 +289,7 @@ fn push_decl<L>(&mut self, id: L)
let dec_loc = DeclarationLocation {
hir_file_id: source.file_id,
ptr: SyntaxNodePtr::new(source.value.syntax()),
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
name_ptr: AstPtr::new(&name_node),
};
if let Some(attrs) = def.attrs(self.db) {
@ -322,7 +322,7 @@ fn push_module(&mut self, module_id: ModuleId) {
let dec_loc = DeclarationLocation {
hir_file_id: declaration.file_id,
ptr: SyntaxNodePtr::new(module.syntax()),
name_ptr: SyntaxNodePtr::new(name_node.syntax()),
name_ptr: AstPtr::new(&name_node),
};
let def = ModuleDef::Module(module_id.into());

View File

@ -27,10 +27,12 @@
kind: STRUCT,
range: 83..119,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 109..118,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 109..118,
},
),
},
container_name: None,
is_alias: false,
@ -54,10 +56,12 @@
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
),
},
container_name: None,
is_alias: false,
@ -81,10 +85,12 @@
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
),
},
container_name: None,
is_alias: true,
@ -108,10 +114,12 @@
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
),
},
container_name: None,
is_alias: true,
@ -135,10 +143,12 @@
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
),
},
container_name: None,
is_alias: true,
@ -162,10 +172,12 @@
kind: STRUCT,
range: 83..119,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 109..118,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 109..118,
},
),
},
container_name: None,
is_alias: true,
@ -189,10 +201,12 @@
kind: STRUCT,
range: 0..81,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 74..80,
},
),
},
container_name: None,
is_alias: true,

View File

@ -25,10 +25,12 @@
kind: TYPE_ALIAS,
range: 397..417,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 402..407,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 402..407,
},
),
},
container_name: None,
is_alias: false,
@ -50,10 +52,12 @@
kind: CONST,
range: 340..361,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 346..351,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 346..351,
},
),
},
container_name: None,
is_alias: false,
@ -75,10 +79,12 @@
kind: CONST,
range: 520..592,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 526..542,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 526..542,
},
),
},
container_name: None,
is_alias: false,
@ -102,10 +108,12 @@
kind: ENUM,
range: 185..207,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 190..194,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 190..194,
},
),
},
container_name: None,
is_alias: false,
@ -129,10 +137,12 @@
kind: USE_TREE,
range: 654..676,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 663..676,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 663..676,
},
),
},
container_name: None,
is_alias: false,
@ -156,10 +166,12 @@
kind: MACRO_DEF,
range: 153..168,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 159..164,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 159..164,
},
),
},
container_name: None,
is_alias: false,
@ -181,10 +193,12 @@
kind: STATIC,
range: 362..396,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 369..375,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 369..375,
},
),
},
container_name: None,
is_alias: false,
@ -208,10 +222,12 @@
kind: STRUCT,
range: 170..184,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 177..183,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 177..183,
},
),
},
container_name: None,
is_alias: false,
@ -235,10 +251,12 @@
kind: STRUCT,
range: 0..22,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 6..21,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 6..21,
},
),
},
container_name: None,
is_alias: false,
@ -262,10 +280,12 @@
kind: STRUCT,
range: 318..336,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 325..335,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 325..335,
},
),
},
container_name: Some(
"main",
@ -291,10 +311,12 @@
kind: STRUCT,
range: 555..581,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 562..580,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 562..580,
},
),
},
container_name: Some(
"CONST_WITH_INNER",
@ -320,10 +342,12 @@
kind: STRUCT,
range: 479..507,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 486..506,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 486..506,
},
),
},
container_name: None,
is_alias: false,
@ -345,10 +369,12 @@
kind: TRAIT,
range: 261..300,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 267..272,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 267..272,
},
),
},
container_name: None,
is_alias: false,
@ -372,10 +398,12 @@
kind: USE_TREE,
range: 682..696,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 691..696,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 691..696,
},
),
},
container_name: None,
is_alias: false,
@ -399,10 +427,12 @@
kind: UNION,
range: 208..222,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 214..219,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 214..219,
},
),
},
container_name: None,
is_alias: false,
@ -426,10 +456,12 @@
kind: MODULE,
range: 419..457,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 423..428,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 423..428,
},
),
},
container_name: None,
is_alias: false,
@ -453,10 +485,12 @@
kind: MODULE,
range: 594..604,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 598..603,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 598..603,
},
),
},
container_name: None,
is_alias: false,
@ -480,10 +514,12 @@
kind: MACRO_RULES,
range: 51..131,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 64..77,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 64..77,
},
),
},
container_name: None,
is_alias: false,
@ -505,10 +541,12 @@
kind: FN,
range: 242..257,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 245..252,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 245..252,
},
),
},
container_name: None,
is_alias: false,
@ -532,10 +570,12 @@
kind: MACRO_RULES,
range: 1..48,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 14..31,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 14..31,
},
),
},
container_name: None,
is_alias: false,
@ -557,10 +597,12 @@
kind: FN,
range: 302..338,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 305..309,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 305..309,
},
),
},
container_name: None,
is_alias: false,
@ -584,10 +626,12 @@
kind: USE_TREE,
range: 611..648,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 628..648,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 628..648,
},
),
},
container_name: None,
is_alias: false,
@ -609,10 +653,12 @@
kind: FN,
range: 279..298,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 282..290,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 282..290,
},
),
},
container_name: Some(
"Trait",
@ -649,10 +695,12 @@
kind: STRUCT,
range: 435..455,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 442..454,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 442..454,
},
),
},
container_name: None,
is_alias: false,
@ -687,10 +735,12 @@
kind: USE_TREE,
range: 111..143,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 127..143,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 127..143,
},
),
},
container_name: None,
is_alias: false,
@ -714,10 +764,12 @@
kind: STRUCT,
range: 0..20,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 7..19,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 7..19,
},
),
},
container_name: None,
is_alias: false,
@ -741,10 +793,12 @@
kind: USE_TREE,
range: 25..59,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 41..59,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 41..59,
},
),
},
container_name: None,
is_alias: false,
@ -768,10 +822,12 @@
kind: USE_TREE,
range: 65..105,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 95..105,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 95..105,
},
),
},
container_name: None,
is_alias: false,
@ -795,10 +851,12 @@
kind: USE_TREE,
range: 65..105,
},
name_ptr: SyntaxNodePtr {
kind: NAME,
range: 95..105,
},
name_ptr: AstPtr(
SyntaxNodePtr {
kind: NAME,
range: 95..105,
},
),
},
container_name: None,
is_alias: false,

View File

@ -14,6 +14,7 @@ doctest = false
[dependencies]
cov-mark = "2.0.0-pre.1"
crossbeam-channel = "0.5.5"
arrayvec = "0.7.4"
either.workspace = true
itertools.workspace = true
tracing.workspace = true

View File

@ -1,5 +1,7 @@
//! Entry point for call-hierarchy
use std::iter;
use hir::{DescendPreference, Semantics};
use ide_db::{
defs::{Definition, NameClass, NameRefClass},
@ -66,7 +68,10 @@ pub(crate) fn incoming_calls(
def.try_to_nav(sema.db)
});
if let Some(nav) = nav {
calls.add(nav, sema.original_range(name.syntax()).range);
calls.add(nav.call_site, sema.original_range(name.syntax()).range);
if let Some(other) = nav.def_site {
calls.add(other, sema.original_range(name.syntax()).range);
}
}
}
}
@ -117,8 +122,9 @@ pub(crate) fn outgoing_calls(
function.try_to_nav(db).zip(Some(range))
}
}?;
Some((nav_target, range))
Some(nav_target.into_iter().zip(iter::repeat(range)))
})
.flatten()
.for_each(|(nav, range)| calls.add(nav, range));
Some(calls.into_items())

View File

@ -1,4 +1,4 @@
use std::ffi::OsStr;
use std::{ffi::OsStr, iter};
use expect_test::{expect, Expect};
use hir::Semantics;
@ -63,10 +63,12 @@ fn check_doc_links(ra_fixture: &str) {
let defs = extract_definitions_from_docs(&docs);
let actual: Vec<_> = defs
.into_iter()
.map(|(_, link, ns)| {
.flat_map(|(_, link, ns)| {
let def = resolve_doc_path_for_def(sema.db, cursor_def, &link, ns)
.unwrap_or_else(|| panic!("Failed to resolve {link}"));
let nav_target = def.try_to_nav(sema.db).unwrap();
def.try_to_nav(sema.db).unwrap().into_iter().zip(iter::repeat(link))
})
.map(|(nav_target, link)| {
let range =
FileRange { file_id: nav_target.file_id, range: nav_target.focus_or_full_range() };
(range, link)

View File

@ -66,6 +66,7 @@ pub(crate) fn goto_declaration(
let item = trait_.items(db).into_iter().find(|it| it.name(db) == name)?;
item.try_to_nav(db)
})
.flatten()
.collect();
if info.is_empty() {

View File

@ -52,7 +52,7 @@ pub(crate) fn goto_definition(
if let Some(doc_comment) = token_as_doc_comment(&original_token) {
return doc_comment.get_definition_with_descend_at(sema, offset, |def, _, link_range| {
let nav = def.try_to_nav(db)?;
Some(RangeInfo::new(link_range, vec![nav]))
Some(RangeInfo::new(link_range, nav.collect()))
});
}
@ -88,6 +88,7 @@ pub(crate) fn goto_definition(
.resolved_crate(db)
.map(|it| it.root_module().to_nav(sema.db))
.into_iter()
.flatten()
.collect();
}
try_filter_trait_item_definition(sema, &def)
@ -138,6 +139,7 @@ fn try_lookup_include_path(
docs: None,
})
}
/// finds the trait definition of an impl'd item, except function
/// e.g.
/// ```rust
@ -166,13 +168,13 @@ fn try_filter_trait_item_definition(
.iter()
.filter(|itm| discriminant(*itm) == discri_value)
.find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(db)).flatten())
.map(|it| vec![it])
.map(|it| it.collect())
}
}
}
fn def_to_nav(db: &RootDatabase, def: Definition) -> Vec<NavigationTarget> {
def.try_to_nav(db).map(|it| vec![it]).unwrap_or_default()
def.try_to_nav(db).map(|it| it.collect()).unwrap_or_default()
}
#[cfg(test)]
@ -405,8 +407,6 @@ fn bar() {
);
}
// FIXME: We should emit two targets here, one for the identifier in the declaration, one for
// the macro call
#[test]
fn goto_def_for_macro_defined_fn_no_arg() {
check(
@ -414,7 +414,7 @@ fn goto_def_for_macro_defined_fn_no_arg() {
//- /lib.rs
macro_rules! define_fn {
() => (fn foo() {})
//^^^
}
define_fn!();
@ -1748,9 +1748,9 @@ macro_rules! foo {
fn $ident(Foo { $ident }: Foo) {}
}
}
foo!(foo$0);
//^^^
//^^^
foo!(foo$0);
//^^^
//^^^
"#,
);
check(

View File

@ -82,7 +82,11 @@ pub(crate) fn goto_implementation(
}
fn impls_for_ty(sema: &Semantics<'_, RootDatabase>, ty: hir::Type) -> Vec<NavigationTarget> {
Impl::all_for_type(sema.db, ty).into_iter().filter_map(|imp| imp.try_to_nav(sema.db)).collect()
Impl::all_for_type(sema.db, ty)
.into_iter()
.filter_map(|imp| imp.try_to_nav(sema.db))
.flatten()
.collect()
}
fn impls_for_trait(
@ -92,6 +96,7 @@ fn impls_for_trait(
Impl::all_for_trait(sema.db, trait_)
.into_iter()
.filter_map(|imp| imp.try_to_nav(sema.db))
.flatten()
.collect()
}
@ -109,6 +114,7 @@ fn impls_for_trait_item(
})?;
item.try_to_nav(sema.db)
})
.flatten()
.collect()
}

View File

@ -31,9 +31,11 @@ pub(crate) fn goto_type_definition(
let mut res = Vec::new();
let mut push = |def: Definition| {
if let Some(nav) = def.try_to_nav(db) {
if !res.contains(&nav) {
res.push(nav);
if let Some(navs) = def.try_to_nav(db) {
for nav in navs {
if !res.contains(&nav) {
res.push(nav);
}
}
}
};

View File

@ -117,7 +117,7 @@ fn highlight_closure_captures(
local
.sources(sema.db)
.into_iter()
.map(|x| x.to_nav(sema.db))
.flat_map(|x| x.to_nav(sema.db))
.filter(|decl| decl.file_id == file_id)
.filter_map(|decl| decl.focus_range)
.map(move |range| HighlightedRange { range, category })
@ -216,7 +216,7 @@ fn highlight_references(
local
.sources(sema.db)
.into_iter()
.map(|x| x.to_nav(sema.db))
.flat_map(|x| x.to_nav(sema.db))
.filter(|decl| decl.file_id == file_id)
.filter_map(|decl| decl.focus_range)
.map(|range| HighlightedRange { range, category })
@ -225,21 +225,27 @@ fn highlight_references(
});
}
def => {
let hl_range = match def {
let navs = match def {
Definition::Module(module) => {
Some(NavigationTarget::from_module_to_decl(sema.db, module))
NavigationTarget::from_module_to_decl(sema.db, module)
}
def => match def.try_to_nav(sema.db) {
Some(it) => it,
None => continue,
},
};
for nav in navs {
if nav.file_id != file_id {
continue;
}
let hl_range = nav.focus_range.map(|range| {
let category = references::decl_mutability(&def, node, range)
.then_some(ReferenceCategory::Write);
HighlightedRange { range, category }
});
if let Some(hl_range) = hl_range {
res.insert(hl_range);
}
def => def.try_to_nav(sema.db),
}
.filter(|decl| decl.file_id == file_id)
.and_then(|decl| decl.focus_range)
.map(|range| {
let category = references::decl_mutability(&def, node, range)
.then_some(ReferenceCategory::Write);
HighlightedRange { range, category }
});
if let Some(hl_range) = hl_range {
res.insert(hl_range);
}
}
}

View File

@ -21,6 +21,7 @@
doc_links::token_as_doc_comment,
markdown_remove::remove_markdown,
markup::Markup,
navigation_target::UpmappingResult,
runnables::{runnable_fn, runnable_mod},
FileId, FilePosition, NavigationTarget, RangeInfo, Runnable, TryToNav,
};
@ -73,7 +74,7 @@ fn goto_type_from_targets(db: &RootDatabase, targets: Vec<hir::ModuleDef>) -> Se
it.module(db)?,
it.name(db).map(|name| name.display(db).to_string()),
),
nav: it.try_to_nav(db)?,
nav: it.try_to_nav(db)?.call_site(),
})
})
.collect();
@ -342,22 +343,26 @@ fn to_action(nav_target: NavigationTarget) -> HoverAction {
}
let adt = match def {
Definition::Trait(it) => return it.try_to_nav(db).map(to_action),
Definition::Trait(it) => {
return it.try_to_nav(db).map(UpmappingResult::call_site).map(to_action)
}
Definition::Adt(it) => Some(it),
Definition::SelfType(it) => it.self_ty(db).as_adt(),
_ => None,
}?;
adt.try_to_nav(db).map(to_action)
adt.try_to_nav(db).map(UpmappingResult::call_site).map(to_action)
}
fn show_fn_references_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
match def {
Definition::Function(it) => it.try_to_nav(db).map(|nav_target| {
HoverAction::Reference(FilePosition {
file_id: nav_target.file_id,
offset: nav_target.focus_or_full_range().start(),
Definition::Function(it) => {
it.try_to_nav(db).map(UpmappingResult::call_site).map(|nav_target| {
HoverAction::Reference(FilePosition {
file_id: nav_target.file_id,
offset: nav_target.focus_or_full_range().start(),
})
})
}),
}
_ => None,
}
}

View File

@ -315,6 +315,7 @@ fn start_location_link(&mut self, def: ModuleDefId) {
}
self.make_new_part();
let Some(location) = ModuleDef::from(def).try_to_nav(self.db) else { return };
let location = location.call_site();
let location =
FileRange { file_id: location.file_id, range: location.focus_or_full_range() };
self.location = Some(location);

View File

@ -100,7 +100,7 @@ macro_rules! eprintln {
markup::Markup,
moniker::{MonikerDescriptorKind, MonikerKind, MonikerResult, PackageInformation},
move_item::Direction,
navigation_target::NavigationTarget,
navigation_target::{NavigationTarget, UpmappingResult},
prime_caches::ParallelPrimeCachesProgress,
references::ReferenceSearchResult,
rename::RenameError,
@ -230,7 +230,7 @@ impl Analysis {
// `AnalysisHost` for creating a fully-featured analysis.
pub fn from_single_file(text: String) -> (Analysis, FileId) {
let mut host = AnalysisHost::default();
let file_id = FileId(0);
let file_id = FileId::from_raw(0);
let mut file_set = FileSet::default();
file_set.insert(file_id, VfsPath::new_virtual_path("/main.rs".to_string()));
let source_root = SourceRoot::new_local(file_set);
@ -413,6 +413,7 @@ pub fn symbol_search(&self, query: Query) -> Cancellable<Vec<NavigationTarget>>
symbol_index::world_symbols(db, query)
.into_iter() // xx: should we make this a par iter?
.filter_map(|s| s.try_to_nav(db))
.map(UpmappingResult::call_site)
.collect::<Vec<_>>()
})
}

View File

@ -2,6 +2,7 @@
use std::fmt;
use arrayvec::ArrayVec;
use either::Either;
use hir::{
db::ExpandDatabase, symbols::FileSymbol, AssocItem, FieldSource, HasContainer, HasSource,
@ -72,15 +73,15 @@ macro_rules! opt {
}
pub(crate) trait ToNav {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget;
fn to_nav(&self, db: &RootDatabase) -> UpmappingResult<NavigationTarget>;
}
pub(crate) trait TryToNav {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget>;
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>>;
}
impl<T: TryToNav, U: TryToNav> TryToNav for Either<T, U> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
match self {
Either::Left(it) => it.try_to_nav(db),
Either::Right(it) => it.try_to_nav(db),
@ -93,23 +94,30 @@ pub fn focus_or_full_range(&self) -> TextRange {
self.focus_range.unwrap_or(self.full_range)
}
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
pub(crate) fn from_module_to_decl(
db: &RootDatabase,
module: hir::Module,
) -> UpmappingResult<NavigationTarget> {
let name = module.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
if let Some(InFile { value, file_id }) = &module.declaration_source(db) {
let (file_id, full_range, focus_range) =
orig_range_with_focus(db, *file_id, value.syntax(), value.name());
let mut res = NavigationTarget::from_syntax(
file_id,
name,
focus_range,
full_range,
SymbolKind::Module,
);
res.docs = module.docs(db);
res.description = Some(module.display(db).to_string());
return res;
match module.declaration_source(db) {
Some(InFile { value, file_id }) => {
orig_range_with_focus(db, file_id, value.syntax(), value.name()).map(
|(FileRange { file_id, range: full_range }, focus_range)| {
let mut res = NavigationTarget::from_syntax(
file_id,
name.clone(),
focus_range,
full_range,
SymbolKind::Module,
);
res.docs = module.docs(db);
res.description = Some(module.display(db).to_string());
res
},
)
}
_ => module.to_nav(db),
}
module.to_nav(db)
}
#[cfg(test)]
@ -135,13 +143,14 @@ pub(crate) fn from_named(
db: &RootDatabase,
InFile { file_id, value }: InFile<&dyn ast::HasName>,
kind: SymbolKind,
) -> NavigationTarget {
let name = value.name().map(|it| it.text().into()).unwrap_or_else(|| "_".into());
) -> UpmappingResult<NavigationTarget> {
let name: SmolStr = value.name().map(|it| it.text().into()).unwrap_or_else(|| "_".into());
let (file_id, full_range, focus_range) =
orig_range_with_focus(db, file_id, value.syntax(), value.name());
NavigationTarget::from_syntax(file_id, name, focus_range, full_range, kind)
orig_range_with_focus(db, file_id, value.syntax(), value.name()).map(
|(FileRange { file_id, range: full_range }, focus_range)| {
NavigationTarget::from_syntax(file_id, name.clone(), focus_range, full_range, kind)
},
)
}
fn from_syntax(
@ -166,49 +175,51 @@ fn from_syntax(
}
impl TryToNav for FileSymbol {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let full_range = self.loc.original_range(db);
let focus_range = self.loc.original_name_range(db);
let focus_range = if focus_range.file_id == full_range.file_id
&& full_range.range.contains_range(focus_range.range)
{
Some(focus_range.range)
} else {
None
};
Some(NavigationTarget {
file_id: full_range.file_id,
name: self
.is_alias
.then(|| self.def.name(db))
.flatten()
.map_or_else(|| self.name.clone(), |it| it.to_smol_str()),
alias: self.is_alias.then(|| self.name.clone()),
kind: Some(hir::ModuleDefId::from(self.def).into()),
full_range: full_range.range,
focus_range,
container_name: self.container_name.clone(),
description: match self.def {
hir::ModuleDef::Module(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Function(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Adt(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Variant(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Const(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Static(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Trait(it) => Some(it.display(db).to_string()),
hir::ModuleDef::TraitAlias(it) => Some(it.display(db).to_string()),
hir::ModuleDef::TypeAlias(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Macro(it) => Some(it.display(db).to_string()),
hir::ModuleDef::BuiltinType(_) => None,
},
docs: None,
})
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let root = db.parse_or_expand(self.loc.hir_file_id);
self.loc.ptr.to_node(&root);
Some(
orig_range_with_focus(
db,
self.loc.hir_file_id,
&self.loc.ptr.to_node(&root),
Some(self.loc.name_ptr.to_node(&root)),
)
.map(|(FileRange { file_id, range: full_range }, focus_range)| {
NavigationTarget {
file_id,
name: self
.is_alias
.then(|| self.def.name(db))
.flatten()
.map_or_else(|| self.name.clone(), |it| it.to_smol_str()),
alias: self.is_alias.then(|| self.name.clone()),
kind: Some(hir::ModuleDefId::from(self.def).into()),
full_range,
focus_range,
container_name: self.container_name.clone(),
description: match self.def {
hir::ModuleDef::Module(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Function(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Adt(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Variant(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Const(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Static(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Trait(it) => Some(it.display(db).to_string()),
hir::ModuleDef::TraitAlias(it) => Some(it.display(db).to_string()),
hir::ModuleDef::TypeAlias(it) => Some(it.display(db).to_string()),
hir::ModuleDef::Macro(it) => Some(it.display(db).to_string()),
hir::ModuleDef::BuiltinType(_) => None,
},
docs: None,
}
}),
)
}
}
impl TryToNav for Definition {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
match self {
Definition::Local(it) => Some(it.to_nav(db)),
Definition::Label(it) => Some(it.to_nav(db)),
@ -236,7 +247,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
}
impl TryToNav for hir::ModuleDef {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
match self {
hir::ModuleDef::Module(it) => Some(it.to_nav(db)),
hir::ModuleDef::Function(it) => it.try_to_nav(db),
@ -334,22 +345,26 @@ impl<D> TryToNav for D
D: HasSource + ToNavFromAst + Copy + HasDocs + HirDisplay,
D::Ast: ast::HasName,
{
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let src = self.source(db)?;
let mut res = NavigationTarget::from_named(
db,
src.as_ref().map(|it| it as &dyn ast::HasName),
D::KIND,
);
res.docs = self.docs(db);
res.description = Some(self.display(db).to_string());
res.container_name = self.container_name(db);
Some(res)
Some(
NavigationTarget::from_named(
db,
src.as_ref().map(|it| it as &dyn ast::HasName),
D::KIND,
)
.map(|mut res| {
res.docs = self.docs(db);
res.description = Some(self.display(db).to_string());
res.container_name = self.container_name(db);
res
}),
)
}
}
impl ToNav for hir::Module {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
fn to_nav(&self, db: &RootDatabase) -> UpmappingResult<NavigationTarget> {
let InFile { file_id, value } = self.definition_source(db);
let name = self.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
@ -358,13 +373,23 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
ModuleSource::Module(node) => (node.syntax(), node.name()),
ModuleSource::BlockExpr(node) => (node.syntax(), None),
};
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, syntax, focus);
NavigationTarget::from_syntax(file_id, name, focus_range, full_range, SymbolKind::Module)
orig_range_with_focus(db, file_id, syntax, focus).map(
|(FileRange { file_id, range: full_range }, focus_range)| {
NavigationTarget::from_syntax(
file_id,
name.clone(),
focus_range,
full_range,
SymbolKind::Module,
)
},
)
}
}
impl TryToNav for hir::Impl {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let InFile { file_id, value } = self.source(db)?;
let derive_path = self.as_builtin_derive_path(db);
@ -373,82 +398,100 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
None => (file_id, value.self_ty(), value.syntax()),
};
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, syntax, focus);
Some(NavigationTarget::from_syntax(
file_id,
"impl".into(),
focus_range,
full_range,
SymbolKind::Impl,
Some(orig_range_with_focus(db, file_id, syntax, focus).map(
|(FileRange { file_id, range: full_range }, focus_range)| {
NavigationTarget::from_syntax(
file_id,
"impl".into(),
focus_range,
full_range,
SymbolKind::Impl,
)
},
))
}
}
impl TryToNav for hir::ExternCrateDecl {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let src = self.source(db)?;
let InFile { file_id, value } = src;
let focus = value
.rename()
.map_or_else(|| value.name_ref().map(Either::Left), |it| it.name().map(Either::Right));
let (file_id, full_range, focus_range) =
orig_range_with_focus(db, file_id, value.syntax(), focus);
let mut res = NavigationTarget::from_syntax(
file_id,
self.alias_or_name(db).unwrap_or_else(|| self.name(db)).to_smol_str(),
focus_range,
full_range,
SymbolKind::Module,
);
res.docs = self.docs(db);
res.description = Some(self.display(db).to_string());
res.container_name = container_name(db, *self);
Some(res)
Some(orig_range_with_focus(db, file_id, value.syntax(), focus).map(
|(FileRange { file_id, range: full_range }, focus_range)| {
let mut res = NavigationTarget::from_syntax(
file_id,
self.alias_or_name(db).unwrap_or_else(|| self.name(db)).to_smol_str(),
focus_range,
full_range,
SymbolKind::Module,
);
res.docs = self.docs(db);
res.description = Some(self.display(db).to_string());
res.container_name = container_name(db, *self);
res
},
))
}
}
impl TryToNav for hir::Field {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let src = self.source(db)?;
let field_source = match &src.value {
FieldSource::Named(it) => {
let mut res =
NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field);
res.docs = self.docs(db);
res.description = Some(self.display(db).to_string());
res
}
FieldSource::Pos(it) => {
let FileRange { file_id, range } =
src.with_value(it.syntax()).original_file_range(db);
NavigationTarget::from_syntax(file_id, "".into(), None, range, SymbolKind::Field)
NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field).map(
|mut res| {
res.docs = self.docs(db);
res.description = Some(self.display(db).to_string());
res
},
)
}
FieldSource::Pos(it) => orig_range(db, src.file_id, it.syntax()).map(
|(FileRange { file_id, range: full_range }, focus_range)| {
NavigationTarget::from_syntax(
file_id,
format!("{}", self.index()).into(),
focus_range,
full_range,
SymbolKind::Field,
)
},
),
};
Some(field_source)
}
}
impl TryToNav for hir::Macro {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let src = self.source(db)?;
let name_owner: &dyn ast::HasName = match &src.value {
Either::Left(it) => it,
Either::Right(it) => it,
};
let mut res = NavigationTarget::from_named(
db,
src.as_ref().with_value(name_owner),
self.kind(db).into(),
);
res.docs = self.docs(db);
Some(res)
Some(
NavigationTarget::from_named(
db,
src.as_ref().with_value(name_owner),
self.kind(db).into(),
)
.map(|mut res| {
res.docs = self.docs(db);
res
}),
)
}
}
impl TryToNav for hir::Adt {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
match self {
hir::Adt::Struct(it) => it.try_to_nav(db),
hir::Adt::Union(it) => it.try_to_nav(db),
@ -458,7 +501,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
}
impl TryToNav for hir::AssocItem {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
match self {
AssocItem::Function(it) => it.try_to_nav(db),
AssocItem::Const(it) => it.try_to_nav(db),
@ -468,7 +511,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
}
impl TryToNav for hir::GenericParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
match self {
hir::GenericParam::TypeParam(it) => it.try_to_nav(db),
hir::GenericParam::ConstParam(it) => it.try_to_nav(db),
@ -478,7 +521,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
}
impl ToNav for LocalSource {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
fn to_nav(&self, db: &RootDatabase) -> UpmappingResult<NavigationTarget> {
let InFile { file_id, value } = &self.source;
let file_id = *file_id;
let local = self.local;
@ -487,60 +530,61 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
Either::Right(it) => (it.syntax(), it.name()),
};
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, node, name);
let name = local.name(db).to_smol_str();
let kind = if local.is_self(db) {
SymbolKind::SelfParam
} else if local.is_param(db) {
SymbolKind::ValueParam
} else {
SymbolKind::Local
};
NavigationTarget {
file_id,
name,
alias: None,
kind: Some(kind),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
}
orig_range_with_focus(db, file_id, node, name).map(
|(FileRange { file_id, range: full_range }, focus_range)| {
let name = local.name(db).to_smol_str();
let kind = if local.is_self(db) {
SymbolKind::SelfParam
} else if local.is_param(db) {
SymbolKind::ValueParam
} else {
SymbolKind::Local
};
NavigationTarget {
file_id,
name,
alias: None,
kind: Some(kind),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
}
},
)
}
}
impl ToNav for hir::Local {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
fn to_nav(&self, db: &RootDatabase) -> UpmappingResult<NavigationTarget> {
self.primary_source(db).to_nav(db)
}
}
impl ToNav for hir::Label {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
fn to_nav(&self, db: &RootDatabase) -> UpmappingResult<NavigationTarget> {
let InFile { file_id, value } = self.source(db);
let name = self.name(db).to_smol_str();
let (file_id, full_range, focus_range) =
orig_range_with_focus(db, file_id, value.syntax(), value.lifetime());
NavigationTarget {
file_id,
name,
alias: None,
kind: Some(SymbolKind::Label),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
}
orig_range_with_focus(db, file_id, value.syntax(), value.lifetime()).map(
|(FileRange { file_id, range: full_range }, focus_range)| NavigationTarget {
file_id,
name: name.clone(),
alias: None,
kind: Some(SymbolKind::Label),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
},
)
}
}
impl TryToNav for hir::TypeParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let InFile { file_id, value } = self.merge().source(db)?;
let name = self.name(db).to_smol_str();
@ -559,51 +603,51 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
};
let focus = value.as_ref().either(|it| it.name(), |it| it.name());
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, syntax, focus);
Some(NavigationTarget {
file_id,
name,
alias: None,
kind: Some(SymbolKind::TypeParam),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
})
Some(orig_range_with_focus(db, file_id, syntax, focus).map(
|(FileRange { file_id, range: full_range }, focus_range)| NavigationTarget {
file_id,
name: name.clone(),
alias: None,
kind: Some(SymbolKind::TypeParam),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
},
))
}
}
impl TryToNav for hir::TypeOrConstParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
self.split(db).try_to_nav(db)
}
}
impl TryToNav for hir::LifetimeParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let InFile { file_id, value } = self.source(db)?;
let name = self.name(db).to_smol_str();
let FileRange { file_id, range } =
InFile::new(file_id, value.syntax()).original_file_range(db);
Some(NavigationTarget {
file_id,
name,
alias: None,
kind: Some(SymbolKind::LifetimeParam),
full_range: range,
focus_range: Some(range),
container_name: None,
description: None,
docs: None,
})
Some(orig_range(db, file_id, value.syntax()).map(
|(FileRange { file_id, range: full_range }, focus_range)| NavigationTarget {
file_id,
name: name.clone(),
alias: None,
kind: Some(SymbolKind::LifetimeParam),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
},
))
}
}
impl TryToNav for hir::ConstParam {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
let InFile { file_id, value } = self.merge().source(db)?;
let name = self.name(db).to_smol_str();
@ -615,46 +659,180 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
}
};
let (file_id, full_range, focus_range) =
orig_range_with_focus(db, file_id, value.syntax(), value.name());
Some(NavigationTarget {
file_id,
name,
alias: None,
kind: Some(SymbolKind::ConstParam),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
})
Some(orig_range_with_focus(db, file_id, value.syntax(), value.name()).map(
|(FileRange { file_id, range: full_range }, focus_range)| NavigationTarget {
file_id,
name: name.clone(),
alias: None,
kind: Some(SymbolKind::ConstParam),
full_range,
focus_range,
container_name: None,
description: None,
docs: None,
},
))
}
}
#[derive(Debug)]
pub struct UpmappingResult<T> {
/// The macro call site.
pub call_site: T,
/// The macro definition site, if relevant.
pub def_site: Option<T>,
}
impl<T> UpmappingResult<T> {
pub fn call_site(self) -> T {
self.call_site
}
pub fn collect<FI: FromIterator<T>>(self) -> FI {
FI::from_iter(self.into_iter())
}
}
impl<T> IntoIterator for UpmappingResult<T> {
type Item = T;
type IntoIter = <ArrayVec<T, 2> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.def_site
.into_iter()
.chain(Some(self.call_site))
.collect::<ArrayVec<_, 2>>()
.into_iter()
}
}
impl<T> UpmappingResult<T> {
fn map<U>(self, f: impl Fn(T) -> U) -> UpmappingResult<U> {
UpmappingResult { call_site: f(self.call_site), def_site: self.def_site.map(f) }
}
}
/// Returns the original range of the syntax node, and the range of the name mapped out of macro expansions
/// Additionally verifies that the name span is in bounds and related to the original range.
/// May return two results if the mapped node originates from a macro definition in which case the
/// second result is the creating macro call.
fn orig_range_with_focus(
db: &RootDatabase,
hir_file: HirFileId,
value: &SyntaxNode,
name: Option<impl AstNode>,
) -> (FileId, TextRange, Option<TextRange>) {
let FileRange { file_id, range } =
match InFile::new(hir_file, value).original_file_range_opt(db) {
Some((range, ctxt)) if ctxt.is_root() => range,
_ => db
.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
.kind
.original_call_range(db),
};
let focus_range = name
.and_then(|it| InFile::new(hir_file, it.syntax()).original_file_range_opt(db))
.filter(|(frange, ctxt)| {
ctxt.is_root() && frange.file_id == file_id && frange.range.contains_range(frange.range)
})
.map(|(frange, _ctxt)| frange.range);
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
let Some(name) = name else { return orig_range(db, hir_file, value) };
(file_id, range, focus_range)
let call_range = || {
db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
.kind
.original_call_range(db)
};
let def_range = || {
db.lookup_intern_macro_call(hir_file.macro_file().unwrap().macro_call_id)
.def
.definition_range(db)
};
// FIXME What about include!d things
let value_range = InFile::new(hir_file, value).original_file_range_opt(db);
let ((call_site_range, call_site_focus), def_site) =
match InFile::new(hir_file, name.syntax()).original_file_range_opt(db) {
// call site name
Some((focus_range, ctxt)) if ctxt.is_root() => {
// Try to upmap the node as well, if it ends up in the def site, go back to the call site
(
(
match value_range {
// name is in the node in the macro input so we can return it
Some((range, ctxt))
if ctxt.is_root()
&& range.file_id == focus_range.file_id
&& range.range.contains_range(focus_range.range) =>
{
range
}
// name lies outside the node, so instead point to the macro call which
// *should* contain the name
_ => call_range(),
},
Some(focus_range),
),
// no def site relevant
None,
)
}
// def site name
// FIXME: This can be de improved
Some((focus_range, _ctxt)) => {
match value_range {
// but overall node is in macro input
Some((range, ctxt)) if ctxt.is_root() => (
// node mapped up in call site, show the node
(range, None),
// def site, if the name is in the (possibly) upmapped def site range, show the
// def site
{
let (def_site, _) = def_range().original_node_file_range(db);
(def_site.file_id == focus_range.file_id
&& def_site.range.contains_range(focus_range.range))
.then_some((def_site, Some(focus_range)))
},
),
// node is in macro def, just show the focus
_ => (
// show the macro call
(call_range(), None),
Some((focus_range, Some(focus_range))),
),
}
}
// lost name? can't happen for single tokens
None => return orig_range(db, hir_file, value),
};
UpmappingResult {
call_site: (
call_site_range,
call_site_focus.and_then(|FileRange { file_id, range }| {
if call_site_range.file_id == file_id && call_site_range.range.contains_range(range)
{
Some(range)
} else {
None
}
}),
),
def_site: def_site.map(|(def_site_range, def_site_focus)| {
(
def_site_range,
def_site_focus.and_then(|FileRange { file_id, range }| {
if def_site_range.file_id == file_id
&& def_site_range.range.contains_range(range)
{
Some(range)
} else {
None
}
}),
)
}),
}
}
fn orig_range(
db: &RootDatabase,
hir_file: HirFileId,
value: &SyntaxNode,
) -> UpmappingResult<(FileRange, Option<TextRange>)> {
UpmappingResult {
call_site: (InFile::new(hir_file, value).original_file_range(db), None),
def_site: None,
}
}
#[cfg(test)]

View File

@ -45,11 +45,11 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
Some(module) => sema
.to_def(&module)
.into_iter()
.map(|module| NavigationTarget::from_module_to_decl(db, module))
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
.collect(),
None => sema
.to_module_defs(position.file_id)
.map(|module| NavigationTarget::from_module_to_decl(db, module))
.flat_map(|module| NavigationTarget::from_module_to_decl(db, module))
.collect(),
}
}

View File

@ -9,6 +9,8 @@
//! at the index that the match starts at and its tree parent is
//! resolved to the search element definition, we get a reference.
use std::collections::HashMap;
use hir::{DescendPreference, PathResolution, Semantics};
use ide_db::{
base_db::FileId,
@ -60,19 +62,6 @@ pub(crate) fn find_all_refs(
let syntax = sema.parse(position.file_id).syntax().clone();
let make_searcher = |literal_search: bool| {
move |def: Definition| {
let declaration = match def {
Definition::Module(module) => {
Some(NavigationTarget::from_module_to_decl(sema.db, module))
}
def => def.try_to_nav(sema.db),
}
.map(|nav| {
let decl_range = nav.focus_or_full_range();
Declaration {
is_mut: decl_mutability(&def, sema.parse(nav.file_id).syntax(), decl_range),
nav,
}
});
let mut usages =
def.usages(sema).set_scope(search_scope.as_ref()).include_self_refs().all();
@ -80,7 +69,7 @@ pub(crate) fn find_all_refs(
retain_adt_literal_usages(&mut usages, def, sema);
}
let references = usages
let mut references = usages
.into_iter()
.map(|(file_id, refs)| {
(
@ -91,8 +80,30 @@ pub(crate) fn find_all_refs(
.collect(),
)
})
.collect();
.collect::<HashMap<_, Vec<_>, _>>();
let declaration = match def {
Definition::Module(module) => {
Some(NavigationTarget::from_module_to_decl(sema.db, module))
}
def => def.try_to_nav(sema.db),
}
.map(|nav| {
let (nav, extra_ref) = match nav.def_site {
Some(call) => (call, Some(nav.call_site)),
None => (nav.call_site, None),
};
if let Some(extra_ref) = extra_ref {
references
.entry(extra_ref.file_id)
.or_default()
.push((extra_ref.focus_or_full_range(), None));
}
let decl_range = nav.focus_or_full_range();
Declaration {
is_mut: decl_mutability(&def, sema.parse(nav.file_id).syntax(), decl_range),
nav,
}
});
ReferenceSearchResult { declaration, references }
}
};
@ -882,7 +893,7 @@ pub fn quux$0() {}
check_with_scope(
code,
Some(SearchScope::single_file(FileId(2))),
Some(SearchScope::single_file(FileId::from_raw(2))),
expect![[r#"
quux Function FileId(0) 19..35 26..30
@ -1181,7 +1192,7 @@ fn bar<'a>(_: &'a ()) {}
}
"#,
expect![[r#"
'a LifetimeParam FileId(0) 55..57 55..57
'a LifetimeParam FileId(0) 55..57
FileId(0) 63..65
FileId(0) 71..73
@ -1199,7 +1210,7 @@ fn test_find_lifetimes_type_alias() {
type Foo<'a, T> where T: 'a$0 = &'a T;
"#,
expect![[r#"
'a LifetimeParam FileId(0) 9..11 9..11
'a LifetimeParam FileId(0) 9..11
FileId(0) 25..27
FileId(0) 31..33
@ -1221,7 +1232,7 @@ fn foo() -> &'a$0 () {
}
"#,
expect![[r#"
'a LifetimeParam FileId(0) 47..49 47..49
'a LifetimeParam FileId(0) 47..49
FileId(0) 55..57
FileId(0) 64..66

View File

@ -335,7 +335,8 @@ pub(crate) fn runnable_fn(
sema.db,
def.source(sema.db)?.as_ref().map(|it| it as &dyn ast::HasName),
SymbolKind::Function,
);
)
.call_site();
let cfg = def.attrs(sema.db).cfg();
Some(Runnable { use_name_in_title: false, nav, kind, cfg })
}
@ -357,7 +358,7 @@ pub(crate) fn runnable_mod(
let attrs = def.attrs(sema.db);
let cfg = attrs.cfg();
let nav = NavigationTarget::from_module_to_decl(sema.db, def);
let nav = NavigationTarget::from_module_to_decl(sema.db, def).call_site();
Some(Runnable { use_name_in_title: false, nav, kind: RunnableKind::TestMod { path }, cfg })
}
@ -370,7 +371,7 @@ pub(crate) fn runnable_impl(
return None;
}
let cfg = attrs.cfg();
let nav = def.try_to_nav(sema.db)?;
let nav = def.try_to_nav(sema.db)?.call_site();
let ty = def.self_ty(sema.db);
let adt_name = ty.as_adt()?.name(sema.db);
let mut ty_args = ty.generic_parameters(sema.db).peekable();
@ -407,7 +408,7 @@ fn runnable_mod_outline_definition(
match def.definition_source(sema.db).value {
hir::ModuleSource::SourceFile(_) => Some(Runnable {
use_name_in_title: false,
nav: def.to_nav(sema.db),
nav: def.to_nav(sema.db).call_site(),
kind: RunnableKind::TestMod { path },
cfg,
}),
@ -465,7 +466,8 @@ fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
let mut nav = match def {
Definition::Module(def) => NavigationTarget::from_module_to_decl(db, def),
def => def.try_to_nav(db)?,
};
}
.call_site();
nav.focus_range = None;
nav.description = None;
nav.docs = None;

View File

@ -13,6 +13,7 @@
use syntax::{AstNode, SyntaxKind::*, TextRange, T};
use crate::inlay_hints::InlayFieldsToResolve;
use crate::navigation_target::UpmappingResult;
use crate::{
hover::hover_for_definition,
inlay_hints::AdjustmentHintsMode,
@ -166,9 +167,8 @@ fn add_file(&mut self, file_id: FileId) {
} else {
let it = self.tokens.insert(TokenStaticData {
hover: hover_for_definition(&sema, file_id, def, &node, &hover_config),
definition: def.try_to_nav(self.db).map(|it| FileRange {
file_id: it.file_id,
range: it.focus_or_full_range(),
definition: def.try_to_nav(self.db).map(UpmappingResult::call_site).map(|it| {
FileRange { file_id: it.file_id, range: it.focus_or_full_range() }
}),
references: vec![],
moniker: current_crate.and_then(|cc| def_to_moniker(self.db, def, cc)),
@ -179,7 +179,7 @@ fn add_file(&mut self, file_id: FileId) {
let token = self.tokens.get_mut(id).unwrap();
token.references.push(ReferenceData {
range: FileRange { range, file_id },
is_definition: match def.try_to_nav(self.db) {
is_definition: match def.try_to_nav(self.db).map(UpmappingResult::call_site) {
Some(it) => it.file_id == file_id && it.focus_or_full_range() == range,
None => false,
},

View File

@ -147,8 +147,10 @@ mod tests {
use super::*;
fn fixture_token_tree() -> Subtree<SpanData> {
let anchor =
SpanAnchor { file_id: FileId(0), ast_id: ErasedFileAstId::from_raw(RawIdx::from(0)) };
let anchor = SpanAnchor {
file_id: FileId::from_raw(0),
ast_id: ErasedFileAstId::from_raw(RawIdx::from(0)),
};
let mut subtree = Subtree {
delimiter: Delimiter {
open: SpanData {

View File

@ -58,7 +58,7 @@ fn load_cargo_with_sysroot(
&mut {
|path| {
let len = file_map.len();
Some(*file_map.entry(path.to_path_buf()).or_insert(FileId(len as u32)))
Some(*file_map.entry(path.to_path_buf()).or_insert(FileId::from_raw(len as u32)))
}
},
&Default::default(),
@ -142,7 +142,7 @@ fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacro
let mut counter = 0;
move |_path| {
counter += 1;
Some(FileId(counter))
Some(FileId::from_raw(counter))
}
},
&Default::default(),

View File

@ -1438,7 +1438,7 @@ pub(crate) fn handle_inlay_hints_resolve(
};
let resolve_data: lsp_ext::InlayHintResolveData = serde_json::from_value(data)?;
let file_id = FileId(resolve_data.file_id);
let file_id = FileId::from_raw(resolve_data.file_id);
anyhow::ensure!(snap.file_exists(file_id), "Invalid LSP resolve data");
let line_index = snap.file_line_index(file_id)?;

View File

@ -457,7 +457,7 @@ pub(crate) fn inlay_hint(
inlay_hint.text_edit.map(|it| text_edit_vec(line_index, it))
};
let data = if needs_resolve && something_to_resolve {
Some(to_value(lsp_ext::InlayHintResolveData { file_id: file_id.0 }).unwrap())
Some(to_value(lsp_ext::InlayHintResolveData { file_id: file_id.index() }).unwrap())
} else {
None
};

View File

@ -22,12 +22,17 @@
pub type SyntaxNodePtr = rowan::ast::SyntaxNodePtr<RustLanguage>;
/// Like `SyntaxNodePtr`, but remembers the type of node.
#[derive(Debug)]
pub struct AstPtr<N: AstNode> {
raw: SyntaxNodePtr,
_ty: PhantomData<fn() -> N>,
}
impl<N: AstNode + std::fmt::Debug> std::fmt::Debug for AstPtr<N> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("AstPtr").field(&self.raw).finish()
}
}
impl<N: AstNode> Clone for AstPtr<N> {
fn clone(&self) -> AstPtr<N> {
AstPtr { raw: self.raw.clone(), _ty: PhantomData }

View File

@ -60,11 +60,21 @@
///
/// Most functions in rust-analyzer use this when they need to refer to a file.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct FileId(pub u32);
pub struct FileId(u32);
impl FileId {
/// Think twice about using this outside of tests. If this ends up in a wrong place it will cause panics!
pub const BOGUS: FileId = FileId(u32::MAX);
pub const BOGUS: FileId = FileId(0xe4e4e);
#[inline]
pub fn from_raw(raw: u32) -> FileId {
FileId(raw)
}
#[inline]
pub fn index(self) -> u32 {
self.0
}
}
/// safe because `FileId` is a newtype of `u32`