Skip to content

Commit c3eb171

Browse files
so far!
1 parent 346ea73 commit c3eb171

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

crates/pgt_hover/src/hoverables/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdow
22

33
mod column;
44
mod function;
5+
mod postgres_type;
56
mod role;
67
mod schema;
78
mod table;
@@ -16,6 +17,7 @@ pub enum Hoverable<'a> {
1617
Function(&'a pgt_schema_cache::Function),
1718
Role(&'a pgt_schema_cache::Role),
1819
Schema(&'a pgt_schema_cache::Schema),
20+
PostgresType(&'a pgt_schema_cache::PostgresType),
1921
}
2022

2123
impl<'a> From<&'a pgt_schema_cache::Schema> for Hoverable<'a> {
@@ -48,6 +50,12 @@ impl<'a> From<&'a pgt_schema_cache::Role> for Hoverable<'a> {
4850
}
4951
}
5052

53+
impl<'a> From<&'a pgt_schema_cache::PostgresType> for Hoverable<'a> {
54+
fn from(value: &'a pgt_schema_cache::PostgresType) -> Self {
55+
Hoverable::PostgresType(value)
56+
}
57+
}
58+
5159
impl ContextualPriority for Hoverable<'_> {
5260
fn relevance_score(&self, ctx: &pgt_treesitter::TreesitterContext) -> f32 {
5361
match self {
@@ -56,6 +64,7 @@ impl ContextualPriority for Hoverable<'_> {
5664
Hoverable::Function(function) => function.relevance_score(ctx),
5765
Hoverable::Role(role) => role.relevance_score(ctx),
5866
Hoverable::Schema(schema) => schema.relevance_score(ctx),
67+
Hoverable::PostgresType(type_) => type_.relevance_score(ctx),
5968
}
6069
}
6170
}
@@ -68,6 +77,7 @@ impl ToHoverMarkdown for Hoverable<'_> {
6877
Hoverable::Function(function) => ToHoverMarkdown::hover_headline(*function, writer),
6978
Hoverable::Role(role) => ToHoverMarkdown::hover_headline(*role, writer),
7079
Hoverable::Schema(schema) => ToHoverMarkdown::hover_headline(*schema, writer),
80+
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_headline(*type_, writer),
7181
}
7282
}
7383

@@ -78,6 +88,7 @@ impl ToHoverMarkdown for Hoverable<'_> {
7888
Hoverable::Function(function) => ToHoverMarkdown::hover_body(*function, writer),
7989
Hoverable::Role(role) => ToHoverMarkdown::hover_body(*role, writer),
8090
Hoverable::Schema(schema) => ToHoverMarkdown::hover_body(*schema, writer),
91+
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_body(*type_, writer),
8192
}
8293
}
8394

@@ -88,6 +99,7 @@ impl ToHoverMarkdown for Hoverable<'_> {
8899
Hoverable::Function(function) => ToHoverMarkdown::hover_footer(*function, writer),
89100
Hoverable::Role(role) => ToHoverMarkdown::hover_footer(*role, writer),
90101
Hoverable::Schema(schema) => ToHoverMarkdown::hover_footer(*schema, writer),
102+
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_footer(*type_, writer),
91103
}
92104
}
93105

@@ -98,6 +110,7 @@ impl ToHoverMarkdown for Hoverable<'_> {
98110
Hoverable::Function(function) => function.body_markdown_type(),
99111
Hoverable::Role(role) => role.body_markdown_type(),
100112
Hoverable::Schema(schema) => schema.body_markdown_type(),
113+
Hoverable::PostgresType(type_) => type_.body_markdown_type(),
101114
}
102115
}
103116

@@ -108,6 +121,7 @@ impl ToHoverMarkdown for Hoverable<'_> {
108121
Hoverable::Function(function) => function.footer_markdown_type(),
109122
Hoverable::Role(role) => role.footer_markdown_type(),
110123
Hoverable::Schema(schema) => schema.footer_markdown_type(),
124+
Hoverable::PostgresType(type_) => type_.footer_markdown_type(),
111125
}
112126
}
113127
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use std::fmt::Write;
2+
3+
use pgt_schema_cache::PostgresType;
4+
use pgt_treesitter::TreesitterContext;
5+
6+
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
7+
8+
impl ToHoverMarkdown for PostgresType {
9+
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
10+
write!(writer, "`{}.{}`", self.schema, self.name)?;
11+
Ok(())
12+
}
13+
14+
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
15+
if let Some(comment) = &self.comment {
16+
write!(writer, "Comment: '{}'", comment)?;
17+
writeln!(writer)?;
18+
writeln!(writer)?;
19+
}
20+
21+
if !self.attributes.attrs.is_empty() {
22+
write!(writer, "Attributes:")?;
23+
writeln!(writer)?;
24+
25+
for attribute in &self.attributes.attrs {
26+
// TODO: look up other types with schema cache.
27+
write!(writer, "- {}", attribute.name)?;
28+
writeln!(writer)?;
29+
}
30+
31+
writeln!(writer)?;
32+
}
33+
34+
if !self.enums.values.is_empty() {
35+
write!(writer, "Enum Permutations:")?;
36+
writeln!(writer)?;
37+
38+
for kind in &self.enums.values {
39+
// TODO: look up other types with schema cache.
40+
write!(writer, "- {}", kind)?;
41+
writeln!(writer)?;
42+
}
43+
44+
writeln!(writer)?;
45+
}
46+
47+
Ok(true)
48+
}
49+
50+
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
51+
writeln!(writer)?;
52+
Ok(true)
53+
}
54+
}
55+
56+
impl ContextualPriority for PostgresType {
57+
// there are no schemas with duplicate names.
58+
fn relevance_score(&self, ctx: &TreesitterContext) -> f32 {
59+
let mut score = 0.0;
60+
61+
if ctx
62+
.get_mentioned_relations(&Some(self.schema.clone()))
63+
.is_some()
64+
{
65+
score += 100.0;
66+
}
67+
68+
if ctx.get_mentioned_relations(&None).is_some() && self.schema == "public" {
69+
score += 100.0;
70+
}
71+
72+
if self.schema == "public" && score == 0.0 {
73+
score += 10.0;
74+
}
75+
76+
score
77+
}
78+
}

crates/pgt_hover/src/hovered_node.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) enum HoveredNode {
1818
Policy(NodeIdentification),
1919
Trigger(NodeIdentification),
2020
Role(NodeIdentification),
21+
PostgresType(NodeIdentification),
2122
}
2223

2324
impl HoveredNode {
@@ -74,6 +75,7 @@ impl HoveredNode {
7475
Some(HoveredNode::Column(NodeIdentification::Name(node_content)))
7576
}
7677
}
78+
7779
"identifier" if ctx.matches_ancestor_history(&["invocation", "object_reference"]) => {
7880
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
7981
Some(HoveredNode::Function(NodeIdentification::SchemaAndName((
@@ -86,10 +88,26 @@ impl HoveredNode {
8688
)))
8789
}
8890
}
91+
8992
"identifier" if ctx.matches_one_of_ancestors(&["alter_role", "policy_to_role"]) => {
9093
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
9194
}
9295

96+
"identifier"
97+
if ctx.matches_ancestor_history(&["type", "object_reference"])
98+
&& ctx.node_under_cursor_is_within_field_name("custom_type") =>
99+
{
100+
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
101+
Some(HoveredNode::PostgresType(
102+
NodeIdentification::SchemaAndName((schema.clone(), node_content)),
103+
))
104+
} else {
105+
Some(HoveredNode::PostgresType(NodeIdentification::Name(
106+
node_content,
107+
)))
108+
}
109+
}
110+
93111
"revoke_role" | "grant_role" | "policy_role" => {
94112
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
95113
}

crates/pgt_hover/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ pub fn on_hover(params: OnHoverParams) -> Vec<String> {
117117
_ => vec![],
118118
},
119119

120+
HoveredNode::PostgresType(node_identification) => match node_identification {
121+
hovered_node::NodeIdentification::Name(type_name) => params
122+
.schema_cache
123+
.find_type(&type_name, None)
124+
.map(Hoverable::from)
125+
.map(|s| vec![s])
126+
.unwrap_or_default(),
127+
128+
hovered_node::NodeIdentification::SchemaAndName((schema, type_name)) => params
129+
.schema_cache
130+
.find_type(&type_name, Some(schema.as_str()))
131+
.map(Hoverable::from)
132+
.map(|s| vec![s])
133+
.unwrap_or_default(),
134+
135+
_ => vec![],
136+
},
137+
120138
_ => todo!(),
121139
};
122140

0 commit comments

Comments
 (0)