Skip to content

Commit f3d3705

Browse files
refactorio
1 parent 5c12f8c commit f3d3705

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

crates/pgt_completions/src/context/context.rs

+32-33
Original file line numberDiff line numberDiff line change
@@ -173,47 +173,46 @@ impl<'a> CompletionContext<'a> {
173173
// policy handling is important to Supabase, but they are a PostgreSQL specific extension,
174174
// so the tree_sitter_sql language does not support it.
175175
// We infer the context manually.
176-
if params.text.to_lowercase().starts_with("create policy")
177-
|| params.text.to_lowercase().starts_with("alter policy")
178-
|| params.text.to_lowercase().starts_with("drop policy")
179-
{
180-
let policy_context = PolicyParser::get_context(&ctx.text, ctx.position);
181-
182-
ctx.node_under_cursor = Some(NodeUnderCursor::CustomNode {
183-
text: policy_context.node_text.into(),
184-
range: policy_context.node_range,
185-
kind: policy_context.node_kind.clone(),
186-
});
187-
188-
if policy_context.node_kind == "policy_table" {
189-
ctx.schema_or_alias_name = policy_context.schema_name.clone();
190-
}
191-
192-
if policy_context.table_name.is_some() {
193-
let mut new = HashSet::new();
194-
new.insert(policy_context.table_name.unwrap());
195-
ctx.mentioned_relations
196-
.insert(policy_context.schema_name, new);
197-
}
198-
199-
ctx.wrapping_clause_type = match policy_context.node_kind.as_str() {
200-
"policy_name" if policy_context.statement_kind != PolicyStmtKind::Create => {
201-
Some(WrappingClause::PolicyName)
202-
}
203-
"policy_role" => Some(WrappingClause::ToRoleAssignment),
204-
"policy_table" => Some(WrappingClause::From),
205-
_ => None,
206-
};
176+
if PolicyParser::looks_like_policy_stmt(&params.text) {
177+
ctx.gather_policy_context();
207178
} else {
208179
ctx.gather_tree_context();
209180
ctx.gather_info_from_ts_queries();
210181
}
211182

212-
tracing::warn!("{:#?}", ctx.get_node_under_cursor_content());
213-
214183
ctx
215184
}
216185

186+
fn gather_policy_context(&mut self) {
187+
let policy_context = PolicyParser::get_context(&self.text, self.position);
188+
189+
self.node_under_cursor = Some(NodeUnderCursor::CustomNode {
190+
text: policy_context.node_text.into(),
191+
range: policy_context.node_range,
192+
kind: policy_context.node_kind.clone(),
193+
});
194+
195+
if policy_context.node_kind == "policy_table" {
196+
self.schema_or_alias_name = policy_context.schema_name.clone();
197+
}
198+
199+
if policy_context.table_name.is_some() {
200+
let mut new = HashSet::new();
201+
new.insert(policy_context.table_name.unwrap());
202+
self.mentioned_relations
203+
.insert(policy_context.schema_name, new);
204+
}
205+
206+
self.wrapping_clause_type = match policy_context.node_kind.as_str() {
207+
"policy_name" if policy_context.statement_kind != PolicyStmtKind::Create => {
208+
Some(WrappingClause::PolicyName)
209+
}
210+
"policy_role" => Some(WrappingClause::ToRoleAssignment),
211+
"policy_table" => Some(WrappingClause::From),
212+
_ => None,
213+
};
214+
}
215+
217216
fn gather_info_from_ts_queries(&mut self) {
218217
let stmt_range = self.wrapping_statement_range.as_ref();
219218
let sql = self.text;

crates/pgt_completions/src/context/policy_parser.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,17 @@ pub(crate) struct PolicyParser {
117117
}
118118

119119
impl PolicyParser {
120+
pub(crate) fn looks_like_policy_stmt(sql: &str) -> bool {
121+
let lowercased = sql.to_ascii_lowercase();
122+
let trimmed = lowercased.trim();
123+
trimmed.starts_with("create policy")
124+
|| trimmed.starts_with("drop policy")
125+
|| trimmed.starts_with("alter policy")
126+
}
127+
120128
pub(crate) fn get_context(sql: &str, cursor_position: usize) -> PolicyContext {
121-
let trimmed = sql.trim();
122129
assert!(
123-
trimmed.starts_with("create policy")
124-
|| trimmed.starts_with("drop policy")
125-
|| trimmed.starts_with("alter policy"),
130+
Self::looks_like_policy_stmt(sql),
126131
"PolicyParser should only be used for policy statements. Developer error!"
127132
);
128133

0 commit comments

Comments
 (0)