@@ -3,7 +3,7 @@ use std::{fs, future::Future, panic::RefUnwindSafe, path::Path, sync::RwLock};
3
3
use analyser:: AnalyserVisitorBuilder ;
4
4
use change:: StatementChange ;
5
5
use dashmap:: { DashMap , DashSet } ;
6
- use document:: { Document , StatementRef } ;
6
+ use document:: { Document , Statement } ;
7
7
use pg_analyse:: { AnalyserOptions , AnalysisFilter } ;
8
8
use pg_analyser:: { Analyser , AnalyserConfig , AnalyserContext } ;
9
9
use pg_diagnostics:: { serde:: Diagnostic as SDiagnostic , Diagnostic , DiagnosticExt , Severity } ;
@@ -12,7 +12,6 @@ use pg_query::PgQueryStore;
12
12
use pg_schema_cache:: SchemaCache ;
13
13
use sqlx:: PgPool ;
14
14
use std:: sync:: LazyLock ;
15
- use store:: Store ;
16
15
use tokio:: runtime:: Runtime ;
17
16
use tracing:: info;
18
17
use tree_sitter:: TreeSitterStore ;
@@ -33,7 +32,6 @@ mod analyser;
33
32
mod change;
34
33
mod document;
35
34
mod pg_query;
36
- mod store;
37
35
mod tree_sitter;
38
36
39
37
/// Simple helper to manage the db connection and the associated connection string
@@ -78,7 +76,7 @@ pub(super) struct WorkspaceServer {
78
76
pg_query : PgQueryStore ,
79
77
80
78
/// Stores the statements that have changed since the last analysis
81
- changed_stmts : DashSet < StatementRef > ,
79
+ changed_stmts : DashSet < Statement > ,
82
80
83
81
connection : RwLock < DbConnection > ,
84
82
}
@@ -220,10 +218,9 @@ impl Workspace for WorkspaceServer {
220
218
221
219
let doc = Document :: new ( params. path . clone ( ) , params. content , params. version ) ;
222
220
223
- doc. statements . iter ( ) . for_each ( |s| {
224
- let stmt = doc. statement ( s) ;
225
- self . tree_sitter . add_statement ( & stmt) ;
226
- self . pg_query . add_statement ( & stmt) ;
221
+ doc. iter_statements_with_text ( ) . for_each ( |( stmt, content) | {
222
+ self . tree_sitter . add_statement ( & stmt, content) ;
223
+ self . pg_query . add_statement ( & stmt, content) ;
227
224
} ) ;
228
225
229
226
self . documents . insert ( params. path , doc) ;
@@ -238,8 +235,9 @@ impl Workspace for WorkspaceServer {
238
235
. remove ( & params. path )
239
236
. ok_or_else ( WorkspaceError :: not_found) ?;
240
237
241
- for stmt in doc. statement_refs ( ) {
238
+ for stmt in doc. iter_statements ( ) {
242
239
self . tree_sitter . remove_statement ( & stmt) ;
240
+ self . pg_query . remove_statement ( & stmt) ;
243
241
}
244
242
245
243
Ok ( ( ) )
@@ -260,12 +258,12 @@ impl Workspace for WorkspaceServer {
260
258
261
259
for c in & doc. apply_file_change ( & params) {
262
260
match c {
263
- StatementChange :: Added ( s ) => {
264
- tracing:: info!( "Adding statement: {:?}" , s ) ;
265
- self . tree_sitter . add_statement ( s ) ;
266
- self . pg_query . add_statement ( s ) ;
261
+ StatementChange :: Added ( added ) => {
262
+ tracing:: info!( "Adding statement: {:?}" , added ) ;
263
+ self . tree_sitter . add_statement ( & added . stmt , & added . text ) ;
264
+ self . pg_query . add_statement ( & added . stmt , & added . text ) ;
267
265
268
- self . changed_stmts . insert ( s . ref_ . to_owned ( ) ) ;
266
+ self . changed_stmts . insert ( added . stmt . clone ( ) ) ;
269
267
}
270
268
StatementChange :: Deleted ( s) => {
271
269
tracing:: info!( "Deleting statement: {:?}" , s) ;
@@ -279,8 +277,8 @@ impl Workspace for WorkspaceServer {
279
277
self . tree_sitter . modify_statement ( s) ;
280
278
self . pg_query . modify_statement ( s) ;
281
279
282
- self . changed_stmts . remove ( & s. old . ref_ ) ;
283
- self . changed_stmts . insert ( s. new_ref . to_owned ( ) ) ;
280
+ self . changed_stmts . remove ( & s. old_stmt ) ;
281
+ self . changed_stmts . insert ( s. new_stmt . clone ( ) ) ;
284
282
}
285
283
}
286
284
}
@@ -339,13 +337,12 @@ impl Workspace for WorkspaceServer {
339
337
} ) ;
340
338
341
339
let diagnostics: Vec < SDiagnostic > = doc
342
- . statement_refs_with_ranges ( )
343
- . iter ( )
340
+ . iter_statements_with_range ( )
344
341
. flat_map ( |( stmt, r) | {
345
342
let mut stmt_diagnostics = vec ! [ ] ;
346
343
347
- stmt_diagnostics. extend ( self . pg_query . diagnostics ( stmt) ) ;
348
- let ast = self . pg_query . load ( stmt) ;
344
+ stmt_diagnostics. extend ( self . pg_query . get_diagnostics ( & stmt) ) ;
345
+ let ast = self . pg_query . get_ast ( & stmt) ;
349
346
if let Some ( ast) = ast {
350
347
stmt_diagnostics. extend (
351
348
analyser
@@ -413,20 +410,19 @@ impl Workspace for WorkspaceServer {
413
410
& params. position
414
411
) ;
415
412
416
- let statement = match doc. statement_at_offset ( & params. position ) {
413
+ let ( statement, stmt_range, text) = match doc
414
+ . iter_statements_with_text_and_range ( )
415
+ . find ( |( _, r, _) | r. contains ( params. position ) )
416
+ {
417
417
Some ( s) => s,
418
418
None => return Ok ( pg_completions:: CompletionResult :: default ( ) ) ,
419
419
} ;
420
420
421
421
// `offset` is the position in the document,
422
422
// but we need the position within the *statement*.
423
- let stmt_range = doc
424
- . statement_range ( & statement. ref_ )
425
- . expect ( "Range of statement should be defined." ) ;
426
423
let position = params. position - stmt_range. start ( ) ;
427
424
428
- let tree = self . tree_sitter . load ( & statement. ref_ ) ;
429
- let text = statement. text ;
425
+ let tree = self . tree_sitter . get_parse_tree ( & statement) ;
430
426
431
427
tracing:: debug!( "Found the statement. We're looking for position {:?}. Statement Range {:?} to {:?}. Statement: {}" , position, stmt_range. start( ) , stmt_range. end( ) , text) ;
432
428
@@ -439,7 +435,7 @@ impl Workspace for WorkspaceServer {
439
435
position,
440
436
schema : & schema_cache,
441
437
tree : tree. as_deref ( ) ,
442
- text,
438
+ text : text . to_string ( ) ,
443
439
} ) ;
444
440
445
441
Ok ( result)
0 commit comments