-
Notifications
You must be signed in to change notification settings - Fork 104
feat: basic hover #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: basic hover #463
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
768f92b
intermeidate
juleswritescode 4d9d35c
add some formatting
juleswritescode 50d789d
so far
juleswritescode a80ba4c
ok
juleswritescode d50db86
some more refatorings
juleswritescode 70cccba
ok
juleswritescode b8367c5
almost there
juleswritescode 563cdcf
another...
juleswritescode bfc2404
wowa wiwa
juleswritescode fee1ccf
ok
juleswritescode 78b8152
renamed it
juleswritescode 64910d8
Merge branch 'refactor/extract-ts-context' of https://github.com/supa...
juleswritescode 986eb29
remove schema caceh
juleswritescode f27cde5
Merge branch 'refactor/extract-ts-context' of https://github.com/supa...
juleswritescode 97f42fc
ok?
juleswritescode a8c7051
wowa wiwa
juleswritescode a065011
just ready
juleswritescode 25d7d31
ok?
juleswritescode dbc3c7d
fixed conflicts
juleswritescode 606bd40
ok?
juleswritescode 07e56b1
remove libpg again
juleswritescode 01e7476
no underscore
juleswritescode ff58d26
thats not a warning
juleswritescode 2dc68ba
Merge branch 'main' into feat/on-hover
juleswritescode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
Cargo.lock
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
crates/pgt_hover/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
authors.workspace = true | ||
categories.workspace = true | ||
description = "<DESCRIPTION>" | ||
edition.workspace = true | ||
homepage.workspace = true | ||
keywords.workspace = true | ||
license.workspace = true | ||
name = "pgt_hover" | ||
repository.workspace = true | ||
version = "0.0.0" | ||
|
||
|
||
[dependencies] | ||
humansize = { version = "2.1.3" } | ||
pgt_query.workspace = true | ||
pgt_schema_cache.workspace = true | ||
pgt_text_size.workspace = true | ||
pgt_treesitter.workspace = true | ||
schemars = { workspace = true, optional = true } | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json = { workspace = true } | ||
sqlx.workspace = true | ||
tokio = { version = "1.41.1", features = ["full"] } | ||
tracing = { workspace = true } | ||
tree-sitter.workspace = true | ||
tree_sitter_sql.workspace = true | ||
|
||
[dev-dependencies] | ||
pgt_test_utils.workspace = true | ||
|
||
[lib] | ||
doctest = false | ||
|
||
[features] | ||
schema = ["dep:schemars"] |
50 changes: 50 additions & 0 deletions
crates/pgt_hover/src/hovered_node.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use pgt_text_size::TextSize; | ||
use pgt_treesitter::TreeSitterContextParams; | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum NodeIdentification { | ||
Name(String), | ||
SchemaAndName((String, String)), | ||
#[allow(unused)] | ||
SchemaAndTableAndName((String, String, String)), | ||
} | ||
|
||
#[allow(unused)] | ||
#[derive(Debug)] | ||
pub(crate) enum HoveredNode { | ||
Schema(NodeIdentification), | ||
Table(NodeIdentification), | ||
Function(NodeIdentification), | ||
Column(NodeIdentification), | ||
Policy(NodeIdentification), | ||
Trigger(NodeIdentification), | ||
Role(NodeIdentification), | ||
} | ||
|
||
impl HoveredNode { | ||
pub(crate) fn get(position: TextSize, text: &str, tree: &tree_sitter::Tree) -> Option<Self> { | ||
let ctx = pgt_treesitter::context::TreesitterContext::new(TreeSitterContextParams { | ||
position, | ||
text, | ||
tree, | ||
}); | ||
|
||
let node_content = ctx.get_node_under_cursor_content()?; | ||
|
||
let under_node = ctx.node_under_cursor.as_ref()?; | ||
|
||
match under_node.kind() { | ||
"identifier" if ctx.parent_matches_one_of_kind(&["object_reference", "relation"]) => { | ||
if let Some(schema) = ctx.schema_or_alias_name { | ||
Some(HoveredNode::Table(NodeIdentification::SchemaAndName(( | ||
schema, | ||
node_content, | ||
)))) | ||
} else { | ||
Some(HoveredNode::Table(NodeIdentification::Name(node_content))) | ||
} | ||
} | ||
_ => None, | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
crates/pgt_hover/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use pgt_schema_cache::SchemaCache; | ||
use pgt_text_size::TextSize; | ||
|
||
use crate::{hovered_node::HoveredNode, to_markdown::ToHoverMarkdown}; | ||
|
||
mod hovered_node; | ||
mod to_markdown; | ||
|
||
pub struct OnHoverParams<'a> { | ||
pub position: TextSize, | ||
pub schema_cache: &'a SchemaCache, | ||
pub stmt_sql: &'a str, | ||
pub ast: Option<&'a pgt_query::NodeEnum>, | ||
pub ts_tree: &'a tree_sitter::Tree, | ||
} | ||
|
||
pub fn on_hover(params: OnHoverParams) -> Vec<String> { | ||
if let Some(hovered_node) = HoveredNode::get(params.position, params.stmt_sql, params.ts_tree) { | ||
match hovered_node { | ||
HoveredNode::Table(node_identification) => { | ||
let table = match node_identification { | ||
hovered_node::NodeIdentification::Name(n) => { | ||
params.schema_cache.find_table(n.as_str(), None) | ||
} | ||
hovered_node::NodeIdentification::SchemaAndName((s, n)) => { | ||
params.schema_cache.find_table(n.as_str(), Some(s.as_str())) | ||
} | ||
hovered_node::NodeIdentification::SchemaAndTableAndName(_) => None, | ||
}; | ||
|
||
table | ||
.map(|t| { | ||
let mut markdown = String::new(); | ||
match t.to_hover_markdown(&mut markdown) { | ||
Ok(_) => vec![markdown], | ||
Err(_) => vec![], | ||
} | ||
}) | ||
.unwrap_or(vec![]) | ||
} | ||
|
||
_ => todo!(), | ||
} | ||
} else { | ||
Default::default() | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
crates/pgt_hover/src/to_markdown.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::fmt::Write; | ||
|
||
use humansize::DECIMAL; | ||
|
||
pub(crate) trait ToHoverMarkdown { | ||
fn to_hover_markdown<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error>; | ||
} | ||
|
||
impl ToHoverMarkdown for pgt_schema_cache::Table { | ||
fn to_hover_markdown<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> { | ||
HeadlineWriter::for_table(writer, self)?; | ||
BodyWriter::for_table(writer, self)?; | ||
FooterWriter::for_table(writer, self)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
struct HeadlineWriter; | ||
|
||
impl HeadlineWriter { | ||
fn for_table<W: Write>( | ||
writer: &mut W, | ||
table: &pgt_schema_cache::Table, | ||
) -> Result<(), std::fmt::Error> { | ||
let table_kind = match table.table_kind { | ||
pgt_schema_cache::TableKind::View => " (View)", | ||
pgt_schema_cache::TableKind::MaterializedView => " (M.View)", | ||
pgt_schema_cache::TableKind::Partitioned => " (Partitioned)", | ||
pgt_schema_cache::TableKind::Ordinary => "", | ||
}; | ||
|
||
let locked_txt = if table.rls_enabled { | ||
" - 🔒 RLS enabled" | ||
} else { | ||
" - 🔓 RLS disabled" | ||
}; | ||
|
||
write!( | ||
writer, | ||
"### {}.{}{}{}", | ||
table.schema, table.name, table_kind, locked_txt | ||
)?; | ||
|
||
markdown_newline(writer)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
struct BodyWriter; | ||
|
||
impl BodyWriter { | ||
fn for_table<W: Write>( | ||
writer: &mut W, | ||
table: &pgt_schema_cache::Table, | ||
) -> Result<(), std::fmt::Error> { | ||
if let Some(c) = table.comment.as_ref() { | ||
write!(writer, "{}", c)?; | ||
markdown_newline(writer)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
struct FooterWriter; | ||
|
||
impl FooterWriter { | ||
fn for_table<W: Write>( | ||
writer: &mut W, | ||
table: &pgt_schema_cache::Table, | ||
) -> Result<(), std::fmt::Error> { | ||
write!( | ||
writer, | ||
"~{} rows, ~{} dead rows, {}", | ||
table.live_rows_estimate, | ||
table.dead_rows_estimate, | ||
humansize::format_size(table.bytes as u64, DECIMAL) | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn markdown_newline<W: Write>(writer: &mut W) -> Result<(), std::fmt::Error> { | ||
write!(writer, " ")?; | ||
writeln!(writer)?; | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
crates/pgt_lsp/src/handlers.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod code_actions; | ||
pub(crate) mod completions; | ||
pub(crate) mod hover; | ||
pub(crate) mod text_document; |
42 changes: 42 additions & 0 deletions
crates/pgt_lsp/src/handlers/hover.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use pgt_workspace::{WorkspaceError, features::on_hover::OnHoverParams}; | ||
use tower_lsp::lsp_types::{self, MarkedString, MarkupContent}; | ||
|
||
use crate::{adapters::get_cursor_position, diagnostics::LspError, session::Session}; | ||
|
||
pub(crate) fn on_hover( | ||
session: &Session, | ||
params: lsp_types::HoverParams, | ||
) -> Result<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)?; | ||
|
||
match session.workspace.on_hover(OnHoverParams { | ||
path, | ||
position: get_cursor_position(session, &url, position)?, | ||
}) { | ||
Ok(result) => { | ||
tracing::debug!("Found hover items: {:#?}", result); | ||
|
||
Ok(lsp_types::HoverContents::Array( | ||
result | ||
.into_iter() | ||
.map(MarkedString::from_markdown) | ||
.collect(), | ||
)) | ||
} | ||
|
||
Err(e) => match e { | ||
WorkspaceError::DatabaseConnectionError(_) => { | ||
Ok(lsp_types::HoverContents::Markup(MarkupContent { | ||
kind: lsp_types::MarkupKind::PlainText, | ||
value: "Cannot connect to database.".into(), | ||
})) | ||
} | ||
_ => { | ||
tracing::error!("Received an error: {:#?}", e); | ||
Err(e.into()) | ||
} | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.