mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
Properly account for editions in names
This PR touches a lot of parts. But the main changes are changing `hir_expand::Name` to be raw edition-dependently and only when necessary (unrelated to how the user originally wrote the identifier), and changing `is_keyword()` and `is_raw_identifier()` to be edition-aware (this was done in #17896, but the FIXMEs were fixed here). It is possible that I missed some cases, but most IDE parts should properly escape (or not escape) identifiers now. The rules of thumb are: - If we show the identifier to the user, its rawness should be determined by the edition of the edited crate. This is nice for IDE features, but really important for changes we insert to the source code. - For tests, I chose `Edition::CURRENT` (so we only have to (maybe) update tests when an edition becomes stable, to avoid churn). - For debugging tools (helper methods and logs), I used `Edition::LATEST`.
This commit is contained in:
parent
91aa3f46b3
commit
9d3368f2c2
179 changed files with 2485 additions and 1250 deletions
|
@ -5,14 +5,15 @@ use std::fmt;
|
|||
use arrayvec::ArrayVec;
|
||||
use either::Either;
|
||||
use hir::{
|
||||
db::ExpandDatabase, symbols::FileSymbol, AssocItem, FieldSource, HasContainer, HasSource,
|
||||
HirDisplay, HirFileId, InFile, LocalSource, ModuleSource,
|
||||
db::ExpandDatabase, symbols::FileSymbol, AssocItem, FieldSource, HasContainer, HasCrate,
|
||||
HasSource, HirDisplay, HirFileId, HirFileIdExt, InFile, LocalSource, ModuleSource,
|
||||
};
|
||||
use ide_db::{
|
||||
defs::Definition,
|
||||
documentation::{Documentation, HasDocs},
|
||||
FileId, FileRange, RootDatabase, SymbolKind,
|
||||
};
|
||||
use span::Edition;
|
||||
use stdx::never;
|
||||
use syntax::{
|
||||
ast::{self, HasName},
|
||||
|
@ -97,7 +98,9 @@ impl NavigationTarget {
|
|||
db: &RootDatabase,
|
||||
module: hir::Module,
|
||||
) -> UpmappingResult<NavigationTarget> {
|
||||
let name = module.name(db).map(|it| it.display_no_db().to_smolstr()).unwrap_or_default();
|
||||
let edition = module.krate().edition(db);
|
||||
let name =
|
||||
module.name(db).map(|it| it.display_no_db(edition).to_smolstr()).unwrap_or_default();
|
||||
match module.declaration_source(db) {
|
||||
Some(InFile { value, file_id }) => {
|
||||
orig_range_with_focus(db, file_id, value.syntax(), value.name()).map(
|
||||
|
@ -110,7 +113,7 @@ impl NavigationTarget {
|
|||
SymbolKind::Module,
|
||||
);
|
||||
res.docs = module.docs(db);
|
||||
res.description = Some(module.display(db).to_string());
|
||||
res.description = Some(module.display(db, edition).to_string());
|
||||
res
|
||||
},
|
||||
)
|
||||
|
@ -175,6 +178,8 @@ impl NavigationTarget {
|
|||
|
||||
impl TryToNav for FileSymbol {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let edition =
|
||||
self.def.module(db).map(|it| it.krate().edition(db)).unwrap_or(Edition::CURRENT);
|
||||
Some(
|
||||
orig_range_with_focus_r(
|
||||
db,
|
||||
|
@ -185,27 +190,26 @@ impl TryToNav for FileSymbol {
|
|||
.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.display_no_db().to_smolstr()),
|
||||
name: self.is_alias.then(|| self.def.name(db)).flatten().map_or_else(
|
||||
|| self.name.clone(),
|
||||
|it| it.display_no_db(edition).to_smolstr(),
|
||||
),
|
||||
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::Module(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::Function(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::Adt(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::Variant(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::Const(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::Static(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::Trait(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::TraitAlias(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::TypeAlias(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::Macro(it) => Some(it.display(db, edition).to_string()),
|
||||
hir::ModuleDef::BuiltinType(_) => None,
|
||||
},
|
||||
docs: None,
|
||||
|
@ -271,11 +275,13 @@ pub(crate) trait ToNavFromAst: Sized {
|
|||
}
|
||||
}
|
||||
|
||||
fn container_name(db: &RootDatabase, t: impl HasContainer) -> Option<SmolStr> {
|
||||
fn container_name(db: &RootDatabase, t: impl HasContainer, edition: Edition) -> Option<SmolStr> {
|
||||
match t.container(db) {
|
||||
hir::ItemContainer::Trait(it) => Some(it.name(db).display_no_db().to_smolstr()),
|
||||
hir::ItemContainer::Trait(it) => Some(it.name(db).display_no_db(edition).to_smolstr()),
|
||||
// FIXME: Handle owners of blocks correctly here
|
||||
hir::ItemContainer::Module(it) => it.name(db).map(|name| name.display_no_db().to_smolstr()),
|
||||
hir::ItemContainer::Module(it) => {
|
||||
it.name(db).map(|name| name.display_no_db(edition).to_smolstr())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -283,32 +289,32 @@ fn container_name(db: &RootDatabase, t: impl HasContainer) -> Option<SmolStr> {
|
|||
impl ToNavFromAst for hir::Function {
|
||||
const KIND: SymbolKind = SymbolKind::Function;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToNavFromAst for hir::Const {
|
||||
const KIND: SymbolKind = SymbolKind::Const;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
impl ToNavFromAst for hir::Static {
|
||||
const KIND: SymbolKind = SymbolKind::Static;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
impl ToNavFromAst for hir::Struct {
|
||||
const KIND: SymbolKind = SymbolKind::Struct;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
impl ToNavFromAst for hir::Enum {
|
||||
const KIND: SymbolKind = SymbolKind::Enum;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
impl ToNavFromAst for hir::Variant {
|
||||
|
@ -317,25 +323,25 @@ impl ToNavFromAst for hir::Variant {
|
|||
impl ToNavFromAst for hir::Union {
|
||||
const KIND: SymbolKind = SymbolKind::Union;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
impl ToNavFromAst for hir::TypeAlias {
|
||||
const KIND: SymbolKind = SymbolKind::TypeAlias;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
impl ToNavFromAst for hir::Trait {
|
||||
const KIND: SymbolKind = SymbolKind::Trait;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
impl ToNavFromAst for hir::TraitAlias {
|
||||
const KIND: SymbolKind = SymbolKind::TraitAlias;
|
||||
fn container_name(self, db: &RootDatabase) -> Option<SmolStr> {
|
||||
container_name(db, self)
|
||||
container_name(db, self, self.krate(db).edition(db))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -346,6 +352,7 @@ where
|
|||
{
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let src = self.source(db)?;
|
||||
let edition = src.file_id.original_file(db).edition();
|
||||
Some(
|
||||
NavigationTarget::from_named(
|
||||
db,
|
||||
|
@ -354,7 +361,7 @@ where
|
|||
)
|
||||
.map(|mut res| {
|
||||
res.docs = self.docs(db);
|
||||
res.description = Some(self.display(db).to_string());
|
||||
res.description = Some(self.display(db, edition).to_string());
|
||||
res.container_name = self.container_name(db);
|
||||
res
|
||||
}),
|
||||
|
@ -365,8 +372,10 @@ where
|
|||
impl ToNav for hir::Module {
|
||||
fn to_nav(&self, db: &RootDatabase) -> UpmappingResult<NavigationTarget> {
|
||||
let InFile { file_id, value } = self.definition_source(db);
|
||||
let edition = self.krate(db).edition(db);
|
||||
|
||||
let name = self.name(db).map(|it| it.display_no_db().to_smolstr()).unwrap_or_default();
|
||||
let name =
|
||||
self.name(db).map(|it| it.display_no_db(edition).to_smolstr()).unwrap_or_default();
|
||||
let (syntax, focus) = match &value {
|
||||
ModuleSource::SourceFile(node) => (node.syntax(), None),
|
||||
ModuleSource::Module(node) => (node.syntax(), node.name()),
|
||||
|
@ -418,6 +427,7 @@ impl TryToNav for hir::ExternCrateDecl {
|
|||
let focus = value
|
||||
.rename()
|
||||
.map_or_else(|| value.name_ref().map(Either::Left), |it| it.name().map(Either::Right));
|
||||
let edition = self.module(db).krate().edition(db);
|
||||
|
||||
Some(orig_range_with_focus(db, file_id, value.syntax(), focus).map(
|
||||
|(FileRange { file_id, range: full_range }, focus_range)| {
|
||||
|
@ -425,7 +435,7 @@ impl TryToNav for hir::ExternCrateDecl {
|
|||
file_id,
|
||||
self.alias_or_name(db)
|
||||
.unwrap_or_else(|| self.name(db))
|
||||
.display_no_db()
|
||||
.display_no_db(edition)
|
||||
.to_smolstr(),
|
||||
focus_range,
|
||||
full_range,
|
||||
|
@ -433,8 +443,8 @@ impl TryToNav for hir::ExternCrateDecl {
|
|||
);
|
||||
|
||||
res.docs = self.docs(db);
|
||||
res.description = Some(self.display(db).to_string());
|
||||
res.container_name = container_name(db, *self);
|
||||
res.description = Some(self.display(db, edition).to_string());
|
||||
res.container_name = container_name(db, *self, edition);
|
||||
res
|
||||
},
|
||||
))
|
||||
|
@ -444,13 +454,14 @@ impl TryToNav for hir::ExternCrateDecl {
|
|||
impl TryToNav for hir::Field {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let src = self.source(db)?;
|
||||
let edition = self.parent_def(db).module(db).krate().edition(db);
|
||||
|
||||
let field_source = match &src.value {
|
||||
FieldSource::Named(it) => {
|
||||
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.description = Some(self.display(db, edition).to_string());
|
||||
res
|
||||
},
|
||||
)
|
||||
|
@ -531,10 +542,11 @@ impl ToNav for LocalSource {
|
|||
Either::Left(bind_pat) => (bind_pat.syntax(), bind_pat.name()),
|
||||
Either::Right(it) => (it.syntax(), it.name()),
|
||||
};
|
||||
let edition = self.local.parent(db).module(db).krate().edition(db);
|
||||
|
||||
orig_range_with_focus(db, file_id, node, name).map(
|
||||
|(FileRange { file_id, range: full_range }, focus_range)| {
|
||||
let name = local.name(db).display_no_db().to_smolstr();
|
||||
let name = local.name(db).display_no_db(edition).to_smolstr();
|
||||
let kind = if local.is_self(db) {
|
||||
SymbolKind::SelfParam
|
||||
} else if local.is_param(db) {
|
||||
|
@ -567,7 +579,8 @@ impl ToNav for hir::Local {
|
|||
impl TryToNav for hir::Label {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let InFile { file_id, value } = self.source(db)?;
|
||||
let name = self.name(db).display_no_db().to_smolstr();
|
||||
// Labels can't be keywords, so no escaping needed.
|
||||
let name = self.name(db).display_no_db(Edition::Edition2015).to_smolstr();
|
||||
|
||||
Some(orig_range_with_focus(db, file_id, value.syntax(), value.lifetime()).map(
|
||||
|(FileRange { file_id, range: full_range }, focus_range)| NavigationTarget {
|
||||
|
@ -588,7 +601,8 @@ impl TryToNav for hir::Label {
|
|||
impl TryToNav for hir::TypeParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let InFile { file_id, value } = self.merge().source(db)?;
|
||||
let name = self.name(db).display_no_db().to_smolstr();
|
||||
let edition = self.module(db).krate().edition(db);
|
||||
let name = self.name(db).display_no_db(edition).to_smolstr();
|
||||
|
||||
let value = match value {
|
||||
Either::Left(ast::TypeOrConstParam::Type(x)) => Either::Left(x),
|
||||
|
@ -630,7 +644,8 @@ impl TryToNav for hir::TypeOrConstParam {
|
|||
impl TryToNav for hir::LifetimeParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let InFile { file_id, value } = self.source(db)?;
|
||||
let name = self.name(db).display_no_db().to_smolstr();
|
||||
// Lifetimes cannot be keywords, so not escaping needed.
|
||||
let name = self.name(db).display_no_db(Edition::Edition2015).to_smolstr();
|
||||
|
||||
Some(orig_range(db, file_id, value.syntax()).map(
|
||||
|(FileRange { file_id, range: full_range }, focus_range)| NavigationTarget {
|
||||
|
@ -651,7 +666,8 @@ impl TryToNav for hir::LifetimeParam {
|
|||
impl TryToNav for hir::ConstParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let InFile { file_id, value } = self.merge().source(db)?;
|
||||
let name = self.name(db).display_no_db().to_smolstr();
|
||||
let edition = self.module(db).krate().edition(db);
|
||||
let name = self.name(db).display_no_db(edition).to_smolstr();
|
||||
|
||||
let value = match value {
|
||||
Either::Left(ast::TypeOrConstParam::Const(x)) => x,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue