45
45
LINE_IS_NORMAL ,
46
46
};
47
47
48
+ enum { SSCORE_COMMENT = 10000 , SSCORE_NESTING = -100 };
49
+
48
50
struct vlbuf {
49
51
union {
50
52
void * vd ;
@@ -396,7 +398,11 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
396
398
case TOK_SPECIAL :
397
399
case TOK_LABEL : {
398
400
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 ;
400
406
} else {
401
407
tokfin = 1 ;
402
408
proctok = TOK_INBETWEEN ;
@@ -642,11 +648,17 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
642
648
/* Line wrapping & printing, oh joy */
643
649
char * tokpos = tokbuf .d .ch ;
644
650
int nests = 0 ;
651
+ int pptok = TOK_INBETWEEN ;
652
+ int pretok = TOK_INBETWEEN ;
653
+ int postok = toks .d .in [0 ];
645
654
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 ];
649
658
int toklen = strlen (tokpos );
659
+ if (pretok == TOK_OBRACE ) {
660
+ nests ++ ;
661
+ }
650
662
651
663
if (pretok == TOK_LCONT ) {
652
664
/* ignore line breaks */
@@ -667,13 +679,24 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
667
679
}
668
680
buildpt += strapp (buildpt , sos );
669
681
splitpoints .d .in [nsplits ] = buildpt - laccum .d .ch ;
670
- split_ratings .d .in [nsplits ] = -1 ;
682
+ split_ratings .d .in [nsplits ] = SSCORE_COMMENT ;
671
683
split_nestings .d .in [nsplits ] = nests ;
672
684
nsplits ++ ;
673
685
} else {
674
686
buildpt += strapp (buildpt , tokpos );
675
687
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
+
677
700
split_nestings .d .in [nsplits ] = nests ;
678
701
nsplits ++ ;
679
702
}
@@ -689,7 +712,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
689
712
} else if (pretok == TOK_LCONT ) {
690
713
space = 0 ;
691
714
} else if (pretok == TOK_EQUAL || postok == TOK_EQUAL ) {
692
- space = nests == 0 ;
715
+ space = ( nests == 0 ) ;
693
716
} else if (pretok == TOK_SPECIAL ) {
694
717
if (postok == TOK_COLON ) {
695
718
space = 0 ;
@@ -706,8 +729,6 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
706
729
space = 0 ;
707
730
} else if (pretok == TOK_OPERATOR && postok == TOK_UNARYOP ) {
708
731
space = 1 ;
709
- } else if (pretok == TOK_LABEL && postok == TOK_STRING ) {
710
- space = 0 ;
711
732
} else if (pretok == TOK_LABEL && postok == TOK_UNARYOP ) {
712
733
space = 1 ;
713
734
} else if (pretok == TOK_CBRACE && postok == TOK_UNARYOP ) {
@@ -749,9 +770,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
749
770
buildpt += strapp (buildpt , " " );
750
771
}
751
772
752
- if (pretok == TOK_OBRACE ) {
753
- nests ++ ;
754
- } else if (postok == TOK_CBRACE ) {
773
+ if (postok == TOK_CBRACE ) {
755
774
nests -- ;
756
775
}
757
776
}
@@ -768,44 +787,43 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
768
787
memcpy (lineout .d .ch , & laccum .d .ch [fr ], to - fr );
769
788
lineout .d .ch [to - fr ] = '\0' ;
770
789
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 ;
772
792
773
793
/* The previous location provides the break-off score */
774
- int bscore = 0 , bk = -1 ;
794
+ int best_score = -1000000 , bk = -1 ;
775
795
for (int rleft = length_left , k = i ; k < nsplits && rleft >= 0 ; k ++ ) {
776
- /* Estimate segment length */
796
+ /* Estimate segment length, walk further */
777
797
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;
779
799
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 ;
795
812
bk = k ;
796
813
}
814
+
797
815
/* Never hold up a terminator */
798
816
if (rleft >= 0 && k == nsplits - 1 ) {
799
817
bk = -1 ;
800
818
}
801
819
}
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 ) ;
804
822
805
823
int continuing = 1 ;
806
824
if (i == 0 ) {
807
825
continuing = 1 ;
808
- } else if (force_split || length_split || want_split ) {
826
+ } else if (comment_split || length_split || want_split ) {
809
827
continuing = 0 ;
810
828
} else {
811
829
continuing = 1 ;
@@ -821,7 +839,7 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
821
839
prn = & lineout .d .ch [1 ];
822
840
nlen -= 1 ;
823
841
}
824
- if (force_split || split_nestings .d .in [i - 1 ] > 0 ) {
842
+ if (comment_split || split_nestings .d .in [i - 1 ] > 0 ) {
825
843
formfilelen = vlbuf_append (formfile , "\n " , formfilelen , out );
826
844
} else {
827
845
formfilelen =
0 commit comments