Render label only record fields as they appear in the code

This commit is contained in:
Chadtech 2021-04-25 14:12:47 -04:00
parent 8f0dfa8091
commit be557d6147
2 changed files with 37 additions and 17 deletions

View File

@ -46,12 +46,19 @@ pub enum TypeAnnotation {
fields: Vec<RecordField>,
},
}
#[derive(Debug, Clone)]
pub struct RecordField {
pub name: String,
pub optional: bool,
pub type_annotation: TypeAnnotation,
pub enum RecordField {
RecordField {
name: String,
type_annotation: TypeAnnotation,
},
OptionalField {
name: String,
type_annotation: TypeAnnotation,
},
LabelOnly {
name: String,
},
}
#[derive(Debug, Clone)]
@ -272,25 +279,21 @@ fn type_to_docs(type_annotation: ast::TypeAnnotation) -> Option<TypeAnnotation>
fn record_field_to_doc(field: ast::AssignedField<'_, ast::TypeAnnotation>) -> Option<RecordField> {
match field {
AssignedField::RequiredValue(name, _, type_ann) => {
type_to_docs(type_ann.value).map(|type_ann_docs| RecordField {
type_to_docs(type_ann.value).map(|type_ann_docs| RecordField::RecordField {
name: name.value.to_string(),
type_annotation: type_ann_docs,
optional: false,
})
}
AssignedField::SpaceBefore(&sub_field, _) => record_field_to_doc(sub_field),
AssignedField::SpaceAfter(&sub_field, _) => record_field_to_doc(sub_field),
AssignedField::OptionalValue(name, _, type_ann) => {
type_to_docs(type_ann.value).map(|type_ann_docs| RecordField {
type_to_docs(type_ann.value).map(|type_ann_docs| RecordField::OptionalField {
name: name.value.to_string(),
type_annotation: type_ann_docs,
optional: true,
})
}
AssignedField::LabelOnly(label) => Some(RecordField {
AssignedField::LabelOnly(label) => Some(RecordField::LabelOnly {
name: label.value.to_string(),
type_annotation: BoundVariable(label.value.to_string()),
optional: false,
}),
AssignedField::Malformed(_) => None,
}

View File

@ -1,8 +1,8 @@
extern crate pulldown_cmark;
use roc_builtins::std::StdLib;
use roc_can::builtins::builtin_defs_map;
use roc_load::docs::ModuleDocumentation;
use roc_load::docs::TypeAnnotation;
use roc_load::docs::{ModuleDocumentation, RecordField};
use roc_load::file::LoadingProblem;
use std::fs;
@ -352,13 +352,30 @@ fn type_annotation_to_html(indent_level: usize, buf: &mut String, type_ann: &Typ
let next_indent_level = record_indent + 1;
for (index, field) in fields.iter().enumerate() {
indent(buf, next_indent_level);
buf.push_str(field.name.as_str());
let separator = if field.optional { " ? " } else { " : " };
let fields_name = match field {
RecordField::RecordField { name, .. } => name,
RecordField::OptionalField { name, .. } => name,
RecordField::LabelOnly { name } => name,
};
buf.push_str(separator);
buf.push_str(fields_name.as_str());
type_annotation_to_html(next_indent_level, buf, &field.type_annotation);
match field {
RecordField::RecordField {
type_annotation, ..
} => {
buf.push_str(" : ");
type_annotation_to_html(next_indent_level, buf, type_annotation);
}
RecordField::OptionalField {
type_annotation, ..
} => {
buf.push_str(" ? ");
type_annotation_to_html(next_indent_level, buf, type_annotation);
}
RecordField::LabelOnly { .. } => {}
}
if index < (fields.len() - 1) {
buf.push(',');