11use crate :: context:: CompletionContext ;
22
3- #[ derive( Debug , Default ) ]
4- pub ( crate ) struct CompletionRelevance {
3+ #[ derive( Debug ) ]
4+ pub ( crate ) enum CompletionRelevanceData < ' a > {
5+ Table ( & ' a pg_schema_cache:: Table ) ,
6+ Function ( & ' a pg_schema_cache:: Function ) ,
7+ }
8+
9+ impl < ' a > CompletionRelevanceData < ' a > {
10+ pub fn get_score ( self , ctx : & CompletionContext ) -> i32 {
11+ CompletionRelevance :: from ( self ) . into_score ( ctx)
12+ }
13+ }
14+
15+ impl < ' a > From < CompletionRelevanceData < ' a > > for CompletionRelevance < ' a > {
16+ fn from ( value : CompletionRelevanceData < ' a > ) -> Self {
17+ Self {
18+ score : 0 ,
19+ data : value,
20+ }
21+ }
22+ }
23+
24+ #[ derive( Debug ) ]
25+ pub ( crate ) struct CompletionRelevance < ' a > {
526 score : i32 ,
27+ data : CompletionRelevanceData < ' a > ,
628}
729
8- impl CompletionRelevance {
9- pub fn score ( & self ) -> i32 {
30+ impl < ' a > CompletionRelevance < ' a > {
31+ pub fn into_score ( mut self , ctx : & CompletionContext ) -> i32 {
32+ self . check_matches_schema ( ctx) ;
33+ self . check_matches_query_input ( ctx) ;
34+ self . check_if_catalog ( ctx) ;
35+
1036 self . score
1137 }
1238
13- pub fn check_matches_query_input ( & mut self , ctx : & CompletionContext , name : & str ) {
39+ fn check_matches_query_input ( & mut self , ctx : & CompletionContext ) {
1440 let node = ctx. ts_node . unwrap ( ) ;
1541
1642 let content = match ctx. get_ts_node_content ( node) {
1743 Some ( c) => c,
1844 None => return ,
1945 } ;
2046
47+ let name = match self . data {
48+ CompletionRelevanceData :: Function ( f) => f. name . as_str ( ) ,
49+ CompletionRelevanceData :: Table ( t) => t. name . as_str ( ) ,
50+ } ;
51+
2152 if name. starts_with ( content) {
2253 let len: i32 = content
2354 . len ( )
@@ -28,21 +59,25 @@ impl CompletionRelevance {
2859 } ;
2960 }
3061
31- pub fn check_matches_schema ( & mut self , ctx : & CompletionContext , schema : & str ) {
32- if ctx. schema_name . is_none ( ) {
33- return ;
34- }
62+ fn check_matches_schema ( & mut self , ctx : & CompletionContext ) {
63+ let schema_name = match ctx. schema_name . as_ref ( ) {
64+ None => return ,
65+ Some ( n) => n,
66+ } ;
3567
36- let name = ctx. schema_name . as_ref ( ) . unwrap ( ) ;
68+ let data_schema = match self . data {
69+ CompletionRelevanceData :: Function ( f) => f. schema . as_str ( ) ,
70+ CompletionRelevanceData :: Table ( t) => t. schema . as_str ( ) ,
71+ } ;
3772
38- if name == schema {
73+ if schema_name == data_schema {
3974 self . score += 25 ;
4075 } else {
4176 self . score -= 10 ;
4277 }
4378 }
4479
45- pub fn check_if_catalog ( & mut self , ctx : & CompletionContext ) {
80+ fn check_if_catalog ( & mut self , ctx : & CompletionContext ) {
4681 if ctx. schema_name . as_ref ( ) . is_some_and ( |n| n == "pg_catalog" ) {
4782 return ;
4883 }
0 commit comments