2020-05-06 03:21:35 -05:00
|
|
|
mod generated;
|
|
|
|
|
2021-02-22 06:18:11 -06:00
|
|
|
use expect_test::expect;
|
2020-05-06 03:16:55 -05:00
|
|
|
use hir::Semantics;
|
2021-01-06 11:43:46 -06:00
|
|
|
use ide_db::{
|
|
|
|
base_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt},
|
2021-05-18 12:49:15 -05:00
|
|
|
helpers::{
|
|
|
|
insert_use::{ImportGranularity, InsertUseConfig},
|
|
|
|
SnippetCap,
|
|
|
|
},
|
2021-01-06 11:43:46 -06:00
|
|
|
source_change::FileSystemEdit,
|
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-02-22 06:18:11 -06:00
|
|
|
use stdx::{format_to, trim_indent};
|
2021-03-02 07:38:51 -06:00
|
|
|
use syntax::TextRange;
|
2021-02-22 06:18:11 -06:00
|
|
|
use test_utils::{assert_eq_text, extract_offset};
|
2020-05-06 03:16:55 -05:00
|
|
|
|
2021-05-03 09:08:09 -05:00
|
|
|
use crate::{
|
|
|
|
handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, AssistResolveStrategy,
|
2021-05-03 10:40:04 -05:00
|
|
|
Assists, SingleResolve,
|
2021-05-03 09:08:09 -05:00
|
|
|
};
|
2020-05-06 03:16:55 -05:00
|
|
|
|
2021-01-06 11:43:46 -06:00
|
|
|
pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
|
|
|
|
snippet_cap: SnippetCap::new(true),
|
|
|
|
allowed: None,
|
|
|
|
insert_use: InsertUseConfig {
|
2021-05-18 12:49:15 -05:00
|
|
|
granularity: ImportGranularity::Crate,
|
2021-01-06 11:43:46 -06:00
|
|
|
prefix_kind: hir::PrefixKind::Plain,
|
2021-05-18 13:21:47 -05:00
|
|
|
enforce_granularity: true,
|
2021-03-06 05:02:26 -06:00
|
|
|
group: true,
|
2021-01-06 11:43:46 -06:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-06 03:16:55 -05:00
|
|
|
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
2020-06-11 04:04:09 -05:00
|
|
|
RootDatabase::with_single_file(text)
|
2020-05-06 03:16:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
|
2020-06-23 17:30:34 -05:00
|
|
|
let ra_fixture_after = trim_indent(ra_fixture_after);
|
2020-09-29 17:22:09 -05:00
|
|
|
check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no way to choose what assist within a group you want to test against,
|
|
|
|
// so this is here to allow you choose.
|
|
|
|
pub(crate) fn check_assist_by_label(
|
|
|
|
assist: Handler,
|
|
|
|
ra_fixture_before: &str,
|
|
|
|
ra_fixture_after: &str,
|
|
|
|
label: &str,
|
|
|
|
) {
|
|
|
|
let ra_fixture_after = trim_indent(ra_fixture_after);
|
|
|
|
check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), Some(label));
|
2020-05-06 03:16:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: instead of having a separate function here, maybe use
|
|
|
|
// `extract_ranges` and mark the target as `<target> </target>` in the
|
2020-08-10 23:09:11 -05:00
|
|
|
// fixture?
|
2021-02-09 17:16:00 -06:00
|
|
|
#[track_caller]
|
2020-05-06 03:16:55 -05:00
|
|
|
pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) {
|
2020-09-29 17:22:09 -05:00
|
|
|
check(assist, ra_fixture, ExpectedResult::Target(target), None);
|
2020-05-06 03:16:55 -05:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:16:00 -06:00
|
|
|
#[track_caller]
|
2020-05-06 03:16:55 -05:00
|
|
|
pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) {
|
2020-09-29 17:22:09 -05:00
|
|
|
check(assist, ra_fixture, ExpectedResult::NotApplicable, None);
|
2020-05-06 03:16:55 -05:00
|
|
|
}
|
|
|
|
|
2021-05-21 01:27:41 -05:00
|
|
|
/// Check assist in unresolved state. Useful to check assists for lazy computation.
|
|
|
|
#[track_caller]
|
|
|
|
pub(crate) fn check_assist_unresolved(assist: Handler, ra_fixture: &str) {
|
|
|
|
check(assist, ra_fixture, ExpectedResult::Unresolved, None);
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:16:00 -06:00
|
|
|
#[track_caller]
|
2020-05-06 03:18:12 -05:00
|
|
|
fn check_doc_test(assist_id: &str, before: &str, after: &str) {
|
2020-06-23 17:48:38 -05:00
|
|
|
let after = trim_indent(after);
|
|
|
|
let (db, file_id, selection) = RootDatabase::with_range_or_offset(&before);
|
|
|
|
let before = db.file_text(file_id).to_string();
|
2020-05-06 03:18:12 -05:00
|
|
|
let frange = FileRange { file_id, range: selection.into() };
|
|
|
|
|
2021-05-03 09:08:09 -05:00
|
|
|
let assist = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::All, frange)
|
2020-05-06 03:18:12 -05:00
|
|
|
.into_iter()
|
2020-12-26 05:11:42 -06:00
|
|
|
.find(|assist| assist.id.0 == assist_id)
|
2020-05-06 03:18:12 -05:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
panic!(
|
|
|
|
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
|
|
|
|
assist_id,
|
2021-05-03 09:08:09 -05:00
|
|
|
Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange)
|
2020-05-06 03:18:12 -05:00
|
|
|
.into_iter()
|
2020-12-26 05:11:42 -06:00
|
|
|
.map(|assist| assist.id.0)
|
2020-05-06 03:18:12 -05:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
let actual = {
|
2021-04-17 14:34:14 -05:00
|
|
|
let source_change =
|
|
|
|
assist.source_change.expect("Assist did not contain any source changes");
|
2020-06-23 17:48:38 -05:00
|
|
|
let mut actual = before;
|
2021-01-14 15:43:36 -06:00
|
|
|
if let Some(source_file_edit) = source_change.get_source_edit(file_id) {
|
2021-01-14 11:35:22 -06:00
|
|
|
source_file_edit.apply(&mut actual);
|
2020-12-07 10:17:13 -06:00
|
|
|
}
|
2020-05-06 03:18:12 -05:00
|
|
|
actual
|
|
|
|
};
|
2020-06-23 17:48:38 -05:00
|
|
|
assert_eq_text!(&after, &actual);
|
2020-05-06 03:18:12 -05:00
|
|
|
}
|
|
|
|
|
2020-05-06 03:16:55 -05:00
|
|
|
enum ExpectedResult<'a> {
|
|
|
|
NotApplicable,
|
2021-05-21 01:27:41 -05:00
|
|
|
Unresolved,
|
2020-05-06 03:16:55 -05:00
|
|
|
After(&'a str),
|
|
|
|
Target(&'a str),
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:16:00 -06:00
|
|
|
#[track_caller]
|
2020-09-29 17:22:09 -05:00
|
|
|
fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: Option<&str>) {
|
2020-06-23 17:30:34 -05:00
|
|
|
let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before);
|
2020-06-23 17:48:38 -05:00
|
|
|
let text_without_caret = db.file_text(file_with_caret_id).to_string();
|
2020-05-06 03:16:55 -05:00
|
|
|
|
|
|
|
let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
|
|
|
|
|
|
|
|
let sema = Semantics::new(&db);
|
2021-01-06 11:43:46 -06:00
|
|
|
let config = TEST_CONFIG;
|
2020-07-15 08:45:30 -05:00
|
|
|
let ctx = AssistContext::new(sema, &config, frange);
|
2021-05-21 01:27:41 -05:00
|
|
|
let resolve = match expected {
|
|
|
|
ExpectedResult::Unresolved => AssistResolveStrategy::None,
|
|
|
|
_ => AssistResolveStrategy::All,
|
|
|
|
};
|
|
|
|
let mut acc = Assists::new(&ctx, resolve);
|
2020-05-06 11:45:35 -05:00
|
|
|
handler(&mut acc, &ctx);
|
2020-12-26 05:11:42 -06:00
|
|
|
let mut res = acc.finish();
|
2020-09-29 17:22:09 -05:00
|
|
|
|
|
|
|
let assist = match assist_label {
|
2020-12-26 05:11:42 -06:00
|
|
|
Some(label) => res.into_iter().find(|resolved| resolved.label == label),
|
2020-09-29 17:22:09 -05:00
|
|
|
None => res.pop(),
|
|
|
|
};
|
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
match (assist, expected) {
|
2020-05-06 03:16:55 -05:00
|
|
|
(Some(assist), ExpectedResult::After(after)) => {
|
2021-04-17 14:34:14 -05:00
|
|
|
let source_change =
|
|
|
|
assist.source_change.expect("Assist did not contain any source changes");
|
2020-11-09 07:22:45 -06:00
|
|
|
assert!(!source_change.source_file_edits.is_empty());
|
2020-12-07 10:17:13 -06:00
|
|
|
let skip_header = source_change.source_file_edits.len() == 1
|
|
|
|
&& source_change.file_system_edits.len() == 0;
|
2020-11-09 07:22:45 -06:00
|
|
|
|
|
|
|
let mut buf = String::new();
|
2021-01-14 15:43:36 -06:00
|
|
|
for (file_id, edit) in source_change.source_file_edits {
|
2021-01-14 11:35:22 -06:00
|
|
|
let mut text = db.file_text(file_id).as_ref().to_owned();
|
|
|
|
edit.apply(&mut text);
|
2020-12-17 07:09:55 -06:00
|
|
|
if !skip_header {
|
2021-01-14 11:35:22 -06:00
|
|
|
let sr = db.file_source_root(file_id);
|
2020-12-17 07:09:55 -06:00
|
|
|
let sr = db.source_root(sr);
|
2021-01-14 11:35:22 -06:00
|
|
|
let path = sr.path_for_file(&file_id).unwrap();
|
2020-12-17 07:09:55 -06:00
|
|
|
format_to!(buf, "//- {}\n", path)
|
2020-12-07 10:17:13 -06:00
|
|
|
}
|
2020-12-17 07:09:55 -06:00
|
|
|
buf.push_str(&text);
|
2020-12-07 10:17:13 -06:00
|
|
|
}
|
|
|
|
|
2021-01-14 15:43:36 -06:00
|
|
|
for file_system_edit in source_change.file_system_edits {
|
|
|
|
if let FileSystemEdit::CreateFile { dst, initial_contents } = file_system_edit {
|
|
|
|
let sr = db.file_source_root(dst.anchor);
|
|
|
|
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..]);
|
|
|
|
format_to!(buf, "//- {}\n", created_file_path);
|
|
|
|
buf.push_str(&initial_contents);
|
2020-11-09 07:22:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq_text!(after, &buf);
|
2020-05-06 03:16:55 -05:00
|
|
|
}
|
|
|
|
(Some(assist), ExpectedResult::Target(target)) => {
|
2020-12-26 05:11:42 -06:00
|
|
|
let range = assist.target;
|
2020-05-06 03:16:55 -05:00
|
|
|
assert_eq_text!(&text_without_caret[range], target);
|
|
|
|
}
|
2021-05-21 01:27:41 -05:00
|
|
|
(Some(assist), ExpectedResult::Unresolved) => assert!(
|
|
|
|
assist.source_change.is_none(),
|
|
|
|
"unresolved assist should not contain source changes"
|
|
|
|
),
|
2020-05-06 03:16:55 -05:00
|
|
|
(Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"),
|
2021-05-21 01:27:41 -05:00
|
|
|
(None, ExpectedResult::After(_))
|
|
|
|
| (None, ExpectedResult::Target(_))
|
|
|
|
| (None, ExpectedResult::Unresolved) => {
|
2020-05-06 03:16:55 -05:00
|
|
|
panic!("code action is not applicable")
|
|
|
|
}
|
|
|
|
(None, ExpectedResult::NotApplicable) => (),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:18:11 -06:00
|
|
|
fn labels(assists: &[Assist]) -> String {
|
|
|
|
let mut labels = assists
|
|
|
|
.iter()
|
|
|
|
.map(|assist| {
|
|
|
|
let mut label = match &assist.group {
|
|
|
|
Some(g) => g.0.clone(),
|
|
|
|
None => assist.label.to_string(),
|
|
|
|
};
|
|
|
|
label.push('\n');
|
|
|
|
label
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
labels.dedup();
|
|
|
|
labels.into_iter().collect::<String>()
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:16:55 -05:00
|
|
|
#[test]
|
|
|
|
fn assist_order_field_struct() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let before = "struct Foo { $0bar: u32 }";
|
2020-05-06 03:16:55 -05:00
|
|
|
let (before_cursor_pos, before) = extract_offset(before);
|
|
|
|
let (db, file_id) = with_single_file(&before);
|
|
|
|
let frange = FileRange { file_id, range: TextRange::empty(before_cursor_pos) };
|
2021-05-03 09:08:09 -05:00
|
|
|
let assists = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
|
2020-05-06 03:16:55 -05:00
|
|
|
let mut assists = assists.iter();
|
|
|
|
|
2020-12-26 05:11:42 -06:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)");
|
2021-04-11 02:31:20 -05:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label, "Generate `Deref` impl using `bar`");
|
2021-02-09 05:30:13 -06:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label, "Generate a mut getter method");
|
2021-02-28 12:11:41 -06:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label, "Generate a getter method");
|
2021-02-09 05:30:13 -06:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label, "Generate a setter method");
|
2020-12-26 05:11:42 -06:00
|
|
|
assert_eq!(assists.next().expect("expected assist").label, "Add `#[derive]`");
|
2020-05-06 03:16:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assist_order_if_expr() {
|
2021-02-22 06:18:11 -06:00
|
|
|
let (db, frange) = RootDatabase::with_range(
|
|
|
|
r#"
|
|
|
|
pub fn test_some_range(a: int) -> bool {
|
|
|
|
if let 2..6 = $05$0 {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
2021-05-03 09:08:09 -05:00
|
|
|
let assists = Assist::get(&db, &TEST_CONFIG, AssistResolveStrategy::None, frange);
|
2021-02-22 06:18:11 -06:00
|
|
|
let expected = labels(&assists);
|
2020-05-06 03:16:55 -05:00
|
|
|
|
2021-02-22 06:18:11 -06:00
|
|
|
expect![[r#"
|
|
|
|
Convert integer base
|
|
|
|
Extract into variable
|
|
|
|
Extract into function
|
|
|
|
Replace with match
|
|
|
|
"#]]
|
|
|
|
.assert_eq(&expected);
|
2020-05-06 03:16:55 -05:00
|
|
|
}
|
2020-07-13 16:41:47 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn assist_filter_works() {
|
2021-02-22 06:18:11 -06:00
|
|
|
let (db, frange) = RootDatabase::with_range(
|
|
|
|
r#"
|
|
|
|
pub fn test_some_range(a: int) -> bool {
|
|
|
|
if let 2..6 = $05$0 {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
2020-07-13 16:41:47 -05:00
|
|
|
{
|
2021-01-06 11:43:46 -06:00
|
|
|
let mut cfg = TEST_CONFIG;
|
2020-07-15 08:45:30 -05:00
|
|
|
cfg.allowed = Some(vec![AssistKind::Refactor]);
|
2020-07-13 16:41:47 -05:00
|
|
|
|
2021-05-03 09:08:09 -05:00
|
|
|
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
|
2021-02-22 06:18:11 -06:00
|
|
|
let expected = labels(&assists);
|
2020-07-13 16:41:47 -05:00
|
|
|
|
2021-02-22 06:18:11 -06:00
|
|
|
expect![[r#"
|
|
|
|
Convert integer base
|
|
|
|
Extract into variable
|
|
|
|
Extract into function
|
|
|
|
Replace with match
|
|
|
|
"#]]
|
|
|
|
.assert_eq(&expected);
|
2020-07-13 16:41:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-01-06 11:43:46 -06:00
|
|
|
let mut cfg = TEST_CONFIG;
|
2020-07-15 08:45:30 -05:00
|
|
|
cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
|
2021-05-03 09:08:09 -05:00
|
|
|
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
|
2021-02-22 06:18:11 -06:00
|
|
|
let expected = labels(&assists);
|
2020-07-13 16:41:47 -05:00
|
|
|
|
2021-02-22 06:18:11 -06:00
|
|
|
expect![[r#"
|
|
|
|
Extract into variable
|
|
|
|
Extract into function
|
|
|
|
"#]]
|
|
|
|
.assert_eq(&expected);
|
2020-07-13 16:41:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-01-06 11:43:46 -06:00
|
|
|
let mut cfg = TEST_CONFIG;
|
2020-07-15 08:45:30 -05:00
|
|
|
cfg.allowed = Some(vec![AssistKind::QuickFix]);
|
2021-05-03 09:08:09 -05:00
|
|
|
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
|
2021-02-22 06:18:11 -06:00
|
|
|
let expected = labels(&assists);
|
|
|
|
|
|
|
|
expect![[r#""#]].assert_eq(&expected);
|
2020-07-13 16:41:47 -05:00
|
|
|
}
|
|
|
|
}
|
2021-05-03 10:40:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn various_resolve_strategies() {
|
|
|
|
let (db, frange) = RootDatabase::with_range(
|
|
|
|
r#"
|
|
|
|
pub fn test_some_range(a: int) -> bool {
|
|
|
|
if let 2..6 = $05$0 {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut cfg = TEST_CONFIG;
|
|
|
|
cfg.allowed = Some(vec![AssistKind::RefactorExtract]);
|
|
|
|
|
|
|
|
{
|
|
|
|
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::None, frange);
|
|
|
|
assert_eq!(2, assists.len());
|
|
|
|
let mut assists = assists.into_iter();
|
|
|
|
|
|
|
|
let extract_into_variable_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_variable",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into variable",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: None,
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_variable_assist);
|
|
|
|
|
|
|
|
let extract_into_function_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_function",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into function",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: None,
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_function_assist);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let assists = Assist::get(
|
|
|
|
&db,
|
|
|
|
&cfg,
|
|
|
|
AssistResolveStrategy::Single(SingleResolve {
|
|
|
|
assist_id: "SOMETHING_MISMATCHING".to_string(),
|
|
|
|
assist_kind: AssistKind::RefactorExtract,
|
|
|
|
}),
|
|
|
|
frange,
|
|
|
|
);
|
|
|
|
assert_eq!(2, assists.len());
|
|
|
|
let mut assists = assists.into_iter();
|
|
|
|
|
|
|
|
let extract_into_variable_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_variable",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into variable",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: None,
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_variable_assist);
|
|
|
|
|
|
|
|
let extract_into_function_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_function",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into function",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: None,
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_function_assist);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let assists = Assist::get(
|
|
|
|
&db,
|
|
|
|
&cfg,
|
|
|
|
AssistResolveStrategy::Single(SingleResolve {
|
|
|
|
assist_id: "extract_variable".to_string(),
|
|
|
|
assist_kind: AssistKind::RefactorExtract,
|
|
|
|
}),
|
|
|
|
frange,
|
|
|
|
);
|
|
|
|
assert_eq!(2, assists.len());
|
|
|
|
let mut assists = assists.into_iter();
|
|
|
|
|
|
|
|
let extract_into_variable_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_variable",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into variable",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: Some(
|
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "let $0var_name = 5;\n ",
|
|
|
|
delete: 45..45,
|
|
|
|
},
|
|
|
|
Indel {
|
|
|
|
insert: "var_name",
|
|
|
|
delete: 59..60,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
file_system_edits: [],
|
|
|
|
is_snippet: true,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_variable_assist);
|
|
|
|
|
|
|
|
let extract_into_function_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_function",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into function",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: None,
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_function_assist);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let assists = Assist::get(&db, &cfg, AssistResolveStrategy::All, frange);
|
|
|
|
assert_eq!(2, assists.len());
|
|
|
|
let mut assists = assists.into_iter();
|
|
|
|
|
|
|
|
let extract_into_variable_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_variable",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into variable",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: Some(
|
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "let $0var_name = 5;\n ",
|
|
|
|
delete: 45..45,
|
|
|
|
},
|
|
|
|
Indel {
|
|
|
|
insert: "var_name",
|
|
|
|
delete: 59..60,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
file_system_edits: [],
|
|
|
|
is_snippet: true,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_variable_assist);
|
|
|
|
|
|
|
|
let extract_into_function_assist = assists.next().unwrap();
|
|
|
|
expect![[r#"
|
|
|
|
Assist {
|
|
|
|
id: AssistId(
|
|
|
|
"extract_function",
|
|
|
|
RefactorExtract,
|
|
|
|
),
|
|
|
|
label: "Extract into function",
|
|
|
|
group: None,
|
|
|
|
target: 59..60,
|
|
|
|
source_change: Some(
|
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "fun_name()",
|
|
|
|
delete: 59..60,
|
|
|
|
},
|
|
|
|
Indel {
|
|
|
|
insert: "\n\nfn $0fun_name() -> i32 {\n 5\n}",
|
|
|
|
delete: 110..110,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
file_system_edits: [],
|
|
|
|
is_snippet: true,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&extract_into_function_assist);
|
|
|
|
}
|
|
|
|
}
|