Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion crates/pgt_completions/src/relevance/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl CompletionFilter<'_> {
// only autocomplete left side of binary expression
WrappingClause::Where => {
ctx.before_cursor_matches_kind(&["keyword_and", "keyword_where"])
|| (ctx.before_cursor_matches_kind(&["."])
|| (ctx.before_cursor_matches_kind(&["field_qualifier"])
&& ctx.matches_ancestor_history(&["field"]))
}

Expand Down
8 changes: 4 additions & 4 deletions crates/pgt_hover/src/hoverables/column.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::fmt::Write;

use pgt_schema_cache::Column;
use pgt_schema_cache::{Column, SchemaCache};
use pgt_treesitter::TreesitterContext;

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

impl ToHoverMarkdown for pgt_schema_cache::Column {
fn hover_headline<W: Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
write!(
writer,
"`{}.{}.{}`",
self.schema_name, self.table_name, self.name
)
}

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

fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
if let Some(default) = &self.default_expr {
writeln!(writer)?;
write!(writer, "Default: {}", default)?;
Expand Down
8 changes: 4 additions & 4 deletions crates/pgt_hover/src/hoverables/function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Write;

use pgt_schema_cache::Function;
use pgt_schema_cache::{Function, SchemaCache};
use pgt_treesitter::TreesitterContext;

use crate::{contextual_priority::ContextualPriority, to_markdown::ToHoverMarkdown};
Expand All @@ -10,7 +10,7 @@ impl ToHoverMarkdown for Function {
"sql"
}

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

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

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

fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
if let Some(def) = self.definition.as_ref() {
/*
* We don't want to show 250 lines of functions to the user.
Expand Down
52 changes: 34 additions & 18 deletions crates/pgt_hover/src/hoverables/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use pgt_schema_cache::SchemaCache;

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

mod column;
mod function;
mod postgres_type;
mod role;
mod schema;
mod table;
Expand All @@ -16,6 +19,7 @@ pub enum Hoverable<'a> {
Function(&'a pgt_schema_cache::Function),
Role(&'a pgt_schema_cache::Role),
Schema(&'a pgt_schema_cache::Schema),
PostgresType(&'a pgt_schema_cache::PostgresType),
}

impl<'a> From<&'a pgt_schema_cache::Schema> for Hoverable<'a> {
Expand Down Expand Up @@ -48,6 +52,12 @@ impl<'a> From<&'a pgt_schema_cache::Role> for Hoverable<'a> {
}
}

impl<'a> From<&'a pgt_schema_cache::PostgresType> for Hoverable<'a> {
fn from(value: &'a pgt_schema_cache::PostgresType) -> Self {
Hoverable::PostgresType(value)
}
}

impl ContextualPriority for Hoverable<'_> {
fn relevance_score(&self, ctx: &pgt_treesitter::TreesitterContext) -> f32 {
match self {
Expand All @@ -56,38 +66,42 @@ impl ContextualPriority for Hoverable<'_> {
Hoverable::Function(function) => function.relevance_score(ctx),
Hoverable::Role(role) => role.relevance_score(ctx),
Hoverable::Schema(schema) => schema.relevance_score(ctx),
Hoverable::PostgresType(type_) => type_.relevance_score(ctx),
}
}
}

impl ToHoverMarkdown for Hoverable<'_> {
fn hover_headline<W: std::fmt::Write>(&self, writer: &mut W) -> Result<(), std::fmt::Error> {
fn hover_headline<W: std::fmt::Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<(), std::fmt::Error> {
match self {
Hoverable::Table(table) => ToHoverMarkdown::hover_headline(*table, writer),
Hoverable::Column(column) => ToHoverMarkdown::hover_headline(*column, writer),
Hoverable::Function(function) => ToHoverMarkdown::hover_headline(*function, writer),
Hoverable::Role(role) => ToHoverMarkdown::hover_headline(*role, writer),
Hoverable::Schema(schema) => ToHoverMarkdown::hover_headline(*schema, writer),
Hoverable::Table(table) => ToHoverMarkdown::hover_headline(*table, writer, schema_cache),
Hoverable::Column(column) => ToHoverMarkdown::hover_headline(*column, writer, schema_cache),
Hoverable::Function(function) => ToHoverMarkdown::hover_headline(*function, writer, schema_cache),
Hoverable::Role(role) => ToHoverMarkdown::hover_headline(*role, writer, schema_cache),
Hoverable::Schema(schema) => ToHoverMarkdown::hover_headline(*schema, writer, schema_cache),
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_headline(*type_, writer, schema_cache),
}
}

fn hover_body<W: std::fmt::Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
fn hover_body<W: std::fmt::Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
match self {
Hoverable::Table(table) => ToHoverMarkdown::hover_body(*table, writer),
Hoverable::Column(column) => ToHoverMarkdown::hover_body(*column, writer),
Hoverable::Function(function) => ToHoverMarkdown::hover_body(*function, writer),
Hoverable::Role(role) => ToHoverMarkdown::hover_body(*role, writer),
Hoverable::Schema(schema) => ToHoverMarkdown::hover_body(*schema, writer),
Hoverable::Table(table) => ToHoverMarkdown::hover_body(*table, writer, schema_cache),
Hoverable::Column(column) => ToHoverMarkdown::hover_body(*column, writer, schema_cache),
Hoverable::Function(function) => ToHoverMarkdown::hover_body(*function, writer, schema_cache),
Hoverable::Role(role) => ToHoverMarkdown::hover_body(*role, writer, schema_cache),
Hoverable::Schema(schema) => ToHoverMarkdown::hover_body(*schema, writer, schema_cache),
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_body(*type_, writer, schema_cache),
}
}

fn hover_footer<W: std::fmt::Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
fn hover_footer<W: std::fmt::Write>(&self, writer: &mut W, schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
match self {
Hoverable::Table(table) => ToHoverMarkdown::hover_footer(*table, writer),
Hoverable::Column(column) => ToHoverMarkdown::hover_footer(*column, writer),
Hoverable::Function(function) => ToHoverMarkdown::hover_footer(*function, writer),
Hoverable::Role(role) => ToHoverMarkdown::hover_footer(*role, writer),
Hoverable::Schema(schema) => ToHoverMarkdown::hover_footer(*schema, writer),
Hoverable::Table(table) => ToHoverMarkdown::hover_footer(*table, writer, schema_cache),
Hoverable::Column(column) => ToHoverMarkdown::hover_footer(*column, writer, schema_cache),
Hoverable::Function(function) => ToHoverMarkdown::hover_footer(*function, writer, schema_cache),
Hoverable::Role(role) => ToHoverMarkdown::hover_footer(*role, writer, schema_cache),
Hoverable::Schema(schema) => ToHoverMarkdown::hover_footer(*schema, writer, schema_cache),
Hoverable::PostgresType(type_) => ToHoverMarkdown::hover_footer(*type_, writer, schema_cache),
}
}

Expand All @@ -98,6 +112,7 @@ impl ToHoverMarkdown for Hoverable<'_> {
Hoverable::Function(function) => function.body_markdown_type(),
Hoverable::Role(role) => role.body_markdown_type(),
Hoverable::Schema(schema) => schema.body_markdown_type(),
Hoverable::PostgresType(type_) => type_.body_markdown_type(),
}
}

Expand All @@ -108,6 +123,7 @@ impl ToHoverMarkdown for Hoverable<'_> {
Hoverable::Function(function) => function.footer_markdown_type(),
Hoverable::Role(role) => role.footer_markdown_type(),
Hoverable::Schema(schema) => schema.footer_markdown_type(),
Hoverable::PostgresType(type_) => type_.footer_markdown_type(),
}
}
}
101 changes: 101 additions & 0 deletions crates/pgt_hover/src/hoverables/postgres_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::fmt::Write;

use pgt_schema_cache::{PostgresType, SchemaCache};
use pgt_treesitter::TreesitterContext;

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

impl ToHoverMarkdown for PostgresType {
fn hover_headline<W: Write>(
&self,
writer: &mut W,
_schema_cache: &SchemaCache,
) -> Result<(), std::fmt::Error> {
write!(writer, "`{}.{}` (Custom Type)", self.schema, self.name)?;
Ok(())
}

fn hover_body<W: Write>(
&self,
writer: &mut W,
schema_cache: &SchemaCache,
) -> Result<bool, std::fmt::Error> {
if let Some(comment) = &self.comment {
write!(writer, "Comment: '{}'", comment)?;
writeln!(writer)?;
writeln!(writer)?;
}

if !self.attributes.attrs.is_empty() {
write!(writer, "Attributes:")?;
writeln!(writer)?;

for attribute in &self.attributes.attrs {
write!(writer, "- {}", attribute.name)?;

if let Some(type_info) = schema_cache.find_type_by_id(attribute.type_id) {
write!(writer, ": ")?;

if type_info.schema != "pg_catalog" {
write!(writer, "{}.", type_info.schema)?;
}

write!(writer, "{}", type_info.name)?;
} else {
write!(writer, " (type_id: {})", attribute.type_id)?;
}

writeln!(writer)?;
}

writeln!(writer)?;
}

if !self.enums.values.is_empty() {
write!(writer, "Enum Permutations:")?;
writeln!(writer)?;

for kind in &self.enums.values {
write!(writer, "- {}", kind)?;
writeln!(writer)?;
}

writeln!(writer)?;
}

Ok(true)
}

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

impl ContextualPriority for PostgresType {
// there are no schemas with duplicate names.
fn relevance_score(&self, ctx: &TreesitterContext) -> f32 {
let mut score = 0.0;

if ctx
.get_mentioned_relations(&Some(self.schema.clone()))
.is_some()
{
score += 100.0;
}

if ctx.get_mentioned_relations(&None).is_some() && self.schema == "public" {
score += 100.0;
}

if self.schema == "public" && score == 0.0 {
score += 10.0;
}

score
}
}
8 changes: 4 additions & 4 deletions crates/pgt_hover/src/hoverables/role.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::fmt::Write;

use pgt_schema_cache::Role;
use pgt_schema_cache::{Role, SchemaCache};
use pgt_treesitter::TreesitterContext;

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

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

Ok(())
}

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

fn hover_footer<W: Write>(&self, _writer: &mut W) -> Result<bool, std::fmt::Error> {
fn hover_footer<W: Write>(&self, _writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
Ok(false)
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/pgt_hover/src/hoverables/schema.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::fmt::Write;

use pgt_schema_cache::Schema;
use pgt_schema_cache::{Schema, SchemaCache};
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> {
fn hover_headline<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> 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> {
fn hover_body<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
if let Some(comment) = &self.comment {
write!(writer, "Comment: '{}'", comment)?;
writeln!(writer)?;
Expand Down Expand Up @@ -46,7 +46,7 @@ impl ToHoverMarkdown for Schema {
Ok(true)
}

fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
writeln!(writer)?;
write!(
writer,
Expand Down
8 changes: 4 additions & 4 deletions crates/pgt_hover/src/hoverables/table.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::fmt::Write;

use humansize::DECIMAL;
use pgt_schema_cache::Table;
use pgt_schema_cache::{SchemaCache, Table};
use pgt_treesitter::TreesitterContext;

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

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

let table_kind = match self.table_kind {
Expand All @@ -30,7 +30,7 @@ impl ToHoverMarkdown for Table {
Ok(())
}

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

fn hover_footer<W: Write>(&self, writer: &mut W) -> Result<bool, std::fmt::Error> {
fn hover_footer<W: Write>(&self, writer: &mut W, _schema_cache: &SchemaCache) -> Result<bool, std::fmt::Error> {
writeln!(writer)?;
write!(
writer,
Expand Down
Loading
Loading