1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
mod generated;
use hir::Semantics;
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
use ra_ide_db::RootDatabase;
use ra_syntax::TextRange;
use test_utils::{
assert_eq_text, extract_offset, extract_range, extract_range_or_offset, RangeOrOffset,
};
use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists};
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
RootDatabase::with_single_file(text)
}
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after));
}
// FIXME: instead of having a separate function here, maybe use
// `extract_ranges` and mark the target as `<target> </target>` in the
// fixuture?
pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) {
check(assist, ra_fixture, ExpectedResult::Target(target));
}
pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) {
check(assist, ra_fixture, ExpectedResult::NotApplicable);
}
fn check_doc_test(assist_id: &str, before: &str, after: &str) {
let (selection, before) = extract_range_or_offset(before);
let (db, file_id) = crate::tests::with_single_file(&before);
let frange = FileRange { file_id, range: selection.into() };
let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange)
.into_iter()
.find(|assist| assist.assist.id.0 == assist_id)
.unwrap_or_else(|| {
panic!(
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
assist_id,
Assist::resolved(&db, &AssistConfig::default(), frange)
.into_iter()
.map(|assist| assist.assist.id.0)
.collect::<Vec<_>>()
.join(", ")
)
});
let actual = {
let change = assist.source_change.source_file_edits.pop().unwrap();
let mut actual = before.clone();
change.edit.apply(&mut actual);
actual
};
assert_eq_text!(after, &actual);
}
enum ExpectedResult<'a> {
NotApplicable,
After(&'a str),
Target(&'a str),
}
fn check(handler: Handler, before: &str, expected: ExpectedResult) {
let (text_without_caret, file_with_caret_id, range_or_offset, db) = if before.contains("//-") {
let (db, position) = RootDatabase::with_position(before);
(
db.file_text(position.file_id).as_ref().to_owned(),
position.file_id,
RangeOrOffset::Offset(position.offset),
db,
)
} else {
let (range_or_offset, text_without_caret) = extract_range_or_offset(before);
let (db, file_id) = with_single_file(&text_without_caret);
(text_without_caret, file_id, range_or_offset, db)
};
let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
let sema = Semantics::new(&db);
let config = AssistConfig::default();
let ctx = AssistContext::new(sema, &config, frange);
let mut acc = Assists::new_resolved(&ctx);
handler(&mut acc, &ctx);
let mut res = acc.finish_resolved();
let assist = res.pop();
match (assist, expected) {
(Some(assist), ExpectedResult::After(after)) => {
let mut source_change = assist.source_change;
let change = source_change.source_file_edits.pop().unwrap();
let mut actual = db.file_text(change.file_id).as_ref().to_owned();
change.edit.apply(&mut actual);
assert_eq_text!(after, &actual);
}
(Some(assist), ExpectedResult::Target(target)) => {
let range = assist.assist.target;
assert_eq_text!(&text_without_caret[range], target);
}
(Some(_), ExpectedResult::NotApplicable) => panic!("assist should not be applicable!"),
(None, ExpectedResult::After(_)) | (None, ExpectedResult::Target(_)) => {
panic!("code action is not applicable")
}
(None, ExpectedResult::NotApplicable) => (),
};
}
#[test]
fn assist_order_field_struct() {
let before = "struct Foo { <|>bar: u32 }";
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) };
let assists = Assist::resolved(&db, &AssistConfig::default(), frange);
let mut assists = assists.iter();
assert_eq!(
assists.next().expect("expected assist").assist.label,
"Change visibility to pub(crate)"
);
assert_eq!(assists.next().expect("expected assist").assist.label, "Add `#[derive]`");
}
#[test]
fn assist_order_if_expr() {
let before = "
pub fn test_some_range(a: int) -> bool {
if let 2..6 = <|>5<|> {
true
} else {
false
}
}";
let (range, before) = extract_range(before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };
let assists = Assist::resolved(&db, &AssistConfig::default(), frange);
let mut assists = assists.iter();
assert_eq!(assists.next().expect("expected assist").assist.label, "Extract into variable");
assert_eq!(assists.next().expect("expected assist").assist.label, "Replace with match");
}
|