@@ -2,14 +2,14 @@ import {
22 ArgumentListCtx ,
33 ArrayAccessSuffixCtx ,
44 ArrayCreationExpressionCtx ,
5- ArrayCreationWithInitializerSuffixCtx ,
65 ArrayCreationExpressionWithoutInitializerSuffixCtx ,
6+ ArrayCreationWithInitializerSuffixCtx ,
77 BinaryExpressionCtx ,
88 CastExpressionCtx ,
99 ClassLiteralSuffixCtx ,
1010 ClassOrInterfaceTypeToInstantiateCtx ,
11- ComponentPatternListCtx ,
1211 ComponentPatternCtx ,
12+ ComponentPatternListCtx ,
1313 ConciseLambdaParameterCtx ,
1414 ConciseLambdaParameterListCtx ,
1515 ConditionalExpressionCtx ,
@@ -63,6 +63,7 @@ import { builders, utils } from "prettier/doc";
6363import { BaseCstPrettierPrinter } from "../base-cst-printer.js" ;
6464import { isAnnotationCstNode } from "../types/utils.js" ;
6565import { printArgumentListWithBraces } from "../utils/index.js" ;
66+ import { hasLeadingComments } from "./comments/comments-utils.js" ;
6667import { printTokenWithComments } from "./comments/format-comments.js" ;
6768import {
6869 handleCommentsBinaryExpression ,
@@ -359,72 +360,49 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
359360 const isBreakableNewExpression =
360361 countMethodInvocation <= 1 &&
361362 this . isBreakableNewExpression ( newExpression ) ;
362- const fqnOrRefType =
363- ctx . primaryPrefix [ 0 ] . children . fqnOrRefType ?. [ 0 ] . children ;
364363 const firstMethodInvocation = ctx . primarySuffix
365364 ?. map ( suffix => suffix . children . methodInvocationSuffix ?. [ 0 ] . children )
366365 . find ( methodInvocationSuffix => methodInvocationSuffix ) ;
367- const isCapitalizedIdentifier = this . isCapitalizedIdentifier ( fqnOrRefType ) ;
366+
367+ const fqnOrRefType =
368+ ctx . primaryPrefix [ 0 ] . children . fqnOrRefType ?. [ 0 ] . children ;
369+ const hasFqnRefPart = fqnOrRefType ?. fqnOrRefTypePartRest !== undefined ;
370+ const {
371+ isCapitalizedIdentifier,
372+ isCapitalizedIdentifierWithoutTrailingComment
373+ } = this . handleStaticInvocations ( fqnOrRefType ) ;
374+
368375 const shouldBreakBeforeFirstMethodInvocation =
369376 countMethodInvocation > 1 &&
370- ! ( isCapitalizedIdentifier ?? true ) &&
371- firstMethodInvocation !== undefined ;
377+ hasFqnRefPart &&
378+ ! isCapitalizedIdentifierWithoutTrailingComment ;
379+
372380 const shouldBreakBeforeMethodInvocations =
373381 shouldBreakBeforeFirstMethodInvocation ||
374382 countMethodInvocation > 2 ||
375- ( countMethodInvocation > 1 && newExpression ) ||
383+ ( countMethodInvocation > 1 && ! ! newExpression ) ||
376384 ! firstMethodInvocation ?. argumentList ;
377385
378386 const primaryPrefix = this . visit ( ctx . primaryPrefix , {
379387 ...params ,
380388 shouldBreakBeforeFirstMethodInvocation
381389 } ) ;
382390
383- const suffixes = [ ] ;
384-
385- if ( ctx . primarySuffix !== undefined ) {
386- // edge case: https://github.com/jhipster/prettier-java/issues/381
387- let hasFirstInvocationArg = true ;
388-
389- if (
390- ctx . primarySuffix . length > 1 &&
391- ctx . primarySuffix [ 1 ] . children . methodInvocationSuffix &&
392- Object . keys (
393- ctx . primarySuffix [ 1 ] . children . methodInvocationSuffix [ 0 ] . children
394- ) . length === 2
395- ) {
396- hasFirstInvocationArg = false ;
397- }
398-
399- if (
400- newExpression &&
401- ! isBreakableNewExpression &&
402- ctx . primarySuffix [ 0 ] . children . Dot !== undefined
403- ) {
404- suffixes . push ( softline ) ;
405- }
406- suffixes . push ( this . visit ( ctx . primarySuffix [ 0 ] ) ) ;
407-
408- for ( let i = 1 ; i < ctx . primarySuffix . length ; i ++ ) {
409- if (
410- shouldBreakBeforeMethodInvocations &&
411- ctx . primarySuffix [ i ] . children . Dot !== undefined &&
412- ctx . primarySuffix [ i - 1 ] . children . methodInvocationSuffix !== undefined
413- ) {
414- suffixes . push ( softline ) ;
415- }
416- suffixes . push ( this . visit ( ctx . primarySuffix [ i ] ) ) ;
417- }
391+ const suffixes = this . getPrimarySuffixes (
392+ ctx ,
393+ newExpression ,
394+ isBreakableNewExpression ,
395+ shouldBreakBeforeMethodInvocations
396+ ) ;
418397
419- if ( ! newExpression && countMethodInvocation === 1 ) {
420- return group (
421- rejectAndConcat ( [
422- primaryPrefix ,
423- hasFirstInvocationArg ? suffixes [ 0 ] : indent ( suffixes [ 0 ] ) ,
424- indent ( rejectAndConcat ( suffixes . slice ( 1 ) ) )
425- ] )
426- ) ;
427- }
398+ if ( ! newExpression && countMethodInvocation === 1 ) {
399+ return group (
400+ rejectAndConcat ( [
401+ primaryPrefix ,
402+ suffixes [ 0 ] ,
403+ indent ( rejectAndConcat ( suffixes . slice ( 1 ) ) )
404+ ] )
405+ ) ;
428406 }
429407
430408 const methodInvocation =
@@ -447,6 +425,20 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
447425 ) ;
448426 }
449427
428+ private handleStaticInvocations ( fqnOrRefType : FqnOrRefTypeCtx | undefined ) {
429+ const lastFqnRefPartDot = this . lastFqnOrRefDot ( fqnOrRefType ) ;
430+ const isCapitalizedIdentifier = this . isCapitalizedIdentifier ( fqnOrRefType ) ;
431+ const isCapitalizedIdentifierWithoutTrailingComment =
432+ isCapitalizedIdentifier &&
433+ ( lastFqnRefPartDot === undefined ||
434+ ! hasLeadingComments ( lastFqnRefPartDot ) ) ;
435+
436+ return {
437+ isCapitalizedIdentifier,
438+ isCapitalizedIdentifierWithoutTrailingComment
439+ } ;
440+ }
441+
450442 primaryPrefix ( ctx : PrimaryPrefixCtx , params : any ) {
451443 if ( ctx . This || ctx . Void ) {
452444 return printTokenWithComments ( this . getSingle ( ctx ) as IToken ) ;
@@ -910,8 +902,50 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
910902 fqnOrRefTypeParts [ fqnOrRefTypeParts . length - 2 ] ?. children
911903 . fqnOrRefTypePartCommon [ 0 ] . children . Identifier ?. [ 0 ] . image ;
912904 return (
913- nextToLastIdentifier &&
905+ ! ! nextToLastIdentifier &&
914906 / ^ \p{ Uppercase_Letter} / u. test ( nextToLastIdentifier )
915907 ) ;
916908 }
909+
910+ private lastFqnOrRefDot ( fqnOrRefType : FqnOrRefTypeCtx | undefined ) {
911+ if ( fqnOrRefType === undefined || fqnOrRefType . Dot === undefined ) {
912+ return undefined ;
913+ }
914+
915+ return fqnOrRefType . Dot [ fqnOrRefType . Dot . length - 1 ] ;
916+ }
917+
918+ private getPrimarySuffixes (
919+ ctx : PrimaryCtx ,
920+ newExpression : NewExpressionCtx | undefined ,
921+ isBreakableNewExpression : boolean ,
922+ shouldBreakBeforeMethodInvocations : NewExpressionCtx | boolean
923+ ) {
924+ if ( ctx . primarySuffix === undefined ) {
925+ return [ ] ;
926+ }
927+
928+ const suffixes = [ ] ;
929+
930+ if (
931+ newExpression &&
932+ ! isBreakableNewExpression &&
933+ ctx . primarySuffix [ 0 ] . children . Dot !== undefined
934+ ) {
935+ suffixes . push ( softline ) ;
936+ }
937+ suffixes . push ( this . visit ( ctx . primarySuffix [ 0 ] ) ) ;
938+
939+ for ( let i = 1 ; i < ctx . primarySuffix . length ; i ++ ) {
940+ if (
941+ shouldBreakBeforeMethodInvocations &&
942+ ctx . primarySuffix [ i ] . children . Dot !== undefined &&
943+ ctx . primarySuffix [ i - 1 ] . children . methodInvocationSuffix !== undefined
944+ ) {
945+ suffixes . push ( softline ) ;
946+ }
947+ suffixes . push ( this . visit ( ctx . primarySuffix [ i ] ) ) ;
948+ }
949+ return suffixes ;
950+ }
917951}
0 commit comments