Skip to content

Commit 13647c2

Browse files
authored
ide: goto def on begin/commit/rollback (#734)
1 parent 4d7c33f commit 13647c2

File tree

1 file changed

+162
-4
lines changed

1 file changed

+162
-4
lines changed

β€Žcrates/squawk_ide/src/goto_definition.rsβ€Ž

Lines changed: 162 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,66 @@ pub fn goto_definition(file: ast::SourceFile, offset: TextSize) -> Option<TextRa
1515
|| (token.kind() == SyntaxKind::END_KW && parent.kind() == SyntaxKind::CASE_EXPR)
1616
{
1717
for parent in token.parent_ancestors() {
18-
if let Some(case_expr) = ast::CaseExpr::cast(parent) {
19-
if let Some(case_token) = case_expr.case_token() {
20-
return Some(case_token.text_range());
21-
}
18+
if let Some(case_expr) = ast::CaseExpr::cast(parent)
19+
&& let Some(case_token) = case_expr.case_token()
20+
{
21+
return Some(case_token.text_range());
2222
}
2323
}
2424
}
2525

26+
// goto def on COMMIT -> BEGIN/START TRANSACTION
27+
if ast::Commit::can_cast(parent.kind()) {
28+
if let Some(begin_range) = find_preceding_begin(&file, token.text_range().start()) {
29+
return Some(begin_range);
30+
}
31+
}
32+
33+
// goto def on ROLLBACK -> BEGIN/START TRANSACTION
34+
if ast::Rollback::can_cast(parent.kind()) {
35+
if let Some(begin_range) = find_preceding_begin(&file, token.text_range().start()) {
36+
return Some(begin_range);
37+
}
38+
}
39+
40+
// goto def on BEGIN/START TRANSACTION -> COMMIT or ROLLBACK
41+
if ast::Begin::can_cast(parent.kind()) {
42+
if let Some(end_range) = find_following_commit_or_rollback(&file, token.text_range().end())
43+
{
44+
return Some(end_range);
45+
}
46+
}
47+
2648
return None;
2749
}
2850

51+
fn find_preceding_begin(file: &ast::SourceFile, before: TextSize) -> Option<TextRange> {
52+
let mut last_begin: Option<TextRange> = None;
53+
for stmt in file.stmts() {
54+
if let ast::Stmt::Begin(begin) = stmt {
55+
let range = begin.syntax().text_range();
56+
if range.end() <= before {
57+
last_begin = Some(range);
58+
}
59+
}
60+
}
61+
last_begin
62+
}
63+
64+
fn find_following_commit_or_rollback(file: &ast::SourceFile, after: TextSize) -> Option<TextRange> {
65+
for stmt in file.stmts() {
66+
let range = match &stmt {
67+
ast::Stmt::Commit(commit) => commit.syntax().text_range(),
68+
ast::Stmt::Rollback(rollback) => rollback.syntax().text_range(),
69+
_ => continue,
70+
};
71+
if range.start() >= after {
72+
return Some(range);
73+
}
74+
}
75+
None
76+
}
77+
2978
#[cfg(test)]
3079
mod test {
3180
use crate::goto_definition::goto_definition;
@@ -136,4 +185,113 @@ select case when x > 1 then$0 1 else 2 end;
136185
",
137186
)
138187
}
188+
189+
#[test]
190+
fn rollback_to_begin() {
191+
assert_snapshot!(goto(
192+
"
193+
begin;
194+
select 1;
195+
rollback$0;
196+
",
197+
), @r"
198+
β•­β–Έ
199+
2 β”‚ begin;
200+
β”‚ ───── 2. destination
201+
3 β”‚ select 1;
202+
4 β”‚ rollback;
203+
β•°β•΄ ─ 1. source
204+
");
205+
}
206+
207+
#[test]
208+
fn begin_to_rollback() {
209+
assert_snapshot!(goto(
210+
"
211+
begin$0;
212+
select 1;
213+
rollback;
214+
commit;
215+
",
216+
), @r"
217+
β•­β–Έ
218+
2 β”‚ begin;
219+
β”‚ ─ 1. source
220+
3 β”‚ select 1;
221+
4 β”‚ rollback;
222+
╰╴──────── 2. destination
223+
");
224+
}
225+
226+
#[test]
227+
fn commit_to_begin() {
228+
assert_snapshot!(goto(
229+
"
230+
begin;
231+
select 1;
232+
commit$0;
233+
",
234+
), @r"
235+
β•­β–Έ
236+
2 β”‚ begin;
237+
β”‚ ───── 2. destination
238+
3 β”‚ select 1;
239+
4 β”‚ commit;
240+
β•°β•΄ ─ 1. source
241+
");
242+
}
243+
244+
#[test]
245+
fn begin_to_commit() {
246+
assert_snapshot!(goto(
247+
"
248+
begin$0;
249+
select 1;
250+
commit;
251+
",
252+
), @r"
253+
β•­β–Έ
254+
2 β”‚ begin;
255+
β”‚ ─ 1. source
256+
3 β”‚ select 1;
257+
4 β”‚ commit;
258+
╰╴────── 2. destination
259+
");
260+
}
261+
262+
#[test]
263+
fn commit_to_start_transaction() {
264+
assert_snapshot!(goto(
265+
"
266+
start transaction;
267+
select 1;
268+
commit$0;
269+
",
270+
), @r"
271+
β•­β–Έ
272+
2 β”‚ start transaction;
273+
β”‚ ───────────────── 2. destination
274+
3 β”‚ select 1;
275+
4 β”‚ commit;
276+
β•°β•΄ ─ 1. source
277+
");
278+
}
279+
280+
#[test]
281+
fn start_transaction_to_commit() {
282+
assert_snapshot!(goto(
283+
"
284+
start$0 transaction;
285+
select 1;
286+
commit;
287+
",
288+
), @r"
289+
β•­β–Έ
290+
2 β”‚ start transaction;
291+
β”‚ ─ 1. source
292+
3 β”‚ select 1;
293+
4 β”‚ commit;
294+
╰╴────── 2. destination
295+
");
296+
}
139297
}

0 commit comments

Comments
Β (0)