-
Notifications
You must be signed in to change notification settings - Fork 102
feat(completions): basic scoring algorithm for tables #150
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
psteinroe
merged 11 commits into
supabase-community:main
from
juleswritescode:completions/table-names
Dec 11, 2024
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
337687f
impl From<Scratch> for PR
juleswritescode 7c279d5
completion context getting ahead
juleswritescode 4ccda40
nice, comes out as expected
juleswritescode 2afc5e0
feat(completions): basic scoring algorithm for tables
juleswritescode 624ea8e
refactorings
juleswritescode 99c1463
pin tower_lsp
juleswritescode 1ecb7f0
ok
juleswritescode aaae3fa
feat: better autocompletions
juleswritescode 1fd51cc
add alphanumeric test
juleswritescode 7d9f8e2
simplify, keep api boundary, add traits
juleswritescode e37202f
remove dep
juleswritescode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Auto-Completions | ||
|
||
## What does this crate do? | ||
|
||
The `pg_completions` identifies and ranks autocompletion items that can be displayed in your code editor. | ||
Its main export is the `complete` function. The function takes a PostgreSQL statement, a cursor position, and a datastructure representing the underlying databases schema. It returns a list of completion items. | ||
|
||
Postgres's statement-parsing-engine, `libpg_query`, which is used in other parts of this LSP, is only capable of parsing _complete and valid_ statements. Since autocompletion should work for incomplete statements, we rely heavily on tree-sitter – an incremental parsing library. | ||
|
||
### Working with TreeSitter | ||
|
||
In the `pg_test_utils` crate, there's a binary that parses an SQL file and prints out the matching tree-sitter tree. | ||
This makes writing tree-sitter queries for this crate easy. | ||
|
||
To print a tree, run `cargo run --bin tree_print -- -f <your_sql_file>`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,55 @@ | ||
use crate::{CompletionItem, CompletionResult}; | ||
use tower_lsp::lsp_types::CompletionItem; | ||
|
||
pub struct CompletionBuilder<'a> { | ||
pub items: Vec<CompletionItem<'a>>, | ||
use crate::{item::CompletionItemWithScore, CompletionResult}; | ||
|
||
pub(crate) struct CompletionBuilder { | ||
items: Vec<CompletionItemWithScore>, | ||
} | ||
|
||
pub struct CompletionConfig {} | ||
impl CompletionBuilder { | ||
pub fn new() -> Self { | ||
CompletionBuilder { items: vec![] } | ||
} | ||
|
||
impl<'a> From<&'a CompletionConfig> for CompletionBuilder<'a> { | ||
fn from(_config: &CompletionConfig) -> Self { | ||
Self { items: Vec::new() } | ||
pub fn add_item(&mut self, item: CompletionItemWithScore) { | ||
self.items.push(item); | ||
} | ||
} | ||
|
||
impl<'a> CompletionBuilder<'a> { | ||
pub fn finish(mut self) -> CompletionResult<'a> { | ||
pub fn finish(mut self) -> CompletionResult { | ||
self.items.sort_by(|a, b| { | ||
b.preselect | ||
.cmp(&a.preselect) | ||
.then_with(|| b.score.cmp(&a.score)) | ||
.then_with(|| a.data.label().cmp(b.data.label())) | ||
b.score | ||
.cmp(&a.score) | ||
.then_with(|| a.label().cmp(&b.label())) | ||
}); | ||
|
||
self.items.dedup_by(|a, b| a.data.label() == b.data.label()); | ||
self.items.dedup_by(|a, b| a.label() == b.label()); | ||
self.items.truncate(crate::LIMIT); | ||
let Self { items, .. } = self; | ||
|
||
let should_preselect_first_item = self.should_preselect_first_item(); | ||
|
||
let items: Vec<CompletionItem> = self | ||
.items | ||
.into_iter() | ||
.enumerate() | ||
.map(|(idx, mut item)| { | ||
if idx == 0 { | ||
item.set_preselected(should_preselect_first_item); | ||
} | ||
item.into() | ||
}) | ||
.collect(); | ||
|
||
CompletionResult { items } | ||
} | ||
|
||
fn should_preselect_first_item(&mut self) -> bool { | ||
let mut items_iter = self.items.iter(); | ||
let first = items_iter.next(); | ||
let second = items_iter.next(); | ||
|
||
first.is_some_and(|f| match second { | ||
Some(s) => (f.score - s.score) > 10, | ||
None => true, | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.