1use rowan::{Direction, NodeOrToken, TextRange, TextSize};
31use squawk_syntax::{
32 SyntaxKind, SyntaxNode, SyntaxToken,
33 ast::{self, AstToken},
34};
35
36use crate::tokens::is_string_or_comment;
37
38const DELIMITED_LIST_KINDS: &[SyntaxKind] = &[
39 SyntaxKind::ALTER_OPTION_LIST,
40 SyntaxKind::ARG_LIST,
41 SyntaxKind::ATTRIBUTE_LIST,
42 SyntaxKind::BEGIN_FUNC_OPTION_LIST,
43 SyntaxKind::COLUMN_LIST,
44 SyntaxKind::CONFLICT_INDEX_ITEM_LIST,
45 SyntaxKind::CONSTRAINT_EXCLUSION_LIST,
46 SyntaxKind::COPY_OPTION_LIST,
47 SyntaxKind::DROP_OP_CLASS_OPTION_LIST,
48 SyntaxKind::FDW_OPTION_LIST,
49 SyntaxKind::FUNCTION_SIG_LIST,
50 SyntaxKind::GROUP_BY_LIST,
51 SyntaxKind::JSON_TABLE_COLUMN_LIST,
52 SyntaxKind::OPERATOR_CLASS_OPTION_LIST,
53 SyntaxKind::OPTION_ITEM_LIST,
54 SyntaxKind::OP_SIG_LIST,
55 SyntaxKind::PARAM_LIST,
56 SyntaxKind::PARTITION_ITEM_LIST,
57 SyntaxKind::PARTITION_LIST,
58 SyntaxKind::RETURNING_OPTION_LIST,
59 SyntaxKind::REVOKE_COMMAND_LIST,
60 SyntaxKind::ROLE_REF_LIST,
61 SyntaxKind::ROW_LIST,
62 SyntaxKind::EXPR_AS_NAME_LIST,
63 SyntaxKind::XML_NAMESPACE_LIST,
64 SyntaxKind::SET_COLUMN_LIST,
65 SyntaxKind::SET_EXPR_LIST,
66 SyntaxKind::SET_OPTIONS_LIST,
67 SyntaxKind::SORT_BY_LIST,
68 SyntaxKind::TABLE_AND_COLUMNS_LIST,
69 SyntaxKind::TABLE_ARG_LIST,
70 SyntaxKind::TABLE_LIST,
71 SyntaxKind::TARGET_LIST,
72 SyntaxKind::TRANSACTION_MODE_LIST,
73 SyntaxKind::VACUUM_OPTION_LIST,
74 SyntaxKind::VARIANT_LIST,
75 SyntaxKind::XML_TABLE_COLUMN_LIST,
76 SyntaxKind::PATH_PATTERN_LIST,
77];
78
79pub fn extend_selection(root: &SyntaxNode, range: TextRange) -> TextRange {
80 try_extend_selection(root, range).unwrap_or(range)
81}
82
83fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> {
84 if range.is_empty() {
85 let offset = range.start();
86 let mut leaves = root.token_at_offset(offset);
87 if leaves.clone().all(|it| it.kind() == SyntaxKind::WHITESPACE) {
90 return Some(extend_ws(root, leaves.next()?, offset));
91 }
92 let leaf_range = match root.token_at_offset(offset) {
93 rowan::TokenAtOffset::None => return None,
94 rowan::TokenAtOffset::Single(l) => {
95 if is_string_or_comment(l.kind()) {
96 extend_single_word_in_comment_or_string(&l, offset)
97 .unwrap_or_else(|| l.text_range())
98 } else {
99 l.text_range()
100 }
101 }
102 rowan::TokenAtOffset::Between(l, r) => pick_best(l, r).text_range(),
103 };
104 return Some(leaf_range);
105 }
106
107 let node = match root.covering_element(range) {
108 NodeOrToken::Token(token) => {
109 if token.text_range() != range {
110 return Some(token.text_range());
111 }
112 if let Some(comment) = ast::Comment::cast(token.clone())
113 && let Some(range) = extend_comments(comment)
114 {
115 return Some(range);
116 }
117 token.parent()?
118 }
119 NodeOrToken::Node(node) => node,
120 };
121
122 if node.text_range() != range {
123 return Some(node.text_range());
124 }
125
126 let node = shallowest_node(&node);
127
128 if node
129 .parent()
130 .is_some_and(|n| DELIMITED_LIST_KINDS.contains(&n.kind()))
131 {
132 if let Some(range) = extend_list_item(&node) {
133 return Some(range);
134 }
135 }
136
137 node.parent().map(|it| it.text_range())
138}
139
140fn shallowest_node(node: &SyntaxNode) -> SyntaxNode {
142 node.ancestors()
143 .take_while(|n| n.text_range() == node.text_range())
144 .last()
145 .unwrap()
146}
147
148fn extend_single_word_in_comment_or_string(
150 leaf: &SyntaxToken,
151 offset: TextSize,
152) -> Option<TextRange> {
153 let text: &str = leaf.text();
154 let cursor_position: u32 = (offset - leaf.text_range().start()).into();
155
156 let (before, after) = text.split_at(cursor_position as usize);
157
158 fn non_word_char(c: char) -> bool {
159 !(c.is_alphanumeric() || c == '_')
160 }
161
162 let start_idx = before.rfind(non_word_char)? as u32;
163 let end_idx = after.find(non_word_char).unwrap_or(after.len()) as u32;
164
165 fn ceil_char_boundary(text: &str, index: u32) -> u32 {
168 (index..)
169 .find(|&index| text.is_char_boundary(index as usize))
170 .unwrap_or(text.len() as u32)
171 }
172
173 let from: TextSize = ceil_char_boundary(text, start_idx + 1).into();
174 let to: TextSize = (cursor_position + end_idx).into();
175
176 let range = TextRange::new(from, to);
177 if range.is_empty() {
178 None
179 } else {
180 Some(range + leaf.text_range().start())
181 }
182}
183
184fn extend_comments(comment: ast::Comment) -> Option<TextRange> {
185 let prev = adj_comments(&comment, Direction::Prev);
186 let next = adj_comments(&comment, Direction::Next);
187 if prev != next {
188 Some(TextRange::new(
189 prev.syntax().text_range().start(),
190 next.syntax().text_range().end(),
191 ))
192 } else {
193 None
194 }
195}
196
197fn adj_comments(comment: &ast::Comment, dir: Direction) -> ast::Comment {
198 let mut res = comment.clone();
199 for element in comment.syntax().siblings_with_tokens(dir) {
200 let Some(token) = element.as_token() else {
201 break;
202 };
203 if let Some(c) = ast::Comment::cast(token.clone()) {
204 res = c
205 } else if token.kind() != SyntaxKind::WHITESPACE || token.text().contains("\n\n") {
206 break;
207 }
208 }
209 res
210}
211
212fn extend_ws(root: &SyntaxNode, ws: SyntaxToken, offset: TextSize) -> TextRange {
213 let ws_text = ws.text();
214 let suffix = TextRange::new(offset, ws.text_range().end()) - ws.text_range().start();
215 let prefix = TextRange::new(ws.text_range().start(), offset) - ws.text_range().start();
216 let ws_suffix = &ws_text[suffix];
217 let ws_prefix = &ws_text[prefix];
218 if ws_text.contains('\n')
219 && !ws_suffix.contains('\n')
220 && let Some(node) = ws.next_sibling_or_token()
221 {
222 let start = match ws_prefix.rfind('\n') {
223 Some(idx) => ws.text_range().start() + TextSize::from((idx + 1) as u32),
224 None => node.text_range().start(),
225 };
226 let end = if root.text().char_at(node.text_range().end()) == Some('\n') {
227 node.text_range().end() + TextSize::of('\n')
228 } else {
229 node.text_range().end()
230 };
231 return TextRange::new(start, end);
232 }
233 ws.text_range()
234}
235
236fn pick_best(l: SyntaxToken, r: SyntaxToken) -> SyntaxToken {
237 return if priority(&r) > priority(&l) { r } else { l };
238 fn priority(n: &SyntaxToken) -> usize {
239 match n.kind() {
240 SyntaxKind::WHITESPACE => 0,
241 SyntaxKind::IDENT => 2,
244 _ => 1,
245 }
246 }
247}
248
249fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
251 fn is_single_line_ws(node: &SyntaxToken) -> bool {
252 node.kind() == SyntaxKind::WHITESPACE && !node.text().contains('\n')
253 }
254
255 fn nearby_comma(node: &SyntaxNode, dir: Direction) -> Option<SyntaxToken> {
256 node.siblings_with_tokens(dir)
257 .skip(1)
258 .find(|node| match node {
259 NodeOrToken::Node(_) => true,
260 NodeOrToken::Token(it) => !is_single_line_ws(it),
261 })
262 .and_then(|it| it.into_token())
263 .filter(|node| node.kind() == SyntaxKind::COMMA)
264 }
265
266 if let Some(comma) = nearby_comma(node, Direction::Next) {
267 let final_node = comma
269 .next_sibling_or_token()
270 .and_then(|n| n.into_token())
271 .filter(is_single_line_ws)
272 .unwrap_or(comma);
273
274 return Some(TextRange::new(
275 node.text_range().start(),
276 final_node.text_range().end(),
277 ));
278 }
279
280 if let Some(comma) = nearby_comma(node, Direction::Prev) {
281 return Some(TextRange::new(
282 comma.text_range().start(),
283 node.text_range().end(),
284 ));
285 }
286
287 None
288}
289
290#[cfg(test)]
291mod tests {
292 use super::*;
293 use crate::test_utils::fixture;
294 use insta::assert_debug_snapshot;
295 use squawk_syntax::{SourceFile, ast::AstNode};
296
297 fn expand(sql: &str) -> Vec<String> {
298 let (offset, sql) = fixture(sql);
299 let parse = SourceFile::parse(&sql);
300 let file = parse.tree();
301 let root = file.syntax();
302
303 let mut range = TextRange::empty(offset);
304 let mut results = vec![];
305
306 for _ in 0..20 {
307 let new_range = extend_selection(root, range);
308 if new_range == range {
309 break;
310 }
311 range = new_range;
312 results.push(sql[range].to_string());
313 }
314
315 results
316 }
317
318 #[test]
319 fn simple() {
320 assert_debug_snapshot!(expand(r#"select $01 + 1"#), @r#"
321 [
322 "1",
323 "1 + 1",
324 "select 1 + 1",
325 ]
326 "#);
327 }
328
329 #[test]
330 fn word_in_string_string() {
331 assert_debug_snapshot!(expand(r"
332select 'some stret$0ched out words in a string'
333"), @r#"
334 [
335 "stretched",
336 "'some stretched out words in a string'",
337 "select 'some stretched out words in a string'",
338 "\nselect 'some stretched out words in a string'\n",
339 ]
340 "#);
341 }
342
343 #[test]
344 fn string() {
345 assert_debug_snapshot!(expand(r"
346select b'foo$0 bar'
347'buzz';
348"), @r#"
349 [
350 "foo",
351 "b'foo bar'",
352 "b'foo bar'\n'buzz'",
353 "select b'foo bar'\n'buzz'",
354 "\nselect b'foo bar'\n'buzz';\n",
355 ]
356 "#);
357 }
358
359 #[test]
360 fn dollar_string() {
361 assert_debug_snapshot!(expand(r"
362select $$foo$0 bar$$;
363"), @r#"
364 [
365 "foo",
366 "$$foo bar$$",
367 "select $$foo bar$$",
368 "\nselect $$foo bar$$;\n",
369 ]
370 "#);
371 }
372
373 #[test]
374 fn comment_muli_line() {
375 assert_debug_snapshot!(expand(r"
376-- foo bar
377-- buzz$0
378-- boo
379select 1
380"), @r#"
381 [
382 "-- buzz",
383 "-- foo bar\n-- buzz\n-- boo",
384 "\n-- foo bar\n-- buzz\n-- boo\nselect 1\n",
385 ]
386 "#);
387 }
388
389 #[test]
390 fn comment() {
391 assert_debug_snapshot!(expand(r"
392-- foo bar$0
393select 1
394"), @r#"
395 [
396 "-- foo bar",
397 "\n-- foo bar\nselect 1\n",
398 ]
399 "#);
400
401 assert_debug_snapshot!(expand(r"
402/* foo bar$0 */
403select 1
404"), @r#"
405 [
406 "bar",
407 "/* foo bar */",
408 "\n/* foo bar */\nselect 1\n",
409 ]
410 "#);
411 }
412
413 #[test]
414 fn create_table_with_comment() {
415 assert_debug_snapshot!(expand(r"
416-- foo bar buzz
417create table t(
418 x int$0,
419 y text
420);
421"), @r#"
422 [
423 "int",
424 "x int",
425 "x int,",
426 "(\n x int,\n y text\n)",
427 "-- foo bar buzz\ncreate table t(\n x int,\n y text\n)",
428 "\n-- foo bar buzz\ncreate table t(\n x int,\n y text\n);\n",
429 ]
430 "#);
431 }
432
433 #[test]
434 fn column_list() {
435 assert_debug_snapshot!(expand(r#"create table t($0x int)"#), @r#"
436 [
437 "x",
438 "x int",
439 "(x int)",
440 "create table t(x int)",
441 ]
442 "#);
443
444 assert_debug_snapshot!(expand(r#"create table t($0x int, y int)"#), @r#"
445 [
446 "x",
447 "x int",
448 "x int, ",
449 "(x int, y int)",
450 "create table t(x int, y int)",
451 ]
452 "#);
453
454 assert_debug_snapshot!(expand(r#"create table t(x int, $0y int)"#), @r#"
455 [
456 "y",
457 "y int",
458 ", y int",
459 "(x int, y int)",
460 "create table t(x int, y int)",
461 ]
462 "#);
463 }
464
465 #[test]
466 fn start_of_line_whitespace_select() {
467 assert_debug_snapshot!(expand(r#"
468select 1;
469
470$0 select 2;"#), @r#"
471 [
472 " select 2",
473 " \nselect 1;\n\n select 2;",
474 ]
475 "#);
476 }
477
478 #[test]
479 fn select_list() {
480 assert_debug_snapshot!(expand(r#"select x$0, y from t"#), @r#"
481 [
482 "x",
483 "x, ",
484 "x, y",
485 "select x, y",
486 "select x, y from t",
487 ]
488 "#);
489
490 assert_debug_snapshot!(expand(r#"select x, y$0 from t"#), @r#"
491 [
492 "y",
493 ", y",
494 "x, y",
495 "select x, y",
496 "select x, y from t",
497 ]
498 "#);
499 }
500
501 #[test]
502 fn expand_whitespace() {
503 assert_debug_snapshot!(expand(r#"select 1 +
504$0
5051;"#), @r#"
506 [
507 " \n\n",
508 "1 + \n\n1",
509 "select 1 + \n\n1",
510 "select 1 + \n\n1;",
511 ]
512 "#);
513 }
514
515 #[test]
516 fn function_args() {
517 assert_debug_snapshot!(expand(r#"select f(1$0, 2)"#), @r#"
518 [
519 "1",
520 "1, ",
521 "(1, 2)",
522 "f(1, 2)",
523 "select f(1, 2)",
524 ]
525 "#);
526 }
527
528 #[test]
529 fn prefer_idents() {
530 assert_debug_snapshot!(expand(r#"select foo$0+bar"#), @r#"
531 [
532 "foo",
533 "foo+bar",
534 "select foo+bar",
535 ]
536 "#);
537
538 assert_debug_snapshot!(expand(r#"select foo+$0bar"#), @r#"
539 [
540 "bar",
541 "foo+bar",
542 "select foo+bar",
543 ]
544 "#);
545 }
546
547 #[test]
548 fn list_variants() {
549 let delimited_ws_list_kinds = &[
550 SyntaxKind::CREATE_DATABASE_OPTION_LIST,
551 SyntaxKind::FUNC_OPTION_LIST,
552 SyntaxKind::ROLE_OPTION_LIST,
553 SyntaxKind::SEQUENCE_OPTION_LIST,
554 SyntaxKind::TRIGGER_EVENT_LIST,
555 SyntaxKind::XML_COLUMN_OPTION_LIST,
556 SyntaxKind::WHEN_CLAUSE_LIST,
557 SyntaxKind::LABEL_AND_PROPERTIES_LIST,
558 ];
559
560 let unhandled_list_kinds = (0..SyntaxKind::__LAST as u16)
561 .map(SyntaxKind::from)
562 .filter(|kind| {
563 format!("{:?}", kind).ends_with("_LIST") && !delimited_ws_list_kinds.contains(kind)
564 })
565 .filter(|kind| !DELIMITED_LIST_KINDS.contains(kind))
566 .collect::<Vec<_>>();
567
568 assert_eq!(
569 unhandled_list_kinds,
570 vec![],
571 "We shouldn't have any unhandled list kinds"
572 )
573 }
574}