-
Notifications
You must be signed in to change notification settings - Fork 104
feat(hover): hover on schemas #514
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
base: fix/hover-quoted
Are you sure you want to change the base?
Changes from 2 commits
7037ce9
7a1f72e
df264f3
8972606
114b279
f22ab9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::fmt::Write; | ||
|
||
use pgt_schema_cache::Schema; | ||
use pgt_treesitter::TreesitterContext; | ||
|
||
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown}; | ||
|
||
impl ToHoverMarkdown for Schema { | ||
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> { | ||
write!(writer, "`{}` - owned by {}", self.name, self.owner)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> { | ||
if let Some(comment) = &self.comment { | ||
write!(writer, "Comment: '{}'", comment)?; | ||
writeln!(writer)?; | ||
writeln!(writer)?; | ||
} | ||
|
||
if !self.allowed_creators.is_empty() { | ||
write!(writer, "CREATE privileges:")?; | ||
writeln!(writer)?; | ||
|
||
for creator in &self.allowed_creators { | ||
write!(writer, "- {}", creator)?; | ||
writeln!(writer)?; | ||
} | ||
|
||
writeln!(writer)?; | ||
} | ||
|
||
if !self.allowed_users.is_empty() { | ||
write!(writer, "USAGE privileges:")?; | ||
writeln!(writer)?; | ||
|
||
for user in &self.allowed_users { | ||
write!(writer, "- {}", user)?; | ||
writeln!(writer)?; | ||
} | ||
|
||
writeln!(writer)?; | ||
} | ||
|
||
Ok(true) | ||
} | ||
|
||
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> { | ||
writeln!(writer)?; | ||
write!( | ||
writer, | ||
"~{}, {} tables, {} views, {} functions", | ||
self.total_size, self.table_count, self.view_count, self.function_count, | ||
)?; | ||
Ok(true) | ||
} | ||
} | ||
|
||
impl ContextualPriority for Schema { | ||
// there are no schemas with duplicate names. | ||
fn relevance_score(&self, _ctx: &TreesitterContext) -> f32 { | ||
0.0 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
source: crates/pgt_hover/tests/hover_integration_tests.rs | ||
expression: snapshot | ||
--- | ||
# Input | ||
```sql | ||
select * from auth.users; | ||
↑ hovered here | ||
``` | ||
|
||
# Hover Results | ||
### `auth` - owned by postgres | ||
```plain | ||
|
||
``` | ||
--- | ||
```plain | ||
|
||
~16 kB, 1 tables, 0 views, 0 functions | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,35 @@ | ||
select | ||
n.oid :: int8 as "id!", | ||
n.nspname as name, | ||
u.rolname as "owner!" | ||
u.rolname as "owner!", | ||
obj_description(n.oid, 'pg_namespace') as "comment", | ||
|
||
coalesce(( | ||
select array_agg(grantee::regrole::text) | ||
from aclexplode(n.nspacl) | ||
where privilege_type = 'USAGE' | ||
and grantee::regrole::text <> '' | ||
and grantee::regrole::text <> '-' | ||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i found some nameless roles in my local supabase database. Probably deleted?
{postgres=UC/postgres,anon=U/postgres,authenticated=U/postgres,service_role=U/postgres, |
||
), ARRAY[]::text[]) as "allowed_users!", | ||
|
||
coalesce(( | ||
select array_agg(grantee::regrole::text) | ||
from aclexplode(n.nspacl) | ||
where privilege_type = 'CREATE' | ||
and grantee::regrole::text <> '' | ||
and grantee::regrole::text <> '-' | ||
), ARRAY[]::text[]) as "allowed_creators!", | ||
|
||
(select count(*) from pg_class c where c.relnamespace = n.oid and c.relkind = 'r') as "table_count!", | ||
(select count(*) from pg_class c where c.relnamespace = n.oid and c.relkind = 'v') as "view_count!", | ||
(select count(*) from pg_proc p where p.pronamespace = n.oid) as "function_count!", | ||
|
||
coalesce( | ||
(select pg_size_pretty(sum(pg_total_relation_size(c.oid))) | ||
from pg_class c | ||
where c.relnamespace = n.oid and c.relkind in ('r', 'i', 'm')), | ||
'0 bytes' | ||
) as "total_size!" | ||
from | ||
pg_namespace n, | ||
pg_roles u | ||
|
Uh oh!
There was an error while loading. Please reload this page.