Skip to content

feat: add pub(crate) mod option for unlinked files #19590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/ide-diagnostics/src/handlers/unlinked_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,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(),
Expand Down Expand Up @@ -234,6 +236,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.
Expand All @@ -244,6 +247,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.
Expand All @@ -261,6 +266,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"));
}
}
}
Expand All @@ -279,6 +286,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,
),
])
}

Expand Down Expand Up @@ -306,6 +319,11 @@ fn f() {}
r#"
pub mod foo;

fn f() {}
"#,
r#"
pub(crate) mod foo;

fn f() {}
"#,
],
Expand Down