diff --git a/crates/pg-analysis/src/index.rs b/crates/pg-analysis/src/index.rs index ab7b067..f48fad6 100644 --- a/crates/pg-analysis/src/index.rs +++ b/crates/pg-analysis/src/index.rs @@ -55,8 +55,22 @@ impl WorkspaceIndex { // Remove old entries, then insert new ones. self.remove_file(uri); + self.index_by_name(&symbol_arcs); + self.definitions.insert(uri.to_string(), symbol_arcs); + self.references.insert(uri.to_string(), ref_arcs); + } + + /// Insert pre-built symbols (e.g., from database introspection). + /// Uses the provided URI to group them, allowing removal via `remove_file`. + pub fn load_symbols(&self, uri: &str, symbols: Vec) { + self.remove_file(uri); + let symbol_arcs: Vec> = symbols.into_iter().map(Arc::new).collect(); + self.index_by_name(&symbol_arcs); + self.definitions.insert(uri.to_string(), symbol_arcs); + } - for sym in &symbol_arcs { + fn index_by_name(&self, symbols: &[Arc]) { + for sym in symbols { let key = (sym.kind, sym.name.name.to_lowercase()); self.by_name.entry(key).or_default().push(Arc::clone(sym)); @@ -66,9 +80,6 @@ impl WorkspaceIndex { self.by_name.entry(child_key).or_default().push(child_arc); } } - - self.definitions.insert(uri.to_string(), symbol_arcs); - self.references.insert(uri.to_string(), ref_arcs); } /// Remove all entries for a file. diff --git a/crates/pg-lsp/Cargo.toml b/crates/pg-lsp/Cargo.toml index 4fd14bb..a1337d1 100644 --- a/crates/pg-lsp/Cargo.toml +++ b/crates/pg-lsp/Cargo.toml @@ -13,6 +13,7 @@ path = "src/main.rs" pg-parse = { path = "../pg-parse" } pg-analysis = { path = "../pg-analysis" } pg-format = { path = "../pg-format" } +pg-schema = { path = "../pg-schema" } tree-sitter.workspace = true tree-sitter-postgres.workspace = true tower-lsp.workspace = true @@ -23,4 +24,4 @@ serde.workspace = true serde_json.workspace = true tracing.workspace = true tracing-subscriber.workspace = true -clap = { version = "4", features = ["derive"] } +clap = { version = "4", features = ["derive", "env"] } diff --git a/crates/pg-lsp/src/main.rs b/crates/pg-lsp/src/main.rs index 384fa34..62c1eba 100644 --- a/crates/pg-lsp/src/main.rs +++ b/crates/pg-lsp/src/main.rs @@ -21,11 +21,23 @@ struct Cli { help = "Formatting style: river, mozilla, aweber, dbt, gitlab, kickstarter, mattmc3" )] format_style: Style, + + /// PostgreSQL connection URL for live schema introspection + #[arg( + long, + short = 'd', + env = "DATABASE_URL", + help = "e.g., postgres://user:pass@localhost/dbname" + )] + database_url: Option, } fn parse_style(s: &str) -> Result { - s.parse::