11use crate :: {
2- context:: { create_indent_trivia, create_newline_trivia, Context } ,
2+ context:: {
3+ create_function_definition_trivia, create_indent_trivia, create_newline_trivia, Context ,
4+ } ,
35 fmt_op, fmt_symbol,
46 formatters:: {
57 assignment:: hang_equal_token,
68 expression:: { format_expression, format_var} ,
9+ functions:: format_function_body,
710 general:: {
811 format_contained_punctuated_multiline, format_contained_span, format_punctuated,
912 format_symbol, format_token_reference,
@@ -23,10 +26,10 @@ use crate::{
2326} ;
2427use full_moon:: ast:: {
2528 luau:: {
26- CompoundAssignment , CompoundOp , ExportedTypeDeclaration , GenericDeclaration ,
27- GenericDeclarationParameter , GenericParameterInfo , IndexedTypeInfo , TypeArgument ,
28- TypeAssertion , TypeDeclaration , TypeField , TypeFieldKey , TypeInfo , TypeIntersection ,
29- TypeSpecifier , TypeUnion ,
29+ CompoundAssignment , CompoundOp , ExportedTypeDeclaration , ExportedTypeFunction ,
30+ GenericDeclaration , GenericDeclarationParameter , GenericParameterInfo , IndexedTypeInfo ,
31+ TypeArgument , TypeAssertion , TypeDeclaration , TypeField , TypeFieldKey , TypeFunction ,
32+ TypeInfo , TypeIntersection , TypeSpecifier , TypeUnion ,
3033 } ,
3134 punctuated:: Pair ,
3235} ;
@@ -1351,6 +1354,63 @@ pub fn format_type_declaration_stmt(
13511354 format_type_declaration ( ctx, type_declaration, true , shape)
13521355}
13531356
1357+ fn format_type_function (
1358+ ctx : & Context ,
1359+ type_function : & TypeFunction ,
1360+ add_leading_trivia : bool ,
1361+ shape : Shape ,
1362+ ) -> TypeFunction {
1363+ const TYPE_TOKEN_LENGTH : usize = "type " . len ( ) ;
1364+ const FUNCTION_TOKEN_LENGTH : usize = "function " . len ( ) ;
1365+
1366+ // Calculate trivia
1367+ let trailing_trivia = vec ! [ create_newline_trivia( ctx) ] ;
1368+ let function_definition_trivia = vec ! [ create_function_definition_trivia( ctx) ] ;
1369+
1370+ let mut type_token = format_symbol (
1371+ ctx,
1372+ type_function. type_token ( ) ,
1373+ & TokenReference :: new (
1374+ vec ! [ ] ,
1375+ Token :: new ( TokenType :: Identifier {
1376+ identifier : "type" . into ( ) ,
1377+ } ) ,
1378+ vec ! [ Token :: new( TokenType :: spaces( 1 ) ) ] ,
1379+ ) ,
1380+ shape,
1381+ ) ;
1382+
1383+ if add_leading_trivia {
1384+ let leading_trivia = vec ! [ create_indent_trivia( ctx, shape) ] ;
1385+ type_token = type_token. update_leading_trivia ( FormatTriviaType :: Append ( leading_trivia) )
1386+ }
1387+
1388+ let function_token = fmt_symbol ! ( ctx, type_function. function_token( ) , "function " , shape) ;
1389+ let function_name = format_token_reference ( ctx, type_function. function_name ( ) , shape)
1390+ . update_trailing_trivia ( FormatTriviaType :: Append ( function_definition_trivia) ) ;
1391+
1392+ let shape = shape
1393+ + ( TYPE_TOKEN_LENGTH
1394+ + FUNCTION_TOKEN_LENGTH
1395+ + strip_trivia ( & function_name) . to_string ( ) . len ( ) ) ;
1396+ let function_body = format_function_body ( ctx, type_function. function_body ( ) , shape)
1397+ . update_trailing_trivia ( FormatTriviaType :: Append ( trailing_trivia) ) ;
1398+
1399+ TypeFunction :: new ( function_name, function_body)
1400+ . with_type_token ( type_token)
1401+ . with_function_token ( function_token)
1402+ }
1403+
1404+ /// Wrapper around `format_type_function` for statements
1405+ /// This is required as `format_type_function` is also used for ExportedTypeFunction, and we don't want leading trivia there
1406+ pub fn format_type_function_stmt (
1407+ ctx : & Context ,
1408+ type_function : & TypeFunction ,
1409+ shape : Shape ,
1410+ ) -> TypeFunction {
1411+ format_type_function ( ctx, type_function, true , shape)
1412+ }
1413+
13541414fn format_generic_parameter (
13551415 ctx : & Context ,
13561416 generic_parameter : & GenericDeclarationParameter ,
@@ -1478,3 +1538,38 @@ pub fn format_exported_type_declaration(
14781538 . with_export_token ( export_token)
14791539 . with_type_declaration ( type_declaration)
14801540}
1541+
1542+ pub fn format_exported_type_function (
1543+ ctx : & Context ,
1544+ exported_type_function : & ExportedTypeFunction ,
1545+ shape : Shape ,
1546+ ) -> ExportedTypeFunction {
1547+ // Calculate trivia
1548+ let shape = shape. reset ( ) ;
1549+ let leading_trivia = vec ! [ create_indent_trivia( ctx, shape) ] ;
1550+
1551+ let export_token = format_symbol (
1552+ ctx,
1553+ exported_type_function. export_token ( ) ,
1554+ & TokenReference :: new (
1555+ vec ! [ ] ,
1556+ Token :: new ( TokenType :: Identifier {
1557+ identifier : "export" . into ( ) ,
1558+ } ) ,
1559+ vec ! [ Token :: new( TokenType :: spaces( 1 ) ) ] ,
1560+ ) ,
1561+ shape,
1562+ )
1563+ . update_leading_trivia ( FormatTriviaType :: Append ( leading_trivia) ) ;
1564+ let type_function = format_type_function (
1565+ ctx,
1566+ exported_type_function. type_function ( ) ,
1567+ false ,
1568+ shape + 7 , // 7 = "export "
1569+ ) ;
1570+
1571+ exported_type_function
1572+ . to_owned ( )
1573+ . with_export_token ( export_token)
1574+ . with_type_function ( type_function)
1575+ }
0 commit comments