4545 LINE_IS_NORMAL ,
4646};
4747
48+ enum { SSCORE_COMMENT = 10000 , SSCORE_NESTING = -100 };
49+
4850struct vlbuf {
4951 union {
5052 void * vd ;
@@ -396,7 +398,11 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
396398 case TOK_SPECIAL :
397399 case TOK_LABEL : {
398400 if (isalpha_lead (nxt ) || ('0' <= nxt && nxt <= '9' )) {
399-
401+ } else if (nxt == '\'' || nxt == '\"' ) {
402+ /* String with prefix */
403+ proctok = TOK_STRING ;
404+ nstrleads = 1 ;
405+ string_starter = nxt ;
400406 } else {
401407 tokfin = 1 ;
402408 proctok = TOK_INBETWEEN ;
@@ -642,11 +648,17 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
642648 /* Line wrapping & printing, oh joy */
643649 char * tokpos = tokbuf .d .ch ;
644650 int nests = 0 ;
651+ int pptok = TOK_INBETWEEN ;
652+ int pretok = TOK_INBETWEEN ;
653+ int postok = toks .d .in [0 ];
645654 for (int i = 0 ; i < ntoks ; i ++ ) {
646- int pptok = i > 0 ? toks . d . in [ i - 1 ] : TOK_INBETWEEN ;
647- int pretok = toks . d . in [ i ] ;
648- int postok = toks .d .in [i + 1 ];
655+ pptok = pretok ;
656+ pretok = postok ;
657+ postok = toks .d .in [i + 1 ];
649658 int toklen = strlen (tokpos );
659+ if (pretok == TOK_OBRACE ) {
660+ nests ++ ;
661+ }
650662
651663 if (pretok == TOK_LCONT ) {
652664 /* ignore line breaks */
@@ -667,13 +679,24 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
667679 }
668680 buildpt += strapp (buildpt , sos );
669681 splitpoints .d .in [nsplits ] = buildpt - laccum .d .ch ;
670- split_ratings .d .in [nsplits ] = -1 ;
682+ split_ratings .d .in [nsplits ] = SSCORE_COMMENT ;
671683 split_nestings .d .in [nsplits ] = nests ;
672684 nsplits ++ ;
673685 } else {
674686 buildpt += strapp (buildpt , tokpos );
675687 splitpoints .d .in [nsplits ] = buildpt - laccum .d .ch ;
676- split_ratings .d .in [nsplits ] = 0 ;
688+ if (pretok == TOK_COMMA && postok != TOK_CBRACE && nests > 0 ) {
689+ split_ratings .d .in [nsplits ] = 1 ;
690+ } else if (pretok == TOK_COLON && postok != TOK_CBRACE ) {
691+ split_ratings .d .in [nsplits ] = 1 ;
692+ } else if (pretok == TOK_LABEL && postok == TOK_OBRACE ) {
693+ split_ratings .d .in [nsplits ] = SSCORE_NESTING ;
694+ } else if (pretok == TOK_DOT || postok == TOK_DOT ) {
695+ split_ratings .d .in [nsplits ] = -2 ;
696+ } else {
697+ split_ratings .d .in [nsplits ] = 0 ;
698+ }
699+
677700 split_nestings .d .in [nsplits ] = nests ;
678701 nsplits ++ ;
679702 }
@@ -689,7 +712,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
689712 } else if (pretok == TOK_LCONT ) {
690713 space = 0 ;
691714 } else if (pretok == TOK_EQUAL || postok == TOK_EQUAL ) {
692- space = nests == 0 ;
715+ space = ( nests == 0 ) ;
693716 } else if (pretok == TOK_SPECIAL ) {
694717 if (postok == TOK_COLON ) {
695718 space = 0 ;
@@ -706,8 +729,6 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
706729 space = 0 ;
707730 } else if (pretok == TOK_OPERATOR && postok == TOK_UNARYOP ) {
708731 space = 1 ;
709- } else if (pretok == TOK_LABEL && postok == TOK_STRING ) {
710- space = 0 ;
711732 } else if (pretok == TOK_LABEL && postok == TOK_UNARYOP ) {
712733 space = 1 ;
713734 } else if (pretok == TOK_CBRACE && postok == TOK_UNARYOP ) {
@@ -749,9 +770,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
749770 buildpt += strapp (buildpt , " " );
750771 }
751772
752- if (pretok == TOK_OBRACE ) {
753- nests ++ ;
754- } else if (postok == TOK_CBRACE ) {
773+ if (postok == TOK_CBRACE ) {
755774 nests -- ;
756775 }
757776 }
@@ -768,44 +787,43 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
768787 memcpy (lineout .d .ch , & laccum .d .ch [fr ], to - fr );
769788 lineout .d .ch [to - fr ] = '\0' ;
770789 int nlen = to - fr ;
771- int force_split = i > 0 ? split_ratings .d .in [i - 1 ] < 0 : 0 ;
790+ int comment_split =
791+ i > 0 ? split_ratings .d .in [i - 1 ] == SSCORE_COMMENT : 0 ;
772792
773793 /* The previous location provides the break-off score */
774- int bscore = 0 , bk = -1 ;
794+ int best_score = -1000000 , bk = -1 ;
775795 for (int rleft = length_left , k = i ; k < nsplits && rleft >= 0 ; k ++ ) {
776- /* Estimate segment length */
796+ /* Estimate segment length, walk further */
777797 int fr = k > 0 ? splitpoints .d .in [k - 1 ] : 0 ;
778- int ofr = k > 1 ? splitpoints .d .in [k - 2 ] : 0 ;
798+ // int ofr = k > 1 ? splitpoints.d.in[k - 2] : 0;
779799 int to = k >= nsplits - 1 ? eoff : splitpoints .d .in [k ];
780- int len = to - fr ;
781- int isc = 100 ;
782- /* Just by previous. Q: split tok? or 'saved length' field, w/
783- * comments counting as -inf ls. Issue: what if there's less than N
784- * fields left ?*/
785- if (laccum .d .ch [ofr ] == ',' ) {
786- isc = rleft - len - 15 ;
787- } else if (laccum .d .ch [ofr ] == ':' ) {
788- isc = rleft - len - 15 ;
789- } else {
790- isc = rleft - len ;
791- }
792- rleft -= len ;
793- if (isc < bscore ) {
794- bscore = isc ;
800+ int seglen = to - fr ;
801+ rleft -= seglen ;
802+
803+ /* We split at the zone with the highest score */
804+ int reduced_nestings = split_nestings .d .in [k - 1 ];
805+ if (reduced_nestings > 0 )
806+ reduced_nestings -- ;
807+ int split_score = k > 0 ? (split_ratings .d .in [k - 1 ] +
808+ SSCORE_NESTING * reduced_nestings )
809+ : 0 ;
810+ if (split_score >= best_score ) {
811+ best_score = split_score ;
795812 bk = k ;
796813 }
814+
797815 /* Never hold up a terminator */
798816 if (rleft >= 0 && k == nsplits - 1 ) {
799817 bk = -1 ;
800818 }
801819 }
802- int want_split = bk == i ;
803- int length_split = nlen >= length_left ;
820+ int want_split = ( bk == i ) ;
821+ int length_split = ( nlen >= length_left ) ;
804822
805823 int continuing = 1 ;
806824 if (i == 0 ) {
807825 continuing = 1 ;
808- } else if (force_split || length_split || want_split ) {
826+ } else if (comment_split || length_split || want_split ) {
809827 continuing = 0 ;
810828 } else {
811829 continuing = 1 ;
@@ -821,7 +839,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
821839 prn = & lineout .d .ch [1 ];
822840 nlen -= 1 ;
823841 }
824- if (force_split || split_nestings .d .in [i - 1 ] > 0 ) {
842+ if (comment_split || split_nestings .d .in [i - 1 ] > 0 ) {
825843 formfilelen = vlbuf_append (formfile , "\n " , formfilelen , out );
826844 } else {
827845 formfilelen =
0 commit comments