@@ -15,6 +15,7 @@ import {
1515import {
1616 concat ,
1717 group ,
18+ ifBreak ,
1819 indent ,
1920 join ,
2021 indentIfBreak
@@ -515,18 +516,23 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
515516
516517 methodDeclaration ( ctx : MethodDeclarationCtx ) {
517518 const modifiers = sortModifiers ( ctx . methodModifier ) ;
519+ const throwsGroupId = Symbol ( "throws" ) ;
518520 const firstAnnotations = this . mapVisit ( modifiers [ 0 ] ) ;
519521 const otherModifiers = this . mapVisit ( modifiers [ 1 ] ) ;
520522
521- const header = this . visit ( ctx . methodHeader ) ;
523+ const header = this . visit ( ctx . methodHeader , { throwsGroupId } ) ;
522524 const body = this . visit ( ctx . methodBody ) ;
523525
524- const headerBodySeparator = isStatementEmptyStatement ( body ) ? "" : " " ;
526+ const headerBodySeparator = isStatementEmptyStatement ( body )
527+ ? ""
528+ : ctx . methodHeader [ 0 ] . children . throws
529+ ? ifBreak ( hardline , " " , { groupId : throwsGroupId } )
530+ : " " ;
525531
526532 return rejectAndJoin ( hardline , [
527- rejectAndJoin ( hardline , firstAnnotations ) ,
533+ ... firstAnnotations ,
528534 rejectAndJoin ( " " , [
529- rejectAndJoin ( " " , otherModifiers ) ,
535+ ... otherModifiers ,
530536 rejectAndJoin ( headerBodySeparator , [ header , body ] )
531537 ] )
532538 ] ) ;
@@ -540,12 +546,12 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
540546 return printTokenWithComments ( this . getSingle ( ctx ) as IToken ) ;
541547 }
542548
543- methodHeader ( ctx : MethodHeaderCtx ) {
549+ methodHeader ( ctx : MethodHeaderCtx , opts : { throwsGroupId : symbol } ) {
544550 const typeParameters = this . visit ( ctx . typeParameters ) ;
545551 const annotations = this . mapVisit ( ctx . annotation ) ;
546552 const result = this . visit ( ctx . result ) ;
547553 const declarator = this . visit ( ctx . methodDeclarator ) ;
548- const throws = this . visit ( ctx . throws ) ;
554+ const throws = this . visit ( ctx . throws , opts ) ;
549555
550556 return group (
551557 concat ( [
@@ -652,15 +658,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
652658 return printTokenWithComments ( this . getSingle ( ctx ) as IToken ) ;
653659 }
654660
655- throws ( ctx : ThrowsCtx ) {
661+ throws ( ctx : ThrowsCtx , opts : { throwsGroupId : symbol } ) {
656662 const exceptionTypeList = this . visit ( ctx . exceptionTypeList ) ;
657- const throwsDeclaration = join ( " " , [ ctx . Throws [ 0 ] , exceptionTypeList ] ) ;
658- return group ( indent ( rejectAndConcat ( [ softline , throwsDeclaration ] ) ) ) ;
663+ return group ( indent ( join ( line , [ ctx . Throws [ 0 ] , exceptionTypeList ] ) ) , {
664+ id : opts . throwsGroupId
665+ } ) ;
659666 }
660667
661668 exceptionTypeList ( ctx : ExceptionTypeListCtx ) {
662669 const exceptionTypes = this . mapVisit ( ctx . exceptionType ) ;
663- const commas = ctx . Comma ? ctx . Comma . map ( elt => concat ( [ elt , " " ] ) ) : [ ] ;
670+ const commas = ctx . Comma ?. map ( comma => concat ( [ comma , line ] ) ) ;
664671 return rejectAndJoinSeps ( commas , exceptionTypes ) ;
665672 }
666673
@@ -687,25 +694,26 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
687694 }
688695
689696 constructorDeclaration ( ctx : ConstructorDeclarationCtx ) {
697+ const throwsGroupId = Symbol ( "throws" ) ;
690698 const modifiers = sortModifiers ( ctx . constructorModifier ) ;
691699 const firstAnnotations = this . mapVisit ( modifiers [ 0 ] ) ;
692700 const otherModifiers = this . mapVisit ( modifiers [ 1 ] ) ;
693-
694701 const constructorDeclarator = this . visit ( ctx . constructorDeclarator ) ;
695- const throws = this . visit ( ctx . throws ) ;
702+ const throws = this . visit ( ctx . throws , { throwsGroupId } ) ;
696703 const constructorBody = this . visit ( ctx . constructorBody ) ;
697704
698- return rejectAndJoin ( " " , [
705+ return concat ( [
699706 group (
700707 rejectAndJoin ( hardline , [
701708 rejectAndJoin ( hardline , firstAnnotations ) ,
702709 rejectAndJoin ( " " , [
703- join ( " " , otherModifiers ) ,
710+ rejectAndJoin ( " " , otherModifiers ) ,
704711 constructorDeclarator ,
705712 throws
706713 ] )
707714 ] )
708715 ) ,
716+ ctx . throws ? ifBreak ( hardline , " " , { groupId : throwsGroupId } ) : " " ,
709717 constructorBody
710718 ] ) ;
711719 }
0 commit comments