11use std:: sync:: Arc ;
22
33use dashmap:: DashMap ;
4+ use pg_diagnostics:: { serde:: Diagnostic as SDiagnostic , Diagnostic , MessageAndDescription } ;
5+ use text_size:: TextRange ;
46
57use super :: {
68 change:: ChangedStatement ,
79 document:: { Statement , StatementRef } ,
810 store:: Store ,
911} ;
1012
13+ /// A specialized diagnostic for the libpg_query parser.
14+ ///
15+ /// Parser diagnostics are always **errors**.
16+ #[ derive( Clone , Debug , Diagnostic ) ]
17+ #[ diagnostic( category = "syntax" , severity = Error ) ]
18+ pub struct SyntaxDiagnostic {
19+ /// The location where the error is occurred
20+ #[ location( span) ]
21+ span : Option < TextRange > ,
22+ #[ message]
23+ #[ description]
24+ pub message : MessageAndDescription ,
25+ }
26+
1127pub struct PgQueryStore {
1228 ast_db : DashMap < StatementRef , Arc < pg_query_ext:: NodeEnum > > ,
13- native_diagnostics : DashMap < StatementRef , Arc < pg_query_ext:: Error > > ,
29+ diagnostics : DashMap < StatementRef , pg_query_ext:: Error > ,
30+ }
31+
32+ impl From < & pg_query_ext:: Error > for SyntaxDiagnostic {
33+ fn from ( err : & pg_query_ext:: Error ) -> Self {
34+ SyntaxDiagnostic {
35+ span : None ,
36+ message : MessageAndDescription :: from ( err. to_string ( ) ) ,
37+ }
38+ }
1439}
1540
1641impl PgQueryStore {
1742 pub fn new ( ) -> PgQueryStore {
1843 PgQueryStore {
1944 ast_db : DashMap :: new ( ) ,
20- native_diagnostics : DashMap :: new ( ) ,
45+ diagnostics : DashMap :: new ( ) ,
2146 }
2247 }
48+
49+ pub fn pull_diagnostics ( & self , ref_ : & StatementRef ) -> Vec < SDiagnostic > {
50+ self . diagnostics . get ( ref_) . map_or_else ( Vec :: new, |err| {
51+ vec ! [ SDiagnostic :: new( SyntaxDiagnostic :: from( err. value( ) ) ) ]
52+ } )
53+ }
2354}
2455
2556impl Store < pg_query_ext:: NodeEnum > for PgQueryStore {
@@ -32,14 +63,14 @@ impl Store<pg_query_ext::NodeEnum> for PgQueryStore {
3263 if let Ok ( ast) = r {
3364 self . ast_db . insert ( statement. ref_ . clone ( ) , Arc :: new ( ast) ) ;
3465 } else {
35- self . native_diagnostics
36- . insert ( statement. ref_ . clone ( ) , Arc :: new ( r. unwrap_err ( ) ) ) ;
66+ self . diagnostics
67+ . insert ( statement. ref_ . clone ( ) , r. unwrap_err ( ) ) ;
3768 }
3869 }
3970
4071 fn remove_statement ( & self , statement : & StatementRef ) {
4172 self . ast_db . remove ( statement) ;
42- self . native_diagnostics . remove ( statement) ;
73+ self . diagnostics . remove ( statement) ;
4374 }
4475
4576 fn modify_statement ( & self , change : & ChangedStatement ) {
0 commit comments