@@ -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,16 @@ 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 ;
889900 }
890901 }
891902
@@ -1613,7 +1624,7 @@ private void addSubstituteWraps() {
16131624
16141625 private boolean formatCode (int openingIndex , int closingIndex , boolean snippetTag ) {
16151626 int codeStartPosition = this .ctm .get (openingIndex ).originalEnd + 1 ;
1616- int codeEndPosition = this .ctm .get (closingIndex ).originalStart - 1 ;
1627+ int codeEndPosition = this .isUnclosedSnippet ? this . ctm . get ( closingIndex ). originalStart + 1 : this . ctm .get (closingIndex ).originalStart - 1 ;
16171628 StringBuilder codeBuilder = new StringBuilder (codeEndPosition - codeStartPosition + 1 );
16181629 int [] positionMapping = new int [codeEndPosition - codeStartPosition + 1 ];
16191630 // ^ index: original source position (minus startPosition), value: position in code string
@@ -1636,11 +1647,12 @@ private boolean formatCode(int openingIndex, int closingIndex, boolean snippetTa
16361647 // there are too few linebreaks at the start and end
16371648 Token start = formattedTokens .get (0 );
16381649 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-
1650+ if (!this .isUnclosedSnippet ) {
1651+ Token end = formattedTokens .get (formattedTokens .size () - 1 );
1652+ end .putLineBreaksAfter (end .getLineBreaksAfter () + 1 );
1653+ // and there may be too many line breaks before closing tag
1654+ this .ctm .get (closingIndex ).clearLineBreaksBefore ();
1655+ }
16441656 List <Token > tokensToReplace = this .commentStructure .subList (openingIndex + 1 , closingIndex );
16451657 tokensToReplace .clear ();
16461658 tokensToReplace .addAll (formattedTokens );
0 commit comments