Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-diagnostics/src/handlers/unlinked_file.rs')
-rw-r--r--crates/ide-diagnostics/src/handlers/unlinked_file.rs66
1 files changed, 52 insertions, 14 deletions
diff --git a/crates/ide-diagnostics/src/handlers/unlinked_file.rs b/crates/ide-diagnostics/src/handlers/unlinked_file.rs
index f3109b9bb7..47fa305936 100644
--- a/crates/ide-diagnostics/src/handlers/unlinked_file.rs
+++ b/crates/ide-diagnostics/src/handlers/unlinked_file.rs
@@ -2,20 +2,19 @@
use std::iter;
-use hir::{db::DefDatabase, DefMap, InFile, ModuleSource};
+use hir::{DefMap, InFile, ModuleSource, db::DefDatabase};
+use ide_db::base_db::RootQueryDb;
use ide_db::text_edit::TextEdit;
use ide_db::{
- base_db::{FileLoader, SourceDatabase, SourceRootDatabase},
- source_change::SourceChange,
- FileId, FileRange, LineIndexDatabase,
+ FileId, FileRange, LineIndexDatabase, base_db::SourceDatabase, source_change::SourceChange,
};
use paths::Utf8Component;
use syntax::{
- ast::{self, edit::IndentLevel, HasModuleItem, HasName},
AstNode, TextRange,
+ ast::{self, HasModuleItem, HasName, edit::IndentLevel},
};
-use crate::{fix, Assist, Diagnostic, DiagnosticCode, DiagnosticsContext, Severity};
+use crate::{Assist, Diagnostic, DiagnosticCode, DiagnosticsContext, Severity, fix};
// Diagnostic: unlinked-file
//
@@ -36,7 +35,9 @@ pub(crate) fn unlinked_file(
"This file is not included anywhere in the module tree, so rust-analyzer can't offer IDE services."
};
- let message = format!("{message}\n\nIf you're intentionally working on unowned files, you can silence this warning by adding \"unlinked-file\" to rust-analyzer.diagnostics.disabled in your settings.");
+ let message = format!(
+ "{message}\n\nIf you're intentionally working on unowned files, you can silence this warning by adding \"unlinked-file\" to rust-analyzer.diagnostics.disabled in your settings."
+ );
let mut unused = true;
@@ -48,6 +49,7 @@ pub(crate) fn unlinked_file(
// Only show this diagnostic on the first three characters of
// the file, to avoid overwhelming the user during startup.
range = SourceDatabase::file_text(ctx.sema.db, file_id)
+ .text(ctx.sema.db)
.char_indices()
.take(3)
.last()
@@ -78,7 +80,11 @@ fn fixes(
// If there's an existing module that could add `mod` or `pub mod` items to include the unlinked file,
// suggest that as a fix.
- let source_root = ctx.sema.db.source_root(ctx.sema.db.file_source_root(file_id));
+ let db = ctx.sema.db;
+
+ let source_root = ctx.sema.db.file_source_root(file_id).source_root_id(db);
+ let source_root = ctx.sema.db.source_root(source_root).source_root(db);
+
let our_path = source_root.path_for_file(&file_id)?;
let parent = our_path.parent()?;
let (module_name, _) = our_path.name_and_extension()?;
@@ -93,12 +99,14 @@ fn fixes(
};
// check crate roots, i.e. main.rs, lib.rs, ...
- 'crates: for &krate in &*ctx.sema.db.relevant_crates(file_id) {
+ let relevant_crates = db.relevant_crates(file_id);
+ 'crates: for &krate in &*relevant_crates {
let crate_def_map = ctx.sema.db.crate_def_map(krate);
let root_module = &crate_def_map[DefMap::ROOT];
let Some(root_file_id) = root_module.origin.file_id() else { continue };
- let Some(crate_root_path) = source_root.path_for_file(&root_file_id.file_id()) else {
+ let Some(crate_root_path) = source_root.path_for_file(&root_file_id.file_id(ctx.sema.db))
+ else {
continue;
};
let Some(rel) = parent.strip_prefix(&crate_root_path.parent()?) else { continue };
@@ -124,7 +132,12 @@ fn fixes(
let InFile { file_id: parent_file_id, value: source } =
current.definition_source(ctx.sema.db);
let parent_file_id = parent_file_id.file_id()?;
- return make_fixes(parent_file_id.file_id(), source, &module_name, trigger_range);
+ return make_fixes(
+ parent_file_id.file_id(ctx.sema.db),
+ source,
+ &module_name,
+ trigger_range,
+ );
}
// if we aren't adding to a crate root, walk backwards such that we support `#[path = ...]` overrides if possible
@@ -141,10 +154,12 @@ fn fixes(
paths.into_iter().find_map(|path| source_root.file_for_path(&path))
})?;
stack.pop();
- 'crates: for &krate in ctx.sema.db.relevant_crates(parent_id).iter() {
+ let relevant_crates = db.relevant_crates(parent_id);
+ 'crates: for &krate in relevant_crates.iter() {
let crate_def_map = ctx.sema.db.crate_def_map(krate);
let Some((_, module)) = crate_def_map.modules().find(|(_, module)| {
- module.origin.file_id().map(Into::into) == Some(parent_id) && !module.origin.is_inline()
+ module.origin.file_id().map(|file_id| file_id.file_id(ctx.sema.db)) == Some(parent_id)
+ && !module.origin.is_inline()
}) else {
continue;
};
@@ -174,7 +189,12 @@ fn fixes(
let InFile { file_id: parent_file_id, value: source } =
current.definition_source(ctx.sema.db);
let parent_file_id = parent_file_id.file_id()?;
- return make_fixes(parent_file_id.file_id(), source, &module_name, trigger_range);
+ return make_fixes(
+ parent_file_id.file_id(ctx.sema.db),
+ source,
+ &module_name,
+ trigger_range,
+ );
}
}
@@ -193,9 +213,11 @@ fn make_fixes(
let mod_decl = format!("mod {new_mod_name};");
let pub_mod_decl = format!("pub mod {new_mod_name};");
+ let pub_crate_mod_decl = format!("pub(crate) mod {new_mod_name};");
let mut mod_decl_builder = TextEdit::builder();
let mut pub_mod_decl_builder = TextEdit::builder();
+ let mut pub_crate_mod_decl_builder = TextEdit::builder();
let mut items = match &source {
ModuleSource::SourceFile(it) => it.items(),
@@ -224,6 +246,7 @@ fn make_fixes(
let indent = IndentLevel::from_node(last.syntax());
mod_decl_builder.insert(offset, format!("\n{indent}{mod_decl}"));
pub_mod_decl_builder.insert(offset, format!("\n{indent}{pub_mod_decl}"));
+ pub_crate_mod_decl_builder.insert(offset, format!("\n{indent}{pub_crate_mod_decl}"));
}
None => {
// Prepend before the first item in the file.
@@ -234,6 +257,8 @@ fn make_fixes(
let indent = IndentLevel::from_node(first.syntax());
mod_decl_builder.insert(offset, format!("{mod_decl}\n\n{indent}"));
pub_mod_decl_builder.insert(offset, format!("{pub_mod_decl}\n\n{indent}"));
+ pub_crate_mod_decl_builder
+ .insert(offset, format!("{pub_crate_mod_decl}\n\n{indent}"));
}
None => {
// No items in the file, so just append at the end.
@@ -251,6 +276,8 @@ fn make_fixes(
};
mod_decl_builder.insert(offset, format!("{indent}{mod_decl}\n"));
pub_mod_decl_builder.insert(offset, format!("{indent}{pub_mod_decl}\n"));
+ pub_crate_mod_decl_builder
+ .insert(offset, format!("{indent}{pub_crate_mod_decl}\n"));
}
}
}
@@ -269,6 +296,12 @@ fn make_fixes(
SourceChange::from_text_edit(parent_file_id, pub_mod_decl_builder.finish()),
trigger_range,
),
+ fix(
+ "add_pub_crate_mod_declaration",
+ &format!("Insert `{pub_crate_mod_decl}`"),
+ SourceChange::from_text_edit(parent_file_id, pub_crate_mod_decl_builder.finish()),
+ trigger_range,
+ ),
])
}
@@ -298,6 +331,11 @@ pub mod foo;
fn f() {}
"#,
+ r#"
+pub(crate) mod foo;
+
+fn f() {}
+"#,
],
);
}