Skip to content

Commit d2d51aa

Browse files
ok (#246)
1 parent c058708 commit d2d51aa

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

crates/pgt_completions/src/providers/columns.rs

+41
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,45 @@ mod tests {
110110
assert_eq!(description, q.description, "{}", q.message);
111111
}
112112
}
113+
114+
#[tokio::test]
115+
async fn shows_multiple_columns_if_no_relation_specified() {
116+
let setup = r#"
117+
create schema private;
118+
119+
create table public.users (
120+
id serial primary key,
121+
name text
122+
);
123+
124+
create table public.audio_books (
125+
id serial primary key,
126+
narrator text
127+
);
128+
129+
create table private.audio_books (
130+
id serial primary key,
131+
narrator_id text
132+
);
133+
"#;
134+
135+
let case = TestCase {
136+
query: format!(r#"select n{};"#, CURSOR_POS),
137+
description: "",
138+
label: "",
139+
message: "",
140+
};
141+
142+
let (tree, cache) = get_test_deps(setup, case.get_input_query()).await;
143+
let params = get_test_params(&tree, &cache, case.get_input_query());
144+
let mut results = complete(params);
145+
146+
let _ = results.items.split_off(3);
147+
148+
results.items.sort_by(|a, b| a.label.cmp(&b.label));
149+
150+
let labels: Vec<String> = results.items.into_iter().map(|c| c.label).collect();
151+
152+
assert_eq!(labels, vec!["name", "narrator", "narrator_id"]);
153+
}
113154
}

crates/pgt_completions/src/relevance.rs

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) struct CompletionRelevance<'a> {
3030

3131
impl CompletionRelevance<'_> {
3232
pub fn into_score(mut self, ctx: &CompletionContext) -> i32 {
33+
self.check_is_user_defined();
3334
self.check_matches_schema(ctx);
3435
self.check_matches_query_input(ctx);
3536
self.check_if_catalog(ctx);
@@ -168,4 +169,18 @@ impl CompletionRelevance<'_> {
168169
self.score += 30;
169170
}
170171
}
172+
173+
fn check_is_user_defined(&mut self) {
174+
let schema = match self.data {
175+
CompletionRelevanceData::Column(c) => &c.schema_name,
176+
CompletionRelevanceData::Function(f) => &f.schema,
177+
CompletionRelevanceData::Table(t) => &t.schema,
178+
};
179+
180+
let system_schemas = ["pg_catalog", "information_schema", "pg_toast"];
181+
182+
if system_schemas.contains(&schema.as_str()) {
183+
self.score -= 10;
184+
}
185+
}
171186
}

0 commit comments

Comments
 (0)