-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtest_utils.rs
More file actions
126 lines (108 loc) · 3.66 KB
/
Copy pathtest_utils.rs
File metadata and controls
126 lines (108 loc) · 3.66 KB
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
use rowan::TextSize;
use salsa::Database as Db;
use squawk_linter::Edit;
use squawk_syntax::ast;
use crate::db::{Database, File};
use crate::test_utils::Fixture;
use super::{ActionKind, CodeAction};
#[must_use]
pub(super) fn apply_code_action(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
) -> String {
let fixture = Fixture::new(sql);
let offset = fixture.marker().offset_before();
let sql = fixture.sql();
let db = Database::default();
let file = File::new(&db, sql.into());
let parse_result = crate::db::parse(&db, file);
let mut actions = vec![];
f(&db, file, &mut actions, offset);
assert!(
!actions.is_empty(),
"We should always have actions for `apply_code_action`. If you want to ensure there are no actions, use `code_action_not_applicable` instead."
);
let action = &actions[0];
match action.kind {
ActionKind::QuickFix => {
// Quickfixes can fix syntax errors so we don't assert
}
ActionKind::RefactorRewrite => {
assert_eq!(parse_result.errors(), vec![]);
}
}
let mut result = sql.to_string();
let mut edits = action.edits.clone();
edits.sort_by_key(|e| e.text_range.start());
check_overlap(&edits);
edits.reverse();
for edit in edits {
let start: usize = edit.text_range.start().into();
let end: usize = edit.text_range.end().into();
let replacement = edit.text.as_deref().unwrap_or("");
result.replace_range(start..end, replacement);
}
let reparse = ast::SourceFile::parse(&result);
match action.kind {
ActionKind::QuickFix => {
// Quickfixes can fix syntax errors so we don't assert
}
ActionKind::RefactorRewrite => {
assert_eq!(
reparse.errors(),
vec![],
"Code actions shouldn't cause syntax errors.\nParsed:\n{result}"
);
}
}
result
}
// There's an invariant where the edits can't overlap.
// For example, if we have an edit that deletes the full `else clause` and
// another edit that deletes the `else` keyword and they overlap, then
// vscode doesn't surface the code action.
fn check_overlap(edits: &[Edit]) {
for (edit_i, edit_j) in edits.iter().zip(edits.iter().skip(1)) {
if let Some(intersection) = edit_i.text_range.intersect(edit_j.text_range) {
assert!(
intersection.is_empty(),
"Edit ranges must not overlap: {:?} and {:?} intersect at {:?}",
edit_i.text_range,
edit_j.text_range,
intersection
);
}
}
}
fn code_action_not_applicable_(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
allow_errors: bool,
) -> bool {
let fixture = Fixture::new(sql);
let offset = fixture.marker().offset_before();
let sql = fixture.sql();
let db = Database::default();
let file = File::new(&db, sql.into());
let parse_result = crate::db::parse(&db, file);
if !allow_errors {
assert_eq!(parse_result.errors(), vec![]);
}
let mut actions = vec![];
f(&db, file, &mut actions, offset);
actions.is_empty()
}
#[must_use]
pub(super) fn code_action_not_applicable(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
) -> bool {
code_action_not_applicable_(f, sql, false)
}
#[must_use]
pub(super) fn code_action_not_applicable_with_errors(
f: impl Fn(&dyn Db, File, &mut Vec<CodeAction>, TextSize) -> Option<()>,
sql: &str,
) -> bool {
code_action_not_applicable_(f, sql, true)
}