@@ -136,6 +136,7 @@ public class CommentsPreparator extends ASTVisitor {
136136 private DefaultCodeFormatter preTagCodeFormatter ;
137137 private DefaultCodeFormatter snippetCodeFormatter ;
138138 private boolean snippetForMarkdown = false ;
139+ private boolean isUnclosedSnippet = false ;
139140
140141 public CommentsPreparator (TokenManager tm , DefaultCodeFormatterOptions options , String sourceLevel ) {
141142 this .tm = tm ;
@@ -886,6 +887,18 @@ private void handleMarkdown(TagElement node) {
886887 }
887888 if (shouldDisable ) {
888889 disableFormatting (tokenIndex , tokenIndexLast , false );
890+ } else if (closingToken == null ) {
891+ if (tokenIndex > 1 )
892+ openingToken .breakBefore ();
893+ openingToken .breakAfter ();
894+ tokenIndexLast = this .ctm .findIndex (startPos + node .getLength (), ANY , true );
895+ this .snippetForMarkdown = true ;
896+ this .isUnclosedSnippet = true ;
897+ formatCode (tokenIndex , tokenIndexLast - 1 , true );
898+ this .snippetForMarkdown = false ;
899+ this .isUnclosedSnippet = false ;
900+ closingToken = this .ctm .get (tokenIndexLast );
901+ closingToken .putLineBreaksAfter (0 );
889902 }
890903 }
891904
@@ -1613,7 +1626,7 @@ private void addSubstituteWraps() {
16131626
16141627 private boolean formatCode (int openingIndex , int closingIndex , boolean snippetTag ) {
16151628 int codeStartPosition = this .ctm .get (openingIndex ).originalEnd + 1 ;
1616- int codeEndPosition = this .ctm .get (closingIndex ).originalStart - 1 ;
1629+ int codeEndPosition = this .isUnclosedSnippet ? this . ctm . get ( closingIndex ). originalStart + 1 : this . ctm .get (closingIndex ).originalStart - 1 ;
16171630 StringBuilder codeBuilder = new StringBuilder (codeEndPosition - codeStartPosition + 1 );
16181631 int [] positionMapping = new int [codeEndPosition - codeStartPosition + 1 ];
16191632 // ^ index: original source position (minus startPosition), value: position in code string
@@ -1636,11 +1649,12 @@ private boolean formatCode(int openingIndex, int closingIndex, boolean snippetTa
16361649 // there are too few linebreaks at the start and end
16371650 Token start = formattedTokens .get (0 );
16381651 start .putLineBreaksBefore (start .getLineBreaksBefore () + 1 );
1639- Token end = formattedTokens .get (formattedTokens .size () - 1 );
1640- end .putLineBreaksAfter (end .getLineBreaksAfter () + 1 );
1641- // and there may be too many line breaks before closing tag
1642- this .ctm .get (closingIndex ).clearLineBreaksBefore ();
1643-
1652+ if (!this .isUnclosedSnippet ) {
1653+ Token end = formattedTokens .get (formattedTokens .size () - 1 );
1654+ end .putLineBreaksAfter (end .getLineBreaksAfter () + 1 );
1655+ // and there may be too many line breaks before closing tag
1656+ this .ctm .get (closingIndex ).clearLineBreaksBefore ();
1657+ }
16441658 List <Token > tokensToReplace = this .commentStructure .subList (openingIndex + 1 , closingIndex );
16451659 tokensToReplace .clear ();
16461660 tokensToReplace .addAll (formattedTokens );
0 commit comments