-
Notifications
You must be signed in to change notification settings - Fork 102
feat(completions): ts_query package, column autocompletion #168
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:feat/ts-queries
Jan 10, 2025
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9a28bee
initial commit
juleswritescode ef1cbfc
merged
juleswritescode 4ca0f96
jeez
juleswritescode 646440e
jeez
juleswritescode 089c50e
not necessary
juleswritescode 4ee920d
beautiful
juleswritescode 7413aa1
fixie fixie
juleswritescode 1003f82
randomly change score until tests do what i want
juleswritescode 5b25305
i like the syntax
juleswritescode e4bb946
format TOML
juleswritescode ffba6eb
use lazyLock
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
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,114 @@ | ||
use crate::{ | ||
builder::CompletionBuilder, context::CompletionContext, relevance::CompletionRelevanceData, | ||
CompletionItem, CompletionItemKind, | ||
}; | ||
|
||
pub fn complete_columns(ctx: &CompletionContext, builder: &mut CompletionBuilder) { | ||
let available_columns = &ctx.schema_cache.columns; | ||
psteinroe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for col in available_columns { | ||
let item = CompletionItem { | ||
label: col.name.clone(), | ||
score: CompletionRelevanceData::Column(col).get_score(ctx), | ||
description: format!("Table: {}.{}", col.schema_name, col.table_name), | ||
preselected: false, | ||
kind: CompletionItemKind::Column, | ||
}; | ||
|
||
builder.add_item(item); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{ | ||
complete, | ||
test_helper::{get_test_deps, get_test_params, InputQuery, CURSOR_POS}, | ||
CompletionItem, | ||
}; | ||
|
||
struct TestCase { | ||
query: String, | ||
message: &'static str, | ||
label: &'static str, | ||
description: &'static str, | ||
} | ||
|
||
impl TestCase { | ||
fn get_input_query(&self) -> InputQuery { | ||
let strs: Vec<&str> = self.query.split_whitespace().collect(); | ||
strs.join(" ").as_str().into() | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn completes_columns() { | ||
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 queries: Vec<TestCase> = vec![ | ||
TestCase { | ||
message: "correctly prefers the columns of present tables", | ||
query: format!(r#"select na{} from public.audio_books;"#, CURSOR_POS), | ||
label: "narrator", | ||
description: "Table: public.audio_books", | ||
juleswritescode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
TestCase { | ||
message: "correctly handles nested queries", | ||
query: format!( | ||
r#" | ||
select | ||
* | ||
from ( | ||
select id, na{} | ||
from private.audio_books | ||
) as subquery | ||
join public.users u | ||
on u.id = subquery.id; | ||
"#, | ||
CURSOR_POS | ||
), | ||
label: "narrator_id", | ||
description: "Table: private.audio_books", | ||
}, | ||
TestCase { | ||
message: "works without a schema", | ||
query: format!(r#"select na{} from users;"#, CURSOR_POS), | ||
label: "name", | ||
description: "Table: public.users", | ||
}, | ||
]; | ||
|
||
for q in queries { | ||
let (tree, cache) = get_test_deps(setup, q.get_input_query()).await; | ||
let params = get_test_params(&tree, &cache, q.get_input_query()); | ||
let results = complete(params); | ||
|
||
let CompletionItem { | ||
label, description, .. | ||
} = results | ||
.into_iter() | ||
.next() | ||
.expect("Should return at least one completion item"); | ||
|
||
assert_eq!(label, q.label, "{}", q.message); | ||
assert_eq!(description, q.description, "{}", q.message); | ||
} | ||
} | ||
} |
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.