Skip to content

Commit

Permalink
hover scalar reference types in models
Browse files Browse the repository at this point in the history
  • Loading branch information
Druue committed Jul 3, 2024
1 parent 051a067 commit 7d1efa7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
31 changes: 27 additions & 4 deletions prisma-fmt/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use lsp_types::{Hover, HoverContents, HoverParams, MarkupContent, MarkupKind};
use psl::{
error_tolerant_parse_configuration,
parser_database::ParserDatabase,
schema_ast::ast::{self, Field, FieldPosition, ModelPosition, WithDocumentation},
schema_ast::ast::{self, Field, FieldPosition, ModelPosition, WithDocumentation, WithName},
Diagnostics, SourceFile,
};

Expand Down Expand Up @@ -48,14 +48,16 @@ fn hover(ctx: HoverContext<'_>) -> Option<Hover> {
let position = match ctx.position() {
Some(pos) => pos,
None => {
warn!("Received a position outside of the document boundaries in HoverParams");
warn!("Received a position outside of the document boundaries in HoverParams");
return None;
}
};

let ast = ctx.db.ast(ctx.initiating_file_id);
let contents = match ast.find_at_position(position) {
let contents = match dbg!(ast.find_at_position(position)) {
psl::schema_ast::ast::SchemaPosition::TopLevel => None,

psl::schema_ast::ast::SchemaPosition::Model(model_id, ModelPosition::Name(name)) => {
let walker = ctx.db.walk((ctx.initiating_file_id, model_id));
let model = walker.ast_model();
Expand All @@ -76,7 +78,28 @@ fn hover(ctx: HoverContext<'_>) -> Option<Hover> {
let initiating_field = &ctx.db.walk((ctx.initiating_file_id, model_id)).field(field_id);

match initiating_field.refine() {
psl::parser_database::walkers::RefinedFieldWalker::Scalar(_) => None,
psl::parser_database::walkers::RefinedFieldWalker::Scalar(scalar) => match scalar.scalar_field_type() {
psl::parser_database::ScalarFieldType::CompositeType(_) => {
let ct = scalar.field_type_as_composite_type().unwrap();
Some(format_hover_content(
ct.ast_composite_type().documentation().unwrap_or_default(),
"type",
ct.ast_composite_type().name(),
None,
))
}
psl::parser_database::ScalarFieldType::Enum(_) => {
let enm = scalar.field_type_as_enum().unwrap();
Some(format_hover_content(
enm.ast_enum().documentation().unwrap_or_default(),
"enum",
enm.ast_enum().name(),
None,
))
}
_ => None,
},

psl::parser_database::walkers::RefinedFieldWalker::Relation(rf) => {
let relation = rf.relation();
let opposite_model = rf.related_model();
Expand Down Expand Up @@ -134,7 +157,7 @@ fn format_hover_content(
(format!("\n\t...\n\t{field}\n"), format!("{rk}{fancy_line_break}"))
});
let prisma_display = match variant {
"model" | "enum" | "view" | "composite type" => {
"model" | "enum" | "view" | "type" => {
format!("```prisma\n{variant} {top_name} {{{field}}}\n```{fancy_line_break}{relation_kind}")
}
_ => "".to_owned(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"contents": {
"kind": "markdown",
"value": "```prisma\ntype Address {}\n```\n___\nAddress Doc"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model User {
id String @id @map("_id")
address Add<|>ress
}

/// Address Doc
type Address {
street String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"contents": {
"kind": "markdown",
"value": "```prisma\nenum Animal {}\n```\n___\n"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model User {
id String @id @map("_id")
pet Ani<|>mal
}

// Animal Doc
enum Animal {
REDPANDA
CAT
DOG
}
2 changes: 2 additions & 0 deletions prisma-fmt/tests/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ macro_rules! scenarios {
}

scenarios! {
composite_from_field_type
enum_from_field_type
model_from_block_name
model_from_view_type
one_to_many_self_relation
Expand Down
5 changes: 5 additions & 0 deletions psl/parser-database/src/walkers/scalar_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ impl<'db> ScalarFieldWalker<'db> {
self.scalar_field_type().as_enum().map(|id| self.db.walk(id))
}

/// Is this field's type an enum? If yes, walk the enum.
pub fn field_type_as_composite_type(self) -> Option<CompositeTypeWalker<'db>> {
self.scalar_field_type().as_composite_type().map(|id| self.db.walk(id))
}

/// The name in the `@map(<name>)` attribute.
pub fn mapped_name(self) -> Option<&'db str> {
self.attributes().mapped_name.map(|id| &self.db[id])
Expand Down

0 comments on commit 7d1efa7

Please sign in to comment.