Skip to content

Commit 94077e8

Browse files
two to go
1 parent 557ad5b commit 94077e8

File tree

3 files changed

+80
-18
lines changed

3 files changed

+80
-18
lines changed

crates/pg_lsp/src/b_server.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ impl LanguageServer for Server {
248248
Ok(actions)
249249
}
250250

251+
async fn inlay_hint(&self, params: InlayHintParams) -> Result<Option<Vec<InlayHint>>> {
252+
let mut uri = params.text_document.uri;
253+
normalize_uri(&mut uri);
254+
255+
let path = file_path(&uri);
256+
let range = params.range;
257+
258+
let hints = self.session.get_inlay_hints(path, range).await;
259+
260+
Ok(hints)
261+
}
262+
251263
async fn execute_command(
252264
&self,
253265
params: ExecuteCommandParams,

crates/pg_lsp/src/db_connection.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ impl DbConnection {
2323
})
2424
}
2525

26-
/// TODO: this should simply take a `Command` type, and the individual
27-
/// enums should have their deps included (i.e. `ExecuteStatement(String)`)
28-
pub async fn run_stmt(&self, stmt: String) -> anyhow::Result<PgQueryResult> {
29-
let command = ExecuteStatementCommand::new(stmt);
30-
let pool = self.pool.clone();
31-
command.run(Some(pool)).await
32-
}
33-
3426
pub(crate) fn connected_to(&self, connection_string: &str) -> bool {
3527
connection_string == self.connection_string
3628
}

crates/pg_lsp/src/session.rs

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::{collections::HashSet, sync::Arc};
22

33
use pg_base_db::{Change, DocumentChange, PgLspPath};
4+
use pg_commands::ExecuteStatementCommand;
45
use pg_diagnostics::Diagnostic;
56
use pg_workspace::Workspace;
67
use tokio::sync::RwLock;
7-
use tower_lsp::lsp_types::{CodeAction, Range};
8+
use tower_lsp::lsp_types::{CodeAction, InlayHint, Range};
89

910
use crate::{db_connection::DbConnection, utils::line_index_ext::LineIndexExt};
1011

@@ -56,13 +57,14 @@ impl Session {
5657
Ok(())
5758
}
5859

60+
/// Runs the passed-in statement against the underlying database.
5961
pub async fn run_stmt(&self, stmt: String) -> anyhow::Result<u64> {
6062
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
6668
}
6769

6870
pub async fn on_file_closed(&self, path: PgLspPath) {
@@ -137,15 +139,17 @@ impl Session {
137139
range: Range,
138140
) -> Option<Vec<CodeAction>> {
139141
let ide = self.ide.read().await;
140-
let db = self.db.read().await;
141-
142142
let doc = ide.documents.get(&path);
143-
if doc.is_none() || db.is_none() {
143+
if doc.is_none() {
144144
return None;
145145
}
146146

147-
let doc = doc.unwrap();
147+
let db = self.db.read().await;
148+
if db.is_none() {
149+
return None;
150+
}
148151

152+
let doc = doc.unwrap();
149153
let range = doc.line_index.offset_lsp_range(range).unwrap();
150154

151155
// for now, we only provide `ExcecuteStatementCommand`s.
@@ -177,4 +181,58 @@ impl Session {
177181

178182
Some(actions)
179183
}
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+
}
180238
}

0 commit comments

Comments
 (0)