11use  pgt_schema_cache:: ProcKind ; 
2- 
32use  pgt_treesitter:: context:: { NodeUnderCursor ,  TreesitterContext ,  WrappingClause ,  WrappingNode } ; 
43
54use  super :: CompletionRelevanceData ; 
@@ -58,8 +57,7 @@ impl CompletionFilter<'_> {
5857 | Some ( WrappingClause :: Insert ) 
5958 | Some ( WrappingClause :: DropColumn ) 
6059 | Some ( WrappingClause :: AlterColumn ) 
61-  | Some ( WrappingClause :: RenameColumn ) 
62-  | Some ( WrappingClause :: PolicyCheck )  => { 
60+  | Some ( WrappingClause :: RenameColumn )  => { 
6361 // the literal is probably a column 
6462 } 
6563 _ => return  None , 
@@ -123,6 +121,13 @@ impl CompletionFilter<'_> {
123121 "keyword_table" , 
124122 ] ) , 
125123
124+  WrappingClause :: CreatePolicy 
125+  | WrappingClause :: AlterPolicy 
126+  | WrappingClause :: DropPolicy  => { 
127+  ctx. matches_ancestor_history ( & [ "object_reference" ] ) 
128+  && ctx. before_cursor_matches_kind ( & [ "keyword_on" ,  "." ] ) 
129+  } 
130+ 
126131 _ => false , 
127132 } , 
128133
@@ -162,8 +167,11 @@ impl CompletionFilter<'_> {
162167 && ctx. matches_ancestor_history ( & [ "field" ] ) ) 
163168 } 
164169
165-  WrappingClause :: PolicyCheck  => { 
166-  ctx. before_cursor_matches_kind ( & [ "keyword_and" ,  "(" ] ) 
170+  WrappingClause :: CheckOrUsingClause  => { 
171+  ctx. before_cursor_matches_kind ( & [ "(" ,  "keyword_and" ] ) 
172+  || ctx. wrapping_node_kind . as_ref ( ) . is_some_and ( |nk| { 
173+  matches ! ( nk,  WrappingNode :: BinaryExpression ) 
174+  } ) 
167175 } 
168176
169177 _ => false , 
@@ -176,9 +184,12 @@ impl CompletionFilter<'_> {
176184 | WrappingClause :: Where 
177185 | WrappingClause :: Join  {  .. }  => true , 
178186
179-  WrappingClause :: PolicyCheck  => { 
180-  ctx. before_cursor_matches_kind ( & [ "=" ] ) 
181-  && matches ! ( f. kind,  ProcKind :: Function  | ProcKind :: Procedure ) 
187+  WrappingClause :: CheckOrUsingClause  => { 
188+  !matches ! ( f. kind,  ProcKind :: Aggregate ) 
189+  && ( ctx. before_cursor_matches_kind ( & [ "(" ,  "keyword_and" ] ) 
190+  || ctx. wrapping_node_kind . as_ref ( ) . is_some_and ( |nk| { 
191+  matches ! ( nk,  WrappingNode :: BinaryExpression ) 
192+  } ) ) 
182193 } 
183194
184195 _ => false , 
@@ -209,11 +220,21 @@ impl CompletionFilter<'_> {
209220 && ctx. before_cursor_matches_kind ( & [ "keyword_into" ] ) 
210221 } 
211222
223+  WrappingClause :: CreatePolicy 
224+  | WrappingClause :: AlterPolicy 
225+  | WrappingClause :: DropPolicy  => { 
226+  ctx. before_cursor_matches_kind ( & [ "keyword_on" ] ) 
227+  } 
228+ 
212229 _ => false , 
213230 } , 
214231
215232 CompletionRelevanceData :: Policy ( _)  => { 
216-  matches ! ( clause,  WrappingClause :: PolicyName ) 
233+  matches ! ( 
234+  clause, 
235+  // not CREATE – there can't be existing policies. 
236+  WrappingClause :: AlterPolicy  | WrappingClause :: DropPolicy 
237+  )  && ctx. before_cursor_matches_kind ( & [ "keyword_policy" ,  "keyword_exists" ] ) 
217238 } 
218239
219240 CompletionRelevanceData :: Role ( _)  => match  clause { 
@@ -224,6 +245,11 @@ impl CompletionFilter<'_> {
224245 WrappingClause :: SetStatement  => ctx
225246 . before_cursor_matches_kind ( & [ "keyword_role" ,  "keyword_authorization" ] ) , 
226247
248+  WrappingClause :: AlterPolicy  | WrappingClause :: CreatePolicy  => { 
249+  ctx. before_cursor_matches_kind ( & [ "keyword_to" ] ) 
250+  && ctx. matches_ancestor_history ( & [ "policy_to_role" ] ) 
251+  } 
252+ 
227253 _ => false , 
228254 } , 
229255 } 
0 commit comments