Skip to content

Commit 51abce1

Browse files
refactor: extract completions context into pgt_treesitter crate (#466)
I wanted to extract the treesitter-context that's used in completions, so it can also be used for the on-hover feature. I've also tried to clean up the APIs, detangled completion-context and sanitization, and extracted the general test helpers into the `pgt_test_utils` crate.
1 parent be2cd02 commit 51abce1

37 files changed

+905
-758
lines changed

Cargo.lock

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pgt_suppressions = { path = "./crates/pgt_suppressions", version = "0.
8282
pgt_text_edit = { path = "./crates/pgt_text_edit", version = "0.0.0" }
8383
pgt_text_size = { path = "./crates/pgt_text_size", version = "0.0.0" }
8484
pgt_tokenizer = { path = "./crates/pgt_tokenizer", version = "0.0.0" }
85-
pgt_treesitter_queries = { path = "./crates/pgt_treesitter_queries", version = "0.0.0" }
85+
pgt_treesitter = { path = "./crates/pgt_treesitter", version = "0.0.0" }
8686
pgt_typecheck = { path = "./crates/pgt_typecheck", version = "0.0.0" }
8787
pgt_workspace = { path = "./crates/pgt_workspace", version = "0.0.0" }
8888
pgt_workspace_macros = { path = "./crates/pgt_workspace_macros", version = "0.0.0" }

crates/pgt_completions/Cargo.toml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ version = "0.0.0"
1414
[dependencies]
1515
async-std = "1.12.0"
1616

17-
pgt_text_size.workspace = true
18-
19-
20-
fuzzy-matcher = "0.3.7"
21-
pgt_schema_cache.workspace = true
22-
pgt_treesitter_queries.workspace = true
23-
schemars = { workspace = true, optional = true }
24-
serde = { workspace = true, features = ["derive"] }
25-
serde_json = { workspace = true }
26-
tracing = { workspace = true }
27-
tree-sitter.workspace = true
28-
tree_sitter_sql.workspace = true
17+
pgt_schema_cache.workspace = true
18+
pgt_text_size.workspace = true
19+
pgt_treesitter.workspace = true
20+
21+
fuzzy-matcher = "0.3.7"
22+
schemars = { workspace = true, optional = true }
23+
serde = { workspace = true, features = ["derive"] }
24+
serde_json = { workspace = true }
25+
tracing = { workspace = true }
26+
tree-sitter.workspace = true
27+
tree_sitter_sql.workspace = true
2928

3029
sqlx.workspace = true
3130

crates/pgt_completions/src/builder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::{
22
CompletionItemKind, CompletionText,
3-
context::CompletionContext,
43
item::CompletionItem,
54
relevance::{filtering::CompletionFilter, scoring::CompletionScore},
65
};
76

7+
use pgt_treesitter::TreesitterContext;
8+
89
pub(crate) struct PossibleCompletionItem<'a> {
910
pub label: String,
1011
pub description: String,
@@ -17,11 +18,11 @@ pub(crate) struct PossibleCompletionItem<'a> {
1718

1819
pub(crate) struct CompletionBuilder<'a> {
1920
items: Vec<PossibleCompletionItem<'a>>,
20-
ctx: &'a CompletionContext<'a>,
21+
ctx: &'a TreesitterContext<'a>,
2122
}
2223

2324
impl<'a> CompletionBuilder<'a> {
24-
pub fn new(ctx: &'a CompletionContext) -> Self {
25+
pub fn new(ctx: &'a TreesitterContext) -> Self {
2526
CompletionBuilder { items: vec![], ctx }
2627
}
2728

crates/pgt_completions/src/complete.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use pgt_text_size::TextSize;
22

3+
use pgt_treesitter::{TreeSitterContextParams, context::TreesitterContext};
4+
35
use crate::{
46
builder::CompletionBuilder,
5-
context::CompletionContext,
67
item::CompletionItem,
78
providers::{
89
complete_columns, complete_functions, complete_policies, complete_roles, complete_schemas,
@@ -28,16 +29,20 @@ pub struct CompletionParams<'a> {
2829
pub fn complete(params: CompletionParams) -> Vec<CompletionItem> {
2930
let sanitized_params = SanitizedCompletionParams::from(params);
3031

31-
let ctx = CompletionContext::new(&sanitized_params);
32+
let ctx = TreesitterContext::new(TreeSitterContextParams {
33+
position: sanitized_params.position,
34+
text: &sanitized_params.text,
35+
tree: &sanitized_params.tree,
36+
});
3237

3338
let mut builder = CompletionBuilder::new(&ctx);
3439

35-
complete_tables(&ctx, &mut builder);
36-
complete_functions(&ctx, &mut builder);
37-
complete_columns(&ctx, &mut builder);
38-
complete_schemas(&ctx, &mut builder);
39-
complete_policies(&ctx, &mut builder);
40-
complete_roles(&ctx, &mut builder);
40+
complete_tables(&ctx, sanitized_params.schema, &mut builder);
41+
complete_functions(&ctx, sanitized_params.schema, &mut builder);
42+
complete_columns(&ctx, sanitized_params.schema, &mut builder);
43+
complete_schemas(&ctx, sanitized_params.schema, &mut builder);
44+
complete_policies(&ctx, sanitized_params.schema, &mut builder);
45+
complete_roles(&ctx, sanitized_params.schema, &mut builder);
4146

4247
builder.finish()
4348
}

crates/pgt_completions/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod builder;
22
mod complete;
3-
mod context;
43
mod item;
54
mod providers;
65
mod relevance;

0 commit comments

Comments
 (0)