Skip to content
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

fix: extract mod to file should respect path attribute #17216

Merged
merged 2 commits into from
May 14, 2024
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
81 changes: 78 additions & 3 deletions crates/ide-assists/src/handlers/move_module_to_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::iter;

use ast::edit::IndentLevel;
use hir::HasAttrs;
use ide_db::base_db::AnchoredPathBuf;
use itertools::Itertools;
use stdx::format_to;
Expand Down Expand Up @@ -50,9 +51,17 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|builder| {
let path = {
let mut buf = String::from("./");
match parent_module.name(ctx.db()) {
Some(name) if !parent_module.is_mod_rs(ctx.db()) => {
format_to!(buf, "{}/", name.display(ctx.db()))
let db = ctx.db();
match parent_module.name(db) {
Some(name)
if !parent_module.is_mod_rs(db)
&& parent_module
.attrs(db)
.by_key("path")
.string_value_unescape()
.is_none() =>
{
format_to!(buf, "{}/", name.display(db))
}
_ => (),
}
Expand Down Expand Up @@ -107,6 +116,72 @@ mod tests {

use super::*;

#[test]
fn extract_with_specified_path_attr() {
check_assist(
move_module_to_file,
r#"
//- /main.rs
#[path="parser/__mod.rs"]
mod parser;
//- /parser/__mod.rs
fn test() {}
mod $0expr {
struct A {}
}
"#,
r#"
//- /parser/__mod.rs
fn test() {}
mod expr;
//- /parser/expr.rs
Comment on lines +136 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be correct, the module would need to be at /parser/__mod/expr.rs for this to be loaded no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parser/expr.rs can be loaded.

mod_with_path

IMO, with #path specified, parser/__mod.rs for mod parser(in the following code), can be seen as parser/mod.rs

#[path="parser/__mod.rs"]
mod parser;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks for double checking.

struct A {}
"#,
);

check_assist(
move_module_to_file,
r#"
//- /main.rs
#[path="parser/a/__mod.rs"]
mod parser;
//- /parser/a/__mod.rs
fn test() {}
mod $0expr {
struct A {}
}
"#,
r#"
//- /parser/a/__mod.rs
fn test() {}
mod expr;
//- /parser/a/expr.rs
struct A {}
"#,
);

check_assist(
move_module_to_file,
r#"
//- /main.rs
#[path="a.rs"]
mod parser;
//- /a.rs
fn test() {}
mod $0expr {
struct A {}
}
"#,
r#"
//- /a.rs
fn test() {}
mod expr;
//- /expr.rs
struct A {}
"#,
);
}

#[test]
fn extract_from_root() {
check_assist(
Expand Down