Skip to content

Commit 8fcf50e

Browse files
use schema_cache
1 parent c3eb171 commit 8fcf50e

File tree

11 files changed

+70
-56
lines changed

11 files changed

+70
-56
lines changed

crates/pgt_hover/src/hoverables/column.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use std::fmt::Write;
22

3-
use pgt_schema_cache::Column;
3+
use pgt_schema_cache::{Column, SchemaCache};
44
use pgt_treesitter::TreesitterContext;
55

66
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
77

88
impl ToHoverMarkdown for pgt_schema_cache::Column {
9-
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
9+
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
1010
write!(
1111
writer,
1212
"`{}.{}.{}`",
1313
self.schema_name, self.table_name, self.name
1414
)
1515
}
1616

17-
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
17+
fn hover_body<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
1818
if let Some(comment) = &self.comment {
1919
write!(writer, "Comment: '{}'", comment)?;
2020
writeln!(writer)?;
@@ -46,7 +46,7 @@ impl ToHoverMarkdown for pgt_schema_cache::Column {
4646
Ok(true)
4747
}
4848

49-
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
49+
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
5050
if let Some(default) = &self.default_expr {
5151
writeln!(writer)?;
5252
write!(writer, "Default: {}", default)?;

crates/pgt_hover/src/hoverables/function.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt::Write;
22

3-
use pgt_schema_cache::Function;
3+
use pgt_schema_cache::{Function, SchemaCache};
44
use pgt_treesitter::TreesitterContext;
55

66
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
@@ -10,7 +10,7 @@ impl ToHoverMarkdown for Function {
1010
"sql"
1111
}
1212

13-
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
13+
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
1414
write!(writer, "`{}.{}", self.schema, self.name)?;
1515

1616
if let Some(args) = &self.argument_types {
@@ -28,7 +28,7 @@ impl ToHoverMarkdown for Function {
2828
Ok(())
2929
}
3030

31-
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
31+
fn hover_body<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
3232
let kind_text = match self.kind {
3333
pgt_schema_cache::ProcKind::Function => "Function",
3434
pgt_schema_cache::ProcKind::Procedure => "Procedure",
@@ -55,7 +55,7 @@ impl ToHoverMarkdown for Function {
5555
Ok(true)
5656
}
5757

58-
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
58+
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
5959
if let Some(def) = self.definition.as_ref() {
6060
/*
6161
* We don't want to show 250 lines of functions to the user.

crates/pgt_hover/src/hoverables/mod.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use pgt_schema_cache::SchemaCache;
2+
13
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
24

35
mod column;
@@ -70,36 +72,36 @@ impl ContextualPriority for Hoverable<'_> {
7072
}
7173

7274
impl ToHoverMarkdown for Hoverable<'_> {
73-
fn hover_headline<W: std::fmt::Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
75+
fn hover_headline<W: std::fmt::Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
7476
match self {
75-
Hoverable::Table(table) => ToHoverMarkdown::hover_headline(*table, writer),
76-
Hoverable::Column(column) => ToHoverMarkdown::hover_headline(*column, writer),
77-
Hoverable::Function(function) => ToHoverMarkdown::hover_headline(*function, writer),
78-
Hoverable::Role(role) => ToHoverMarkdown::hover_headline(*role, writer),
79-
Hoverable::Schema(schema) => ToHoverMarkdown::hover_headline(*schema, writer),
80-
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_headline(*type_, writer),
77+
Hoverable::Table(table) => ToHoverMarkdown::hover_headline(*table, writer, schema_cache),
78+
Hoverable::Column(column) => ToHoverMarkdown::hover_headline(*column, writer, schema_cache),
79+
Hoverable::Function(function) => ToHoverMarkdown::hover_headline(*function, writer, schema_cache),
80+
Hoverable::Role(role) => ToHoverMarkdown::hover_headline(*role, writer, schema_cache),
81+
Hoverable::Schema(schema) => ToHoverMarkdown::hover_headline(*schema, writer, schema_cache),
82+
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_headline(*type_, writer, schema_cache),
8183
}
8284
}
8385

84-
fn hover_body<W: std::fmt::Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
86+
fn hover_body<W: std::fmt::Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
8587
match self {
86-
Hoverable::Table(table) => ToHoverMarkdown::hover_body(*table, writer),
87-
Hoverable::Column(column) => ToHoverMarkdown::hover_body(*column, writer),
88-
Hoverable::Function(function) => ToHoverMarkdown::hover_body(*function, writer),
89-
Hoverable::Role(role) => ToHoverMarkdown::hover_body(*role, writer),
90-
Hoverable::Schema(schema) => ToHoverMarkdown::hover_body(*schema, writer),
91-
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_body(*type_, writer),
88+
Hoverable::Table(table) => ToHoverMarkdown::hover_body(*table, writer, schema_cache),
89+
Hoverable::Column(column) => ToHoverMarkdown::hover_body(*column, writer, schema_cache),
90+
Hoverable::Function(function) => ToHoverMarkdown::hover_body(*function, writer, schema_cache),
91+
Hoverable::Role(role) => ToHoverMarkdown::hover_body(*role, writer, schema_cache),
92+
Hoverable::Schema(schema) => ToHoverMarkdown::hover_body(*schema, writer, schema_cache),
93+
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_body(*type_, writer, schema_cache),
9294
}
9395
}
9496

95-
fn hover_footer<W: std::fmt::Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
97+
fn hover_footer<W: std::fmt::Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
9698
match self {
97-
Hoverable::Table(table) => ToHoverMarkdown::hover_footer(*table, writer),
98-
Hoverable::Column(column) => ToHoverMarkdown::hover_footer(*column, writer),
99-
Hoverable::Function(function) => ToHoverMarkdown::hover_footer(*function, writer),
100-
Hoverable::Role(role) => ToHoverMarkdown::hover_footer(*role, writer),
101-
Hoverable::Schema(schema) => ToHoverMarkdown::hover_footer(*schema, writer),
102-
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_footer(*type_, writer),
99+
Hoverable::Table(table) => ToHoverMarkdown::hover_footer(*table, writer, schema_cache),
100+
Hoverable::Column(column) => ToHoverMarkdown::hover_footer(*column, writer, schema_cache),
101+
Hoverable::Function(function) => ToHoverMarkdown::hover_footer(*function, writer, schema_cache),
102+
Hoverable::Role(role) => ToHoverMarkdown::hover_footer(*role, writer, schema_cache),
103+
Hoverable::Schema(schema) => ToHoverMarkdown::hover_footer(*schema, writer, schema_cache),
104+
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_footer(*type_, writer, schema_cache),
103105
}
104106
}
105107

crates/pgt_hover/src/hoverables/postgres_type.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
use std::fmt::Write;
22

3-
use pgt_schema_cache::PostgresType;
3+
use pgt_schema_cache::{PostgresType, SchemaCache};
44
use pgt_treesitter::TreesitterContext;
55

66
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
77

88
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)?;
9+
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
10+
write!(writer, "`{}.{}` (Custom Type)", self.schema, self.name)?;
1111
Ok(())
1212
}
1313

14-
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
14+
fn hover_body<W: Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
1515
if let Some(comment) = &self.comment {
1616
write!(writer, "Comment: '{}'", comment)?;
1717
writeln!(writer)?;
@@ -23,8 +23,14 @@ impl ToHoverMarkdown for PostgresType {
2323
writeln!(writer)?;
2424

2525
for attribute in &self.attributes.attrs {
26-
// TODO: look up other types with schema cache.
2726
write!(writer, "- {}", attribute.name)?;
27+
28+
if let Some(type_info) = schema_cache.find_type_by_id(attribute.type_id) {
29+
write!(writer, ": {}.{}", type_info.schema, type_info.name)?;
30+
} else {
31+
write!(writer, " (type_id: {})", attribute.type_id)?;
32+
}
33+
2834
writeln!(writer)?;
2935
}
3036

@@ -36,7 +42,6 @@ impl ToHoverMarkdown for PostgresType {
3642
writeln!(writer)?;
3743

3844
for kind in &self.enums.values {
39-
// TODO: look up other types with schema cache.
4045
write!(writer, "- {}", kind)?;
4146
writeln!(writer)?;
4247
}
@@ -47,7 +52,7 @@ impl ToHoverMarkdown for PostgresType {
4752
Ok(true)
4853
}
4954

50-
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
55+
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
5156
writeln!(writer)?;
5257
Ok(true)
5358
}

crates/pgt_hover/src/hoverables/role.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use std::fmt::Write;
22

3-
use pgt_schema_cache::Role;
3+
use pgt_schema_cache::{Role, SchemaCache};
44
use pgt_treesitter::TreesitterContext;
55

66
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
77

88
impl ToHoverMarkdown for pgt_schema_cache::Role {
9-
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
9+
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
1010
write!(writer, "`{}`", self.name)?;
1111

1212
Ok(())
1313
}
1414

15-
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
15+
fn hover_body<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
1616
if let Some(comm) = self.comment.as_ref() {
1717
write!(writer, "Comment: '{}'", comm)?;
1818
writeln!(writer)?;
@@ -81,7 +81,7 @@ impl ToHoverMarkdown for pgt_schema_cache::Role {
8181
Ok(true)
8282
}
8383

84-
fn hover_footer<W: Write>(&self, _writer: &mut W) -> Result<bool, std::fmt::Error> {
84+
fn hover_footer<W: Write>(&self, _writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
8585
Ok(false)
8686
}
8787
}

crates/pgt_hover/src/hoverables/schema.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use std::fmt::Write;
22

3-
use pgt_schema_cache::Schema;
3+
use pgt_schema_cache::{Schema, SchemaCache};
44
use pgt_treesitter::TreesitterContext;
55

66
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
77

88
impl ToHoverMarkdown for Schema {
9-
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
9+
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
1010
write!(writer, "`{}` - owned by {}", self.name, self.owner)?;
1111

1212
Ok(())
1313
}
1414

15-
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
15+
fn hover_body<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
1616
if let Some(comment) = &self.comment {
1717
write!(writer, "Comment: '{}'", comment)?;
1818
writeln!(writer)?;
@@ -46,7 +46,7 @@ impl ToHoverMarkdown for Schema {
4646
Ok(true)
4747
}
4848

49-
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
49+
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
5050
writeln!(writer)?;
5151
write!(
5252
writer,

crates/pgt_hover/src/hoverables/table.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::fmt::Write;
22

33
use humansize::DECIMAL;
4-
use pgt_schema_cache::Table;
4+
use pgt_schema_cache::{SchemaCache, Table};
55
use pgt_treesitter::TreesitterContext;
66

77
use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
88

99
impl ToHoverMarkdown for Table {
10-
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
10+
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
1111
write!(writer, "`{}.{}`", self.schema, self.name)?;
1212

1313
let table_kind = match self.table_kind {
@@ -30,7 +30,7 @@ impl ToHoverMarkdown for Table {
3030
Ok(())
3131
}
3232

33-
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
33+
fn hover_body<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
3434
if let Some(comment) = &self.comment {
3535
write!(writer, "Comment: '{}'", comment)?;
3636
writeln!(writer)?;
@@ -40,7 +40,7 @@ impl ToHoverMarkdown for Table {
4040
}
4141
}
4242

43-
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
43+
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
4444
writeln!(writer)?;
4545
write!(
4646
writer,

crates/pgt_hover/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn on_hover(params: OnHoverParams) -> Vec<String> {
140140

141141
prioritize_by_context(items, &ctx)
142142
.into_iter()
143-
.map(|item| format_hover_markdown(&item))
143+
.map(|item| format_hover_markdown(&item, params.schema_cache))
144144
.filter_map(Result::ok)
145145
.collect()
146146
} else {

crates/pgt_hover/src/to_markdown.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::fmt::Write;
22

3+
use pgt_schema_cache::SchemaCache;
4+
35
pub(crate) trait ToHoverMarkdown {
46
fn body_markdown_type(&self) -> &'static str {
57
"plain"
@@ -9,25 +11,26 @@ pub(crate) trait ToHoverMarkdown {
911
"plain"
1012
}
1113

12-
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error>;
14+
fn hover_headline<W: Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<(), std::fmt::Error>;
1315

14-
fn hover_body<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error>; // returns true if something was written
16+
fn hover_body<W: Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error>; // returns true if something was written
1517

16-
fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error>; // returns true if something was written
18+
fn hover_footer<W: Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error>; // returns true if something was written
1719
}
1820

1921
pub(crate) fn format_hover_markdown<T: ToHoverMarkdown>(
2022
item: &T,
23+
schema_cache: &SchemaCache,
2124
) -> Result<String, std::fmt::Error> {
2225
let mut markdown = String::new();
2326

2427
write!(markdown, "### ")?;
25-
item.hover_headline(&mut markdown)?;
28+
item.hover_headline(&mut markdown, schema_cache)?;
2629
markdown_newline(&mut markdown)?;
2730

2831
write!(markdown, "```{}", item.body_markdown_type())?;
2932
markdown_newline(&mut markdown)?;
30-
item.hover_body(&mut markdown)?;
33+
item.hover_body(&mut markdown, schema_cache)?;
3134
markdown_newline(&mut markdown)?;
3235
write!(markdown, "```")?;
3336

@@ -37,7 +40,7 @@ pub(crate) fn format_hover_markdown<T: ToHoverMarkdown>(
3740

3841
write!(markdown, "```{}", item.footer_markdown_type())?;
3942
markdown_newline(&mut markdown)?;
40-
item.hover_footer(&mut markdown)?;
43+
item.hover_footer(&mut markdown, schema_cache)?;
4144
markdown_newline(&mut markdown)?;
4245
write!(markdown, "```")?;
4346

crates/pgt_schema_cache/src/schema_cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ impl SchemaCache {
9898
})
9999
}
100100

101+
pub fn find_type_by_id(&self, id: i64) -> Option<&PostgresType> {
102+
self.types.iter().find(|t| t.id == id)
103+
}
104+
101105
pub fn find_cols(&self, name: &str, table: Option<&str>, schema: Option<&str>) -> Vec<&Column> {
102106
let sanitized_name = Self::sanitize_identifier(name);
103107
self.columns

0 commit comments

Comments
 (0)