Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions crates/pgls_hover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct OnHoverParams<'a> {
text = params.stmt_sql,
position = params.position.to_string()
))]
pub fn on_hover(params: OnHoverParams) -> Vec<String> {
pub fn on_hover(params: OnHoverParams) -> Option<Vec<String>> {
let ctx = pgls_treesitter::context::TreesitterContext::new(TreeSitterContextParams {
position: params.position,
text: params.stmt_sql,
Expand Down Expand Up @@ -123,15 +123,47 @@ pub fn on_hover(params: OnHoverParams) -> Vec<String> {
.unwrap_or_default(),
},

_ => todo!(),
HoveredNode::Policy(_) | HoveredNode::Trigger(_) => return None,
};

prioritize_by_context(items, &ctx)
let markdown_blocks: Vec<String> = prioritize_by_context(items, &ctx)
.into_iter()
.map(|item| format_hover_markdown(&item, params.schema_cache))
.filter_map(Result::ok)
.collect()
.collect();

(!markdown_blocks.is_empty()).then_some(markdown_blocks)
} else {
Default::default()
None
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn hover_over_drop_policy_name_returns_none() {
let query =
r#"drop policy if exists "Av{}atar images are publicly readable" on storage.objects;"#;
let position = query.find("{}").unwrap();
let sql = query.replace("{}", "");

let mut parser = tree_sitter::Parser::new();
parser
.set_language(&pgls_treesitter_grammar::LANGUAGE.into())
.unwrap();
let tree = parser.parse(&sql, None).unwrap();
let schema_cache = SchemaCache::default();

let hover = on_hover(OnHoverParams {
position: TextSize::new(position as u32),
schema_cache: &schema_cache,
stmt_sql: &sql,
ast: None,
ts_tree: &tree,
});

assert!(hover.is_none());
}
}
3 changes: 2 additions & 1 deletion crates/pgls_hover/tests/hover_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async fn test_hover_at_cursor(name: &str, query: String, setup: Option<&str>, te
stmt_sql: &sql,
ast: ast.as_ref(),
ts_tree: &tree,
});
})
.unwrap_or_default();

let mut snapshot = String::new();
snapshot.push_str("# Input\n");
Expand Down
22 changes: 13 additions & 9 deletions crates/pgls_lsp/src/handlers/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{adapters::get_cursor_position, diagnostics::LspError, session::Sessi
pub(crate) fn on_hover(
session: &Session,
params: lsp_types::HoverParams,
) -> Result<lsp_types::HoverContents, LspError> {
) -> Result<Option<lsp_types::HoverContents>, LspError> {
let url = params.text_document_position_params.text_document.uri;
let position = params.text_document_position_params.position;
let path = session.file_path(&url)?;
Expand All @@ -19,20 +19,24 @@ pub(crate) fn on_hover(
Ok(result) => {
tracing::debug!("Found hover items: {:#?}", result);

Ok(lsp_types::HoverContents::Array(
result
.into_iter()
.map(MarkedString::from_markdown)
.collect(),
))
let hover_items: Vec<MarkedString> = result
.into_iter()
.map(MarkedString::from_markdown)
.collect();

if hover_items.is_empty() {
Ok(None)
} else {
Ok(Some(lsp_types::HoverContents::Array(hover_items)))
}
}

Err(e) => match e {
WorkspaceError::DatabaseConnectionError(_) => {
Ok(lsp_types::HoverContents::Markup(MarkupContent {
Ok(Some(lsp_types::HoverContents::Markup(MarkupContent {
kind: lsp_types::MarkupKind::PlainText,
value: "Cannot connect to database.".into(),
}))
})))
}
_ => {
tracing::error!("Received an error: {:#?}", e);
Expand Down
4 changes: 2 additions & 2 deletions crates/pgls_lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ impl LanguageServer for LSPServer {
#[tracing::instrument(level = "trace", skip_all)]
async fn hover(&self, params: HoverParams) -> LspResult<Option<Hover>> {
match handlers::hover::on_hover(&self.session, params) {
Ok(result) => LspResult::Ok(Some(Hover {
contents: result,
Ok(result) => LspResult::Ok(result.map(|contents| Hover {
contents,
range: None,
})),
Err(e) => LspResult::Err(into_lsp_error(e)),
Expand Down
3 changes: 2 additions & 1 deletion crates/pgls_workspace/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,8 @@ impl Workspace for WorkspaceServer {
ast: maybe_ast.as_ref(),
position: position_in_stmt,
stmt_sql: stmt_id.content(),
});
})
.unwrap_or_default();

Ok(OnHoverResult { markdown_blocks })
}
Expand Down
Loading