Skip to content

Commit d94bd1f

Browse files
ok
1 parent ad327d2 commit d94bd1f

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

crates/pgt_completions/src/providers/schemas.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn complete_schemas(ctx: &CompletionContext, builder: &mut CompletionBuilder
2525
mod tests {
2626

2727
use crate::{
28-
CompletionItem, CompletionItemKind, complete,
28+
CompletionItemKind, complete,
2929
test_helper::{CURSOR_POS, get_test_deps, get_test_params},
3030
};
3131

@@ -35,6 +35,13 @@ mod tests {
3535
create schema private;
3636
create schema auth;
3737
create schema internal;
38+
39+
-- add a table to compete against schemas
40+
create table users (
41+
id serial primary key,
42+
name text,
43+
password text
44+
);
3845
"#;
3946

4047
let query = format!("select * from {}", CURSOR_POS);
@@ -45,24 +52,18 @@ mod tests {
4552

4653
assert!(!items.is_empty());
4754

48-
for item in items.iter().take(10) {
49-
println!(
50-
r#""{}", score: {}, kind: {:?}"#,
51-
item.label, item.score, item.kind
52-
);
53-
}
54-
5555
assert_eq!(
5656
items
5757
.into_iter()
58-
.take(4)
59-
.map(|i| i.label)
60-
.collect::<Vec<String>>(),
58+
.take(5)
59+
.map(|i| (i.label, i.kind))
60+
.collect::<Vec<(String, CompletionItemKind)>>(),
6161
vec![
62-
"public".to_string(), // public always preferred
63-
"auth".to_string(),
64-
"internal".to_string(),
65-
"private".to_string()
62+
("public".to_string(), CompletionItemKind::Schema),
63+
("auth".to_string(), CompletionItemKind::Schema),
64+
("internal".to_string(), CompletionItemKind::Schema),
65+
("private".to_string(), CompletionItemKind::Schema),
66+
("users".to_string(), CompletionItemKind::Table),
6667
]
6768
);
6869
}

crates/pgt_completions/src/relevance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl CompletionRelevance<'_> {
100100
_ => -15,
101101
},
102102
CompletionRelevanceData::Schema(_) => match clause_type {
103-
ClauseType::From => 15,
103+
ClauseType::From => 10,
104104
_ => -50,
105105
},
106106
}

crates/pgt_completions/src/sanitization.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,28 @@ where
4242
let cursor_pos: usize = params.position.into();
4343
let mut sql = String::new();
4444

45-
for (idx, c) in params.text.chars().enumerate() {
46-
if idx == cursor_pos {
47-
sql.push_str(SANITIZED_TOKEN);
48-
sql.push(' ');
45+
let mut sql_iter = params.text.chars();
46+
47+
for idx in 0..cursor_pos + 1 {
48+
match sql_iter.next() {
49+
Some(c) => {
50+
if idx == cursor_pos {
51+
sql.push_str(SANITIZED_TOKEN);
52+
sql.push(' ');
53+
}
54+
sql.push(c);
55+
}
56+
None => {
57+
// the cursor is outside the statement,
58+
// we want to push spaces until we arrive at the cursor position.
59+
// we'll then add the SANITIZED_TOKEN
60+
if idx == cursor_pos {
61+
sql.push_str(SANITIZED_TOKEN);
62+
} else {
63+
sql.push(' ');
64+
}
65+
}
4966
}
50-
sql.push(c);
5167
}
5268

5369
let mut parser = tree_sitter::Parser::new();
@@ -213,7 +229,7 @@ mod tests {
213229

214230
#[test]
215231
fn test_cursor_after_nodes() {
216-
let input = "select * from ";
232+
let input = "select * from";
217233

218234
let mut parser = tree_sitter::Parser::new();
219235
parser

crates/pgt_completions/src/test_helper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl From<&str> for InputQuery {
1818
.expect("Insert Cursor Position into your Query.");
1919

2020
InputQuery {
21-
sql: value.replace(CURSOR_POS, ""),
21+
sql: value.replace(CURSOR_POS, "").trim().to_string(),
2222
position,
2323
}
2424
}

0 commit comments

Comments
 (0)