Skip to content

feat(completions): prefer user-created things #246

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 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions crates/pgt_completions/src/providers/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,45 @@ mod tests {
assert_eq!(description, q.description, "{}", q.message);
}
}

#[tokio::test]
async fn shows_multiple_columns_if_no_relation_specified() {
let setup = r#"
create schema private;

create table public.users (
id serial primary key,
name text
);

create table public.audio_books (
id serial primary key,
narrator text
);

create table private.audio_books (
id serial primary key,
narrator_id text
);
"#;

let case = TestCase {
query: format!(r#"select n{};"#, CURSOR_POS),
description: "",
label: "",
message: "",
};

let (tree, cache) = get_test_deps(setup, case.get_input_query()).await;
let params = get_test_params(&tree, &cache, case.get_input_query());
let mut results = complete(params);

let _ = results.items.split_off(3);

results.items.sort_by(|a, b| a.label.cmp(&b.label));

let labels: Vec<String> = results.items.into_iter().map(|c| c.label).collect();

assert_eq!(labels, vec!["name", "narrator", "narrator_id"]);
}
}
15 changes: 15 additions & 0 deletions crates/pgt_completions/src/relevance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) struct CompletionRelevance<'a> {

impl CompletionRelevance<'_> {
pub fn into_score(mut self, ctx: &CompletionContext) -> i32 {
self.check_is_user_defined();
self.check_matches_schema(ctx);
self.check_matches_query_input(ctx);
self.check_if_catalog(ctx);
Expand Down Expand Up @@ -168,4 +169,18 @@ impl CompletionRelevance<'_> {
self.score += 30;
}
}

fn check_is_user_defined(&mut self) {
let schema = match self.data {
CompletionRelevanceData::Column(c) => &c.schema_name,
CompletionRelevanceData::Function(f) => &f.schema,
CompletionRelevanceData::Table(t) => &t.schema,
};

let system_schemas = ["pg_catalog", "information_schema", "pg_toast"];

if system_schemas.contains(&schema.as_str()) {
self.score -= 10;
}
}
}