Skip to content

Commit 00b09f4

Browse files
laying the infra…
1 parent 26e82c7 commit 00b09f4

File tree

9 files changed

+58
-8
lines changed

9 files changed

+58
-8
lines changed

crates/pgt_completions/src/item.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum CompletionItemKind {
1111
Function,
1212
Column,
1313
Schema,
14+
Policy,
1415
}
1516

1617
impl Display for CompletionItemKind {
@@ -20,6 +21,7 @@ impl Display for CompletionItemKind {
2021
CompletionItemKind::Function => "Function",
2122
CompletionItemKind::Column => "Column",
2223
CompletionItemKind::Schema => "Schema",
24+
CompletionItemKind::Policy => "Policy",
2325
};
2426

2527
write!(f, "{txt}")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod columns;
22
mod functions;
33
mod helper;
4+
mod policies;
45
mod schemas;
56
mod tables;
67

78
pub use columns::*;
89
pub use functions::*;
10+
pub use policies::*;
911
pub use schemas::*;
1012
pub use tables::*;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::{
2+
CompletionItemKind,
3+
builder::{CompletionBuilder, PossibleCompletionItem},
4+
context::{CompletionContext, WrappingClause},
5+
relevance::{CompletionRelevanceData, filtering::CompletionFilter, scoring::CompletionScore},
6+
};
7+
8+
use super::helper::{find_matching_alias_for_table, get_completion_text_with_schema_or_alias};
9+
10+
pub fn complete_policies<'a>(ctx: &CompletionContext<'a>, builder: &mut CompletionBuilder<'a>) {
11+
let available_policies = &ctx.schema_cache.policies;
12+
13+
for pol in available_policies {
14+
let relevance = CompletionRelevanceData::Policy(pol);
15+
16+
let mut item = PossibleCompletionItem {
17+
label: pol.name.clone(),
18+
score: CompletionScore::from(relevance.clone()),
19+
filter: CompletionFilter::from(relevance),
20+
description: format!("Table: {}.{}", pol.schema_name, pol.table_name),
21+
kind: CompletionItemKind::Column,
22+
completion_text: None,
23+
};
24+
25+
// autocomplete with the alias in a join clause if we find one
26+
if matches!(ctx.wrapping_clause_type, Some(WrappingClause::Join { .. })) {
27+
item.completion_text = find_matching_alias_for_table(ctx, pol.table_name.as_str())
28+
.and_then(|alias| {
29+
get_completion_text_with_schema_or_alias(ctx, pol.name.as_str(), alias.as_str())
30+
});
31+
}
32+
33+
builder.add_item(item);
34+
}
35+
}

crates/pgt_completions/src/relevance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ pub(crate) enum CompletionRelevanceData<'a> {
77
Function(&'a pgt_schema_cache::Function),
88
Column(&'a pgt_schema_cache::Column),
99
Schema(&'a pgt_schema_cache::Schema),
10+
Policy(&'a pgt_schema_cache::Policy),
1011
}

crates/pgt_completions/src/relevance/filtering.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ impl CompletionFilter<'_> {
130130
// we should never allow schema suggestions if there already was one.
131131
false
132132
}
133+
134+
// no aliases and schemas for policies
135+
CompletionRelevanceData::Policy(_) => false,
133136
};
134137

135138
if !matches {

crates/pgt_completions/src/relevance/scoring.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl CompletionScore<'_> {
4545
CompletionRelevanceData::Table(t) => t.name.as_str(),
4646
CompletionRelevanceData::Column(c) => c.name.as_str(),
4747
CompletionRelevanceData::Schema(s) => s.name.as_str(),
48+
CompletionRelevanceData::Policy(p) => p.name.as_str(),
4849
};
4950

5051
let fz_matcher = SkimMatcherV2::default();
@@ -116,6 +117,7 @@ impl CompletionScore<'_> {
116117
WrappingClause::Delete if !has_mentioned_schema => 15,
117118
_ => -50,
118119
},
120+
CompletionRelevanceData::Policy(_) => 0,
119121
}
120122
}
121123

@@ -149,6 +151,7 @@ impl CompletionScore<'_> {
149151
WrappingNode::Relation if !has_mentioned_schema && has_node_text => 0,
150152
_ => -50,
151153
},
154+
CompletionRelevanceData::Policy(_) => 0,
152155
}
153156
}
154157

@@ -182,13 +185,15 @@ impl CompletionScore<'_> {
182185
CompletionRelevanceData::Table(t) => t.schema.as_str(),
183186
CompletionRelevanceData::Column(c) => c.schema_name.as_str(),
184187
CompletionRelevanceData::Schema(s) => s.name.as_str(),
188+
CompletionRelevanceData::Policy(p) => p.name.as_str(),
185189
}
186190
}
187191

188192
fn get_table_name(&self) -> Option<&str> {
189193
match self.data {
190194
CompletionRelevanceData::Column(c) => Some(c.table_name.as_str()),
191195
CompletionRelevanceData::Table(t) => Some(t.name.as_str()),
196+
CompletionRelevanceData::Policy(p) => Some(p.table_name.as_str()),
192197
_ => None,
193198
}
194199
}

crates/pgt_lsp/src/handlers/completions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ fn to_lsp_types_completion_item_kind(
6565
pgt_completions::CompletionItemKind::Table => lsp_types::CompletionItemKind::CLASS,
6666
pgt_completions::CompletionItemKind::Column => lsp_types::CompletionItemKind::FIELD,
6767
pgt_completions::CompletionItemKind::Schema => lsp_types::CompletionItemKind::CLASS,
68+
pgt_completions::CompletionItemKind::Policy => lsp_types::CompletionItemKind::VALUE,
6869
}
6970
}

crates/pgt_schema_cache/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod versions;
1313

1414
pub use columns::*;
1515
pub use functions::{Behavior, Function, FunctionArg, FunctionArgs};
16+
pub use policies::{Policy, PolicyCommand};
1617
pub use schema_cache::SchemaCache;
1718
pub use schemas::Schema;
1819
pub use tables::{ReplicaIdentity, Table};

crates/pgt_schema_cache/src/policies.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ impl From<PolicyQueried> for Policy {
5656

5757
#[derive(Debug, Clone, PartialEq, Eq)]
5858
pub struct Policy {
59-
name: String,
60-
table_name: String,
61-
schema_name: String,
62-
is_permissive: bool,
63-
command: PolicyCommand,
64-
role_names: Vec<String>,
65-
security_qualification: Option<String>,
66-
with_check: Option<String>,
59+
pub name: String,
60+
pub table_name: String,
61+
pub schema_name: String,
62+
pub is_permissive: bool,
63+
pub command: PolicyCommand,
64+
pub role_names: Vec<String>,
65+
pub security_qualification: Option<String>,
66+
pub with_check: Option<String>,
6767
}
6868

6969
impl SchemaCacheItem for Policy {

0 commit comments

Comments
 (0)