Skip to content

Commit 5c8e8e8

Browse files
so far…
1 parent b978a5a commit 5c8e8e8

File tree

8 files changed

+97
-18
lines changed

8 files changed

+97
-18
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pgt_completions/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pgt_treesitter_queries.workspace = true
2222
schemars = { workspace = true, optional = true }
2323
serde = { workspace = true, features = ["derive"] }
2424
serde_json = { workspace = true }
25+
tracing = { workspace = true }
2526
tree-sitter.workspace = true
2627
tree_sitter_sql.workspace = true
2728

crates/pgt_completions/src/complete.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct CompletionParams<'a> {
1717
pub tree: Option<&'a tree_sitter::Tree>,
1818
}
1919

20+
#[tracing::instrument(level = "debug")]
2021
pub fn complete(params: CompletionParams) -> Vec<CompletionItem> {
2122
let ctx = CompletionContext::new(&params);
2223

crates/pgt_completions/src/context.rs

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ impl<'a> CompletionContext<'a> {
8181
ctx.gather_tree_context();
8282
ctx.gather_info_from_ts_queries();
8383

84+
println!("Here's my node: {:?}", ctx.ts_node.unwrap());
85+
8486
ctx
8587
}
8688

crates/pgt_completions/src/providers/columns.rs

+78-4
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,86 @@ mod tests {
143143
let params = get_test_params(&tree, &cache, case.get_input_query());
144144
let mut items = complete(params);
145145

146-
let _ = items.split_off(3);
146+
let _ = items.split_off(6);
147147

148-
items.sort_by(|a, b| a.label.cmp(&b.label));
148+
#[derive(Eq, PartialEq, Debug)]
149+
struct LabelAndDesc {
150+
label: String,
151+
desc: String,
152+
}
153+
154+
let labels: Vec<LabelAndDesc> = items
155+
.into_iter()
156+
.map(|c| LabelAndDesc {
157+
label: c.label,
158+
desc: c.description,
159+
})
160+
.collect();
161+
162+
let expected = vec![
163+
("name", "Table: public.users"),
164+
("narrator", "Table: public.audio_books"),
165+
("narrator_id", "Table: private.audio_books"),
166+
("name", "Schema: pg_catalog"),
167+
("nameconcatoid", "Schema: pg_catalog"),
168+
("nameeq", "Schema: pg_catalog"),
169+
]
170+
.into_iter()
171+
.map(|(label, schema)| LabelAndDesc {
172+
label: label.into(),
173+
desc: schema.into(),
174+
})
175+
.collect::<Vec<LabelAndDesc>>();
176+
177+
assert_eq!(labels, expected);
178+
}
179+
180+
#[tokio::test]
181+
async fn suggests_relevant_columns_without_letters() {
182+
let setup = r#"
183+
create table users (
184+
id serial primary key,
185+
name text,
186+
address text,
187+
email text
188+
);
189+
"#;
190+
191+
let test_case = TestCase {
192+
message: "suggests user created tables first",
193+
query: format!(r#"select {} from users"#, CURSOR_POS),
194+
label: "",
195+
description: "",
196+
};
197+
198+
let (tree, cache) = get_test_deps(setup, test_case.get_input_query()).await;
199+
let params = get_test_params(&tree, &cache, test_case.get_input_query());
200+
let results = complete(params);
149201

150-
let labels: Vec<String> = items.into_iter().map(|c| c.label).collect();
202+
let (first_four, _rest) = results.split_at(4);
203+
204+
let has_column_in_first_four = |col: &'static str| {
205+
first_four
206+
.iter()
207+
.find(|compl_item| compl_item.label.as_str() == col)
208+
.is_some()
209+
};
151210

152-
assert_eq!(labels, vec!["name", "narrator", "narrator_id"]);
211+
assert!(
212+
has_column_in_first_four("id"),
213+
"`id` not present in first four completion items."
214+
);
215+
assert!(
216+
has_column_in_first_four("name"),
217+
"`name` not present in first four completion items."
218+
);
219+
assert!(
220+
has_column_in_first_four("address"),
221+
"`address` not present in first four completion items."
222+
);
223+
assert!(
224+
has_column_in_first_four("email"),
225+
"`email` not present in first four completion items."
226+
);
153227
}
154228
}

crates/pgt_completions/src/providers/tables.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ 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"),
76+
// (format!("select * from us{}", CURSOR_POS), "users"),
77+
// (format!("select * from em{}", CURSOR_POS), "emails"),
7878
(format!("select * from {}", CURSOR_POS), "addresses"),
7979
];
8080

crates/pgt_completions/src/relevance.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ impl CompletionRelevance<'_> {
3333
self.check_is_user_defined();
3434
self.check_matches_schema(ctx);
3535
self.check_matches_query_input(ctx);
36-
self.check_if_catalog(ctx);
3736
self.check_is_invocation(ctx);
3837
self.check_matching_clause_type(ctx);
3938
self.check_relations_in_stmt(ctx);
@@ -52,7 +51,10 @@ impl CompletionRelevance<'_> {
5251
let name = match self.data {
5352
CompletionRelevanceData::Function(f) => f.name.as_str(),
5453
CompletionRelevanceData::Table(t) => t.name.as_str(),
55-
CompletionRelevanceData::Column(c) => c.name.as_str(),
54+
CompletionRelevanceData::Column(c) => {
55+
//
56+
c.name.as_str()
57+
}
5658
};
5759

5860
if name.starts_with(content) {
@@ -61,7 +63,7 @@ impl CompletionRelevance<'_> {
6163
.try_into()
6264
.expect("The length of the input exceeds i32 capacity");
6365

64-
self.score += len * 5;
66+
self.score += len * 10;
6567
};
6668
}
6769

@@ -135,14 +137,6 @@ impl CompletionRelevance<'_> {
135137
}
136138
}
137139

138-
fn check_if_catalog(&mut self, ctx: &CompletionContext) {
139-
if ctx.schema_name.as_ref().is_some_and(|n| n == "pg_catalog") {
140-
return;
141-
}
142-
143-
self.score -= 5; // unlikely that the user wants schema data
144-
}
145-
146140
fn check_relations_in_stmt(&mut self, ctx: &CompletionContext) {
147141
match self.data {
148142
CompletionRelevanceData::Table(_) | CompletionRelevanceData::Function(_) => return,
@@ -182,5 +176,11 @@ impl CompletionRelevance<'_> {
182176
if system_schemas.contains(&schema.as_str()) {
183177
self.score -= 10;
184178
}
179+
180+
// "public" is the default postgres schema where users
181+
// create objects. Prefer it by a slight bit.
182+
if schema.as_str() == "public" {
183+
self.score += 2;
184+
}
185185
}
186186
}

postgrestools.jsonc

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// YOU CAN COMMENT ME OUT :)
1818
"db": {
1919
"host": "127.0.0.1",
20-
"port": 5432,
20+
"port": 54322,
2121
"username": "postgres",
2222
"password": "postgres",
2323
"database": "postgres",

0 commit comments

Comments
 (0)