rust/crates/ide_assists/src/handlers/promote_mod_file.rs

173 lines
3.9 KiB
Rust
Raw Normal View History

2021-09-11 13:30:56 -05:00
use ide_db::{
assists::{AssistId, AssistKind},
base_db::AnchoredPathBuf,
};
use syntax::{
2021-09-20 07:43:13 -05:00
ast::{self, Whitespace},
AstNode, AstToken, SourceFile, TextRange, TextSize,
2021-09-11 13:30:56 -05:00
};
use crate::assist_context::{AssistContext, Assists};
2021-09-20 07:43:13 -05:00
/// 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
}
2021-09-11 21:53:56 -05:00
// Assist: promote_mod_file
//
// Moves inline module's contents to a separate file.
//
// ```
2021-09-11 22:20:28 -05:00
// //- /main.rs
// mod a;
// //- /a.rs
2021-09-20 07:43:13 -05:00
// $0fn t() {}$0
2021-09-11 21:53:56 -05:00
// ```
// ->
// ```
// fn t() {}
// ```
2021-09-11 13:30:56 -05:00
pub(crate) fn promote_mod_file(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)?;
2021-09-20 07:43:13 -05:00
// 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()) || trimmed_selected_range != trimmed_file_range {
2021-09-11 13:30:56 -05:00
return None;
}
2021-09-20 07:43:13 -05:00
2021-09-11 13:30:56 -05:00
let target = TextRange::new(
source_file.syntax().text_range().start(),
source_file.syntax().text_range().end(),
);
2021-09-20 07:43:13 -05:00
let module_name = module.name(ctx.db())?.to_string();
let path = format!("./{}/mod.rs", module_name);
2021-09-11 13:30:56 -05:00
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
acc.add(
AssistId("promote_mod_file", AssistKind::Refactor),
2021-09-20 07:43:13 -05:00
format!("Turn {}.rs to {}/mod.rs", module_name, module_name),
2021-09-11 13:30:56 -05:00
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(
promote_mod_file,
r#"
//- /main.rs
mod a;
//- /a.rs
$0fn t() {}
2021-09-20 07:43:13 -05:00
$0"#,
2021-09-11 13:30:56 -05:00
r#"
//- /a/mod.rs
fn t() {}
"#,
);
}
#[test]
2021-09-20 07:43:13 -05:00
fn must_select_all_file() {
check_assist_not_applicable(
2021-09-11 13:30:56 -05:00
promote_mod_file,
r#"
//- /main.rs
mod a;
//- /a.rs
fn t() {}$0
"#,
);
2021-09-20 07:43:13 -05:00
check_assist_not_applicable(
2021-09-11 13:30:56 -05:00
promote_mod_file,
r#"
//- /main.rs
mod a;
//- /a.rs
2021-09-20 07:43:13 -05:00
$0fn$0 t() {}
2021-09-11 13:30:56 -05:00
"#,
);
}
#[test]
fn cannot_promote_mod_rs() {
check_assist_not_applicable(
promote_mod_file,
r#"//- /main.rs
mod a;
//- /a/mod.rs
$0fn t() {}
"#,
);
}
#[test]
fn cannot_promote_main_and_lib_rs() {
check_assist_not_applicable(
promote_mod_file,
r#"//- /main.rs
$0fn t() {}
"#,
);
check_assist_not_applicable(
promote_mod_file,
r#"//- /lib.rs
$0fn t() {}
"#,
);
}
#[test]
fn works_in_mod() {
// note: /a/b.rs remains untouched
check_assist(
promote_mod_file,
r#"//- /main.rs
mod a;
//- /a.rs
2021-09-20 07:43:13 -05:00
$0mod b;
fn t() {}$0
2021-09-11 13:30:56 -05:00
//- /a/b.rs
fn t1() {}
"#,
r#"
//- /a/mod.rs
mod b;
fn t() {}
"#,
);
}
}