Skip to content

Commit

Permalink
Apparently we had Type as a variant of FieldPosition, but it was neve…
Browse files Browse the repository at this point in the history
…r actually returned as a valid variant, so now it is.

Made `iter_tops` on walkers public. Unsure if there's a better way about this; it seemed the most convenient for right now.

Added a helper to FieldType to retrieve names.

Further clean-up of hover
  • Loading branch information
Druue committed Jun 20, 2024
1 parent 345bcca commit 9413105
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
37 changes: 26 additions & 11 deletions prisma-fmt/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,40 +54,55 @@ fn hover(ctx: HoverContext<'_>) -> Hover {

let ast = ctx.db.ast(ctx.initiating_file_id);
let contents = match ast.find_at_position(position) {
psl::schema_ast::ast::SchemaPosition::TopLevel => {
format_hover_content("documentation", "top_variant", "top_name")
}
psl::schema_ast::ast::SchemaPosition::TopLevel => format_hover_content("", "", ""),
psl::schema_ast::ast::SchemaPosition::Model(_model_id, model_position) => {
info!("We're inside a model");
info!("We are here: {:?}", model_position);

let name = match model_position {
ast::ModelPosition::Name(name) => name,
ast::ModelPosition::Field(_, FieldPosition::Type(name)) => name,
_ => todo!(),
_ => "",
};

let top = ast.iter_tops().find(|(_, top)| top.name() == name);
info!("{}", name);

let top = ctx.db.iter_tops().find(|(_, _, top)| top.name() == name);

info!("{:?}", top);

let doc = top.and_then(|(_, top)| top.documentation()).unwrap_or("");
let (variant, doc) = match top.map(|(_file_id, _top_id, top)| top) {
Some(top) => {
let doc = top.documentation().unwrap_or("");
(top.get_type(), doc)
}
None => ("", ""),
};

format_hover_content(doc, "model", name)
format_hover_content(doc, variant, name)
}
psl::schema_ast::ast::SchemaPosition::Enum(_enum_id, enum_position) => {
info!("We are here: {:?}", enum_position);
format_hover_content("documentation", "top_variant", "top_name")
format_hover_content("", "", "")
}
psl::schema_ast::ast::SchemaPosition::DataSource(_ds_id, source_position) => {
info!("We are here: {:?}", source_position);
format_hover_content("documentation", "top_variant", "top_name")
format_hover_content("", "", "")
}
};

Hover { contents, range: None }
}

fn format_hover_content(documentation: &str, top_variant: &str, top_name: &str) -> HoverContents {
let full_signature = format!("```prisma\n{top_variant} {top_name} {{}}\n```\n___\n{documentation}");
fn format_hover_content(documentation: &str, variant: &str, top_name: &str) -> HoverContents {
let fancy_line_break = String::from("\n___\n");
let prisma_display = match variant {
"model" | "enum" | "view" | "composite type" => {
format!("```prisma\n{variant} {top_name} {{}}\n```{fancy_line_break}")
}
_ => "".to_owned(),
};
let full_signature = format!("{prisma_display}{documentation}");

HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
Expand Down
3 changes: 2 additions & 1 deletion psl/parser-database/src/walkers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ pub(crate) fn newline(source: &str, span: Span) -> NewlineType {
}

impl crate::ParserDatabase {
fn iter_tops(&self) -> impl Iterator<Item = (FileId, ast::TopId, &ast::Top)> + '_ {
/// Iterate all top level blocks.
pub fn iter_tops(&self) -> impl Iterator<Item = (FileId, ast::TopId, &ast::Top)> + '_ {
self.asts
.iter()
.flat_map(move |(file_id, _, _, ast)| ast.iter_tops().map(move |(top_id, top)| (file_id, top_id, top)))
Expand Down
7 changes: 7 additions & 0 deletions psl/schema-ast/src/ast/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ impl FieldType {
}
}

pub fn name(&self) -> &str {
match self {
FieldType::Supported(supported) => &supported.name,
FieldType::Unsupported(name, _) => name,
}
}

pub fn as_unsupported(&self) -> Option<(&str, &Span)> {
match self {
FieldType::Unsupported(name, span) => Some((name, span)),
Expand Down
4 changes: 4 additions & 0 deletions psl/schema-ast/src/ast/find_at_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ impl<'ast> FieldPosition<'ast> {
}
}

if field.field_type.span().contains(position) {
return FieldPosition::Type(field.field_type.name());
}

FieldPosition::Field
}
}
Expand Down

0 comments on commit 9413105

Please sign in to comment.