Skip to content

Commit d7ad288

Browse files
fix(completions): convert SQL to lowercase (#416)
1 parent 5c0e255 commit d7ad288

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

crates/pgt_completions/src/sanitization.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ impl<'larger, 'smaller> From<CompletionParams<'larger>> for SanitizedCompletionP
4848
where
4949
'larger: 'smaller,
5050
{
51-
fn from(params: CompletionParams<'larger>) -> Self {
51+
fn from(mut params: CompletionParams<'larger>) -> Self {
52+
params.text = params.text.to_ascii_lowercase();
5253
if cursor_inbetween_nodes(&params.text, params.position)
5354
|| cursor_prepared_to_write_token_after_last_node(&params.text, params.position)
5455
|| cursor_before_semicolon(params.tree, params.position)
@@ -263,13 +264,43 @@ fn cursor_between_parentheses(sql: &str, position: TextSize) -> bool {
263264

264265
#[cfg(test)]
265266
mod tests {
267+
use pgt_schema_cache::SchemaCache;
266268
use pgt_text_size::TextSize;
267269

268-
use crate::sanitization::{
269-
cursor_before_semicolon, cursor_between_parentheses, cursor_inbetween_nodes,
270-
cursor_on_a_dot, cursor_prepared_to_write_token_after_last_node,
270+
use crate::{
271+
CompletionParams, SanitizedCompletionParams,
272+
sanitization::{
273+
cursor_before_semicolon, cursor_between_parentheses, cursor_inbetween_nodes,
274+
cursor_on_a_dot, cursor_prepared_to_write_token_after_last_node,
275+
},
271276
};
272277

278+
#[test]
279+
fn should_lowercase_everything_except_replaced_token() {
280+
let input = "SELECT FROM users WHERE ts = NOW();";
281+
282+
let position = TextSize::new(7);
283+
let cache = SchemaCache::default();
284+
285+
let mut ts = tree_sitter::Parser::new();
286+
ts.set_language(tree_sitter_sql::language()).unwrap();
287+
let tree = ts.parse(input, None).unwrap();
288+
289+
let params = CompletionParams {
290+
position,
291+
schema: &cache,
292+
text: input.into(),
293+
tree: &tree,
294+
};
295+
296+
let sanitized = SanitizedCompletionParams::from(params);
297+
298+
assert_eq!(
299+
sanitized.text,
300+
"select REPLACED_TOKEN from users where ts = now();"
301+
);
302+
}
303+
273304
#[test]
274305
fn test_cursor_inbetween_nodes() {
275306
// note: two spaces between select and from.

0 commit comments

Comments
 (0)