11use text_size:: TextSize ;
22
33use crate :: {
4- builder:: CompletionBuilder , context:: CompletionContext , item:: CompletionItem , providers,
4+ builder:: CompletionBuilder , context:: CompletionContext , item:: CompletionItemWithRelevance ,
5+ providers,
56} ;
67
78pub const LIMIT : usize = 50 ;
@@ -16,7 +17,7 @@ pub struct CompletionParams<'a> {
1617
1718#[ derive( Debug , Default ) ]
1819pub struct CompletionResult {
19- pub items : Vec < CompletionItem > ,
20+ pub items : Vec < CompletionItemWithRelevance > ,
2021}
2122
2223pub fn complete ( params : CompletionParams ) -> CompletionResult {
@@ -45,48 +46,34 @@ mod tests {
4546 use crate :: { complete, CompletionParams } ;
4647
4748 #[ tokio:: test]
48- async fn test_complete ( ) {
49- let pool = get_new_test_db ( ) . await ;
50-
51- let input = "select id from c;" ;
52-
53- let mut parser = tree_sitter:: Parser :: new ( ) ;
54- parser
55- . set_language ( tree_sitter_sql:: language ( ) )
56- . expect ( "Error loading sql language" ) ;
57-
58- let tree = parser. parse ( input, None ) . unwrap ( ) ;
59-
60- let schema_cache = SchemaCache :: load ( & pool) . await ;
61-
62- let p = CompletionParams {
63- position : 15 . into ( ) ,
64- schema : & schema_cache,
65- text : input,
66- tree : Some ( & tree) ,
67- } ;
68-
69- let result = complete ( p) ;
49+ async fn autocompletes_simple_table ( ) {
50+ let test_db = get_new_test_db ( ) . await ;
7051
71- assert ! ( result. items. len( ) > 0 ) ;
72- }
52+ let setup = r#"
53+ create table users (
54+ id serial primary key,
55+ name text,
56+ password text
57+ );
58+ "# ;
7359
74- #[ tokio:: test]
75- async fn test_complete_two ( ) {
76- let pool = get_new_test_db ( ) . await ;
60+ test_db
61+ . execute ( setup)
62+ . await
63+ . expect ( "Failed to execute setup query" ) ;
7764
78- let input = "select id, name, test1231234123, unknown from co; " ;
65+ let input = "select * from u " ;
7966
8067 let mut parser = tree_sitter:: Parser :: new ( ) ;
8168 parser
8269 . set_language ( tree_sitter_sql:: language ( ) )
8370 . expect ( "Error loading sql language" ) ;
8471
8572 let tree = parser. parse ( input, None ) . unwrap ( ) ;
86- let schema_cache = SchemaCache :: load ( & pool ) . await ;
73+ let schema_cache = SchemaCache :: load ( & test_db ) . await ;
8774
8875 let p = CompletionParams {
89- position : 47 . into ( ) ,
76+ position : ( ( input . len ( ) - 1 ) as u32 ) . into ( ) ,
9077 schema : & schema_cache,
9178 text : input,
9279 tree : Some ( & tree) ,
@@ -95,52 +82,71 @@ mod tests {
9582 let result = complete ( p) ;
9683
9784 assert ! ( result. items. len( ) > 0 ) ;
85+
86+ let best_match = & result. items [ 0 ] ;
87+
88+ assert_eq ! (
89+ best_match. label, "users" ,
90+ "Does not return the expected table to autocomplete: {}" ,
91+ best_match. label
92+ )
9893 }
9994
100- #[ tokio:: test]
101- async fn test_complete_three ( ) {
95+ async fn autocompletes_table_with_schema ( ) {
10296 let test_db = get_new_test_db ( ) . await ;
10397
10498 let setup = r#"
105- create table users (
99+ create schema public;
100+ create schema private;
101+
102+ create table private.users (
106103 id serial primary key,
107104 name text,
108105 password text
109106 );
107+
108+ create table public.user_requests (
109+ id serial primary key,
110+ request text,
111+ send_at timestamp with time zone
112+ );
110113 "# ;
111114
112115 test_db
113116 . execute ( setup)
114117 . await
115118 . expect ( "Failed to execute setup query" ) ;
116119
117- let input = "select * from u" ;
120+ let schema_cache = SchemaCache :: load ( & test_db ) . await ;
118121
119122 let mut parser = tree_sitter:: Parser :: new ( ) ;
120123 parser
121124 . set_language ( tree_sitter_sql:: language ( ) )
122125 . expect ( "Error loading sql language" ) ;
123126
124- let tree = parser. parse ( input, None ) . unwrap ( ) ;
125- let schema_cache = SchemaCache :: load ( & test_db) . await ;
127+ // testing the private schema
128+ {
129+ let input = "select * from private.u" ;
130+ let tree = parser. parse ( input, None ) . unwrap ( ) ;
126131
127- let p = CompletionParams {
128- position : ( ( input. len ( ) - 1 ) as u32 ) . into ( ) ,
129- schema : & schema_cache,
130- text : input,
131- tree : Some ( & tree) ,
132- } ;
132+ let p = CompletionParams {
133+ position : ( ( input. len ( ) - 1 ) as u32 ) . into ( ) ,
134+ schema : & schema_cache,
135+ text : input,
136+ tree : Some ( & tree) ,
137+ } ;
133138
134- let result = complete ( p) ;
139+ let result = complete ( p) ;
135140
136- assert ! ( result. items. len( ) > 0 ) ;
141+ assert ! ( result. items. len( ) > 0 ) ;
137142
138- let best_match = & result. items [ 0 ] ;
143+ let best_match = & result. items [ 0 ] ;
139144
140- assert_eq ! (
141- best_match. label, "users" ,
142- "Does not return the expected table to autocomplete: {}" ,
143- best_match. label
144- )
145+ assert_eq ! (
146+ best_match. label, "users" ,
147+ "Does not return the expected table to autocomplete: {}" ,
148+ best_match. label
149+ )
150+ }
145151 }
146152}
0 commit comments