@@ -8,7 +8,9 @@ use pgt_treesitter_queries::{
88} ;
99
1010use crate :: {
11- NodeText , context:: policy_parser:: PolicyParser , sanitization:: SanitizedCompletionParams ,
11+ NodeText ,
12+ context:: policy_parser:: { PolicyParser , PolicyStmtKind } ,
13+ sanitization:: SanitizedCompletionParams ,
1214} ;
1315
1416#[ derive( Debug , PartialEq , Eq ) ]
@@ -40,6 +42,7 @@ pub enum WrappingNode {
4042 Assignment ,
4143}
4244
45+ #[ derive( Debug ) ]
4346pub ( crate ) enum NodeUnderCursor < ' a > {
4447 TsNode ( tree_sitter:: Node < ' a > ) ,
4548 CustomNode {
@@ -64,6 +67,13 @@ impl<'a> NodeUnderCursor<'a> {
6467 }
6568 }
6669
70+ pub fn range ( & self ) -> TextRange {
71+ let start: u32 = self . start_byte ( ) . try_into ( ) . unwrap ( ) ;
72+ let end: u32 = self . end_byte ( ) . try_into ( ) . unwrap ( ) ;
73+
74+ TextRange :: new ( start. into ( ) , end. into ( ) )
75+ }
76+
6777 pub fn kind ( & self ) -> & str {
6878 match self {
6979 NodeUnderCursor :: TsNode ( node) => node. kind ( ) ,
@@ -182,6 +192,10 @@ impl<'a> CompletionContext<'a> {
182192 kind : policy_context. node_kind . clone ( ) ,
183193 } ) ;
184194
195+ if policy_context. node_kind == "policy_table" {
196+ ctx. schema_or_alias_name = policy_context. schema_name . clone ( ) ;
197+ }
198+
185199 if policy_context. table_name . is_some ( ) {
186200 let mut new = HashSet :: new ( ) ;
187201 new. insert ( policy_context. table_name . unwrap ( ) ) ;
@@ -190,7 +204,9 @@ impl<'a> CompletionContext<'a> {
190204 }
191205
192206 ctx. wrapping_clause_type = match policy_context. node_kind . as_str ( ) {
193- "policy_name" => Some ( WrappingClause :: PolicyName ) ,
207+ "policy_name" if policy_context. statement_kind != PolicyStmtKind :: Create => {
208+ Some ( WrappingClause :: PolicyName )
209+ }
194210 "policy_role" => Some ( WrappingClause :: ToRole ) ,
195211 "policy_table" => Some ( WrappingClause :: From ) ,
196212 _ => None ,
@@ -200,19 +216,11 @@ impl<'a> CompletionContext<'a> {
200216 ctx. gather_info_from_ts_queries ( ) ;
201217 }
202218
203- tracing:: warn!( "sql: {}" , ctx. text) ;
204- tracing:: warn!( "position: {}" , ctx. position) ;
205- tracing:: warn!(
206- "node range: {} - {}" ,
207- ctx. node_under_cursor
208- . as_ref( )
209- . map( |n| n. start_byte( ) )
210- . unwrap_or( 0 ) ,
211- ctx. node_under_cursor
212- . as_ref( )
213- . map( |n| n. end_byte( ) )
214- . unwrap_or( 0 )
215- ) ;
219+ tracing:: warn!( "SQL: {}" , ctx. text) ;
220+ tracing:: warn!( "Position: {}" , ctx. position) ;
221+ tracing:: warn!( "Node: {:#?}" , ctx. node_under_cursor) ;
222+ tracing:: warn!( "Relations: {:#?}" , ctx. mentioned_relations) ;
223+ tracing:: warn!( "Clause: {:#?}" , ctx. wrapping_clause_type) ;
216224
217225 ctx
218226 }
0 commit comments