Skip to content

Commit 896cd6d

Browse files
add scaffolding for function completions
1 parent 652da2b commit 896cd6d

File tree

7 files changed

+119
-11
lines changed

7 files changed

+119
-11
lines changed

crates/pg_completions/src/complete.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub const LIMIT: usize = 50;
1111
pub struct CompletionParams<'a> {
1212
pub position: TextSize,
1313
pub schema: &'a pg_schema_cache::SchemaCache,
14-
pub text: &'a str,
14+
pub text: String,
1515
pub tree: Option<&'a tree_sitter::Tree>,
1616
}
1717

@@ -77,7 +77,7 @@ mod tests {
7777
let p = CompletionParams {
7878
position: ((input.len() - 1) as u32).into(),
7979
schema: &schema_cache,
80-
text: input,
80+
text: input.into(),
8181
tree: Some(&tree),
8282
};
8383

@@ -136,7 +136,7 @@ mod tests {
136136
let p = CompletionParams {
137137
position: ((input.len() - 1) as u32).into(),
138138
schema: &schema_cache,
139-
text: input,
139+
text: input.into(),
140140
tree: Some(&tree),
141141
};
142142

@@ -199,7 +199,7 @@ mod tests {
199199
let p = CompletionParams {
200200
position: ((input.len() - 1) as u32).into(),
201201
schema: &schema_cache,
202-
text: input,
202+
text: input.into(),
203203
tree: Some(&tree),
204204
};
205205

crates/pg_completions/src/context.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'a> CompletionContext<'a> {
1818
pub fn new(params: &'a CompletionParams) -> Self {
1919
let mut tree = Self {
2020
tree: params.tree,
21-
text: params.text,
21+
text: &params.text,
2222
schema_cache: params.schema,
2323
position: usize::from(params.position),
2424

@@ -102,7 +102,7 @@ impl<'a> CompletionContext<'a> {
102102

103103
#[cfg(test)]
104104
mod tests {
105-
use crate::context::CompletionContext;
105+
use crate::{context::CompletionContext, test_helper::CURSOR_POS};
106106

107107
fn get_tree(input: &str) -> tree_sitter::Tree {
108108
let mut parser = tree_sitter::Parser::new();
@@ -113,8 +113,6 @@ mod tests {
113113
parser.parse(input, None).expect("Unable to parse tree")
114114
}
115115

116-
static CURSOR_POS: &str = "XXX";
117-
118116
#[test]
119117
fn identifies_clauses() {
120118
let test_cases = vec![
@@ -151,7 +149,7 @@ mod tests {
151149
let tree = get_tree(text.as_str());
152150
let params = crate::CompletionParams {
153151
position: (position as u32).into(),
154-
text: text.as_str(),
152+
text: text,
155153
tree: Some(&tree),
156154
schema: &pg_schema_cache::SchemaCache::new(),
157155
};
@@ -184,7 +182,7 @@ mod tests {
184182
let tree = get_tree(text.as_str());
185183
let params = crate::CompletionParams {
186184
position: (position as u32).into(),
187-
text: text.as_str(),
185+
text: text,
188186
tree: Some(&tree),
189187
schema: &pg_schema_cache::SchemaCache::new(),
190188
};
@@ -219,7 +217,7 @@ mod tests {
219217
let tree = get_tree(text.as_str());
220218
let params = crate::CompletionParams {
221219
position: (position as u32).into(),
222-
text: text.as_str(),
220+
text: text,
223221
tree: Some(&tree),
224222
schema: &pg_schema_cache::SchemaCache::new(),
225223
};

crates/pg_completions/src/item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[derive(Debug)]
22
pub enum CompletionItemKind {
33
Table,
4+
Function,
45
}
56

67
#[derive(Debug)]

crates/pg_completions/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod context;
44
mod item;
55
mod providers;
66
mod relevance;
7+
mod test_helper;
78

89
pub use complete::*;
910
pub use item::*;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use pg_schema_cache::Function;
2+
3+
use crate::{
4+
builder::CompletionBuilder, context::CompletionContext, CompletionItem, CompletionItemKind,
5+
};
6+
7+
pub fn complete_functions(ctx: &CompletionContext, builder: &mut CompletionBuilder) {
8+
let available_functions = &ctx.schema_cache.functions;
9+
10+
let completion_items: Vec<CompletionItem> = available_functions
11+
.iter()
12+
.map(|foo| CompletionItem {
13+
label: foo.name,
14+
score: get_score(ctx, foo),
15+
description: format!("Schema: {}", foo.schema),
16+
preselected: None,
17+
kind: CompletionItemKind::Function,
18+
})
19+
.collect();
20+
21+
for item in completion_items {
22+
builder.add_item(item);
23+
}
24+
}
25+
26+
fn get_score(ctx: &CompletionContext, foo: &Function) -> i32 {
27+
0
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use crate::{
33+
context::CompletionContext,
34+
providers::complete_tables,
35+
test_helper::{get_test_deps, get_test_params, CURSOR_POS},
36+
};
37+
38+
#[tokio::test]
39+
async fn completes_fn() {
40+
let setup = r#"
41+
create or replace function cool() returns trigger
42+
begin;
43+
## Yeahhhh
44+
end;
45+
"#;
46+
47+
let query = format!("select coo{}", CURSOR_POS);
48+
49+
let (tree, cache, mut builder) = get_test_deps(setup, &query).await;
50+
let params = get_test_params(&tree, &cache, &query);
51+
let ctx = CompletionContext::new(&params);
52+
53+
complete_tables(&ctx, &mut builder);
54+
}
55+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod functions;
12
mod tables;
23

4+
pub use functions::*;
35
pub use tables::*;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use pg_schema_cache::SchemaCache;
2+
use pg_test_utils::test_database::get_new_test_db;
3+
use sqlx::Executor;
4+
5+
use crate::{builder::CompletionBuilder, CompletionParams};
6+
7+
pub static CURSOR_POS: &str = "XXX";
8+
9+
pub(crate) async fn get_test_deps(
10+
setup: &str,
11+
input: &str,
12+
) -> (
13+
tree_sitter::Tree,
14+
pg_schema_cache::SchemaCache,
15+
CompletionBuilder,
16+
) {
17+
let test_db = get_new_test_db().await;
18+
19+
test_db
20+
.execute(setup)
21+
.await
22+
.expect("Failed to execute setup query");
23+
24+
let schema_cache = SchemaCache::load(&test_db).await;
25+
26+
let mut parser = tree_sitter::Parser::new();
27+
parser
28+
.set_language(tree_sitter_sql::language())
29+
.expect("Error loading sql language");
30+
31+
let tree = parser.parse(input, None).unwrap();
32+
let builder = CompletionBuilder::new();
33+
34+
(tree, schema_cache, builder)
35+
}
36+
37+
pub(crate) fn get_test_params<'a>(
38+
tree: &'a tree_sitter::Tree,
39+
schema_cache: &'a pg_schema_cache::SchemaCache,
40+
sql: &'a str,
41+
) -> CompletionParams<'a> {
42+
let position = sql.find(CURSOR_POS).unwrap();
43+
let text = sql.replace(CURSOR_POS, "");
44+
45+
CompletionParams {
46+
position: (position as u32).into(),
47+
schema: schema_cache,
48+
tree: Some(tree),
49+
text,
50+
}
51+
}

0 commit comments

Comments
 (0)