@@ -186,6 +186,7 @@ static int isoptype(char c) {
186186}
187187
188188/* import keyword; print(*keyword.kwlist) */
189+ /* todo: build fast detection state matchine, testing inclusion is 30% */
189190static const char * specnames [] = {
190191 "and" , "as" , "assert" , "break" , "class" , "continue" , "def" ,
191192 "del" , "elif" , "else" , "except" , "finally" , "for" , "from" ,
@@ -633,6 +634,10 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
633634 int space ;
634635 if (pretok == TOK_COMMENT ) {
635636 space = 0 ;
637+ } else if (pptok == TOK_INBETWEEN && pretok == TOK_OPERATOR &&
638+ postok == TOK_LABEL ) {
639+ /* annotation */
640+ space = 0 ;
636641 } else if (pretok == TOK_LCONT ) {
637642 space = 0 ;
638643 } else if (pretok == TOK_EQUAL || postok == TOK_EQUAL ) {
@@ -715,15 +720,41 @@ static void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
715720 int fr = i > 0 ? splitpoints .d .in [i - 1 ] : 0 ;
716721 int to = i >= nsplits - 1 ? eoff : splitpoints .d .in [i ];
717722 memcpy (lineout .d .ch , & laccum .d .ch [fr ], to - fr );
718- lineout .d .ch [to - fr ] = '\0' ;
719723 if (split_ratings .d .in [i ] < 0 ) {
720724 to -= 2 ;
721- lineout .d .ch [to - fr ] = '\0' ;
722725 }
726+ lineout .d .ch [to - fr ] = '\0' ;
723727 int nlen = to - fr ;
724728 int force_split = i > 0 ? split_ratings .d .in [i - 1 ] < 0 : 0 ;
725729
726- if ((nlen < length_left || first ) && !force_split ) {
730+ /* The previous location provides the break-off score */
731+ int bscore = 0 , bk = -1 ;
732+ for (int rleft = length_left , k = i ; k < nsplits && rleft >= 0 ; k ++ ) {
733+ /* Estimate segment length */
734+ int fr = k > 0 ? splitpoints .d .in [k - 1 ] : 0 ;
735+ int ofr = k > 1 ? splitpoints .d .in [k - 2 ] : 0 ;
736+ int to = k >= nsplits - 1 ? eoff : splitpoints .d .in [k ];
737+ if (split_ratings .d .in [k ] < 0 ) {
738+ to -= 2 ;
739+ }
740+ int len = to - fr ;
741+ int isc = 100 ;
742+ /* Just by previous. Q: split tok? or 'saved length' field, w/
743+ * comments counting as -inf */
744+ if (laccum .d .ch [ofr ] == ',' ) {
745+ isc = rleft - len - 15 ;
746+ } else if (laccum .d .ch [ofr ] == ':' ) {
747+ isc = rleft - len - 15 ;
748+ } else {
749+ isc = rleft - len ;
750+ }
751+ rleft -= len ;
752+ if (isc < bscore ) {
753+ bscore = isc ;
754+ bk = k ;
755+ }
756+ }
757+ if ((nlen < length_left || first ) && !force_split && bk != i ) {
727758 first = 0 ;
728759 length_left -= nlen ;
729760 formfilelen =
0 commit comments