Skip to content

Commit cf61598

Browse files
hell yeah
1 parent 5c0a7a6 commit cf61598

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

crates/pgt_completions/src/providers/tables.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ mod tests {
7373
"#;
7474

7575
let test_cases = vec![
76-
// (format!("select * from us{}", CURSOR_POS), "users"),
77-
// (format!("select * from em{}", CURSOR_POS), "emails"),
78-
(format!("select * from {}", CURSOR_POS), "addresses"),
76+
(format!("select * from u{}", CURSOR_POS), "users"),
77+
(format!("select * from e{}", CURSOR_POS), "emails"),
78+
(format!("select * from a{}", CURSOR_POS), "addresses"),
7979
];
8080

8181
for (query, expected_label) in test_cases {
8282
let (tree, cache) = get_test_deps(setup, query.as_str().into()).await;
8383
let params = get_test_params(&tree, &cache, query.as_str().into());
84-
println!("{}, {}", &params.text, &params.position);
8584
let items = complete(params);
8685

8786
assert!(!items.is_empty());

crates/pgt_completions/src/sanitization.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ where
2525

2626
if cursor_inbetween_nodes(tree, params.position)
2727
|| cursor_prepared_to_write_token_after_last_node(tree, params.position)
28+
|| cursor_before_semicolon(tree, params.position)
2829
{
2930
Ok(SanitizedCompletionParams::with_adjusted_sql(params))
3031
} else {
@@ -134,7 +135,7 @@ fn cursor_prepared_to_write_token_after_last_node(
134135
cursor_pos == tree.root_node().end_byte() + 1
135136
}
136137

137-
fn cursor_before_or_on_semicolon(tree: &tree_sitter::Tree, position: TextSize) -> bool {
138+
fn cursor_before_semicolon(tree: &tree_sitter::Tree, position: TextSize) -> bool {
138139
let mut cursor = tree.walk();
139140
let mut leaf_node = tree.root_node();
140141

@@ -183,7 +184,7 @@ mod tests {
183184
use pgt_text_size::TextSize;
184185

185186
use crate::sanitization::{
186-
cursor_before_or_on_semicolon, cursor_inbetween_nodes,
187+
cursor_before_semicolon, cursor_inbetween_nodes,
187188
cursor_prepared_to_write_token_after_last_node,
188189
};
189190

@@ -262,23 +263,23 @@ mod tests {
262263
let mut tree = parser.parse(input.to_string(), None).unwrap();
263264

264265
// select * from ;| <-- it's after the statement
265-
assert!(!cursor_before_or_on_semicolon(&mut tree, TextSize::new(19)));
266+
assert!(!cursor_before_semicolon(&mut tree, TextSize::new(19)));
266267

267268
// select * from| ; <-- still touches the from
268-
assert!(!cursor_before_or_on_semicolon(&mut tree, TextSize::new(13)));
269+
assert!(!cursor_before_semicolon(&mut tree, TextSize::new(13)));
269270

270271
// not okay to be ON the semi.
271272
// select * from |;
272-
assert!(!cursor_before_or_on_semicolon(&mut tree, TextSize::new(18)));
273+
assert!(!cursor_before_semicolon(&mut tree, TextSize::new(18)));
273274

274275
// anything is fine here
275276
// select * from | ;
276277
// select * from | ;
277278
// select * from | ;
278279
// select * from |;
279-
assert!(cursor_before_or_on_semicolon(&mut tree, TextSize::new(14)));
280-
assert!(cursor_before_or_on_semicolon(&mut tree, TextSize::new(15)));
281-
assert!(cursor_before_or_on_semicolon(&mut tree, TextSize::new(16)));
282-
assert!(cursor_before_or_on_semicolon(&mut tree, TextSize::new(17)));
280+
assert!(cursor_before_semicolon(&mut tree, TextSize::new(14)));
281+
assert!(cursor_before_semicolon(&mut tree, TextSize::new(15)));
282+
assert!(cursor_before_semicolon(&mut tree, TextSize::new(16)));
283+
assert!(cursor_before_semicolon(&mut tree, TextSize::new(17)));
283284
}
284285
}

crates/pgt_completions/src/test_helper.rs

+40-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ impl From<&str> for InputQuery {
1515
fn from(value: &str) -> Self {
1616
let position = value
1717
.find(CURSOR_POS)
18-
.map(|p| p.saturating_add(1))
1918
.expect("Insert Cursor Position into your Query.");
2019

21-
println!("{}", position);
22-
2320
InputQuery {
2421
sql: value.replace(CURSOR_POS, ""),
2522
position,
@@ -76,3 +73,43 @@ pub(crate) fn get_test_params<'a>(
7673
text,
7774
}
7875
}
76+
77+
#[cfg(test)]
78+
mod tests {
79+
use crate::test_helper::CURSOR_POS;
80+
81+
use super::InputQuery;
82+
83+
#[test]
84+
fn input_query_should_extract_correct_position() {
85+
struct TestCase {
86+
query: String,
87+
expected_pos: usize,
88+
expected_sql_len: usize,
89+
}
90+
91+
let cases = vec![
92+
TestCase {
93+
query: format!("select * from{}", CURSOR_POS),
94+
expected_pos: 13,
95+
expected_sql_len: 13,
96+
},
97+
TestCase {
98+
query: format!("{}select * from", CURSOR_POS),
99+
expected_pos: 0,
100+
expected_sql_len: 13,
101+
},
102+
TestCase {
103+
query: format!("select {} from", CURSOR_POS),
104+
expected_pos: 7,
105+
expected_sql_len: 12,
106+
},
107+
];
108+
109+
for case in cases {
110+
let query = InputQuery::from(case.query.as_str());
111+
assert_eq!(query.position, case.expected_pos);
112+
assert_eq!(query.sql.len(), case.expected_sql_len);
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)