Skip to content
Merged
Show file tree
Hide file tree
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
130 changes: 130 additions & 0 deletions crates/ide_assists/src/handlers/move_from_mod_rs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use ide_db::{
assists::{AssistId, AssistKind},
base_db::AnchoredPathBuf,
};
use syntax::{ast, AstNode};

use crate::{
assist_context::{AssistContext, Assists},
utils::trimmed_text_range,
};

// Assist: move_from_mod_rs
//
// Moves xxx/mod.rs to xxx.rs.
//
// ```
// //- /main.rs
// mod a;
// //- /a/mod.rs
// $0fn t() {}$0
// ```
// ->
// ```
// fn t() {}
// ```
pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
let module = ctx.sema.to_module_def(ctx.frange.file_id)?;
// Enable this assist if the user select all "meaningful" content in the source file
let trimmed_selected_range = trimmed_text_range(&source_file, ctx.frange.range);
let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
if !module.is_mod_rs(ctx.db()) {
cov_mark::hit!(not_mod_rs);
return None;
}
if trimmed_selected_range != trimmed_file_range {
cov_mark::hit!(not_all_selected);
return None;
}

let target = source_file.syntax().text_range();
let module_name = module.name(ctx.db())?.to_string();
let path = format!("../{}.rs", module_name);
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
acc.add(
AssistId("move_from_mod_rs", AssistKind::Refactor),
format!("Convert {}/mod.rs to {}.rs", module_name, module_name),
target,
|builder| {
builder.move_file(ctx.frange.file_id, dst);
},
)
}

#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};

use super::*;

#[test]
fn trivial() {
check_assist(
move_from_mod_rs,
r#"
//- /main.rs
mod a;
//- /a/mod.rs
$0fn t() {}
$0"#,
r#"
//- /a.rs
fn t() {}
"#,
);
}

#[test]
fn must_select_all_file() {
cov_mark::check!(not_all_selected);
check_assist_not_applicable(
move_from_mod_rs,
r#"
//- /main.rs
mod a;
//- /a/mod.rs
fn t() {}$0
"#,
);
cov_mark::check!(not_all_selected);
check_assist_not_applicable(
move_from_mod_rs,
r#"
//- /main.rs
mod a;
//- /a/mod.rs
$0fn$0 t() {}
"#,
);
}

#[test]
fn cannot_move_not_mod_rs() {
cov_mark::check!(not_mod_rs);
check_assist_not_applicable(
move_from_mod_rs,
r#"//- /main.rs
mod a;
//- /a.rs
$0fn t() {}$0
"#,
);
}

#[test]
fn cannot_downgrade_main_and_lib_rs() {
check_assist_not_applicable(
move_from_mod_rs,
r#"//- /main.rs
$0fn t() {}$0
"#,
);
check_assist_not_applicable(
move_from_mod_rs,
r#"//- /lib.rs
$0fn t() {}$0
"#,
);
}
}
43 changes: 7 additions & 36 deletions crates/ide_assists/src/handlers/move_to_mod_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,12 @@ use ide_db::{
assists::{AssistId, AssistKind},
base_db::AnchoredPathBuf,
};
use syntax::{
ast::{self, Whitespace},
AstNode, AstToken, SourceFile, TextRange, TextSize,
};

use crate::assist_context::{AssistContext, Assists};
use syntax::{ast, AstNode};

/// Trim(remove leading and trailing whitespace) `initial_range` in `source_file`, return the trimmed range.
fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> TextRange {
let mut trimmed_range = initial_range;
while source_file
.syntax()
.token_at_offset(trimmed_range.start())
.find_map(Whitespace::cast)
.is_some()
&& trimmed_range.start() < trimmed_range.end()
{
let start = trimmed_range.start() + TextSize::from(1);
trimmed_range = TextRange::new(start, trimmed_range.end());
}
while source_file
.syntax()
.token_at_offset(trimmed_range.end())
.find_map(Whitespace::cast)
.is_some()
&& trimmed_range.start() < trimmed_range.end()
{
let end = trimmed_range.end() - TextSize::from(1);
trimmed_range = TextRange::new(trimmed_range.start(), end);
}
trimmed_range
}
use crate::{
assist_context::{AssistContext, Assists},
utils::trimmed_text_range,
};

// Assist: move_to_mod_rs
//
Expand Down Expand Up @@ -64,16 +38,13 @@ pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<(
return None;
}

let target = TextRange::new(
source_file.syntax().text_range().start(),
source_file.syntax().text_range().end(),
);
let target = source_file.syntax().text_range();
let module_name = module.name(ctx.db())?.to_string();
let path = format!("./{}/mod.rs", module_name);
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
acc.add(
AssistId("move_to_mod_rs", AssistKind::Refactor),
format!("Turn {}.rs to {}/mod.rs", module_name, module_name),
format!("Convert {}.rs to {}/mod.rs", module_name, module_name),
target,
|builder| {
builder.move_file(ctx.frange.file_id, dst);
Expand Down
2 changes: 2 additions & 0 deletions crates/ide_assists/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ mod handlers {
mod move_guard;
mod move_module_to_file;
mod move_to_mod_rs;
mod move_from_mod_rs;
mod pull_assignment_up;
mod qualify_path;
mod raw_string;
Expand Down Expand Up @@ -229,6 +230,7 @@ mod handlers {
move_guard::move_guard_to_arm_body,
move_module_to_file::move_module_to_file,
move_to_mod_rs::move_to_mod_rs,
move_from_mod_rs::move_from_mod_rs,
pull_assignment_up::pull_assignment_up,
qualify_path::qualify_path,
raw_string::add_hash,
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label:
let sr = db.source_root(sr);
let mut base = sr.path_for_file(&dst.anchor).unwrap().clone();
base.pop();
let created_file_path = format!("{}{}", base.to_string(), &dst.path[1..]);
let created_file_path = base.join(&dst.path).unwrap();
format_to!(buf, "//- {}\n", created_file_path);
buf.push_str(&contents);
}
Expand Down
16 changes: 16 additions & 0 deletions crates/ide_assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,22 @@ fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
)
}

#[test]
fn doctest_move_from_mod_rs() {
check_doc_test(
"move_from_mod_rs",
r#####"
//- /main.rs
mod a;
//- /a/mod.rs
$0fn t() {}$0
"#####,
r#####"
fn t() {}
"#####,
)
}

#[test]
fn doctest_move_guard_to_arm_body() {
check_doc_test(
Expand Down
32 changes: 29 additions & 3 deletions crates/ide_assists/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use syntax::{
self,
edit::{self, AstNodeEdit},
edit_in_place::AttrsOwnerEdit,
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds,
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
},
ted, AstNode, Direction, SmolStr,
ted, AstNode, AstToken, Direction, SmolStr, SourceFile,
SyntaxKind::*,
SyntaxNode, TextSize, T,
SyntaxNode, TextRange, TextSize, T,
};

use crate::assist_context::{AssistBuilder, AssistContext};
Expand Down Expand Up @@ -500,3 +500,29 @@ pub(crate) fn get_methods(items: &ast::AssocItemList) -> Vec<ast::Fn> {
.filter(|f| f.name().is_some())
.collect()
}

/// Trim(remove leading and trailing whitespace) `initial_range` in `source_file`, return the trimmed range.
pub(crate) fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> TextRange {
let mut trimmed_range = initial_range;
while source_file
.syntax()
.token_at_offset(trimmed_range.start())
.find_map(Whitespace::cast)
.is_some()
&& trimmed_range.start() < trimmed_range.end()
{
let start = trimmed_range.start() + TextSize::from(1);
trimmed_range = TextRange::new(start, trimmed_range.end());
}
while source_file
.syntax()
.token_at_offset(trimmed_range.end())
.find_map(Whitespace::cast)
.is_some()
&& trimmed_range.start() < trimmed_range.end()
{
let end = trimmed_range.end() - TextSize::from(1);
trimmed_range = TextRange::new(trimmed_range.start(), end);
}
trimmed_range
}
1 change: 1 addition & 0 deletions crates/vfs/src/vfs_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ impl VirtualPath {
}
path = &path["../".len()..]
}
path = path.trim_start_matches("./");
res.0 = format!("{}/{}", res.0, path);
Some(res)
}
Expand Down