|
1 | 1 | use std::{collections::HashSet, sync::Arc};
|
2 | 2 |
|
3 | 3 | use pg_base_db::{Change, DocumentChange, PgLspPath};
|
| 4 | +use pg_commands::ExecuteStatementCommand; |
4 | 5 | use pg_diagnostics::Diagnostic;
|
5 | 6 | use pg_workspace::Workspace;
|
6 | 7 | use tokio::sync::RwLock;
|
7 |
| -use tower_lsp::lsp_types::{CodeAction, Range}; |
| 8 | +use tower_lsp::lsp_types::{CodeAction, InlayHint, Range}; |
8 | 9 |
|
9 | 10 | use crate::{db_connection::DbConnection, utils::line_index_ext::LineIndexExt};
|
10 | 11 |
|
@@ -56,13 +57,14 @@ impl Session {
|
56 | 57 | Ok(())
|
57 | 58 | }
|
58 | 59 |
|
| 60 | + /// Runs the passed-in statement against the underlying database. |
59 | 61 | pub async fn run_stmt(&self, stmt: String) -> anyhow::Result<u64> {
|
60 | 62 | let db = self.db.read().await;
|
61 |
| - db.as_ref() |
62 |
| - .expect("No Db Connection") |
63 |
| - .run_stmt(stmt) |
64 |
| - .await |
65 |
| - .map(|pg_query_result| pg_query_result.rows_affected()) |
| 63 | + let pool = db.map(|d| d.get_pool()); |
| 64 | + |
| 65 | + let cmd = ExecuteStatementCommand::new(stmt); |
| 66 | + |
| 67 | + cmd.run(pool).await |
66 | 68 | }
|
67 | 69 |
|
68 | 70 | pub async fn on_file_closed(&self, path: PgLspPath) {
|
@@ -137,15 +139,17 @@ impl Session {
|
137 | 139 | range: Range,
|
138 | 140 | ) -> Option<Vec<CodeAction>> {
|
139 | 141 | let ide = self.ide.read().await;
|
140 |
| - let db = self.db.read().await; |
141 |
| - |
142 | 142 | let doc = ide.documents.get(&path);
|
143 |
| - if doc.is_none() || db.is_none() { |
| 143 | + if doc.is_none() { |
144 | 144 | return None;
|
145 | 145 | }
|
146 | 146 |
|
147 |
| - let doc = doc.unwrap(); |
| 147 | + let db = self.db.read().await; |
| 148 | + if db.is_none() { |
| 149 | + return None; |
| 150 | + } |
148 | 151 |
|
| 152 | + let doc = doc.unwrap(); |
149 | 153 | let range = doc.line_index.offset_lsp_range(range).unwrap();
|
150 | 154 |
|
151 | 155 | // for now, we only provide `ExcecuteStatementCommand`s.
|
@@ -177,4 +181,58 @@ impl Session {
|
177 | 181 |
|
178 | 182 | Some(actions)
|
179 | 183 | }
|
| 184 | + |
| 185 | + pub async fn get_inlay_hints(&self, path: PgLspPath, range: Range) -> Option<Vec<InlayHint>> { |
| 186 | + let ide = self.ide.read().await; |
| 187 | + let doc = ide.documents.get(&path); |
| 188 | + if doc.is_none() { |
| 189 | + return None; |
| 190 | + } |
| 191 | + |
| 192 | + let doc = doc.unwrap(); |
| 193 | + let range = doc.line_index.offset_lsp_range(range).unwrap(); |
| 194 | + |
| 195 | + let schema_cache = ide.schema_cache.read().expect("Unable to get Schema Cache"); |
| 196 | + |
| 197 | + let hints = doc |
| 198 | + .statements_at_range(&range) |
| 199 | + .into_iter() |
| 200 | + .flat_map(|stmt| { |
| 201 | + ::pg_inlay_hints::inlay_hints(::pg_inlay_hints::InlayHintsParams { |
| 202 | + ast: ide.pg_query.ast(&stmt).as_ref().map(|x| x.as_ref()), |
| 203 | + enriched_ast: ide |
| 204 | + .pg_query |
| 205 | + .enriched_ast(&stmt) |
| 206 | + .as_ref() |
| 207 | + .map(|x| x.as_ref()), |
| 208 | + tree: ide.tree_sitter.tree(&stmt).as_ref().map(|x| x.as_ref()), |
| 209 | + cst: ide.pg_query.cst(&stmt).as_ref().map(|x| x.as_ref()), |
| 210 | + schema_cache: &schema_cache, |
| 211 | + }) |
| 212 | + }) |
| 213 | + .map(|hint| InlayHint { |
| 214 | + position: doc.line_index.line_col_lsp(hint.offset).unwrap(), |
| 215 | + label: match hint.content { |
| 216 | + pg_inlay_hints::InlayHintContent::FunctionArg(arg) => { |
| 217 | + InlayHintLabel::String(match arg.name { |
| 218 | + Some(name) => format!("{} ({})", name, arg.type_name), |
| 219 | + None => arg.type_name.clone(), |
| 220 | + }) |
| 221 | + } |
| 222 | + }, |
| 223 | + kind: match hint.content { |
| 224 | + pg_inlay_hints::InlayHintContent::FunctionArg(_) => { |
| 225 | + Some(InlayHintKind::PARAMETER) |
| 226 | + } |
| 227 | + }, |
| 228 | + text_edits: None, |
| 229 | + tooltip: None, |
| 230 | + padding_left: None, |
| 231 | + padding_right: None, |
| 232 | + data: None, |
| 233 | + }) |
| 234 | + .collect(); |
| 235 | + |
| 236 | + Some(hints) |
| 237 | + } |
180 | 238 | }
|
0 commit comments