Support types in the standard library (#5651)

* Parse an unparse type decls (and refactor impl attributes slightly)

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Remove special treatment of geometric types from parser and executor

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Generate docs for std types

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Hover tool-tips for types and fixup the frontend

Signed-off-by: Nick Cameron <nrc@ncameron.org>

* Fixes

Signed-off-by: Nick Cameron <nrc@ncameron.org>

---------

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-03-08 03:53:34 +13:00
committed by GitHub
parent 79d37d360a
commit 5d25f4a0e5
175 changed files with 2023 additions and 1647 deletions

View File

@ -6,7 +6,8 @@ use crate::parsing::{
CallExpression, CallExpressionKw, CommentStyle, DefaultParamVal, Expr, FormatOptions, FunctionExpression,
IfExpression, ImportSelector, ImportStatement, ItemVisibility, LabeledArg, Literal, LiteralIdentifier,
LiteralValue, MemberExpression, MemberObject, Node, NonCodeNode, NonCodeValue, ObjectExpression, Parameter,
PipeExpression, Program, TagDeclarator, Type, UnaryExpression, VariableDeclaration, VariableKind,
PipeExpression, Program, TagDeclarator, Type, TypeDeclaration, UnaryExpression, VariableDeclaration,
VariableKind,
},
token::NumericSuffix,
PIPE_OPERATOR,
@ -48,6 +49,7 @@ impl Program {
BodyItem::VariableDeclaration(variable_declaration) => {
variable_declaration.recast(options, indentation_level)
}
BodyItem::TypeDeclaration(ty_declaration) => ty_declaration.recast(),
BodyItem::ReturnStatement(return_statement) => {
format!(
"{}return {}",
@ -414,6 +416,28 @@ impl VariableDeclaration {
}
}
impl TypeDeclaration {
pub fn recast(&self) -> String {
let vis = match self.visibility {
ItemVisibility::Default => String::new(),
ItemVisibility::Export => "export ".to_owned(),
};
let mut arg_str = String::new();
if let Some(args) = &self.args {
arg_str.push('(');
for a in args {
if arg_str.len() > 1 {
arg_str.push_str(", ");
}
arg_str.push_str(&a.name);
}
arg_str.push(')');
}
format!("{}type {}{}", vis, self.name.name, arg_str)
}
}
// Used by TS.
pub fn format_number(value: f64, suffix: NumericSuffix) -> String {
format!("{value}{suffix}")
@ -2270,6 +2294,19 @@ thickness = sqrt(distance * p * FOS * 6 / (sigmaAllow * width))"#;
assert_eq!(recasted.trim(), some_program_string);
}
#[test]
fn recast_types() {
let some_program_string = r#"type foo
// A comment
@(impl = primitive)
export type bar(unit, baz)
"#;
let program = crate::parsing::top_level_parse(some_program_string).unwrap();
let recasted = program.recast(&Default::default(), 0);
assert_eq!(recasted, some_program_string);
}
#[test]
fn recast_nested_fn() {
let some_program_string = r#"fn f = () => {