Skip to content

fix(completions): convert SQL to lowercase #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions crates/pgt_completions/src/sanitization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ impl<'larger, 'smaller> From<CompletionParams<'larger>> for SanitizedCompletionP
where
'larger: 'smaller,
{
fn from(params: CompletionParams<'larger>) -> Self {
fn from(mut params: CompletionParams<'larger>) -> Self {
params.text = params.text.to_ascii_lowercase();
if cursor_inbetween_nodes(&params.text, params.position)
|| cursor_prepared_to_write_token_after_last_node(&params.text, params.position)
|| cursor_before_semicolon(params.tree, params.position)
Expand Down Expand Up @@ -263,13 +264,43 @@ fn cursor_between_parentheses(sql: &str, position: TextSize) -> bool {

#[cfg(test)]
mod tests {
use pgt_schema_cache::SchemaCache;
use pgt_text_size::TextSize;

use crate::sanitization::{
cursor_before_semicolon, cursor_between_parentheses, cursor_inbetween_nodes,
cursor_on_a_dot, cursor_prepared_to_write_token_after_last_node,
use crate::{
CompletionParams, SanitizedCompletionParams,
sanitization::{
cursor_before_semicolon, cursor_between_parentheses, cursor_inbetween_nodes,
cursor_on_a_dot, cursor_prepared_to_write_token_after_last_node,
},
};

#[test]
fn should_lowercase_everything_except_replaced_token() {
let input = "SELECT FROM users WHERE ts = NOW();";

let position = TextSize::new(7);
let cache = SchemaCache::default();

let mut ts = tree_sitter::Parser::new();
ts.set_language(tree_sitter_sql::language()).unwrap();
let tree = ts.parse(input, None).unwrap();

let params = CompletionParams {
position,
schema: &cache,
text: input.into(),
tree: &tree,
};

let sanitized = SanitizedCompletionParams::from(params);

assert_eq!(
sanitized.text,
"select REPLACED_TOKEN from users where ts = now();"
);
}

#[test]
fn test_cursor_inbetween_nodes() {
// note: two spaces between select and from.
Expand Down