@@ -9,7 +9,7 @@ mod revoke_parser;
9
9
10
10
use crate :: queries:: { self , QueryResult , TreeSitterQueriesExecutor } ;
11
11
use pgt_schema_cache:: SchemaCache ;
12
- use pgt_text_size:: TextRange ;
12
+ use pgt_text_size:: { TextRange , TextSize } ;
13
13
14
14
use crate :: {
15
15
NodeText ,
@@ -147,6 +147,13 @@ impl TryFrom<String> for WrappingNode {
147
147
}
148
148
}
149
149
150
+ struct TreeSitterContextParams < ' a > {
151
+ pub position : TextSize ,
152
+ pub text : & ' a str ,
153
+ pub schema : & ' a pgt_schema_cache:: SchemaCache ,
154
+ pub tree : & ' a tree_sitter:: Tree ,
155
+ }
156
+
150
157
#[ derive( Debug ) ]
151
158
pub ( crate ) struct CompletionContext < ' a > {
152
159
pub node_under_cursor : Option < NodeUnderCursor < ' a > > ,
@@ -189,10 +196,10 @@ pub(crate) struct CompletionContext<'a> {
189
196
}
190
197
191
198
impl < ' a > CompletionContext < ' a > {
192
- pub fn new ( params : & ' a SanitizedCompletionParams ) -> Self {
199
+ pub fn new ( params : TreeSitterContextParams < ' a > ) -> Self {
193
200
let mut ctx = Self {
194
- tree : params. tree . as_ref ( ) ,
195
- text : & params. text ,
201
+ tree : params. tree ,
202
+ text : params. text ,
196
203
schema_cache : params. schema ,
197
204
position : usize:: from ( params. position ) ,
198
205
node_under_cursor : None ,
@@ -816,9 +823,7 @@ impl<'a> CompletionContext<'a> {
816
823
mod tests {
817
824
use crate :: {
818
825
NodeText ,
819
- context:: { CompletionContext , WrappingClause } ,
820
- sanitization:: SanitizedCompletionParams ,
821
- test_helper:: get_text_and_position,
826
+ context:: { CompletionContext , TreeSitterContextParams , WrappingClause } ,
822
827
} ;
823
828
824
829
use pgt_test_utils:: QueryWithCursorPosition ;
@@ -907,14 +912,14 @@ mod tests {
907
912
908
913
let tree = get_tree ( text. as_str ( ) ) ;
909
914
910
- let params = SanitizedCompletionParams {
915
+ let params = TreeSitterContextParams {
911
916
position : ( position as u32 ) . into ( ) ,
912
- text,
913
- tree : std :: borrow :: Cow :: Owned ( tree) ,
917
+ text : & text ,
918
+ tree : & tree,
914
919
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
915
920
} ;
916
921
917
- let ctx = CompletionContext :: new ( & params) ;
922
+ let ctx = CompletionContext :: new ( params) ;
918
923
919
924
assert_eq ! ( ctx. wrapping_clause_type, Some ( expected_clause) ) ;
920
925
}
@@ -954,17 +959,17 @@ mod tests {
954
959
] ;
955
960
956
961
for ( query, expected_schema) in test_cases {
957
- let ( position, text) = get_text_and_position ( query. as_str ( ) . into ( ) ) ;
962
+ let ( position, text) = QueryWithCursorPosition :: from ( query) . get_text_and_position ( ) ;
958
963
959
964
let tree = get_tree ( text. as_str ( ) ) ;
960
- let params = SanitizedCompletionParams {
965
+ let params = TreeSitterContextParams {
961
966
position : ( position as u32 ) . into ( ) ,
962
- text,
963
- tree : std :: borrow :: Cow :: Owned ( tree) ,
967
+ text : & text ,
968
+ tree : & tree,
964
969
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
965
970
} ;
966
971
967
- let ctx = CompletionContext :: new ( & params) ;
972
+ let ctx = CompletionContext :: new ( params) ;
968
973
969
974
assert_eq ! (
970
975
ctx. schema_or_alias_name,
@@ -1015,17 +1020,17 @@ mod tests {
1015
1020
] ;
1016
1021
1017
1022
for ( query, is_invocation) in test_cases {
1018
- let ( position, text) = get_text_and_position ( query. as_str ( ) . into ( ) ) ;
1023
+ let ( position, text) = QueryWithCursorPosition :: from ( query) . get_text_and_position ( ) ;
1019
1024
1020
1025
let tree = get_tree ( text. as_str ( ) ) ;
1021
- let params = SanitizedCompletionParams {
1026
+ let params = TreeSitterContextParams {
1022
1027
position : ( position as u32 ) . into ( ) ,
1023
- text,
1024
- tree : std :: borrow :: Cow :: Owned ( tree) ,
1028
+ text : text . as_str ( ) ,
1029
+ tree : & tree,
1025
1030
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
1026
1031
} ;
1027
1032
1028
- let ctx = CompletionContext :: new ( & params) ;
1033
+ let ctx = CompletionContext :: new ( params) ;
1029
1034
1030
1035
assert_eq ! ( ctx. is_invocation, is_invocation) ;
1031
1036
}
@@ -1045,18 +1050,18 @@ mod tests {
1045
1050
] ;
1046
1051
1047
1052
for query in cases {
1048
- let ( position, text) = get_text_and_position ( query. as_str ( ) . into ( ) ) ;
1053
+ let ( position, text) = QueryWithCursorPosition :: from ( query) . get_text_and_position ( ) ;
1049
1054
1050
1055
let tree = get_tree ( text. as_str ( ) ) ;
1051
1056
1052
- let params = SanitizedCompletionParams {
1057
+ let params = TreeSitterContextParams {
1053
1058
position : ( position as u32 ) . into ( ) ,
1054
- text,
1055
- tree : std :: borrow :: Cow :: Owned ( tree) ,
1059
+ text : & text ,
1060
+ tree : & tree,
1056
1061
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
1057
1062
} ;
1058
1063
1059
- let ctx = CompletionContext :: new ( & params) ;
1064
+ let ctx = CompletionContext :: new ( params) ;
1060
1065
1061
1066
let node = ctx. node_under_cursor . as_ref ( ) . unwrap ( ) ;
1062
1067
@@ -1084,18 +1089,18 @@ mod tests {
1084
1089
QueryWithCursorPosition :: cursor_marker( )
1085
1090
) ;
1086
1091
1087
- let ( position, text) = get_text_and_position ( query. as_str ( ) . into ( ) ) ;
1092
+ let ( position, text) = QueryWithCursorPosition :: from ( query) . get_text_and_position ( ) ;
1088
1093
1089
1094
let tree = get_tree ( text. as_str ( ) ) ;
1090
1095
1091
- let params = SanitizedCompletionParams {
1096
+ let params = TreeSitterContextParams {
1092
1097
position : ( position as u32 ) . into ( ) ,
1093
- text,
1094
- tree : std :: borrow :: Cow :: Owned ( tree) ,
1098
+ text : & text ,
1099
+ tree : & tree,
1095
1100
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
1096
1101
} ;
1097
1102
1098
- let ctx = CompletionContext :: new ( & params) ;
1103
+ let ctx = CompletionContext :: new ( params) ;
1099
1104
1100
1105
let node = ctx. node_under_cursor . as_ref ( ) . unwrap ( ) ;
1101
1106
@@ -1114,18 +1119,18 @@ mod tests {
1114
1119
fn does_not_fail_with_empty_statements ( ) {
1115
1120
let query = format ! ( "{}" , QueryWithCursorPosition :: cursor_marker( ) ) ;
1116
1121
1117
- let ( position, text) = get_text_and_position ( query. as_str ( ) . into ( ) ) ;
1122
+ let ( position, text) = QueryWithCursorPosition :: from ( query) . get_text_and_position ( ) ;
1118
1123
1119
1124
let tree = get_tree ( text. as_str ( ) ) ;
1120
1125
1121
- let params = SanitizedCompletionParams {
1126
+ let params = TreeSitterContextParams {
1122
1127
position : ( position as u32 ) . into ( ) ,
1123
- text,
1124
- tree : std :: borrow :: Cow :: Owned ( tree) ,
1128
+ text : & text ,
1129
+ tree : & tree,
1125
1130
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
1126
1131
} ;
1127
1132
1128
- let ctx = CompletionContext :: new ( & params) ;
1133
+ let ctx = CompletionContext :: new ( params) ;
1129
1134
1130
1135
let node = ctx. node_under_cursor . as_ref ( ) . unwrap ( ) ;
1131
1136
@@ -1147,18 +1152,18 @@ mod tests {
1147
1152
// is selecting a certain column name, such as `frozen_account`.
1148
1153
let query = format ! ( "select * fro{}" , QueryWithCursorPosition :: cursor_marker( ) ) ;
1149
1154
1150
- let ( position, text) = get_text_and_position ( query. as_str ( ) . into ( ) ) ;
1155
+ let ( position, text) = QueryWithCursorPosition :: from ( query) . get_text_and_position ( ) ;
1151
1156
1152
1157
let tree = get_tree ( text. as_str ( ) ) ;
1153
1158
1154
- let params = SanitizedCompletionParams {
1159
+ let params = TreeSitterContextParams {
1155
1160
position : ( position as u32 ) . into ( ) ,
1156
- text,
1157
- tree : std :: borrow :: Cow :: Owned ( tree) ,
1161
+ text : & text ,
1162
+ tree : & tree,
1158
1163
schema : & pgt_schema_cache:: SchemaCache :: default ( ) ,
1159
1164
} ;
1160
1165
1161
- let ctx = CompletionContext :: new ( & params) ;
1166
+ let ctx = CompletionContext :: new ( params) ;
1162
1167
1163
1168
let node = ctx. node_under_cursor . as_ref ( ) . unwrap ( ) ;
1164
1169
0 commit comments