@@ -12,7 +12,14 @@ import {
1212 sortClassTypeChildren ,
1313 sortModifiers
1414} from "./printer-utils" ;
15- import { concat , group , indent , join , indentIfBreak } from "./prettier-builder" ;
15+ import {
16+ concat ,
17+ group ,
18+ ifBreak ,
19+ indent ,
20+ join ,
21+ indentIfBreak
22+ } from "./prettier-builder" ;
1623import { printTokenWithComments } from "./comments/format-comments" ;
1724import {
1825 hasLeadingComments ,
@@ -509,18 +516,23 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
509516
510517 methodDeclaration ( ctx : MethodDeclarationCtx ) {
511518 const modifiers = sortModifiers ( ctx . methodModifier ) ;
519+ const throwsGroupId = Symbol ( "throws" ) ;
512520 const firstAnnotations = this . mapVisit ( modifiers [ 0 ] ) ;
513521 const otherModifiers = this . mapVisit ( modifiers [ 1 ] ) ;
514522
515- const header = this . visit ( ctx . methodHeader ) ;
523+ const header = this . visit ( ctx . methodHeader , { throwsGroupId } ) ;
516524 const body = this . visit ( ctx . methodBody ) ;
517525
518- const headerBodySeparator = isStatementEmptyStatement ( body ) ? "" : " " ;
526+ const headerBodySeparator = isStatementEmptyStatement ( body )
527+ ? ""
528+ : ctx . methodHeader [ 0 ] . children . throws
529+ ? ifBreak ( hardline , " " , { groupId : throwsGroupId } )
530+ : " " ;
519531
520532 return rejectAndJoin ( hardline , [
521- rejectAndJoin ( hardline , firstAnnotations ) ,
533+ ... firstAnnotations ,
522534 rejectAndJoin ( " " , [
523- rejectAndJoin ( " " , otherModifiers ) ,
535+ ... otherModifiers ,
524536 rejectAndJoin ( headerBodySeparator , [ header , body ] )
525537 ] )
526538 ] ) ;
@@ -534,12 +546,12 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
534546 return printTokenWithComments ( this . getSingle ( ctx ) as IToken ) ;
535547 }
536548
537- methodHeader ( ctx : MethodHeaderCtx ) {
549+ methodHeader ( ctx : MethodHeaderCtx , opts ?: { throwsGroupId ?: symbol } ) {
538550 const typeParameters = this . visit ( ctx . typeParameters ) ;
539551 const annotations = this . mapVisit ( ctx . annotation ) ;
540552 const result = this . visit ( ctx . result ) ;
541553 const declarator = this . visit ( ctx . methodDeclarator ) ;
542- const throws = this . visit ( ctx . throws ) ;
554+ const throws = this . visit ( ctx . throws , opts ) ;
543555
544556 return group (
545557 concat ( [
@@ -646,15 +658,16 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
646658 return printTokenWithComments ( this . getSingle ( ctx ) as IToken ) ;
647659 }
648660
649- throws ( ctx : ThrowsCtx ) {
661+ throws ( ctx : ThrowsCtx , opts ?: { throwsGroupId ?: symbol } ) {
650662 const exceptionTypeList = this . visit ( ctx . exceptionTypeList ) ;
651- const throwsDeclaration = join ( " " , [ ctx . Throws [ 0 ] , exceptionTypeList ] ) ;
652- return group ( indent ( rejectAndConcat ( [ softline , throwsDeclaration ] ) ) ) ;
663+ return group ( indent ( join ( line , [ ctx . Throws [ 0 ] , exceptionTypeList ] ) ) , {
664+ id : opts ?. throwsGroupId
665+ } ) ;
653666 }
654667
655668 exceptionTypeList ( ctx : ExceptionTypeListCtx ) {
656669 const exceptionTypes = this . mapVisit ( ctx . exceptionType ) ;
657- const commas = ctx . Comma ? ctx . Comma . map ( elt => concat ( [ elt , " " ] ) ) : [ ] ;
670+ const commas = ctx . Comma ?. map ( comma => concat ( [ comma , line ] ) ) ;
658671 return rejectAndJoinSeps ( commas , exceptionTypes ) ;
659672 }
660673
@@ -681,25 +694,26 @@ export class ClassesPrettierVisitor extends BaseCstPrettierPrinter {
681694 }
682695
683696 constructorDeclaration ( ctx : ConstructorDeclarationCtx ) {
697+ const throwsGroupId = Symbol ( "throws" ) ;
684698 const modifiers = sortModifiers ( ctx . constructorModifier ) ;
685699 const firstAnnotations = this . mapVisit ( modifiers [ 0 ] ) ;
686700 const otherModifiers = this . mapVisit ( modifiers [ 1 ] ) ;
687-
688701 const constructorDeclarator = this . visit ( ctx . constructorDeclarator ) ;
689- const throws = this . visit ( ctx . throws ) ;
702+ const throws = this . visit ( ctx . throws , { throwsGroupId } ) ;
690703 const constructorBody = this . visit ( ctx . constructorBody ) ;
691704
692- return rejectAndJoin ( " " , [
705+ return concat ( [
693706 group (
694707 rejectAndJoin ( hardline , [
695708 rejectAndJoin ( hardline , firstAnnotations ) ,
696709 rejectAndJoin ( " " , [
697- join ( " " , otherModifiers ) ,
710+ rejectAndJoin ( " " , otherModifiers ) ,
698711 constructorDeclarator ,
699712 throws
700713 ] )
701714 ] )
702715 ) ,
716+ ctx . throws ? ifBreak ( hardline , " " , { groupId : throwsGroupId } ) : " " ,
703717 constructorBody
704718 ] ) ;
705719 }
0 commit comments