rust/crates/ide-diagnostics/src/handlers/unlinked_file.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

333 lines
8.6 KiB
Rust
Raw Normal View History

2021-03-14 20:23:00 -05:00
//! Diagnostic emitted for files that aren't part of any crate.
use hir::db::DefDatabase;
2021-03-14 19:39:23 -05:00
use ide_db::{
base_db::{FileId, FileLoader, SourceDatabase, SourceDatabaseExt},
source_change::SourceChange,
RootDatabase,
};
use syntax::{
2021-09-27 05:54:24 -05:00
ast::{self, HasModuleItem, HasName},
AstNode, TextRange, TextSize,
2021-03-14 19:39:23 -05:00
};
use text_edit::TextEdit;
use crate::{fix, Assist, Diagnostic, DiagnosticsContext, Severity};
2021-03-14 19:39:23 -05:00
2021-03-24 16:18:17 -05:00
// Diagnostic: unlinked-file
//
// 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.
2021-06-14 13:23:59 -05:00
pub(crate) fn unlinked_file(ctx: &DiagnosticsContext, acc: &mut Vec<Diagnostic>, file_id: FileId) {
// 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.
2021-06-14 13:23:59 -05:00
let range = ctx.sema.db.parse(file_id).syntax_node().text_range();
// FIXME: This is wrong if one of the first three characters is not ascii: `//Ы`.
let range = range.intersect(TextRange::up_to(TextSize::of("..."))).unwrap_or(range);
2021-06-14 13:23:59 -05:00
acc.push(
Diagnostic::new("unlinked-file", "file not included in module tree", range)
.severity(Severity::WeakWarning)
2021-06-14 13:23:59 -05:00
.with_fixes(fixes(ctx, file_id)),
);
2021-03-14 19:39:23 -05:00
}
2021-06-14 13:23:59 -05:00
fn fixes(ctx: &DiagnosticsContext, file_id: FileId) -> Option<Vec<Assist>> {
// If there's an existing module that could add `mod` or `pub mod` items to include the unlinked file,
// suggest that as a fix.
2021-03-14 19:39:23 -05:00
2021-06-14 13:23:59 -05:00
let source_root = ctx.sema.db.source_root(ctx.sema.db.file_source_root(file_id));
let our_path = source_root.path_for_file(&file_id)?;
let (mut module_name, _) = our_path.name_and_extension()?;
2021-03-14 19:39:23 -05:00
// Candidates to look for:
// - `mod.rs`, `main.rs` and `lib.rs` in the same folder
// - `$dir.rs` in the parent folder, where `$dir` is the directory containing `self.file_id`
let parent = our_path.parent()?;
let paths = {
let parent = if module_name == "mod" {
// for mod.rs we need to actually look up one higher
// and take the parent as our to be module name
let (name, _) = parent.name_and_extension()?;
module_name = name;
2021-10-04 08:16:13 -05:00
parent.parent()?
} else {
2021-10-04 08:16:13 -05:00
parent
};
let mut paths =
vec![parent.join("mod.rs")?, parent.join("lib.rs")?, parent.join("main.rs")?];
// `submod/bla.rs` -> `submod.rs`
let parent_mod = (|| {
let (name, _) = parent.name_and_extension()?;
parent.parent()?.join(&format!("{}.rs", name))
})();
paths.extend(parent_mod);
paths
};
2021-03-14 19:39:23 -05:00
2021-08-07 15:16:15 -05:00
for &parent_id in paths.iter().filter_map(|path| source_root.file_for_path(path)) {
for &krate in ctx.sema.db.relevant_crates(parent_id).iter() {
let crate_def_map = ctx.sema.db.crate_def_map(krate);
for (_, module) in crate_def_map.modules() {
if module.origin.is_inline() {
// We don't handle inline `mod parent {}`s, they use different paths.
continue;
}
2021-03-14 19:39:23 -05:00
2021-08-07 15:16:15 -05:00
if module.origin.file_id() == Some(parent_id) {
return make_fixes(ctx.sema.db, parent_id, module_name, file_id);
2021-03-14 19:39:23 -05:00
}
}
}
}
None
2021-03-14 19:39:23 -05:00
}
2021-05-17 18:11:07 -05:00
fn make_fixes(
2021-03-14 19:39:23 -05:00
db: &RootDatabase,
parent_file_id: FileId,
new_mod_name: &str,
added_file_id: FileId,
2021-05-17 18:11:07 -05:00
) -> Option<Vec<Assist>> {
2021-03-14 19:39:23 -05:00
fn is_outline_mod(item: &ast::Item) -> bool {
matches!(item, ast::Item::Module(m) if m.item_list().is_none())
}
let mod_decl = format!("mod {};", new_mod_name);
2021-05-17 18:11:07 -05:00
let pub_mod_decl = format!("pub mod {};", new_mod_name);
2021-03-14 19:39:23 -05:00
let ast: ast::SourceFile = db.parse(parent_file_id).tree();
2021-05-17 18:11:07 -05:00
let mut mod_decl_builder = TextEdit::builder();
let mut pub_mod_decl_builder = TextEdit::builder();
2021-03-14 19:39:23 -05:00
// If there's an existing `mod m;` statement matching the new one, don't emit a fix (it's
// probably `#[cfg]`d out).
for item in ast.items() {
if let ast::Item::Module(m) = item {
if let Some(name) = m.name() {
if m.item_list().is_none() && name.to_string() == new_mod_name {
cov_mark::hit!(unlinked_file_skip_fix_when_mod_already_exists);
return None;
}
}
}
}
// If there are existing `mod m;` items, append after them (after the first group of them, rather).
2022-03-12 06:35:31 -06:00
match ast.items().skip_while(|item| !is_outline_mod(item)).take_while(is_outline_mod).last() {
2021-03-14 19:39:23 -05:00
Some(last) => {
cov_mark::hit!(unlinked_file_append_to_existing_mods);
2021-05-17 18:11:07 -05:00
let offset = last.syntax().text_range().end();
mod_decl_builder.insert(offset, format!("\n{}", mod_decl));
pub_mod_decl_builder.insert(offset, format!("\n{}", pub_mod_decl));
2021-03-14 19:39:23 -05:00
}
None => {
// Prepend before the first item in the file.
match ast.items().next() {
Some(item) => {
cov_mark::hit!(unlinked_file_prepend_before_first_item);
2021-05-17 18:11:07 -05:00
let offset = item.syntax().text_range().start();
mod_decl_builder.insert(offset, format!("{}\n\n", mod_decl));
pub_mod_decl_builder.insert(offset, format!("{}\n\n", pub_mod_decl));
2021-03-14 19:39:23 -05:00
}
None => {
// No items in the file, so just append at the end.
cov_mark::hit!(unlinked_file_empty_file);
2021-05-17 18:11:07 -05:00
let offset = ast.syntax().text_range().end();
mod_decl_builder.insert(offset, format!("{}\n", mod_decl));
pub_mod_decl_builder.insert(offset, format!("{}\n", pub_mod_decl));
2021-03-14 19:39:23 -05:00
}
}
}
}
let trigger_range = db.parse(added_file_id).tree().syntax().text_range();
2021-05-17 18:11:07 -05:00
Some(vec![
fix(
"add_mod_declaration",
&format!("Insert `{}`", mod_decl),
SourceChange::from_text_edit(parent_file_id, mod_decl_builder.finish()),
trigger_range,
),
fix(
"add_pub_mod_declaration",
&format!("Insert `{}`", pub_mod_decl),
SourceChange::from_text_edit(parent_file_id, pub_mod_decl_builder.finish()),
trigger_range,
),
])
2021-03-14 19:39:23 -05:00
}
#[cfg(test)]
mod tests {
use crate::tests::{check_diagnostics, check_fix, check_fixes, check_no_fix};
#[test]
fn unlinked_file_prepend_first_item() {
cov_mark::check!(unlinked_file_prepend_before_first_item);
// Only tests the first one for `pub mod` since the rest are the same
check_fixes(
r#"
//- /main.rs
fn f() {}
//- /foo.rs
$0
"#,
vec![
r#"
mod foo;
fn f() {}
"#,
r#"
pub mod foo;
fn f() {}
"#,
],
);
}
#[test]
fn unlinked_file_append_mod() {
cov_mark::check!(unlinked_file_append_to_existing_mods);
check_fix(
r#"
//- /main.rs
//! Comment on top
mod preexisting;
mod preexisting2;
struct S;
mod preexisting_bottom;)
//- /foo.rs
$0
"#,
r#"
//! Comment on top
mod preexisting;
mod preexisting2;
mod foo;
struct S;
mod preexisting_bottom;)
"#,
);
}
#[test]
fn unlinked_file_insert_in_empty_file() {
cov_mark::check!(unlinked_file_empty_file);
check_fix(
r#"
//- /main.rs
//- /foo.rs
$0
"#,
r#"
mod foo;
"#,
);
}
#[test]
fn unlinked_file_insert_in_empty_file_mod_file() {
check_fix(
r#"
//- /main.rs
//- /foo/mod.rs
$0
"#,
r#"
mod foo;
"#,
);
check_fix(
r#"
//- /main.rs
mod bar;
//- /bar.rs
// bar module
//- /bar/foo/mod.rs
$0
"#,
r#"
// bar module
mod foo;
"#,
);
}
#[test]
fn unlinked_file_old_style_modrs() {
check_fix(
r#"
//- /main.rs
mod submod;
//- /submod/mod.rs
// in mod.rs
//- /submod/foo.rs
$0
"#,
r#"
// in mod.rs
mod foo;
"#,
);
}
#[test]
fn unlinked_file_new_style_mod() {
check_fix(
r#"
//- /main.rs
mod submod;
//- /submod.rs
//- /submod/foo.rs
$0
"#,
r#"
mod foo;
"#,
);
}
#[test]
fn unlinked_file_with_cfg_off() {
cov_mark::check!(unlinked_file_skip_fix_when_mod_already_exists);
check_no_fix(
r#"
//- /main.rs
#[cfg(never)]
mod foo;
//- /foo.rs
$0
"#,
);
}
#[test]
fn unlinked_file_with_cfg_on() {
check_diagnostics(
r#"
//- /main.rs
#[cfg(not(never))]
mod foo;
//- /foo.rs
"#,
);
}
}