Skip to content

Commit 41f3eb8

Browse files
committed
woooorks now and its much simpler
1 parent 001318d commit 41f3eb8

File tree

9 files changed

+473
-585
lines changed

9 files changed

+473
-585
lines changed

crates/pg_completions/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ async-std = "1.12.0"
1616

1717
text-size.workspace = true
1818

19+
pg_schema_cache.workspace = true
1920
serde = { workspace = true, features = ["derive"] }
2021
serde_json = { workspace = true }
21-
pg_schema_cache.workspace = true
2222
tree-sitter.workspace = true
2323
tree_sitter_sql.workspace = true
2424

crates/pg_lsp_new/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ anyhow = { workspace = true }
1616
biome_deserialize = { workspace = true }
1717
futures = "0.3.31"
1818
pg_analyse = { workspace = true }
19+
pg_completions = { workspace = true }
1920
pg_configuration = { workspace = true }
2021
pg_console = { workspace = true }
2122
pg_diagnostics = { workspace = true }
22-
pg_completions = { workspace = true }
2323
pg_fs = { workspace = true }
2424
pg_lsp_converters = { workspace = true }
2525
pg_text_edit = { workspace = true }

crates/pg_workspace_new/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ futures = "0.3.31"
1818
ignore = { workspace = true }
1919
pg_analyse = { workspace = true, features = ["serde"] }
2020
pg_analyser = { workspace = true }
21+
pg_completions = { workspace = true }
2122
pg_configuration = { workspace = true }
2223
pg_console = { workspace = true }
2324
pg_diagnostics = { workspace = true }
24-
pg_completions = { workspace = true }
2525
pg_fs = { workspace = true, features = ["serde"] }
2626
pg_query_ext = { workspace = true }
2727
pg_schema_cache = { workspace = true }

crates/pg_workspace_new/src/workspace/server.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fs, future::Future, panic::RefUnwindSafe, path::Path, sync::RwLock};
33
use analyser::AnalyserVisitorBuilder;
44
use change::StatementChange;
55
use dashmap::{DashMap, DashSet};
6-
use document::{Document, StatementRef};
6+
use document::{Document, Statement};
77
use pg_analyse::{AnalyserOptions, AnalysisFilter};
88
use pg_analyser::{Analyser, AnalyserConfig, AnalyserContext};
99
use pg_diagnostics::{serde::Diagnostic as SDiagnostic, Diagnostic, DiagnosticExt, Severity};
@@ -12,7 +12,6 @@ use pg_query::PgQueryStore;
1212
use pg_schema_cache::SchemaCache;
1313
use sqlx::PgPool;
1414
use std::sync::LazyLock;
15-
use store::Store;
1615
use tokio::runtime::Runtime;
1716
use tracing::info;
1817
use tree_sitter::TreeSitterStore;
@@ -33,7 +32,6 @@ mod analyser;
3332
mod change;
3433
mod document;
3534
mod pg_query;
36-
mod store;
3735
mod tree_sitter;
3836

3937
/// Simple helper to manage the db connection and the associated connection string
@@ -78,7 +76,7 @@ pub(super) struct WorkspaceServer {
7876
pg_query: PgQueryStore,
7977

8078
/// Stores the statements that have changed since the last analysis
81-
changed_stmts: DashSet<StatementRef>,
79+
changed_stmts: DashSet<Statement>,
8280

8381
connection: RwLock<DbConnection>,
8482
}
@@ -220,10 +218,9 @@ impl Workspace for WorkspaceServer {
220218

221219
let doc = Document::new(params.path.clone(), params.content, params.version);
222220

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);
227224
});
228225

229226
self.documents.insert(params.path, doc);
@@ -238,8 +235,9 @@ impl Workspace for WorkspaceServer {
238235
.remove(&params.path)
239236
.ok_or_else(WorkspaceError::not_found)?;
240237

241-
for stmt in doc.statement_refs() {
238+
for stmt in doc.iter_statements() {
242239
self.tree_sitter.remove_statement(&stmt);
240+
self.pg_query.remove_statement(&stmt);
243241
}
244242

245243
Ok(())
@@ -260,12 +258,12 @@ impl Workspace for WorkspaceServer {
260258

261259
for c in &doc.apply_file_change(&params) {
262260
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);
267265

268-
self.changed_stmts.insert(s.ref_.to_owned());
266+
self.changed_stmts.insert(added.stmt.clone());
269267
}
270268
StatementChange::Deleted(s) => {
271269
tracing::info!("Deleting statement: {:?}", s);
@@ -279,8 +277,8 @@ impl Workspace for WorkspaceServer {
279277
self.tree_sitter.modify_statement(s);
280278
self.pg_query.modify_statement(s);
281279

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());
284282
}
285283
}
286284
}
@@ -339,13 +337,12 @@ impl Workspace for WorkspaceServer {
339337
});
340338

341339
let diagnostics: Vec<SDiagnostic> = doc
342-
.statement_refs_with_ranges()
343-
.iter()
340+
.iter_statements_with_range()
344341
.flat_map(|(stmt, r)| {
345342
let mut stmt_diagnostics = vec![];
346343

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);
349346
if let Some(ast) = ast {
350347
stmt_diagnostics.extend(
351348
analyser
@@ -413,20 +410,19 @@ impl Workspace for WorkspaceServer {
413410
&params.position
414411
);
415412

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+
{
417417
Some(s) => s,
418418
None => return Ok(pg_completions::CompletionResult::default()),
419419
};
420420

421421
// `offset` is the position in the document,
422422
// 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.");
426423
let position = params.position - stmt_range.start();
427424

428-
let tree = self.tree_sitter.load(&statement.ref_);
429-
let text = statement.text;
425+
let tree = self.tree_sitter.get_parse_tree(&statement);
430426

431427
tracing::debug!("Found the statement. We're looking for position {:?}. Statement Range {:?} to {:?}. Statement: {}", position, stmt_range.start(), stmt_range.end(), text);
432428

@@ -439,7 +435,7 @@ impl Workspace for WorkspaceServer {
439435
position,
440436
schema: &schema_cache,
441437
tree: tree.as_deref(),
442-
text,
438+
text: text.to_string(),
443439
});
444440

445441
Ok(result)

0 commit comments

Comments
 (0)