9272: internal: move diagnostics to a dedicated crate r=matklad a=matklad

bors r+

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-06-14 16:46:25 +00:00 committed by GitHub
commit 38ae18b759
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 934 additions and 795 deletions

20
Cargo.lock generated
View file

@ -591,6 +591,7 @@ dependencies = [
"ide_assists",
"ide_completion",
"ide_db",
"ide_diagnostics",
"ide_ssr",
"indexmap",
"itertools",
@ -668,6 +669,25 @@ dependencies = [
"text_edit",
]
[[package]]
name = "ide_diagnostics"
version = "0.0.0"
dependencies = [
"cfg",
"cov-mark",
"either",
"expect-test",
"hir",
"ide_db",
"itertools",
"profile",
"rustc-hash",
"stdx",
"syntax",
"test_utils",
"text_edit",
]
[[package]]
name = "ide_ssr"
version = "0.0.0"

View file

@ -24,6 +24,14 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
(db, fixture.files[0])
}
fn with_many_files(ra_fixture: &str) -> (Self, Vec<FileId>) {
let fixture = ChangeFixture::parse(ra_fixture);
let mut db = Self::default();
fixture.change.apply(&mut db);
assert!(fixture.file_position.is_none());
(db, fixture.files)
}
fn with_files(ra_fixture: &str) -> Self {
let fixture = ChangeFixture::parse(ra_fixture);
let mut db = Self::default();

View file

@ -29,6 +29,7 @@ ide_db = { path = "../ide_db", version = "0.0.0" }
cfg = { path = "../cfg", version = "0.0.0" }
profile = { path = "../profile", version = "0.0.0" }
ide_assists = { path = "../ide_assists", version = "0.0.0" }
ide_diagnostics = { path = "../ide_diagnostics", version = "0.0.0" }
ide_ssr = { path = "../ide_ssr", version = "0.0.0" }
ide_completion = { path = "../ide_completion", version = "0.0.0" }

View file

@ -12,14 +12,6 @@ pub(crate) fn file(ra_fixture: &str) -> (Analysis, FileId) {
(host.analysis(), change_fixture.files[0])
}
/// Creates analysis for many files.
pub(crate) fn files(ra_fixture: &str) -> (Analysis, Vec<FileId>) {
let mut host = AnalysisHost::default();
let change_fixture = ChangeFixture::parse(ra_fixture);
host.db.apply_change(change_fixture.change);
(host.analysis(), change_fixture.files)
}
/// Creates analysis from a multi-file fixture, returns positions marked with $0.
pub(crate) fn position(ra_fixture: &str) -> (Analysis, FilePosition) {
let mut host = AnalysisHost::default();

View file

@ -24,7 +24,6 @@ mod display;
mod annotations;
mod call_hierarchy;
mod diagnostics;
mod expand_macro;
mod extend_selection;
mod file_structure;
@ -40,6 +39,7 @@ mod matching_brace;
mod move_item;
mod parent_module;
mod references;
mod rename;
mod fn_references;
mod runnables;
mod ssr;
@ -71,7 +71,6 @@ use crate::display::ToNav;
pub use crate::{
annotations::{Annotation, AnnotationConfig, AnnotationKind},
call_hierarchy::CallItem,
diagnostics::{Diagnostic, DiagnosticsConfig, Severity},
display::navigation_target::NavigationTarget,
expand_macro::ExpandedMacro,
file_structure::{StructureNode, StructureNodeKind},
@ -81,7 +80,8 @@ pub use crate::{
markup::Markup,
move_item::Direction,
prime_caches::PrimeCachesProgress,
references::{rename::RenameError, ReferenceSearchResult},
references::ReferenceSearchResult,
rename::RenameError,
runnables::{Runnable, RunnableKind, TestId},
syntax_highlighting::{
tags::{Highlight, HlMod, HlMods, HlOperator, HlPunct, HlTag},
@ -109,6 +109,7 @@ pub use ide_db::{
symbol_index::Query,
RootDatabase, SymbolKind,
};
pub use ide_diagnostics::{Diagnostic, DiagnosticsConfig, Severity};
pub use ide_ssr::SsrError;
pub use syntax::{TextRange, TextSize};
pub use text_edit::{Indel, TextEdit};
@ -536,7 +537,7 @@ impl Analysis {
) -> Cancellable<Vec<Assist>> {
self.with_db(|db| {
let ssr_assists = ssr::ssr_assists(db, &resolve, frange);
let mut acc = Assist::get(db, config, resolve, frange);
let mut acc = ide_assists::assists(db, config, resolve, frange);
acc.extend(ssr_assists.into_iter());
acc
})
@ -549,7 +550,7 @@ impl Analysis {
resolve: AssistResolveStrategy,
file_id: FileId,
) -> Cancellable<Vec<Diagnostic>> {
self.with_db(|db| diagnostics::diagnostics(db, config, &resolve, file_id))
self.with_db(|db| ide_diagnostics::diagnostics(db, config, &resolve, file_id))
}
/// Convenience function to return assists + quick fixes for diagnostics
@ -568,7 +569,7 @@ impl Analysis {
self.with_db(|db| {
let ssr_assists = ssr::ssr_assists(db, &resolve, frange);
let diagnostic_assists = if include_fixes {
diagnostics::diagnostics(db, diagnostics_config, &resolve, frange.file_id)
ide_diagnostics::diagnostics(db, diagnostics_config, &resolve, frange.file_id)
.into_iter()
.flat_map(|it| it.fixes.unwrap_or_default())
.filter(|it| it.target.intersect(frange.range).is_some())
@ -577,7 +578,7 @@ impl Analysis {
Vec::new()
};
let mut res = Assist::get(db, assist_config, resolve, frange);
let mut res = ide_assists::assists(db, assist_config, resolve, frange);
res.extend(ssr_assists.into_iter());
res.extend(diagnostic_assists.into_iter());
@ -592,14 +593,14 @@ impl Analysis {
position: FilePosition,
new_name: &str,
) -> Cancellable<Result<SourceChange, RenameError>> {
self.with_db(|db| references::rename::rename(db, position, new_name))
self.with_db(|db| rename::rename(db, position, new_name))
}
pub fn prepare_rename(
&self,
position: FilePosition,
) -> Cancellable<Result<RangeInfo<()>, RenameError>> {
self.with_db(|db| references::rename::prepare_rename(db, position))
self.with_db(|db| rename::prepare_rename(db, position))
}
pub fn will_rename_file(
@ -607,7 +608,7 @@ impl Analysis {
file_id: FileId,
new_name_stem: &str,
) -> Cancellable<Option<SourceChange>> {
self.with_db(|db| references::rename::will_rename_file(db, file_id, new_name_stem))
self.with_db(|db| rename::will_rename_file(db, file_id, new_name_stem))
}
pub fn structural_search_replace(

View file

@ -9,8 +9,6 @@
//! at the index that the match starts at and its tree parent is
//! resolved to the search element definition, we get a reference.
pub(crate) mod rename;
use hir::{PathResolution, Semantics};
use ide_db::{
base_db::FileId,

View file

@ -1,45 +1,25 @@
//! Renaming functionality
//! Renaming functionality.
//!
//! All reference and file rename requests go through here where the corresponding [`SourceChange`]s
//! will be calculated.
use std::fmt::{self, Display};
use either::Either;
use hir::{AsAssocItem, FieldSource, HasSource, InFile, ModuleSource, Semantics};
//! This is mostly front-end for [`ide_db::rename`], but it also includes the
//! tests. This module also implements a couple of magic tricks, like renaming
//! `self` and to `self` (to switch between associated function and method).
use hir::{AsAssocItem, InFile, Semantics};
use ide_db::{
base_db::{AnchoredPathBuf, FileId, FileRange},
base_db::FileId,
defs::{Definition, NameClass, NameRefClass},
search::FileReference,
rename::{bail, format_err, source_edit_from_references, IdentifierKind},
RootDatabase,
};
use stdx::never;
use syntax::{
ast::{self, NameOwner},
lex_single_syntax_kind, AstNode, SyntaxKind, SyntaxNode, T,
};
use syntax::{ast, AstNode, SyntaxNode};
use text_edit::TextEdit;
use crate::{FilePosition, FileSystemEdit, RangeInfo, SourceChange, TextRange};
use crate::{FilePosition, RangeInfo, SourceChange};
pub use ide_db::rename::RenameError;
type RenameResult<T> = Result<T, RenameError>;
#[derive(Debug)]
pub struct RenameError(String);
impl fmt::Display for RenameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
macro_rules! format_err {
($fmt:expr) => {RenameError(format!($fmt))};
($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))}
}
macro_rules! bail {
($($tokens:tt)*) => {return Err(format_err!($($tokens)*))}
}
/// Prepares a rename. The sole job of this function is to return the TextRange of the thing that is
/// being targeted for a rename.
@ -52,7 +32,8 @@ pub(crate) fn prepare_rename(
let syntax = source_file.syntax();
let def = find_definition(&sema, syntax, position)?;
let frange = def_name_range(&&sema, def)
let frange = def
.range_for_rename(&sema)
.ok_or_else(|| format_err!("No references found at position"))?;
Ok(RangeInfo::new(frange.range, ()))
}
@ -98,14 +79,7 @@ pub(crate) fn rename_with_semantics(
}
}
match def {
Definition::ModuleDef(hir::ModuleDef::Module(module)) => rename_mod(sema, module, new_name),
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => {
bail!("Cannot rename builtin type")
}
def => rename_reference(sema, def, new_name),
}
def.rename(sema, new_name)
}
/// Called by the client when it is about to rename a file.
@ -116,38 +90,12 @@ pub(crate) fn will_rename_file(
) -> Option<SourceChange> {
let sema = Semantics::new(db);
let module = sema.to_module_def(file_id)?;
let mut change = rename_mod(&sema, module, new_name_stem).ok()?;
let def = Definition::ModuleDef(module.into());
let mut change = def.rename(&sema, new_name_stem).ok()?;
change.file_system_edits.clear();
Some(change)
}
#[derive(Copy, Clone, Debug, PartialEq)]
enum IdentifierKind {
Ident,
Lifetime,
Underscore,
}
impl IdentifierKind {
fn classify(new_name: &str) -> RenameResult<IdentifierKind> {
match lex_single_syntax_kind(new_name) {
Some(res) => match res {
(SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident),
(T![_], _) => Ok(IdentifierKind::Underscore),
(SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
Ok(IdentifierKind::Lifetime)
}
(SyntaxKind::LIFETIME_IDENT, _) => {
bail!("Invalid name `{}`: not a lifetime identifier", new_name)
}
(_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
(_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
},
None => bail!("Invalid name `{}`: not an identifier", new_name),
}
}
}
fn find_definition(
sema: &Semantics<RootDatabase>,
syntax: &SyntaxNode,
@ -189,126 +137,6 @@ fn find_definition(
.ok_or_else(|| format_err!("No references found at position"))
}
fn rename_mod(
sema: &Semantics<RootDatabase>,
module: hir::Module,
new_name: &str,
) -> RenameResult<SourceChange> {
if IdentifierKind::classify(new_name)? != IdentifierKind::Ident {
bail!("Invalid name `{0}`: cannot rename module to {0}", new_name);
}
let mut source_change = SourceChange::default();
let InFile { file_id, value: def_source } = module.definition_source(sema.db);
let file_id = file_id.original_file(sema.db);
if let ModuleSource::SourceFile(..) = def_source {
// mod is defined in path/to/dir/mod.rs
let path = if module.is_mod_rs(sema.db) {
format!("../{}/mod.rs", new_name)
} else {
format!("{}.rs", new_name)
};
let dst = AnchoredPathBuf { anchor: file_id, path };
let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
source_change.push_file_system_edit(move_file);
}
if let Some(InFile { file_id, value: decl_source }) = module.declaration_source(sema.db) {
let file_id = file_id.original_file(sema.db);
match decl_source.name() {
Some(name) => source_change.insert_source_edit(
file_id,
TextEdit::replace(name.syntax().text_range(), new_name.to_string()),
),
_ => never!("Module source node is missing a name"),
}
}
let def = Definition::ModuleDef(hir::ModuleDef::Module(module));
let usages = def.usages(sema).all();
let ref_edits = usages.iter().map(|(&file_id, references)| {
(file_id, source_edit_from_references(references, def, new_name))
});
source_change.extend(ref_edits);
Ok(source_change)
}
fn rename_reference(
sema: &Semantics<RootDatabase>,
mut def: Definition,
new_name: &str,
) -> RenameResult<SourceChange> {
let ident_kind = IdentifierKind::classify(new_name)?;
if matches!(
def, // is target a lifetime?
Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
) {
match ident_kind {
IdentifierKind::Ident | IdentifierKind::Underscore => {
cov_mark::hit!(rename_not_a_lifetime_ident_ref);
bail!("Invalid name `{}`: not a lifetime identifier", new_name);
}
IdentifierKind::Lifetime => cov_mark::hit!(rename_lifetime),
}
} else {
match (ident_kind, def) {
(IdentifierKind::Lifetime, _) => {
cov_mark::hit!(rename_not_an_ident_ref);
bail!("Invalid name `{}`: not an identifier", new_name);
}
(IdentifierKind::Ident, _) => cov_mark::hit!(rename_non_local),
(IdentifierKind::Underscore, _) => (),
}
}
def = match def {
// HACK: resolve trait impl items to the item def of the trait definition
// so that we properly resolve all trait item references
Definition::ModuleDef(mod_def) => mod_def
.as_assoc_item(sema.db)
.and_then(|it| it.containing_trait_impl(sema.db))
.and_then(|it| {
it.items(sema.db).into_iter().find_map(|it| match (it, mod_def) {
(hir::AssocItem::Function(trait_func), hir::ModuleDef::Function(func))
if trait_func.name(sema.db) == func.name(sema.db) =>
{
Some(Definition::ModuleDef(hir::ModuleDef::Function(trait_func)))
}
(hir::AssocItem::Const(trait_konst), hir::ModuleDef::Const(konst))
if trait_konst.name(sema.db) == konst.name(sema.db) =>
{
Some(Definition::ModuleDef(hir::ModuleDef::Const(trait_konst)))
}
(
hir::AssocItem::TypeAlias(trait_type_alias),
hir::ModuleDef::TypeAlias(type_alias),
) if trait_type_alias.name(sema.db) == type_alias.name(sema.db) => {
Some(Definition::ModuleDef(hir::ModuleDef::TypeAlias(trait_type_alias)))
}
_ => None,
})
})
.unwrap_or(def),
_ => def,
};
let usages = def.usages(sema).all();
if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {
cov_mark::hit!(rename_underscore_multiple);
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
}
let mut source_change = SourceChange::default();
source_change.extend(usages.iter().map(|(&file_id, references)| {
(file_id, source_edit_from_references(references, def, new_name))
}));
let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
source_change.insert_source_edit(file_id, edit);
Ok(source_change)
}
fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameResult<SourceChange> {
if never!(local.is_self(sema.db)) {
bail!("rename_to_self invoked on self");
@ -426,243 +254,6 @@ fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Opt
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
}
fn source_edit_from_references(
references: &[FileReference],
def: Definition,
new_name: &str,
) -> TextEdit {
let mut edit = TextEdit::builder();
for reference in references {
let (range, replacement) = match &reference.name {
// if the ranges differ then the node is inside a macro call, we can't really attempt
// to make special rewrites like shorthand syntax and such, so just rename the node in
// the macro input
ast::NameLike::NameRef(name_ref)
if name_ref.syntax().text_range() == reference.range =>
{
source_edit_from_name_ref(name_ref, new_name, def)
}
ast::NameLike::Name(name) if name.syntax().text_range() == reference.range => {
source_edit_from_name(name, new_name)
}
_ => None,
}
.unwrap_or_else(|| (reference.range, new_name.to_string()));
edit.replace(range, replacement);
}
edit.finish()
}
fn source_edit_from_name(name: &ast::Name, new_name: &str) -> Option<(TextRange, String)> {
if let Some(_) = ast::RecordPatField::for_field_name(name) {
if let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) {
return Some((
TextRange::empty(ident_pat.syntax().text_range().start()),
[new_name, ": "].concat(),
));
}
}
None
}
fn source_edit_from_name_ref(
name_ref: &ast::NameRef,
new_name: &str,
def: Definition,
) -> Option<(TextRange, String)> {
if let Some(record_field) = ast::RecordExprField::for_name_ref(name_ref) {
let rcf_name_ref = record_field.name_ref();
let rcf_expr = record_field.expr();
match (rcf_name_ref, rcf_expr.and_then(|it| it.name_ref())) {
// field: init-expr, check if we can use a field init shorthand
(Some(field_name), Some(init)) => {
if field_name == *name_ref {
if init.text() == new_name {
cov_mark::hit!(test_rename_field_put_init_shorthand);
// same names, we can use a shorthand here instead.
// we do not want to erase attributes hence this range start
let s = field_name.syntax().text_range().start();
let e = record_field.syntax().text_range().end();
return Some((TextRange::new(s, e), new_name.to_owned()));
}
} else if init == *name_ref {
if field_name.text() == new_name {
cov_mark::hit!(test_rename_local_put_init_shorthand);
// same names, we can use a shorthand here instead.
// we do not want to erase attributes hence this range start
let s = field_name.syntax().text_range().start();
let e = record_field.syntax().text_range().end();
return Some((TextRange::new(s, e), new_name.to_owned()));
}
}
None
}
// init shorthand
// FIXME: instead of splitting the shorthand, recursively trigger a rename of the
// other name https://github.com/rust-analyzer/rust-analyzer/issues/6547
(None, Some(_)) if matches!(def, Definition::Field(_)) => {
cov_mark::hit!(test_rename_field_in_field_shorthand);
let s = name_ref.syntax().text_range().start();
Some((TextRange::empty(s), format!("{}: ", new_name)))
}
(None, Some(_)) if matches!(def, Definition::Local(_)) => {
cov_mark::hit!(test_rename_local_in_field_shorthand);
let s = name_ref.syntax().text_range().end();
Some((TextRange::empty(s), format!(": {}", new_name)))
}
_ => None,
}
} else if let Some(record_field) = ast::RecordPatField::for_field_name_ref(name_ref) {
let rcf_name_ref = record_field.name_ref();
let rcf_pat = record_field.pat();
match (rcf_name_ref, rcf_pat) {
// field: rename
(Some(field_name), Some(ast::Pat::IdentPat(pat))) if field_name == *name_ref => {
// field name is being renamed
if pat.name().map_or(false, |it| it.text() == new_name) {
cov_mark::hit!(test_rename_field_put_init_shorthand_pat);
// same names, we can use a shorthand here instead/
// we do not want to erase attributes hence this range start
let s = field_name.syntax().text_range().start();
let e = record_field.syntax().text_range().end();
Some((TextRange::new(s, e), pat.to_string()))
} else {
None
}
}
_ => None,
}
} else {
None
}
}
fn source_edit_from_def(
sema: &Semantics<RootDatabase>,
def: Definition,
new_name: &str,
) -> RenameResult<(FileId, TextEdit)> {
let frange: FileRange = def_name_range(sema, def)
.ok_or_else(|| format_err!("No identifier available to rename"))?;
let mut replacement_text = String::new();
let mut repl_range = frange.range;
if let Definition::Local(local) = def {
if let Either::Left(pat) = local.source(sema.db).value {
if matches!(
pat.syntax().parent().and_then(ast::RecordPatField::cast),
Some(pat_field) if pat_field.name_ref().is_none()
) {
replacement_text.push_str(": ");
replacement_text.push_str(new_name);
repl_range = TextRange::new(
pat.syntax().text_range().end(),
pat.syntax().text_range().end(),
);
}
}
}
if replacement_text.is_empty() {
replacement_text.push_str(new_name);
}
let edit = TextEdit::replace(repl_range, replacement_text);
Ok((frange.file_id, edit))
}
fn def_name_range(sema: &Semantics<RootDatabase>, def: Definition) -> Option<FileRange> {
// FIXME: the `original_file_range` calls here are wrong -- they never fail,
// and _fall back_ to the entirety of the macro call. Such fall back is
// incorrect for renames. The safe behavior would be to return an error for
// such cases. The correct behavior would be to return an auxiliary list of
// "can't rename these occurrences in macros" items, and then show some kind
// of a dialog to the user.
let res = match def {
Definition::Macro(mac) => {
let src = mac.source(sema.db)?;
let name = match &src.value {
Either::Left(it) => it.name()?,
Either::Right(it) => it.name()?,
};
src.with_value(name.syntax()).original_file_range(sema.db)
}
Definition::Field(field) => {
let src = field.source(sema.db)?;
match &src.value {
FieldSource::Named(record_field) => {
let name = record_field.name()?;
src.with_value(name.syntax()).original_file_range(sema.db)
}
FieldSource::Pos(_) => {
return None;
}
}
}
Definition::ModuleDef(module_def) => match module_def {
hir::ModuleDef::Module(module) => {
let src = module.declaration_source(sema.db)?;
let name = src.value.name()?;
src.with_value(name.syntax()).original_file_range(sema.db)
}
hir::ModuleDef::Function(it) => name_range(it, sema)?,
hir::ModuleDef::Adt(adt) => match adt {
hir::Adt::Struct(it) => name_range(it, sema)?,
hir::Adt::Union(it) => name_range(it, sema)?,
hir::Adt::Enum(it) => name_range(it, sema)?,
},
hir::ModuleDef::Variant(it) => name_range(it, sema)?,
hir::ModuleDef::Const(it) => name_range(it, sema)?,
hir::ModuleDef::Static(it) => name_range(it, sema)?,
hir::ModuleDef::Trait(it) => name_range(it, sema)?,
hir::ModuleDef::TypeAlias(it) => name_range(it, sema)?,
hir::ModuleDef::BuiltinType(_) => return None,
},
Definition::SelfType(_) => return None,
Definition::Local(local) => {
let src = local.source(sema.db);
let name = match &src.value {
Either::Left(bind_pat) => bind_pat.name()?,
Either::Right(_) => return None,
};
src.with_value(name.syntax()).original_file_range(sema.db)
}
Definition::GenericParam(generic_param) => match generic_param {
hir::GenericParam::TypeParam(type_param) => {
let src = type_param.source(sema.db)?;
let name = match &src.value {
Either::Left(_) => return None,
Either::Right(type_param) => type_param.name()?,
};
src.with_value(name.syntax()).original_file_range(sema.db)
}
hir::GenericParam::LifetimeParam(lifetime_param) => {
let src = lifetime_param.source(sema.db)?;
let lifetime = src.value.lifetime()?;
src.with_value(lifetime.syntax()).original_file_range(sema.db)
}
hir::GenericParam::ConstParam(it) => name_range(it, sema)?,
},
Definition::Label(label) => {
let src = label.source(sema.db);
let lifetime = src.value.lifetime()?;
src.with_value(lifetime.syntax()).original_file_range(sema.db)
}
};
return Some(res);
fn name_range<D>(def: D, sema: &Semantics<RootDatabase>) -> Option<FileRange>
where
D: HasSource,
D::Ast: ast::NameOwner,
{
let src = def.source(sema.db)?;
let name = src.value.name()?;
let res = src.with_value(name.syntax()).original_file_range(sema.db);
Some(res)
}
}
#[cfg(test)]
mod tests {
use expect_test::{expect, Expect};
@ -2178,4 +1769,22 @@ fn f() { <()>::BAR$0; }"#,
res,
);
}
#[test]
fn macros_are_broken_lol() {
cov_mark::check!(macros_are_broken_lol);
check(
"lol",
r#"
macro_rules! m { () => { fn f() {} } }
m!();
fn main() { f$0() }
"#,
r#"
macro_rules! m { () => { fn f() {} } }
lol
fn main() { lol() }
"#,
)
}
}

View file

@ -17,148 +17,24 @@ mod tests;
pub mod utils;
pub mod path_transform;
use std::str::FromStr;
use hir::Semantics;
use ide_db::{base_db::FileRange, label::Label, source_change::SourceChange, RootDatabase};
use ide_db::{base_db::FileRange, RootDatabase};
use syntax::TextRange;
pub(crate) use crate::assist_context::{AssistContext, Assists};
pub use assist_config::AssistConfig;
pub use ide_db::assists::{
Assist, AssistId, AssistKind, AssistResolveStrategy, GroupLabel, SingleResolve,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssistKind {
// FIXME: does the None variant make sense? Probably not.
None,
QuickFix,
Generate,
Refactor,
RefactorExtract,
RefactorInline,
RefactorRewrite,
}
impl AssistKind {
pub fn contains(self, other: AssistKind) -> bool {
if self == other {
return true;
}
match self {
AssistKind::None | AssistKind::Generate => true,
AssistKind::Refactor => match other {
AssistKind::RefactorExtract
| AssistKind::RefactorInline
| AssistKind::RefactorRewrite => true,
_ => false,
},
_ => false,
}
}
pub fn name(&self) -> &str {
match self {
AssistKind::None => "None",
AssistKind::QuickFix => "QuickFix",
AssistKind::Generate => "Generate",
AssistKind::Refactor => "Refactor",
AssistKind::RefactorExtract => "RefactorExtract",
AssistKind::RefactorInline => "RefactorInline",
AssistKind::RefactorRewrite => "RefactorRewrite",
}
}
}
impl FromStr for AssistKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"None" => Ok(AssistKind::None),
"QuickFix" => Ok(AssistKind::QuickFix),
"Generate" => Ok(AssistKind::Generate),
"Refactor" => Ok(AssistKind::Refactor),
"RefactorExtract" => Ok(AssistKind::RefactorExtract),
"RefactorInline" => Ok(AssistKind::RefactorInline),
"RefactorRewrite" => Ok(AssistKind::RefactorRewrite),
unknown => Err(format!("Unknown AssistKind: '{}'", unknown)),
}
}
}
/// Unique identifier of the assist, should not be shown to the user
/// directly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AssistId(pub &'static str, pub AssistKind);
/// A way to control how many asssist to resolve during the assist resolution.
/// When an assist is resolved, its edits are calculated that might be costly to always do by default.
#[derive(Debug)]
pub enum AssistResolveStrategy {
/// No assists should be resolved.
None,
/// All assists should be resolved.
All,
/// Only a certain assist should be resolved.
Single(SingleResolve),
}
/// Hold the [`AssistId`] data of a certain assist to resolve.
/// The original id object cannot be used due to a `'static` lifetime
/// and the requirement to construct this struct dynamically during the resolve handling.
#[derive(Debug)]
pub struct SingleResolve {
/// The id of the assist.
pub assist_id: String,
// The kind of the assist.
pub assist_kind: AssistKind,
}
impl AssistResolveStrategy {
pub fn should_resolve(&self, id: &AssistId) -> bool {
match self {
AssistResolveStrategy::None => false,
AssistResolveStrategy::All => true,
AssistResolveStrategy::Single(single_resolve) => {
single_resolve.assist_id == id.0 && single_resolve.assist_kind == id.1
}
}
}
}
#[derive(Clone, Debug)]
pub struct GroupLabel(pub String);
#[derive(Debug, Clone)]
pub struct Assist {
pub id: AssistId,
/// Short description of the assist, as shown in the UI.
pub label: Label,
pub group: Option<GroupLabel>,
/// Target ranges are used to sort assists: the smaller the target range,
/// the more specific assist is, and so it should be sorted first.
pub target: TextRange,
/// Computing source change sometimes is much more costly then computing the
/// other fields. Additionally, the actual change is not required to show
/// the lightbulb UI, it only is needed when the user tries to apply an
/// assist. So, we compute it lazily: the API allow requesting assists with
/// or without source change. We could (and in fact, used to) distinguish
/// between resolved and unresolved assists at the type level, but this is
/// cumbersome, especially if you want to embed an assist into another data
/// structure, such as a diagnostic.
pub source_change: Option<SourceChange>,
}
impl Assist {
/// Return all the assists applicable at the given position.
pub fn get(
/// Return all the assists applicable at the given position.
pub fn assists(
db: &RootDatabase,
config: &AssistConfig,
resolve: AssistResolveStrategy,
range: FileRange,
) -> Vec<Assist> {
) -> Vec<Assist> {
let sema = Semantics::new(db);
let ctx = AssistContext::new(sema, config, range);
let mut acc = Assists::new(&ctx, resolve);
@ -166,7 +42,6 @@ impl Assist {
handler(&mut acc, &ctx);
});
acc.finish()
}
}
mod handlers {

View file

@ -16,8 +16,8 @@ use syntax::TextRange;
use test_utils::{assert_eq_text, extract_offset};
use crate::{
handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, AssistResolveStrategy,
Assists, SingleResolve,
assists, handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind,
AssistResolveStrategy, Assists, SingleResolve,
};
pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
@ -78,14 +78,14 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
let before = db.file_text(file_id).to_string();
let frange = FileRange { file_id, range: selection.into() };
let assist = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange)
let assist = assists(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange)
.into_iter()
.find(|assist| assist.id.0 == assist_id)
.unwrap_or_else(|| {
panic!(
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
assist_id,
Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange)
assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange)
.into_iter()
.map(|assist| assist.id.0)
.collect::<Vec<_>>()
@ -210,7 +210,7 @@ fn assist_order_field_struct() {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
let assists = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
let assists = assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
let mut assists = assists.iter();
assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)");
@ -235,7 +235,7 @@ pub fn test_some_range(a: int) -> bool {
"#,
);
let assists = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
let assists = assists(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
let expected = labels(&assists);
expect![[r#"
@ -264,7 +264,7 @@ pub fn test_some_range(a: int) -> bool {
let mut cfg = TEST_CONFIG;
cfg.allowed = Some(vec![AssistKind::Refactor]);
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
let expected = labels(&assists);
expect![[r#"
@ -279,7 +279,7 @@ pub fn test_some_range(a: int) -> bool {
{
let mut cfg = TEST_CONFIG;
cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
let expected = labels(&assists);
expect![[r#"
@ -292,7 +292,7 @@ pub fn test_some_range(a: int) -> bool {
{
let mut cfg = TEST_CONFIG;
cfg.allowed = Some(vec![AssistKind::QuickFix]);
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
let expected = labels(&assists);
expect![[r#""#]].assert_eq(&expected);
@ -317,7 +317,7 @@ pub fn test_some_range(a: int) -> bool {
cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
{
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange);
assert_eq!(2, assists.len());
let mut assists = assists.into_iter();
@ -353,7 +353,7 @@ pub fn test_some_range(a: int) -> bool {
}
{
let assists = Assist::get(
let assists = assists(
&db,
&cfg,
AssistResolveStrategy::Single(SingleResolve {
@ -397,7 +397,7 @@ pub fn test_some_range(a: int) -> bool {
}
{
let assists = Assist::get(
let assists = assists(
&db,
&cfg,
AssistResolveStrategy::Single(SingleResolve {
@ -462,7 +462,7 @@ pub fn test_some_range(a: int) -> bool {
}
{
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::All, frange);
let assists = assists(&db, &cfg, AssistResolveStrategy::All, frange);
assert_eq!(2, assists.len());
let mut assists = assists.into_iter();

View file

@ -0,0 +1,136 @@
//! This module defines the `Assist` data structure. The actual assist live in
//! the `ide_assists` downstream crate. We want to define the data structures in
//! this low-level crate though, because `ide_diagnostics` also need them
//! (fixits for diagnostics and assists are the same thing under the hood). We
//! want to compile `ide_assists` and `ide_diagnostics` in parallel though, so
//! we pull the common definitions upstream, to this crate.
use std::str::FromStr;
use syntax::TextRange;
use crate::{label::Label, source_change::SourceChange};
#[derive(Debug, Clone)]
pub struct Assist {
pub id: AssistId,
/// Short description of the assist, as shown in the UI.
pub label: Label,
pub group: Option<GroupLabel>,
/// Target ranges are used to sort assists: the smaller the target range,
/// the more specific assist is, and so it should be sorted first.
pub target: TextRange,
/// Computing source change sometimes is much more costly then computing the
/// other fields. Additionally, the actual change is not required to show
/// the lightbulb UI, it only is needed when the user tries to apply an
/// assist. So, we compute it lazily: the API allow requesting assists with
/// or without source change. We could (and in fact, used to) distinguish
/// between resolved and unresolved assists at the type level, but this is
/// cumbersome, especially if you want to embed an assist into another data
/// structure, such as a diagnostic.
pub source_change: Option<SourceChange>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssistKind {
// FIXME: does the None variant make sense? Probably not.
None,
QuickFix,
Generate,
Refactor,
RefactorExtract,
RefactorInline,
RefactorRewrite,
}
impl AssistKind {
pub fn contains(self, other: AssistKind) -> bool {
if self == other {
return true;
}
match self {
AssistKind::None | AssistKind::Generate => true,
AssistKind::Refactor => match other {
AssistKind::RefactorExtract
| AssistKind::RefactorInline
| AssistKind::RefactorRewrite => true,
_ => false,
},
_ => false,
}
}
pub fn name(&self) -> &str {
match self {
AssistKind::None => "None",
AssistKind::QuickFix => "QuickFix",
AssistKind::Generate => "Generate",
AssistKind::Refactor => "Refactor",
AssistKind::RefactorExtract => "RefactorExtract",
AssistKind::RefactorInline => "RefactorInline",
AssistKind::RefactorRewrite => "RefactorRewrite",
}
}
}
impl FromStr for AssistKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"None" => Ok(AssistKind::None),
"QuickFix" => Ok(AssistKind::QuickFix),
"Generate" => Ok(AssistKind::Generate),
"Refactor" => Ok(AssistKind::Refactor),
"RefactorExtract" => Ok(AssistKind::RefactorExtract),
"RefactorInline" => Ok(AssistKind::RefactorInline),
"RefactorRewrite" => Ok(AssistKind::RefactorRewrite),
unknown => Err(format!("Unknown AssistKind: '{}'", unknown)),
}
}
}
/// Unique identifier of the assist, should not be shown to the user
/// directly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AssistId(pub &'static str, pub AssistKind);
/// A way to control how many asssist to resolve during the assist resolution.
/// When an assist is resolved, its edits are calculated that might be costly to always do by default.
#[derive(Debug)]
pub enum AssistResolveStrategy {
/// No assists should be resolved.
None,
/// All assists should be resolved.
All,
/// Only a certain assist should be resolved.
Single(SingleResolve),
}
/// Hold the [`AssistId`] data of a certain assist to resolve.
/// The original id object cannot be used due to a `'static` lifetime
/// and the requirement to construct this struct dynamically during the resolve handling.
#[derive(Debug)]
pub struct SingleResolve {
/// The id of the assist.
pub assist_id: String,
// The kind of the assist.
pub assist_kind: AssistKind,
}
impl AssistResolveStrategy {
pub fn should_resolve(&self, id: &AssistId) -> bool {
match self {
AssistResolveStrategy::None => false,
AssistResolveStrategy::All => true,
AssistResolveStrategy::Single(single_resolve) => {
single_resolve.assist_id == id.0 && single_resolve.assist_kind == id.1
}
}
}
}
#[derive(Clone, Debug)]
pub struct GroupLabel(pub String);

View file

@ -3,11 +3,11 @@
//! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search.
mod apply_change;
pub mod assists;
pub mod label;
pub mod line_index;
pub mod symbol_index;
pub mod defs;
pub mod search;
pub mod items_locator;
pub mod source_change;
pub mod ty_filter;
@ -15,6 +15,9 @@ pub mod traits;
pub mod call_info;
pub mod helpers;
pub mod search;
pub mod rename;
use std::{fmt, sync::Arc};
use base_db::{

468
crates/ide_db/src/rename.rs Normal file
View file

@ -0,0 +1,468 @@
//! Rename infrastructure for rust-analyzer. It is used primarily for the
//! literal "rename" in the ide (look for tests there), but it is also available
//! as a general-purpose service. For example, it is used by the fix for the
//! "incorrect case" diagnostic.
//!
//! It leverages the [`crate::search`] functionality to find what needs to be
//! renamed. The actual renames are tricky -- field shorthands need special
//! attention, and, when renaming modules, you also want to rename files on the
//! file system.
//!
//! Another can of worms are macros:
//!
//! ```
//! macro_rules! m { () => { fn f() {} } }
//! m!();
//! fn main() {
//! f() // <- rename me
//! }
//! ```
//!
//! The correct behavior in such cases is probably to show a dialog to the user.
//! Our current behavior is ¯\_(ツ)_/¯.
use std::fmt;
use base_db::{AnchoredPathBuf, FileId, FileRange};
use either::Either;
use hir::{AsAssocItem, FieldSource, HasSource, InFile, ModuleSource, Semantics};
use stdx::never;
use syntax::{
ast::{self, NameOwner},
lex_single_syntax_kind, AstNode, SyntaxKind, TextRange, T,
};
use text_edit::TextEdit;
use crate::{
defs::Definition,
search::FileReference,
source_change::{FileSystemEdit, SourceChange},
RootDatabase,
};
pub type Result<T, E = RenameError> = std::result::Result<T, E>;
#[derive(Debug)]
pub struct RenameError(pub String);
impl fmt::Display for RenameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
#[macro_export]
macro_rules! _format_err {
($fmt:expr) => { RenameError(format!($fmt)) };
($fmt:expr, $($arg:tt)+) => { RenameError(format!($fmt, $($arg)+)) }
}
pub use _format_err as format_err;
#[macro_export]
macro_rules! _bail {
($($tokens:tt)*) => { return Err(format_err!($($tokens)*)) }
}
pub use _bail as bail;
impl Definition {
pub fn rename(&self, sema: &Semantics<RootDatabase>, new_name: &str) -> Result<SourceChange> {
match *self {
Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
rename_mod(sema, module, new_name)
}
Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => {
bail!("Cannot rename builtin type")
}
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
def => rename_reference(sema, def, new_name),
}
}
/// Textual range of the identifier which will change when renaming this
/// `Definition`. Note that some definitions, like buitin types, can't be
/// renamed.
pub fn range_for_rename(self, sema: &Semantics<RootDatabase>) -> Option<FileRange> {
// FIXME: the `original_file_range` calls here are wrong -- they never fail,
// and _fall back_ to the entirety of the macro call. Such fall back is
// incorrect for renames. The safe behavior would be to return an error for
// such cases. The correct behavior would be to return an auxiliary list of
// "can't rename these occurrences in macros" items, and then show some kind
// of a dialog to the user. See:
cov_mark::hit!(macros_are_broken_lol);
let res = match self {
Definition::Macro(mac) => {
let src = mac.source(sema.db)?;
let name = match &src.value {
Either::Left(it) => it.name()?,
Either::Right(it) => it.name()?,
};
src.with_value(name.syntax()).original_file_range(sema.db)
}
Definition::Field(field) => {
let src = field.source(sema.db)?;
match &src.value {
FieldSource::Named(record_field) => {
let name = record_field.name()?;
src.with_value(name.syntax()).original_file_range(sema.db)
}
FieldSource::Pos(_) => {
return None;
}
}
}
Definition::ModuleDef(module_def) => match module_def {
hir::ModuleDef::Module(module) => {
let src = module.declaration_source(sema.db)?;
let name = src.value.name()?;
src.with_value(name.syntax()).original_file_range(sema.db)
}
hir::ModuleDef::Function(it) => name_range(it, sema)?,
hir::ModuleDef::Adt(adt) => match adt {
hir::Adt::Struct(it) => name_range(it, sema)?,
hir::Adt::Union(it) => name_range(it, sema)?,
hir::Adt::Enum(it) => name_range(it, sema)?,
},
hir::ModuleDef::Variant(it) => name_range(it, sema)?,
hir::ModuleDef::Const(it) => name_range(it, sema)?,
hir::ModuleDef::Static(it) => name_range(it, sema)?,
hir::ModuleDef::Trait(it) => name_range(it, sema)?,
hir::ModuleDef::TypeAlias(it) => name_range(it, sema)?,
hir::ModuleDef::BuiltinType(_) => return None,
},
Definition::SelfType(_) => return None,
Definition::Local(local) => {
let src = local.source(sema.db);
let name = match &src.value {
Either::Left(bind_pat) => bind_pat.name()?,
Either::Right(_) => return None,
};
src.with_value(name.syntax()).original_file_range(sema.db)
}
Definition::GenericParam(generic_param) => match generic_param {
hir::GenericParam::TypeParam(type_param) => {
let src = type_param.source(sema.db)?;
let name = match &src.value {
Either::Left(_) => return None,
Either::Right(type_param) => type_param.name()?,
};
src.with_value(name.syntax()).original_file_range(sema.db)
}
hir::GenericParam::LifetimeParam(lifetime_param) => {
let src = lifetime_param.source(sema.db)?;
let lifetime = src.value.lifetime()?;
src.with_value(lifetime.syntax()).original_file_range(sema.db)
}
hir::GenericParam::ConstParam(it) => name_range(it, sema)?,
},
Definition::Label(label) => {
let src = label.source(sema.db);
let lifetime = src.value.lifetime()?;
src.with_value(lifetime.syntax()).original_file_range(sema.db)
}
};
return Some(res);
fn name_range<D>(def: D, sema: &Semantics<RootDatabase>) -> Option<FileRange>
where
D: HasSource,
D::Ast: ast::NameOwner,
{
let src = def.source(sema.db)?;
let name = src.value.name()?;
let res = src.with_value(name.syntax()).original_file_range(sema.db);
Some(res)
}
}
}
fn rename_mod(
sema: &Semantics<RootDatabase>,
module: hir::Module,
new_name: &str,
) -> Result<SourceChange> {
if IdentifierKind::classify(new_name)? != IdentifierKind::Ident {
bail!("Invalid name `{0}`: cannot rename module to {0}", new_name);
}
let mut source_change = SourceChange::default();
let InFile { file_id, value: def_source } = module.definition_source(sema.db);
let file_id = file_id.original_file(sema.db);
if let ModuleSource::SourceFile(..) = def_source {
// mod is defined in path/to/dir/mod.rs
let path = if module.is_mod_rs(sema.db) {
format!("../{}/mod.rs", new_name)
} else {
format!("{}.rs", new_name)
};
let dst = AnchoredPathBuf { anchor: file_id, path };
let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
source_change.push_file_system_edit(move_file);
}
if let Some(InFile { file_id, value: decl_source }) = module.declaration_source(sema.db) {
let file_id = file_id.original_file(sema.db);
match decl_source.name() {
Some(name) => source_change.insert_source_edit(
file_id,
TextEdit::replace(name.syntax().text_range(), new_name.to_string()),
),
_ => never!("Module source node is missing a name"),
}
}
let def = Definition::ModuleDef(hir::ModuleDef::Module(module));
let usages = def.usages(sema).all();
let ref_edits = usages.iter().map(|(&file_id, references)| {
(file_id, source_edit_from_references(references, def, new_name))
});
source_change.extend(ref_edits);
Ok(source_change)
}
fn rename_reference(
sema: &Semantics<RootDatabase>,
mut def: Definition,
new_name: &str,
) -> Result<SourceChange> {
let ident_kind = IdentifierKind::classify(new_name)?;
if matches!(
def, // is target a lifetime?
Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
) {
match ident_kind {
IdentifierKind::Ident | IdentifierKind::Underscore => {
cov_mark::hit!(rename_not_a_lifetime_ident_ref);
bail!("Invalid name `{}`: not a lifetime identifier", new_name);
}
IdentifierKind::Lifetime => cov_mark::hit!(rename_lifetime),
}
} else {
match (ident_kind, def) {
(IdentifierKind::Lifetime, _) => {
cov_mark::hit!(rename_not_an_ident_ref);
bail!("Invalid name `{}`: not an identifier", new_name);
}
(IdentifierKind::Ident, _) => cov_mark::hit!(rename_non_local),
(IdentifierKind::Underscore, _) => (),
}
}
def = match def {
// HACK: resolve trait impl items to the item def of the trait definition
// so that we properly resolve all trait item references
Definition::ModuleDef(mod_def) => mod_def
.as_assoc_item(sema.db)
.and_then(|it| it.containing_trait_impl(sema.db))
.and_then(|it| {
it.items(sema.db).into_iter().find_map(|it| match (it, mod_def) {
(hir::AssocItem::Function(trait_func), hir::ModuleDef::Function(func))
if trait_func.name(sema.db) == func.name(sema.db) =>
{
Some(Definition::ModuleDef(hir::ModuleDef::Function(trait_func)))
}
(hir::AssocItem::Const(trait_konst), hir::ModuleDef::Const(konst))
if trait_konst.name(sema.db) == konst.name(sema.db) =>
{
Some(Definition::ModuleDef(hir::ModuleDef::Const(trait_konst)))
}
(
hir::AssocItem::TypeAlias(trait_type_alias),
hir::ModuleDef::TypeAlias(type_alias),
) if trait_type_alias.name(sema.db) == type_alias.name(sema.db) => {
Some(Definition::ModuleDef(hir::ModuleDef::TypeAlias(trait_type_alias)))
}
_ => None,
})
})
.unwrap_or(def),
_ => def,
};
let usages = def.usages(sema).all();
if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {
cov_mark::hit!(rename_underscore_multiple);
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
}
let mut source_change = SourceChange::default();
source_change.extend(usages.iter().map(|(&file_id, references)| {
(file_id, source_edit_from_references(references, def, new_name))
}));
let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
source_change.insert_source_edit(file_id, edit);
Ok(source_change)
}
pub fn source_edit_from_references(
references: &[FileReference],
def: Definition,
new_name: &str,
) -> TextEdit {
let mut edit = TextEdit::builder();
for reference in references {
let (range, replacement) = match &reference.name {
// if the ranges differ then the node is inside a macro call, we can't really attempt
// to make special rewrites like shorthand syntax and such, so just rename the node in
// the macro input
ast::NameLike::NameRef(name_ref)
if name_ref.syntax().text_range() == reference.range =>
{
source_edit_from_name_ref(name_ref, new_name, def)
}
ast::NameLike::Name(name) if name.syntax().text_range() == reference.range => {
source_edit_from_name(name, new_name)
}
_ => None,
}
.unwrap_or_else(|| (reference.range, new_name.to_string()));
edit.replace(range, replacement);
}
edit.finish()
}
fn source_edit_from_name(name: &ast::Name, new_name: &str) -> Option<(TextRange, String)> {
if let Some(_) = ast::RecordPatField::for_field_name(name) {
if let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) {
return Some((
TextRange::empty(ident_pat.syntax().text_range().start()),
[new_name, ": "].concat(),
));
}
}
None
}
fn source_edit_from_name_ref(
name_ref: &ast::NameRef,
new_name: &str,
def: Definition,
) -> Option<(TextRange, String)> {
if let Some(record_field) = ast::RecordExprField::for_name_ref(name_ref) {
let rcf_name_ref = record_field.name_ref();
let rcf_expr = record_field.expr();
match (rcf_name_ref, rcf_expr.and_then(|it| it.name_ref())) {
// field: init-expr, check if we can use a field init shorthand
(Some(field_name), Some(init)) => {
if field_name == *name_ref {
if init.text() == new_name {
cov_mark::hit!(test_rename_field_put_init_shorthand);
// same names, we can use a shorthand here instead.
// we do not want to erase attributes hence this range start
let s = field_name.syntax().text_range().start();
let e = record_field.syntax().text_range().end();
return Some((TextRange::new(s, e), new_name.to_owned()));
}
} else if init == *name_ref {
if field_name.text() == new_name {
cov_mark::hit!(test_rename_local_put_init_shorthand);
// same names, we can use a shorthand here instead.
// we do not want to erase attributes hence this range start
let s = field_name.syntax().text_range().start();
let e = record_field.syntax().text_range().end();
return Some((TextRange::new(s, e), new_name.to_owned()));
}
}
None
}
// init shorthand
// FIXME: instead of splitting the shorthand, recursively trigger a rename of the
// other name https://github.com/rust-analyzer/rust-analyzer/issues/6547
(None, Some(_)) if matches!(def, Definition::Field(_)) => {
cov_mark::hit!(test_rename_field_in_field_shorthand);
let s = name_ref.syntax().text_range().start();
Some((TextRange::empty(s), format!("{}: ", new_name)))
}
(None, Some(_)) if matches!(def, Definition::Local(_)) => {
cov_mark::hit!(test_rename_local_in_field_shorthand);
let s = name_ref.syntax().text_range().end();
Some((TextRange::empty(s), format!(": {}", new_name)))
}
_ => None,
}
} else if let Some(record_field) = ast::RecordPatField::for_field_name_ref(name_ref) {
let rcf_name_ref = record_field.name_ref();
let rcf_pat = record_field.pat();
match (rcf_name_ref, rcf_pat) {
// field: rename
(Some(field_name), Some(ast::Pat::IdentPat(pat))) if field_name == *name_ref => {
// field name is being renamed
if pat.name().map_or(false, |it| it.text() == new_name) {
cov_mark::hit!(test_rename_field_put_init_shorthand_pat);
// same names, we can use a shorthand here instead/
// we do not want to erase attributes hence this range start
let s = field_name.syntax().text_range().start();
let e = record_field.syntax().text_range().end();
Some((TextRange::new(s, e), pat.to_string()))
} else {
None
}
}
_ => None,
}
} else {
None
}
}
fn source_edit_from_def(
sema: &Semantics<RootDatabase>,
def: Definition,
new_name: &str,
) -> Result<(FileId, TextEdit)> {
let frange = def
.range_for_rename(sema)
.ok_or_else(|| format_err!("No identifier available to rename"))?;
let mut replacement_text = String::new();
let mut repl_range = frange.range;
if let Definition::Local(local) = def {
if let Either::Left(pat) = local.source(sema.db).value {
if matches!(
pat.syntax().parent().and_then(ast::RecordPatField::cast),
Some(pat_field) if pat_field.name_ref().is_none()
) {
replacement_text.push_str(": ");
replacement_text.push_str(new_name);
repl_range = TextRange::new(
pat.syntax().text_range().end(),
pat.syntax().text_range().end(),
);
}
}
}
if replacement_text.is_empty() {
replacement_text.push_str(new_name);
}
let edit = TextEdit::replace(repl_range, replacement_text);
Ok((frange.file_id, edit))
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum IdentifierKind {
Ident,
Lifetime,
Underscore,
}
impl IdentifierKind {
pub fn classify(new_name: &str) -> Result<IdentifierKind> {
match lex_single_syntax_kind(new_name) {
Some(res) => match res {
(SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident),
(T![_], _) => Ok(IdentifierKind::Underscore),
(SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
Ok(IdentifierKind::Lifetime)
}
(SyntaxKind::LIFETIME_IDENT, _) => {
bail!("Invalid name `{}`: not a lifetime identifier", new_name)
}
(_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
(_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
},
None => bail!("Invalid name `{}`: not an identifier", new_name),
}
}
}

View file

@ -0,0 +1,29 @@
[package]
name = "ide_diagnostics"
version = "0.0.0"
description = "TBD"
license = "MIT OR Apache-2.0"
authors = ["rust-analyzer developers"]
edition = "2018"
[lib]
doctest = false
[dependencies]
cov-mark = "2.0.0-pre.1"
itertools = "0.10.0"
rustc-hash = "1.1.0"
either = "1.5.3"
profile = { path = "../profile", version = "0.0.0" }
stdx = { path = "../stdx", version = "0.0.0" }
syntax = { path = "../syntax", version = "0.0.0" }
text_edit = { path = "../text_edit", version = "0.0.0" }
cfg = { path = "../cfg", version = "0.0.0" }
hir = { path = "../hir", version = "0.0.0" }
ide_db = { path = "../ide_db", version = "0.0.0" }
[dev-dependencies]
expect-test = "1.1"
test_utils = { path = "../test_utils" }

View file

@ -5,7 +5,7 @@ use ide_db::{base_db::FileId, source_change::SourceChange};
use syntax::{ast, match_ast, AstNode, SyntaxNode};
use text_edit::TextEdit;
use crate::{diagnostics::fix, Diagnostic, Severity};
use crate::{fix, Diagnostic, Severity};
pub(super) fn check(acc: &mut Vec<Diagnostic>, file_id: FileId, node: &SyntaxNode) {
match_ast! {
@ -101,7 +101,7 @@ fn check_pat_field_shorthand(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::{check_diagnostics, check_fix};
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn test_check_expr_field_shorthand() {

View file

@ -1,9 +1,9 @@
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: break-outside-of-loop
//
// This diagnostic is triggered if the `break` keyword is used outside of a loop.
pub(super) fn break_outside_of_loop(
pub(crate) fn break_outside_of_loop(
ctx: &DiagnosticsContext<'_>,
d: &hir::BreakOutsideOfLoop,
) -> Diagnostic {
@ -16,7 +16,7 @@ pub(super) fn break_outside_of_loop(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_diagnostics;
use crate::tests::check_diagnostics;
#[test]
fn break_outside_of_loop() {

View file

@ -1,15 +1,12 @@
use cfg::DnfExpr;
use stdx::format_to;
use crate::{
diagnostics::{Diagnostic, DiagnosticsContext},
Severity,
};
use crate::{Diagnostic, DiagnosticsContext, Severity};
// Diagnostic: inactive-code
//
// This diagnostic is shown for code with inactive `#[cfg]` attributes.
pub(super) fn inactive_code(
pub(crate) fn inactive_code(
ctx: &DiagnosticsContext<'_>,
d: &hir::InactiveCode,
) -> Option<Diagnostic> {
@ -37,7 +34,7 @@ pub(super) fn inactive_code(
#[cfg(test)]
mod tests {
use crate::{diagnostics::tests::check_diagnostics_with_config, DiagnosticsConfig};
use crate::{tests::check_diagnostics_with_config, DiagnosticsConfig};
pub(crate) fn check(ra_fixture: &str) {
let config = DiagnosticsConfig::default();

View file

@ -1,18 +1,19 @@
use hir::{db::AstDatabase, InFile};
use ide_assists::Assist;
use ide_db::base_db::FilePosition;
use ide_db::{assists::Assist, defs::NameClass};
use syntax::AstNode;
use crate::{
diagnostics::{unresolved_fix, Diagnostic, DiagnosticsContext},
references::rename::rename_with_semantics,
// references::rename::rename_with_semantics,
unresolved_fix,
Diagnostic,
DiagnosticsContext,
Severity,
};
// Diagnostic: incorrect-ident-case
//
// This diagnostic is triggered if an item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention].
pub(super) fn incorrect_case(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Diagnostic {
pub(crate) fn incorrect_case(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Diagnostic {
Diagnostic::new(
"incorrect-ident-case",
format!(
@ -28,15 +29,15 @@ pub(super) fn incorrect_case(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCas
fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Option<Vec<Assist>> {
let root = ctx.sema.db.parse_or_expand(d.file)?;
let name_node = d.ident.to_node(&root);
let def = NameClass::classify(&ctx.sema, &name_node)?.defined(ctx.sema.db)?;
let name_node = InFile::new(d.file, name_node.syntax());
let frange = name_node.original_file_range(ctx.sema.db);
let file_position = FilePosition { file_id: frange.file_id, offset: frange.range.start() };
let label = format!("Rename to {}", d.suggested_text);
let mut res = unresolved_fix("change_case", &label, frange.range);
if ctx.resolve.should_resolve(&res.id) {
let source_change = rename_with_semantics(&ctx.sema, file_position, &d.suggested_text);
let source_change = def.rename(&ctx.sema, &d.suggested_text);
res.source_change = Some(source_change.ok().unwrap_or_default());
}
@ -45,10 +46,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Option<Vec<Ass
#[cfg(test)]
mod change_case {
use crate::{
diagnostics::tests::{check_diagnostics, check_fix},
fixture, AssistResolveStrategy, DiagnosticsConfig,
};
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn test_rename_incorrect_case() {
@ -116,7 +114,7 @@ fn some_fn() {
check_diagnostics(
r#"
fn foo() {
const ANOTHER_ITEM$0: &str = "some_item";
const ANOTHER_ITEM: &str = "some_item";
}
"#,
);
@ -148,20 +146,13 @@ impl TestStruct {
#[test]
fn test_single_incorrect_case_diagnostic_in_function_name_issue_6970() {
let input = r#"fn FOO$0() {}"#;
let expected = r#"fn foo() {}"#;
let (analysis, file_position) = fixture::position(input);
let diagnostics = analysis
.diagnostics(
&DiagnosticsConfig::default(),
AssistResolveStrategy::All,
file_position.file_id,
)
.unwrap();
assert_eq!(diagnostics.len(), 1);
check_fix(input, expected);
check_diagnostics(
r#"
fn FOO() {}
// ^^^ Function `FOO` should have snake_case name, e.g. `foo`
"#,
);
check_fix(r#"fn FOO$0() {}"#, r#"fn foo() {}"#);
}
#[test]

View file

@ -1,9 +1,9 @@
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: macro-error
//
// This diagnostic is shown for macro expansion errors.
pub(super) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic {
pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic {
Diagnostic::new(
"macro-error",
d.message.clone(),
@ -15,7 +15,7 @@ pub(super) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) ->
#[cfg(test)]
mod tests {
use crate::{
diagnostics::tests::{check_diagnostics, check_diagnostics_with_config},
tests::{check_diagnostics, check_diagnostics_with_config},
DiagnosticsConfig,
};

View file

@ -1,9 +1,9 @@
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: mismatched-arg-count
//
// This diagnostic is triggered if a function is invoked with an incorrect amount of arguments.
pub(super) fn mismatched_arg_count(
pub(crate) fn mismatched_arg_count(
ctx: &DiagnosticsContext<'_>,
d: &hir::MismatchedArgCount,
) -> Diagnostic {
@ -18,7 +18,7 @@ pub(super) fn mismatched_arg_count(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_diagnostics;
use crate::tests::check_diagnostics;
#[test]
fn simple_free_fn_zero() {

View file

@ -1,12 +1,11 @@
use either::Either;
use hir::{db::AstDatabase, InFile};
use ide_assists::Assist;
use ide_db::source_change::SourceChange;
use ide_db::{assists::Assist, source_change::SourceChange};
use stdx::format_to;
use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
use text_edit::TextEdit;
use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
use crate::{fix, Diagnostic, DiagnosticsContext};
// Diagnostic: missing-fields
//
@ -19,7 +18,7 @@ use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
//
// let a = A { a: 10 };
// ```
pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Diagnostic {
pub(crate) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Diagnostic {
let mut message = String::from("Missing structure fields:\n");
for field in &d.missed_fields {
format_to!(message, "- {}\n", field);
@ -77,7 +76,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::{check_diagnostics, check_fix};
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn missing_record_pat_field_diagnostic() {

View file

@ -1,11 +1,11 @@
use hir::InFile;
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: missing-match-arm
//
// This diagnostic is triggered if `match` block is missing one or more match arms.
pub(super) fn missing_match_arms(
pub(crate) fn missing_match_arms(
ctx: &DiagnosticsContext<'_>,
d: &hir::MissingMatchArms,
) -> Diagnostic {
@ -17,12 +17,12 @@ pub(super) fn missing_match_arms(
}
#[cfg(test)]
pub(super) mod tests {
use crate::diagnostics::tests::check_diagnostics;
mod tests {
use crate::tests::check_diagnostics;
fn check_diagnostics_no_bails(ra_fixture: &str) {
cov_mark::check_count!(validate_match_bailed_out, 0);
crate::diagnostics::tests::check_diagnostics(ra_fixture)
crate::tests::check_diagnostics(ra_fixture)
}
#[test]

View file

@ -1,10 +1,9 @@
use hir::db::AstDatabase;
use ide_assists::Assist;
use ide_db::source_change::SourceChange;
use ide_db::{assists::Assist, source_change::SourceChange};
use syntax::AstNode;
use text_edit::TextEdit;
use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
use crate::{fix, Diagnostic, DiagnosticsContext};
// Diagnostic: missing-ok-or-some-in-tail-expr
//
@ -18,7 +17,7 @@ use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
// 10
// }
// ```
pub(super) fn missing_ok_or_some_in_tail_expr(
pub(crate) fn missing_ok_or_some_in_tail_expr(
ctx: &DiagnosticsContext<'_>,
d: &hir::MissingOkOrSomeInTailExpr,
) -> Diagnostic {
@ -44,7 +43,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingOkOrSomeInTailExpr) -> Op
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::{check_diagnostics, check_fix};
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn test_wrap_return_type_option() {

View file

@ -1,9 +1,9 @@
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: missing-unsafe
//
// This diagnostic is triggered if an operation marked as `unsafe` is used outside of an `unsafe` function or block.
pub(super) fn missing_unsafe(ctx: &DiagnosticsContext<'_>, d: &hir::MissingUnsafe) -> Diagnostic {
pub(crate) fn missing_unsafe(ctx: &DiagnosticsContext<'_>, d: &hir::MissingUnsafe) -> Diagnostic {
Diagnostic::new(
"missing-unsafe",
"this operation is unsafe and requires an unsafe function or block",
@ -13,7 +13,7 @@ pub(super) fn missing_unsafe(ctx: &DiagnosticsContext<'_>, d: &hir::MissingUnsaf
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_diagnostics;
use crate::tests::check_diagnostics;
#[test]
fn missing_unsafe_diagnostic_with_raw_ptr() {

View file

@ -6,15 +6,12 @@ use syntax::{
};
use text_edit::TextEdit;
use crate::{
diagnostics::{fix, Diagnostic, DiagnosticsContext},
Assist,
};
use crate::{fix, Assist, Diagnostic, DiagnosticsContext};
// Diagnostic: no-such-field
//
// This diagnostic is triggered if created structure does not have field provided in record.
pub(super) fn no_such_field(ctx: &DiagnosticsContext<'_>, d: &hir::NoSuchField) -> Diagnostic {
pub(crate) fn no_such_field(ctx: &DiagnosticsContext<'_>, d: &hir::NoSuchField) -> Diagnostic {
Diagnostic::new(
"no-such-field",
"no such field",
@ -112,7 +109,7 @@ fn missing_record_expr_field_fixes(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::{check_diagnostics, check_fix};
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn no_such_field_diagnostics() {

View file

@ -3,15 +3,12 @@ use ide_db::source_change::SourceChange;
use syntax::{ast, AstNode};
use text_edit::TextEdit;
use crate::{
diagnostics::{fix, Diagnostic, DiagnosticsContext},
Assist,
};
use crate::{fix, Assist, Diagnostic, DiagnosticsContext};
// Diagnostic: remove-this-semicolon
//
// This diagnostic is triggered when there's an erroneous `;` at the end of the block.
pub(super) fn remove_this_semicolon(
pub(crate) fn remove_this_semicolon(
ctx: &DiagnosticsContext<'_>,
d: &hir::RemoveThisSemicolon,
) -> Diagnostic {
@ -45,7 +42,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::RemoveThisSemicolon) -> Option<V
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::{check_diagnostics, check_fix};
use crate::tests::{check_diagnostics, check_fix};
#[test]
fn missing_semicolon() {

View file

@ -6,15 +6,12 @@ use syntax::{
};
use text_edit::TextEdit;
use crate::{
diagnostics::{fix, Diagnostic, DiagnosticsContext},
Assist, Severity,
};
use crate::{fix, Assist, Diagnostic, DiagnosticsContext, Severity};
// Diagnostic: replace-filter-map-next-with-find-map
//
// This diagnostic is triggered when `.filter_map(..).next()` is used, rather than the more concise `.find_map(..)`.
pub(super) fn replace_filter_map_next_with_find_map(
pub(crate) fn replace_filter_map_next_with_find_map(
ctx: &DiagnosticsContext<'_>,
d: &hir::ReplaceFilterMapNextWithFindMap,
) -> Diagnostic {
@ -58,7 +55,7 @@ fn fixes(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_fix;
use crate::tests::check_fix;
// Register the required standard library types to make the tests work
#[track_caller]
@ -86,7 +83,7 @@ pub mod iter {
}
}
"#;
crate::diagnostics::tests::check_diagnostics(&format!("{}{}{}", prefix, ra_fixture, suffix))
crate::tests::check_diagnostics(&format!("{}{}{}", prefix, ra_fixture, suffix))
}
#[test]

View file

@ -1,12 +1,9 @@
use crate::{
diagnostics::{Diagnostic, DiagnosticsContext},
Severity,
};
use crate::{Diagnostic, DiagnosticsContext, Severity};
// Diagnostic: unimplemented-builtin-macro
//
// This diagnostic is shown for builtin macros which are not yet implemented by rust-analyzer
pub(super) fn unimplemented_builtin_macro(
pub(crate) fn unimplemented_builtin_macro(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnimplementedBuiltinMacro,
) -> Diagnostic {

View file

@ -12,10 +12,7 @@ use syntax::{
};
use text_edit::TextEdit;
use crate::{
diagnostics::{fix, DiagnosticsContext},
Assist, Diagnostic,
};
use crate::{fix, Assist, Diagnostic, DiagnosticsContext};
#[derive(Debug)]
pub(crate) struct UnlinkedFile {
@ -26,7 +23,7 @@ pub(crate) struct UnlinkedFile {
//
// This diagnostic is shown for files that are not included in any crate, or files that are part of
// crates rust-analyzer failed to discover. The file will not have IDE features available.
pub(super) fn unlinked_file(ctx: &DiagnosticsContext, d: &UnlinkedFile) -> Diagnostic {
pub(crate) fn unlinked_file(ctx: &DiagnosticsContext, d: &UnlinkedFile) -> Diagnostic {
// Limit diagnostic to the first few characters in the file. This matches how VS Code
// renders it with the full span, but on other editors, and is less invasive.
let range = ctx.sema.db.parse(d.file).syntax_node().text_range();
@ -164,7 +161,7 @@ fn make_fixes(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::{check_diagnostics, check_fix, check_fixes, check_no_fix};
use crate::tests::{check_diagnostics, check_fix, check_fixes, check_no_fix};
#[test]
fn unlinked_file_prepend_first_item() {

View file

@ -1,9 +1,9 @@
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: unresolved-extern-crate
//
// This diagnostic is triggered if rust-analyzer is unable to discover referred extern crate.
pub(super) fn unresolved_extern_crate(
pub(crate) fn unresolved_extern_crate(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedExternCrate,
) -> Diagnostic {
@ -16,7 +16,7 @@ pub(super) fn unresolved_extern_crate(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_diagnostics;
use crate::tests::check_diagnostics;
#[test]
fn unresolved_extern_crate() {

View file

@ -1,10 +1,10 @@
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: unresolved-import
//
// This diagnostic is triggered if rust-analyzer is unable to resolve a path in
// a `use` declaration.
pub(super) fn unresolved_import(
pub(crate) fn unresolved_import(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedImport,
) -> Diagnostic {
@ -22,7 +22,7 @@ pub(super) fn unresolved_import(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_diagnostics;
use crate::tests::check_diagnostics;
#[test]
fn unresolved_import() {

View file

@ -1,13 +1,13 @@
use hir::{db::AstDatabase, InFile};
use syntax::{AstNode, SyntaxNodePtr};
use crate::diagnostics::{Diagnostic, DiagnosticsContext};
use crate::{Diagnostic, DiagnosticsContext};
// Diagnostic: unresolved-macro-call
//
// This diagnostic is triggered if rust-analyzer is unable to resolve the path
// to a macro in a macro invocation.
pub(super) fn unresolved_macro_call(
pub(crate) fn unresolved_macro_call(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedMacroCall,
) -> Diagnostic {
@ -32,7 +32,7 @@ pub(super) fn unresolved_macro_call(
#[cfg(test)]
mod tests {
use crate::diagnostics::tests::check_diagnostics;
use crate::tests::check_diagnostics;
#[test]
fn unresolved_macro_diag() {

View file

@ -1,14 +1,13 @@
use hir::db::AstDatabase;
use ide_assists::Assist;
use ide_db::{base_db::AnchoredPathBuf, source_change::FileSystemEdit};
use ide_db::{assists::Assist, base_db::AnchoredPathBuf, source_change::FileSystemEdit};
use syntax::AstNode;
use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
use crate::{fix, Diagnostic, DiagnosticsContext};
// Diagnostic: unresolved-module
//
// This diagnostic is triggered if rust-analyzer is unable to discover referred module.
pub(super) fn unresolved_module(
pub(crate) fn unresolved_module(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedModule,
) -> Diagnostic {
@ -42,7 +41,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedModule) -> Option<Vec<
mod tests {
use expect_test::expect;
use crate::diagnostics::tests::{check_diagnostics, check_expect};
use crate::tests::{check_diagnostics, check_expect};
#[test]
fn unresolved_module() {

View file

@ -1,7 +1,4 @@
use crate::{
diagnostics::{Diagnostic, DiagnosticsContext},
Severity,
};
use crate::{Diagnostic, DiagnosticsContext, Severity};
// Diagnostic: unresolved-proc-macro
//
@ -12,7 +9,7 @@ use crate::{
// If you are seeing a lot of "proc macro not expanded" warnings, you can add this option to the
// `rust-analyzer.diagnostics.disabled` list to prevent them from showing. Alternatively you can
// enable support for procedural macros (see `rust-analyzer.procMacro.enable`).
pub(super) fn unresolved_proc_macro(
pub(crate) fn unresolved_proc_macro(
ctx: &DiagnosticsContext<'_>,
d: &hir::UnresolvedProcMacro,
) -> Diagnostic {

View file

@ -1,34 +1,60 @@
//! Collects diagnostics & fixits for a single file.
//! Diagnostics rendering and fixits.
//!
//! The tricky bit here is that diagnostics are produced by hir in terms of
//! macro-expanded files, but we need to present them to the users in terms of
//! original files. So we need to map the ranges.
//! Most of the diagnostics originate from the dark depth of the compiler, and
//! are originally expressed in term of IR. When we emit the diagnostic, we are
//! usually not in the position to decide how to best "render" it in terms of
//! user-authored source code. We are especially not in the position to offer
//! fixits, as the compiler completely lacks the infrastructure to edit the
//! source code.
//!
//! Instead, we "bubble up" raw, structured diagnostics until the `hir` crate,
//! where we "cook" them so that each diagnostic is formulated in terms of `hir`
//! types. Well, at least that's the aspiration, the "cooking" is somewhat
//! ad-hoc at the moment. Anyways, we get a bunch of ide-friendly diagnostic
//! structs from hir, and we want to render them to unified serializable
//! representation (span, level, message) here. If we can, we also provide
//! fixits. By the way, that's why we want to keep diagnostics structured
//! internally -- so that we have all the info to make fixes.
//!
//! We have one "handler" module per diagnostic code. Such a module contains
//! rendering, optional fixes and tests. It's OK if some low-level compiler
//! functionality ends up being tested via a diagnostic.
//!
//! There are also a couple of ad-hoc diagnostics implemented directly here, we
//! don't yet have a great pattern for how to do them properly.
mod break_outside_of_loop;
mod inactive_code;
mod incorrect_case;
mod macro_error;
mod mismatched_arg_count;
mod missing_fields;
mod missing_match_arms;
mod missing_ok_or_some_in_tail_expr;
mod missing_unsafe;
mod no_such_field;
mod remove_this_semicolon;
mod replace_filter_map_next_with_find_map;
mod unimplemented_builtin_macro;
mod unlinked_file;
mod unresolved_extern_crate;
mod unresolved_import;
mod unresolved_macro_call;
mod unresolved_module;
mod unresolved_proc_macro;
mod handlers {
pub(crate) mod break_outside_of_loop;
pub(crate) mod inactive_code;
pub(crate) mod incorrect_case;
pub(crate) mod macro_error;
pub(crate) mod mismatched_arg_count;
pub(crate) mod missing_fields;
pub(crate) mod missing_match_arms;
pub(crate) mod missing_ok_or_some_in_tail_expr;
pub(crate) mod missing_unsafe;
pub(crate) mod no_such_field;
pub(crate) mod remove_this_semicolon;
pub(crate) mod replace_filter_map_next_with_find_map;
pub(crate) mod unimplemented_builtin_macro;
pub(crate) mod unlinked_file;
pub(crate) mod unresolved_extern_crate;
pub(crate) mod unresolved_import;
pub(crate) mod unresolved_macro_call;
pub(crate) mod unresolved_module;
pub(crate) mod unresolved_proc_macro;
}
mod field_shorthand;
use hir::{diagnostics::AnyDiagnostic, Semantics};
use ide_assists::AssistResolveStrategy;
use ide_db::{base_db::SourceDatabase, RootDatabase};
use ide_db::{
assists::{Assist, AssistId, AssistKind, AssistResolveStrategy},
base_db::{FileId, SourceDatabase},
label::Label,
source_change::SourceChange,
RootDatabase,
};
use itertools::Itertools;
use rustc_hash::FxHashSet;
use syntax::{
@ -36,9 +62,8 @@ use syntax::{
SyntaxNode, TextRange,
};
use text_edit::TextEdit;
use unlinked_file::UnlinkedFile;
use crate::{Assist, AssistId, AssistKind, FileId, Label, SourceChange};
use crate::handlers::unlinked_file::UnlinkedFile;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct DiagnosticCode(pub &'static str);
@ -113,7 +138,7 @@ struct DiagnosticsContext<'a> {
resolve: &'a AssistResolveStrategy,
}
pub(crate) fn diagnostics(
pub fn diagnostics(
db: &RootDatabase,
config: &DiagnosticsConfig,
resolve: &AssistResolveStrategy,
@ -145,32 +170,32 @@ pub(crate) fn diagnostics(
let ctx = DiagnosticsContext { config, sema, resolve };
if module.is_none() {
let d = UnlinkedFile { file: file_id };
let d = unlinked_file::unlinked_file(&ctx, &d);
let d = handlers::unlinked_file::unlinked_file(&ctx, &d);
res.push(d)
}
for diag in diags {
#[rustfmt::skip]
let d = match diag {
AnyDiagnostic::BreakOutsideOfLoop(d) => break_outside_of_loop::break_outside_of_loop(&ctx, &d),
AnyDiagnostic::IncorrectCase(d) => incorrect_case::incorrect_case(&ctx, &d),
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
AnyDiagnostic::MismatchedArgCount(d) => mismatched_arg_count::mismatched_arg_count(&ctx, &d),
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
AnyDiagnostic::MissingMatchArms(d) => missing_match_arms::missing_match_arms(&ctx, &d),
AnyDiagnostic::MissingOkOrSomeInTailExpr(d) => missing_ok_or_some_in_tail_expr::missing_ok_or_some_in_tail_expr(&ctx, &d),
AnyDiagnostic::MissingUnsafe(d) => missing_unsafe::missing_unsafe(&ctx, &d),
AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
AnyDiagnostic::RemoveThisSemicolon(d) => remove_this_semicolon::remove_this_semicolon(&ctx, &d),
AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d),
AnyDiagnostic::UnimplementedBuiltinMacro(d) => unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
AnyDiagnostic::UnresolvedImport(d) => unresolved_import::unresolved_import(&ctx, &d),
AnyDiagnostic::UnresolvedMacroCall(d) => unresolved_macro_call::unresolved_macro_call(&ctx, &d),
AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d),
AnyDiagnostic::UnresolvedProcMacro(d) => unresolved_proc_macro::unresolved_proc_macro(&ctx, &d),
AnyDiagnostic::BreakOutsideOfLoop(d) => handlers::break_outside_of_loop::break_outside_of_loop(&ctx, &d),
AnyDiagnostic::IncorrectCase(d) => handlers::incorrect_case::incorrect_case(&ctx, &d),
AnyDiagnostic::MacroError(d) => handlers::macro_error::macro_error(&ctx, &d),
AnyDiagnostic::MismatchedArgCount(d) => handlers::mismatched_arg_count::mismatched_arg_count(&ctx, &d),
AnyDiagnostic::MissingFields(d) => handlers::missing_fields::missing_fields(&ctx, &d),
AnyDiagnostic::MissingMatchArms(d) => handlers::missing_match_arms::missing_match_arms(&ctx, &d),
AnyDiagnostic::MissingOkOrSomeInTailExpr(d) => handlers::missing_ok_or_some_in_tail_expr::missing_ok_or_some_in_tail_expr(&ctx, &d),
AnyDiagnostic::MissingUnsafe(d) => handlers::missing_unsafe::missing_unsafe(&ctx, &d),
AnyDiagnostic::NoSuchField(d) => handlers::no_such_field::no_such_field(&ctx, &d),
AnyDiagnostic::RemoveThisSemicolon(d) => handlers::remove_this_semicolon::remove_this_semicolon(&ctx, &d),
AnyDiagnostic::ReplaceFilterMapNextWithFindMap(d) => handlers::replace_filter_map_next_with_find_map::replace_filter_map_next_with_find_map(&ctx, &d),
AnyDiagnostic::UnimplementedBuiltinMacro(d) => handlers::unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
AnyDiagnostic::UnresolvedExternCrate(d) => handlers::unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
AnyDiagnostic::UnresolvedImport(d) => handlers::unresolved_import::unresolved_import(&ctx, &d),
AnyDiagnostic::UnresolvedMacroCall(d) => handlers::unresolved_macro_call::unresolved_macro_call(&ctx, &d),
AnyDiagnostic::UnresolvedModule(d) => handlers::unresolved_module::unresolved_module(&ctx, &d),
AnyDiagnostic::UnresolvedProcMacro(d) => handlers::unresolved_proc_macro::unresolved_proc_macro(&ctx, &d),
AnyDiagnostic::InactiveCode(d) => match inactive_code::inactive_code(&ctx, &d) {
AnyDiagnostic::InactiveCode(d) => match handlers::inactive_code::inactive_code(&ctx, &d) {
Some(it) => it,
None => continue,
}
@ -261,11 +286,15 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist {
#[cfg(test)]
mod tests {
use expect_test::Expect;
use ide_assists::AssistResolveStrategy;
use ide_db::{
assists::AssistResolveStrategy,
base_db::{fixture::WithFixture, SourceDatabaseExt},
RootDatabase,
};
use stdx::trim_indent;
use test_utils::{assert_eq_text, extract_annotations};
use crate::{fixture, DiagnosticsConfig};
use crate::DiagnosticsConfig;
/// Takes a multi-file input fixture with annotated cursor positions,
/// and checks that:
@ -291,21 +320,20 @@ mod tests {
fn check_nth_fix(nth: usize, ra_fixture_before: &str, ra_fixture_after: &str) {
let after = trim_indent(ra_fixture_after);
let (analysis, file_position) = fixture::position(ra_fixture_before);
let diagnostic = analysis
.diagnostics(
let (db, file_position) = RootDatabase::with_position(ra_fixture_before);
let diagnostic = super::diagnostics(
&db,
&DiagnosticsConfig::default(),
AssistResolveStrategy::All,
&AssistResolveStrategy::All,
file_position.file_id,
)
.unwrap()
.pop()
.expect("no diagnostics");
let fix = &diagnostic.fixes.expect("diagnostic misses fixes")[nth];
let actual = {
let source_change = fix.source_change.as_ref().unwrap();
let file_id = *source_change.source_file_edits.keys().next().unwrap();
let mut actual = analysis.file_text(file_id).unwrap().to_string();
let mut actual = db.file_text(file_id).to_string();
for edit in source_change.source_file_edits.values() {
edit.apply(&mut actual);
@ -324,24 +352,26 @@ mod tests {
/// Checks that there's a diagnostic *without* fix at `$0`.
pub(crate) fn check_no_fix(ra_fixture: &str) {
let (analysis, file_position) = fixture::position(ra_fixture);
let diagnostic = analysis
.diagnostics(
let (db, file_position) = RootDatabase::with_position(ra_fixture);
let diagnostic = super::diagnostics(
&db,
&DiagnosticsConfig::default(),
AssistResolveStrategy::All,
&AssistResolveStrategy::All,
file_position.file_id,
)
.unwrap()
.pop()
.unwrap();
assert!(diagnostic.fixes.is_none(), "got a fix when none was expected: {:?}", diagnostic);
}
pub(crate) fn check_expect(ra_fixture: &str, expect: Expect) {
let (analysis, file_id) = fixture::file(ra_fixture);
let diagnostics = analysis
.diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
.unwrap();
let (db, file_id) = RootDatabase::with_single_file(ra_fixture);
let diagnostics = super::diagnostics(
&db,
&DiagnosticsConfig::default(),
&AssistResolveStrategy::All,
file_id,
);
expect.assert_debug_eq(&diagnostics)
}
@ -354,12 +384,12 @@ mod tests {
#[track_caller]
pub(crate) fn check_diagnostics_with_config(config: DiagnosticsConfig, ra_fixture: &str) {
let (analysis, files) = fixture::files(ra_fixture);
let (db, files) = RootDatabase::with_many_files(ra_fixture);
for file_id in files {
let diagnostics =
analysis.diagnostics(&config, AssistResolveStrategy::All, file_id).unwrap();
super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);
let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
let expected = extract_annotations(&*db.file_text(file_id));
let mut actual =
diagnostics.into_iter().map(|d| (d.range, d.message)).collect::<Vec<_>>();
actual.sort_by_key(|(range, _)| range.start());
@ -455,15 +485,17 @@ mod a {
let mut config = DiagnosticsConfig::default();
config.disabled.insert("unresolved-module".into());
let (analysis, file_id) = fixture::file(r#"mod foo;"#);
let (db, file_id) = RootDatabase::with_single_file(r#"mod foo;"#);
let diagnostics =
analysis.diagnostics(&config, AssistResolveStrategy::All, file_id).unwrap();
let diagnostics = super::diagnostics(&db, &config, &AssistResolveStrategy::All, file_id);
assert!(diagnostics.is_empty());
let diagnostics = analysis
.diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id)
.unwrap();
let diagnostics = super::diagnostics(
&db,
&DiagnosticsConfig::default(),
&AssistResolveStrategy::All,
file_id,
);
assert!(!diagnostics.is_empty());
}