Auto merge of #14939 - Veykril:nav-focus-ranges, r=Veykril
fix: Fix nav target calculation discarding file ids from differing macro upmapping Fixes https://github.com/rust-lang/rust-analyzer/issues/14792 Turns out there was the assumption that upmapping from a macro will always end in the same root file, which is no longer the case thanks to `include!`
This commit is contained in:
commit
117f9b7752
@ -5,7 +5,7 @@
|
|||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{
|
use hir::{
|
||||||
symbols::FileSymbol, AssocItem, Documentation, FieldSource, HasAttrs, HasContainer, HasSource,
|
symbols::FileSymbol, AssocItem, Documentation, FieldSource, HasAttrs, HasContainer, HasSource,
|
||||||
HirDisplay, InFile, LocalSource, ModuleSource,
|
HirDisplay, HirFileId, InFile, LocalSource, ModuleSource,
|
||||||
};
|
};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::{FileId, FileRange},
|
base_db::{FileId, FileRange},
|
||||||
@ -92,10 +92,9 @@ pub fn focus_or_full_range(&self) -> TextRange {
|
|||||||
|
|
||||||
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) -> NavigationTarget {
|
||||||
let name = module.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
|
let name = module.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
|
||||||
if let Some(src @ InFile { value, .. }) = &module.declaration_source(db) {
|
if let Some(InFile { value, file_id }) = &module.declaration_source(db) {
|
||||||
let FileRange { file_id, range: full_range } = src.syntax().original_file_range(db);
|
let (file_id, full_range, focus_range) =
|
||||||
let focus_range =
|
orig_range_with_focus(db, *file_id, value.syntax(), value.name());
|
||||||
value.name().and_then(|it| orig_focus_range(db, src.file_id, it.syntax()));
|
|
||||||
let mut res = NavigationTarget::from_syntax(
|
let mut res = NavigationTarget::from_syntax(
|
||||||
file_id,
|
file_id,
|
||||||
name,
|
name,
|
||||||
@ -131,14 +130,15 @@ pub(crate) fn debug_render(&self) -> String {
|
|||||||
/// Allows `NavigationTarget` to be created from a `NameOwner`
|
/// Allows `NavigationTarget` to be created from a `NameOwner`
|
||||||
pub(crate) fn from_named(
|
pub(crate) fn from_named(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
node @ InFile { file_id, value }: InFile<&dyn ast::HasName>,
|
InFile { file_id, value }: InFile<&dyn ast::HasName>,
|
||||||
kind: SymbolKind,
|
kind: SymbolKind,
|
||||||
) -> NavigationTarget {
|
) -> NavigationTarget {
|
||||||
let name = value.name().map(|it| it.text().into()).unwrap_or_else(|| "_".into());
|
let name = value.name().map(|it| it.text().into()).unwrap_or_else(|| "_".into());
|
||||||
let focus_range = value.name().and_then(|it| orig_focus_range(db, file_id, it.syntax()));
|
|
||||||
let FileRange { file_id, range } = node.map(|it| it.syntax()).original_file_range(db);
|
|
||||||
|
|
||||||
NavigationTarget::from_syntax(file_id, name, focus_range, range, kind)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_syntax(
|
fn from_syntax(
|
||||||
@ -165,7 +165,13 @@ fn from_syntax(
|
|||||||
impl TryToNav for FileSymbol {
|
impl TryToNav for FileSymbol {
|
||||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
||||||
let full_range = self.loc.original_range(db);
|
let full_range = self.loc.original_range(db);
|
||||||
let name_range = self.loc.original_name_range(db)?;
|
let focus_range = self.loc.original_name_range(db).and_then(|it| {
|
||||||
|
if it.file_id == full_range.file_id {
|
||||||
|
Some(it.range)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Some(NavigationTarget {
|
Some(NavigationTarget {
|
||||||
file_id: full_range.file_id,
|
file_id: full_range.file_id,
|
||||||
@ -173,7 +179,7 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|||||||
alias: if self.is_alias { Some(self.name.clone()) } else { None },
|
alias: if self.is_alias { Some(self.name.clone()) } else { None },
|
||||||
kind: Some(hir::ModuleDefId::from(self.def).into()),
|
kind: Some(hir::ModuleDefId::from(self.def).into()),
|
||||||
full_range: full_range.range,
|
full_range: full_range.range,
|
||||||
focus_range: Some(name_range.range),
|
focus_range,
|
||||||
container_name: self.container_name.clone(),
|
container_name: self.container_name.clone(),
|
||||||
description: match self.def {
|
description: match self.def {
|
||||||
hir::ModuleDef::Module(it) => Some(it.display(db).to_string()),
|
hir::ModuleDef::Module(it) => Some(it.display(db).to_string()),
|
||||||
@ -340,15 +346,11 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|||||||
let name = self.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
|
let name = self.name(db).map(|it| it.to_smol_str()).unwrap_or_default();
|
||||||
let (syntax, focus) = match &value {
|
let (syntax, focus) = match &value {
|
||||||
ModuleSource::SourceFile(node) => (node.syntax(), None),
|
ModuleSource::SourceFile(node) => (node.syntax(), None),
|
||||||
ModuleSource::Module(node) => (
|
ModuleSource::Module(node) => (node.syntax(), node.name()),
|
||||||
node.syntax(),
|
|
||||||
node.name().and_then(|it| orig_focus_range(db, file_id, it.syntax())),
|
|
||||||
),
|
|
||||||
ModuleSource::BlockExpr(node) => (node.syntax(), None),
|
ModuleSource::BlockExpr(node) => (node.syntax(), None),
|
||||||
};
|
};
|
||||||
let FileRange { file_id, range: full_range } =
|
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, syntax, focus);
|
||||||
InFile::new(file_id, syntax).original_file_range(db);
|
NavigationTarget::from_syntax(file_id, name, focus_range, full_range, SymbolKind::Module)
|
||||||
NavigationTarget::from_syntax(file_id, name, focus, full_range, SymbolKind::Module)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,17 +359,14 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|||||||
let InFile { file_id, value } = self.source(db)?;
|
let InFile { file_id, value } = self.source(db)?;
|
||||||
let derive_attr = self.is_builtin_derive(db);
|
let derive_attr = self.is_builtin_derive(db);
|
||||||
|
|
||||||
let focus_range = if derive_attr.is_some() {
|
let focus = if derive_attr.is_some() { None } else { value.self_ty() };
|
||||||
None
|
|
||||||
} else {
|
let syntax = match &derive_attr {
|
||||||
value.self_ty().and_then(|ty| orig_focus_range(db, file_id, ty.syntax()))
|
Some(attr) => attr.value.syntax(),
|
||||||
};
|
None => value.syntax(),
|
||||||
|
|
||||||
let FileRange { file_id, range: full_range } = match &derive_attr {
|
|
||||||
Some(attr) => attr.syntax().original_file_range(db),
|
|
||||||
None => InFile::new(file_id, value.syntax()).original_file_range(db),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, syntax, focus);
|
||||||
Some(NavigationTarget::from_syntax(
|
Some(NavigationTarget::from_syntax(
|
||||||
file_id,
|
file_id,
|
||||||
"impl".into(),
|
"impl".into(),
|
||||||
@ -456,9 +455,8 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|||||||
Either::Left(bind_pat) => (bind_pat.syntax(), bind_pat.name()),
|
Either::Left(bind_pat) => (bind_pat.syntax(), bind_pat.name()),
|
||||||
Either::Right(it) => (it.syntax(), it.name()),
|
Either::Right(it) => (it.syntax(), it.name()),
|
||||||
};
|
};
|
||||||
let focus_range = name.and_then(|it| orig_focus_range(db, file_id, it.syntax()));
|
|
||||||
let FileRange { file_id, range: full_range } =
|
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, node, name);
|
||||||
InFile::new(file_id, node).original_file_range(db);
|
|
||||||
|
|
||||||
let name = local.name(db).to_smol_str();
|
let name = local.name(db).to_smol_str();
|
||||||
let kind = if local.is_self(db) {
|
let kind = if local.is_self(db) {
|
||||||
@ -493,9 +491,8 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
|
|||||||
let InFile { file_id, value } = self.source(db);
|
let InFile { file_id, value } = self.source(db);
|
||||||
let name = self.name(db).to_smol_str();
|
let name = self.name(db).to_smol_str();
|
||||||
|
|
||||||
let range = |syntax: &_| InFile::new(file_id, syntax).original_file_range(db);
|
let (file_id, full_range, focus_range) =
|
||||||
let FileRange { file_id, range: full_range } = range(value.syntax());
|
orig_range_with_focus(db, file_id, value.syntax(), value.lifetime());
|
||||||
let focus_range = value.lifetime().map(|lt| range(lt.syntax()).range);
|
|
||||||
|
|
||||||
NavigationTarget {
|
NavigationTarget {
|
||||||
file_id,
|
file_id,
|
||||||
@ -525,19 +522,14 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|||||||
Either::Right(x) => Either::Right(x),
|
Either::Right(x) => Either::Right(x),
|
||||||
};
|
};
|
||||||
|
|
||||||
let range = |syntax: &_| InFile::new(file_id, syntax).original_file_range(db);
|
let syntax = match &value {
|
||||||
let focus_range = |syntax: &_| InFile::new(file_id, syntax).original_file_range_opt(db);
|
Either::Left(type_param) => type_param.syntax(),
|
||||||
let FileRange { file_id, range: full_range } = match &value {
|
Either::Right(trait_) => trait_.syntax(),
|
||||||
Either::Left(type_param) => range(type_param.syntax()),
|
|
||||||
Either::Right(trait_) => trait_
|
|
||||||
.name()
|
|
||||||
.and_then(|name| focus_range(name.syntax()))
|
|
||||||
.unwrap_or_else(|| range(trait_.syntax())),
|
|
||||||
};
|
};
|
||||||
let focus_range = value
|
let focus = value.as_ref().either(|it| it.name(), |it| it.name());
|
||||||
.either(|it| it.name(), |it| it.name())
|
|
||||||
.and_then(|it| focus_range(it.syntax()))
|
let (file_id, full_range, focus_range) = orig_range_with_focus(db, file_id, syntax, focus);
|
||||||
.map(|it| it.range);
|
|
||||||
Some(NavigationTarget {
|
Some(NavigationTarget {
|
||||||
file_id,
|
file_id,
|
||||||
name,
|
name,
|
||||||
@ -563,15 +555,15 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|||||||
let InFile { file_id, value } = self.source(db)?;
|
let InFile { file_id, value } = self.source(db)?;
|
||||||
let name = self.name(db).to_smol_str();
|
let name = self.name(db).to_smol_str();
|
||||||
|
|
||||||
let FileRange { file_id, range: full_range } =
|
let FileRange { file_id, range } =
|
||||||
InFile::new(file_id, value.syntax()).original_file_range(db);
|
InFile::new(file_id, value.syntax()).original_file_range(db);
|
||||||
Some(NavigationTarget {
|
Some(NavigationTarget {
|
||||||
file_id,
|
file_id,
|
||||||
name,
|
name,
|
||||||
alias: None,
|
alias: None,
|
||||||
kind: Some(SymbolKind::LifetimeParam),
|
kind: Some(SymbolKind::LifetimeParam),
|
||||||
full_range,
|
full_range: range,
|
||||||
focus_range: Some(full_range),
|
focus_range: Some(range),
|
||||||
container_name: None,
|
container_name: None,
|
||||||
description: None,
|
description: None,
|
||||||
docs: None,
|
docs: None,
|
||||||
@ -592,9 +584,8 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let focus_range = value.name().and_then(|it| orig_focus_range(db, file_id, it.syntax()));
|
let (file_id, full_range, focus_range) =
|
||||||
let FileRange { file_id, range: full_range } =
|
orig_range_with_focus(db, file_id, value.syntax(), value.name());
|
||||||
InFile::new(file_id, value.syntax()).original_file_range(db);
|
|
||||||
Some(NavigationTarget {
|
Some(NavigationTarget {
|
||||||
file_id,
|
file_id,
|
||||||
name,
|
name,
|
||||||
@ -609,12 +600,19 @@ fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn orig_focus_range(
|
fn orig_range_with_focus(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
file_id: hir::HirFileId,
|
hir_file: HirFileId,
|
||||||
syntax: &SyntaxNode,
|
value: &SyntaxNode,
|
||||||
) -> Option<TextRange> {
|
name: Option<impl AstNode>,
|
||||||
InFile::new(file_id, syntax).original_file_range_opt(db).map(|it| it.range)
|
) -> (FileId, TextRange, Option<TextRange>) {
|
||||||
|
let FileRange { file_id, range: full_range } =
|
||||||
|
InFile::new(hir_file, value).original_file_range(db);
|
||||||
|
let focus_range = name
|
||||||
|
.and_then(|it| InFile::new(hir_file, it.syntax()).original_file_range_opt(db))
|
||||||
|
.and_then(|range| if range.file_id == file_id { Some(range.range) } else { None });
|
||||||
|
|
||||||
|
(file_id, full_range, focus_range)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -1289,7 +1289,7 @@ trait Foo where Self$0 {
|
|||||||
impl Foo for () {}
|
impl Foo for () {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
Self TypeParam FileId(0) 6..9 6..9
|
Self TypeParam FileId(0) 0..44 6..9
|
||||||
|
|
||||||
FileId(0) 16..20
|
FileId(0) 16..20
|
||||||
FileId(0) 37..41
|
FileId(0) 37..41
|
||||||
@ -1380,7 +1380,7 @@ fn test_trait_alias_self() {
|
|||||||
trait Foo = where Self$0: ;
|
trait Foo = where Self$0: ;
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
Self TypeParam FileId(0) 6..9 6..9
|
Self TypeParam FileId(0) 0..25 6..9
|
||||||
|
|
||||||
FileId(0) 18..22
|
FileId(0) 18..22
|
||||||
"#]],
|
"#]],
|
||||||
|
Loading…
Reference in New Issue
Block a user