Skip to content

fix(completions): sort items by relevance on client #362

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 30 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e54fc93
ok
juleswritescode Apr 20, 2025
ecd0034
ignore all irrelevant schemas
juleswritescode Apr 20, 2025
be10534
fix bug
juleswritescode Apr 21, 2025
1f7ef84
ok
juleswritescode Apr 21, 2025
7731b68
fix sorting
juleswritescode Apr 21, 2025
79b0fb9
ok
juleswritescode Apr 21, 2025
5f97ae0
ok
juleswritescode Apr 21, 2025
0701724
fix(completions): cursor on dot, other issues
juleswritescode Apr 21, 2025
0847f45
Merge branch 'fix/workflow-file' of https://github.com/supabase-commu…
juleswritescode Apr 21, 2025
d2ce18a
relevance
juleswritescode Apr 21, 2025
fa61904
Merge branch 'main' of https://github.com/supabase-community/postgres…
juleswritescode Apr 21, 2025
d315a59
ok
juleswritescode Apr 21, 2025
29056ad
ok
juleswritescode Apr 21, 2025
6e73a3b
add folder
juleswritescode Apr 21, 2025
a53cc46
fixes
juleswritescode Apr 21, 2025
5065b8e
oops
juleswritescode Apr 21, 2025
3e9462f
ok
juleswritescode Apr 21, 2025
dc54e9e
Merge branch 'fix/workflow-file' of https://github.com/supabase-commu…
juleswritescode Apr 21, 2025
db49040
revert things
juleswritescode Apr 21, 2025
b7913b1
oK
juleswritescode Apr 21, 2025
e3e54e4
?
juleswritescode Apr 21, 2025
ac122a5
test belongs here
juleswritescode Apr 21, 2025
f9f2502
test goes in other PR
juleswritescode Apr 21, 2025
8ca854b
Merge branch 'feat/compl-filtering' of https://github.com/supabase-co…
juleswritescode Apr 21, 2025
46ad86a
ok
juleswritescode Apr 21, 2025
3120724
merged
juleswritescode Apr 21, 2025
28ca671
Merge branch 'feat/compl-filtering' of https://github.com/supabase-co…
juleswritescode Apr 21, 2025
cc51524
fixes
juleswritescode Apr 21, 2025
61d4a92
Merge branch 'feat/compl-filtering' of https://github.com/supabase-co…
juleswritescode Apr 21, 2025
1c660f1
Merge branch 'fix/workflow-file' into fix/compl-item-sorting
juleswritescode Apr 21, 2025
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
12 changes: 11 additions & 1 deletion crates/pgt_completions/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ impl<'a> CompletionBuilder<'a> {

let should_preselect_first_item = should_preselect_first_item(&items);

/*
* LSP Clients themselves sort the completion items.
* They'll use the `sort_text` property if present (or fallback to the `label`).
* Since our items are already sorted, we're 'hijacking' the sort_text.
* We're simply adding the index of the item, padded by zeroes to the max length.
*/
let max_padding = items.len().to_string().len();

items
.into_iter()
.enumerate()
Expand All @@ -61,7 +69,9 @@ impl<'a> CompletionBuilder<'a> {
kind: item.kind,
label: item.label,
preselected,
score: item.score.get_score(),

// wonderous Rust syntax ftw
sort_text: format!("{:0>padding$}", idx, padding = max_padding),
}
})
.collect()
Expand Down
18 changes: 17 additions & 1 deletion crates/pgt_completions/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand All @@ -10,12 +12,26 @@ pub enum CompletionItemKind {
Schema,
}

impl Display for CompletionItemKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let txt = match self {
CompletionItemKind::Table => "Table",
CompletionItemKind::Function => "Function",
CompletionItemKind::Column => "Column",
CompletionItemKind::Schema => "Schema",
};

write!(f, "{txt}")
}
}

#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CompletionItem {
pub label: String,
pub(crate) score: i32,
pub description: String,
pub preselected: bool,
pub kind: CompletionItemKind,
/// String used for sorting by LSP clients.
pub sort_text: String,
}
3 changes: 2 additions & 1 deletion crates/pgt_lsp/src/handlers/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ pub fn get_completions(
label: i.label,
label_details: Some(CompletionItemLabelDetails {
description: Some(i.description),
detail: None,
detail: Some(format!(" {}", i.kind)),
}),
preselect: Some(i.preselected),
sort_text: Some(i.sort_text),
kind: Some(to_lsp_types_completion_item_kind(i.kind)),
..CompletionItem::default()
})
Expand Down
Loading