private void handleCompilerTags(Token commentToken, int commentIndex) { final String commentText = this.tm.toString(commentToken); final List<Token> structure = commentToken.getInternalStructure(); if (commentText.startsWith("//$FALL-THROUGH$") //$NON-NLS-1$ || commentText.startsWith("//$IDENTITY-COMPARISON$")) { //$NON-NLS-1$ structure.get(1).clearSpaceBefore(); } if (commentText.contains("//$IDENTITY-COMPARISON$")) { //$NON-NLS-1$ // make sure the whole line is not broken Token token = commentToken; for (int i = commentIndex; i > 0; i--) { Token left = this.tm.get(i - 1); if (this.tm.countLineBreaksBetween(left, token) > 0) break; token.clearLineBreaksBefore(); left.clearLineBreaksAfter(); token.setWrapPolicy(WrapPolicy.DISABLE_WRAP); token = left; } } }
private void disableFormattingExclusively(int openingTagIndex, int closingTagIndex) { Token openingTag = this.ctm.get(openingTagIndex); int noFormatStart = openingTag.originalEnd + 1; int noFormatEnd = this.ctm.get(closingTagIndex - 1).originalEnd; if (noFormatStart <= noFormatEnd) { Token noFormatToken = new Token(noFormatStart, noFormatEnd, TokenNameCOMMENT_JAVADOC); int commentStart = findCommentLineIndent(openingTagIndex); List<Token> lines = commentToLines(noFormatToken, commentStart); List<Token> tokensToReplace = this.commentStructure.subList(openingTagIndex + 1, closingTagIndex); tokensToReplace.clear(); tokensToReplace.addAll(lines); } else { this.commentStructure.subList(openingTagIndex + 1, closingTagIndex).clear(); Token closingTag = this.ctm.get(closingTagIndex); if (this.ctm.countLineBreaksBetween(openingTag, closingTag) == 0) { openingTag.clearLineBreaksAfter(); closingTag.clearLineBreaksBefore(); } } }
private void handleCompilerTags(Token commentToken, int commentIndex) { final String commentText = this.tm.toString(commentToken); final List<Token> structure = commentToken.getInternalStructure(); if (commentText.startsWith("//$FALL-THROUGH$") //$NON-NLS-1$ || commentText.startsWith("//$IDENTITY-COMPARISON$")) { //$NON-NLS-1$ structure.get(1).clearSpaceBefore(); } if (commentText.contains("//$IDENTITY-COMPARISON$")) { //$NON-NLS-1$ // make sure the whole line is not broken Token token = commentToken; for (int i = commentIndex; i > 0; i--) { Token left = this.tm.get(i - 1); if (this.tm.countLineBreaksBetween(left, token) > 0) break; token.clearLineBreaksBefore(); left.clearLineBreaksAfter(); token.setWrapPolicy(WrapPolicy.DISABLE_WRAP); token = left; } } }
private void disableFormattingExclusively(int openingTagIndex, int closingTagIndex) { Token openingTag = this.ctm.get(openingTagIndex); int noFormatStart = openingTag.originalEnd + 1; int noFormatEnd = this.ctm.get(closingTagIndex - 1).originalEnd; if (noFormatStart <= noFormatEnd) { Token noFormatToken = new Token(noFormatStart, noFormatEnd, TokenNameCOMMENT_JAVADOC); int commentStart = findCommentLineIndent(openingTagIndex); List<Token> lines = commentToLines(noFormatToken, commentStart); List<Token> tokensToReplace = this.commentStructure.subList(openingTagIndex + 1, closingTagIndex); tokensToReplace.clear(); tokensToReplace.addAll(lines); } else { this.commentStructure.subList(openingTagIndex + 1, closingTagIndex).clear(); Token closingTag = this.ctm.get(closingTagIndex); if (this.ctm.countLineBreaksBetween(openingTag, closingTag) == 0) { openingTag.clearLineBreaksAfter(); closingTag.clearLineBreaksBefore(); } } }
private void handleCompilerTags(Token commentToken, int commentIndex) { final String commentText = this.tm.toString(commentToken); final List<Token> structure = commentToken.getInternalStructure(); if (commentText.startsWith("//$FALL-THROUGH$") //$NON-NLS-1$ || commentText.startsWith("//$IDENTITY-COMPARISON$")) { //$NON-NLS-1$ structure.get(1).clearSpaceBefore(); } if (commentText.contains("//$IDENTITY-COMPARISON$")) { //$NON-NLS-1$ // make sure the whole line is not broken Token token = commentToken; for (int i = commentIndex; i > 0; i--) { Token left = this.tm.get(i - 1); if (this.tm.countLineBreaksBetween(left, token) > 0) break; token.clearLineBreaksBefore(); left.clearLineBreaksAfter(); token.setWrapPolicy(WrapPolicy.DISABLE_WRAP); token = left; } } }
private void disableFormattingExclusively(int openingTagIndex, int closingTagIndex) { Token openingTag = this.ctm.get(openingTagIndex); int noFormatStart = openingTag.originalEnd + 1; int noFormatEnd = this.ctm.get(closingTagIndex - 1).originalEnd; if (noFormatStart <= noFormatEnd) { Token noFormatToken = new Token(noFormatStart, noFormatEnd, TokenNameCOMMENT_JAVADOC); int commentStart = findCommentLineIndent(openingTagIndex); List<Token> lines = commentToLines(noFormatToken, commentStart); List<Token> tokensToReplace = this.commentStructure.subList(openingTagIndex + 1, closingTagIndex); tokensToReplace.clear(); tokensToReplace.addAll(lines); } else { this.commentStructure.subList(openingTagIndex + 1, closingTagIndex).clear(); Token closingTag = this.ctm.get(closingTagIndex); if (this.ctm.countLineBreaksBetween(openingTag, closingTag) == 0) { openingTag.clearLineBreaksAfter(); closingTag.clearLineBreaksBefore(); } } }
structure.get(1).clearLineBreaksBefore(); Token last = structure.get(structure.size() - 1); this.lineCounter -= last.getLineBreaksBefore(); last.clearLineBreaksBefore();
} finally { if (!wasLineBreak) token.clearLineBreaksBefore();
private void formatCode(int javadocNoFormatCloseStart, int javadocNoFormatCloseEnd) { int openingTagLastIndex = tokenEndingAt(this.formatCodeTagOpenEnd); int closingTagFirstIndex = tokenStartingAt(javadocNoFormatCloseStart); int codeStartPosition = this.formatCodeTagOpenEnd + 1; int codeEndPosition = javadocNoFormatCloseStart - 1; StringBuilder codeBuilder = new StringBuilder(codeEndPosition - codeStartPosition + 1); int[] positionMapping = new int[codeEndPosition - codeStartPosition + 1]; // ^ index: original source position (minus startPosition), value: position in code string getCodeToFormat(codeStartPosition, codeEndPosition, codeBuilder, positionMapping); List<Token> formattedTokens = getCommentCodeFormatter().prepareFormattedCode(codeBuilder.toString()); if (formattedTokens == null) { disableFormattingExclusively(openingTagLastIndex, closingTagFirstIndex); closingTagFirstIndex = tokenStartingAt(javadocNoFormatCloseStart); cleanupHTMLElement(openingTagLastIndex, closingTagFirstIndex, false); return; } formattedTokens = translateFormattedTokens(codeStartPosition, formattedTokens, positionMapping, null); // there are too few linebreaks at the start and end Token start = formattedTokens.get(0); start.putLineBreaksBefore(start.getLineBreaksBefore() + 1); Token end = formattedTokens.get(formattedTokens.size() - 1); end.putLineBreaksAfter(end.getLineBreaksAfter() + 1); // and there may be too many line breaks before closing tag this.ctm.get(closingTagFirstIndex).clearLineBreaksBefore(); List<Token> tokensToReplace = this.commentStructure.subList(openingTagLastIndex + 1, closingTagFirstIndex); tokensToReplace.clear(); tokensToReplace.addAll(formattedTokens); cleanupHTMLElement(openingTagLastIndex, openingTagLastIndex + formattedTokens.size() + 1, true); }
private void formatCode(int javadocNoFormatCloseStart, int javadocNoFormatCloseEnd) { int openingTagLastIndex = tokenEndingAt(this.formatCodeTagOpenEnd); int closingTagFirstIndex = tokenStartingAt(javadocNoFormatCloseStart); int codeStartPosition = this.formatCodeTagOpenEnd + 1; int codeEndPosition = javadocNoFormatCloseStart - 1; StringBuilder codeBuilder = new StringBuilder(codeEndPosition - codeStartPosition + 1); int[] positionMapping = new int[codeEndPosition - codeStartPosition + 1]; // ^ index: original source position (minus startPosition), value: position in code string getCodeToFormat(codeStartPosition, codeEndPosition, codeBuilder, positionMapping); List<Token> formattedTokens = getCommentCodeFormatter().prepareFormattedCode(codeBuilder.toString()); if (formattedTokens == null) { disableFormattingExclusively(openingTagLastIndex, closingTagFirstIndex); closingTagFirstIndex = tokenStartingAt(javadocNoFormatCloseStart); cleanupHTMLElement(openingTagLastIndex, closingTagFirstIndex, false); return; } formattedTokens = translateFormattedTokens(codeStartPosition, formattedTokens, positionMapping, null); // there are too few linebreaks at the start and end Token start = formattedTokens.get(0); start.putLineBreaksBefore(start.getLineBreaksBefore() + 1); Token end = formattedTokens.get(formattedTokens.size() - 1); end.putLineBreaksAfter(end.getLineBreaksAfter() + 1); // and there may be too many line breaks before closing tag this.ctm.get(closingTagFirstIndex).clearLineBreaksBefore(); List<Token> tokensToReplace = this.commentStructure.subList(openingTagLastIndex + 1, closingTagFirstIndex); tokensToReplace.clear(); tokensToReplace.addAll(formattedTokens); cleanupHTMLElement(openingTagLastIndex, openingTagLastIndex + formattedTokens.size() + 1, true); }
private void formatCode(int javadocNoFormatCloseStart, int javadocNoFormatCloseEnd) { int openingTagLastIndex = tokenEndingAt(this.formatCodeTagOpenEnd); int closingTagFirstIndex = tokenStartingAt(javadocNoFormatCloseStart); int codeStartPosition = this.formatCodeTagOpenEnd + 1; int codeEndPosition = javadocNoFormatCloseStart - 1; StringBuilder codeBuilder = new StringBuilder(codeEndPosition - codeStartPosition + 1); int[] positionMapping = new int[codeEndPosition - codeStartPosition + 1]; // ^ index: original source position (minus startPosition), value: position in code string getCodeToFormat(codeStartPosition, codeEndPosition, codeBuilder, positionMapping); List<Token> formattedTokens = getCommentCodeFormatter().prepareFormattedCode(codeBuilder.toString(), CodeFormatter.K_UNKNOWN); if (formattedTokens == null) { disableFormattingExclusively(openingTagLastIndex, closingTagFirstIndex); closingTagFirstIndex = tokenStartingAt(javadocNoFormatCloseStart); cleanupHTMLElement(openingTagLastIndex, closingTagFirstIndex, false); return; } formattedTokens = translateFormattedTokens(codeStartPosition, formattedTokens, positionMapping, null); // there are too few linebreaks at the start and end Token start = formattedTokens.get(0); start.putLineBreaksBefore(start.getLineBreaksBefore() + 1); Token end = formattedTokens.get(formattedTokens.size() - 1); end.putLineBreaksAfter(end.getLineBreaksAfter() + 1); // and there may be too many line breaks before closing tag this.ctm.get(closingTagFirstIndex).clearLineBreaksBefore(); List<Token> tokensToReplace = this.commentStructure.subList(openingTagLastIndex + 1, closingTagFirstIndex); tokensToReplace.clear(); tokensToReplace.addAll(formattedTokens); cleanupHTMLElement(openingTagLastIndex, openingTagLastIndex + formattedTokens.size() + 1, true); }
private void setTokenWrapPolicy(int wrapIndexesIndex, WrapPolicy policy, boolean wrapPreceedingComments) { int index = this.wrapIndexes.get(wrapIndexesIndex); if (wrapPreceedingComments) { for (int i = index - 1; i >= 0; i--) { Token previous = this.tm.get(i); if (!previous.isComment()) break; if (previous.getWrapPolicy() == WrapPolicy.FORCE_FIRST_COLUMN) break; if (previous.getLineBreaksAfter() == 0 && i == index - 1) index = i; if (previous.getLineBreaksBefore() > 0) previous.setWrapPolicy(policy); } this.wrapIndexes.set(wrapIndexesIndex, index); } Token token = this.tm.get(index); if (token.getWrapPolicy() == WrapPolicy.DISABLE_WRAP) return; token.setWrapPolicy(policy); if (policy.wrapMode == WrapMode.FORCE) { token.breakBefore(); } else if (this.options.join_wrapped_lines && token.tokenType == TokenNameCOMMENT_BLOCK) { // allow wrap preparator to decide if this comment should be wrapped token.clearLineBreaksBefore(); } }
private void setTokenWrapPolicy(int wrapIndexesIndex, WrapPolicy policy, boolean wrapPreceedingComments) { int index = this.wrapIndexes.get(wrapIndexesIndex); if (wrapPreceedingComments) { for (int i = index - 1; i >= 0; i--) { Token previous = this.tm.get(i); if (!previous.isComment()) break; if (previous.getWrapPolicy() == WrapPolicy.FORCE_FIRST_COLUMN) break; if (previous.getLineBreaksAfter() == 0 && i == index - 1) index = i; if (previous.getLineBreaksBefore() > 0) previous.setWrapPolicy(policy); } this.wrapIndexes.set(wrapIndexesIndex, index); } Token token = this.tm.get(index); if (token.getWrapPolicy() == WrapPolicy.DISABLE_WRAP) return; token.setWrapPolicy(policy); if (policy.wrapMode == WrapMode.FORCE) { token.breakBefore(); } else if (this.options.join_wrapped_lines && token.tokenType == TokenNameCOMMENT_BLOCK) { // allow wrap preparator to decide if this comment should be wrapped token.clearLineBreaksBefore(); } }
private void setTokenWrapPolicy(int index, WrapPolicy policy, boolean wrapPreceedingComments) { if (wrapPreceedingComments) { for (int i = index - 1; i >= 0; i--) { Token previous = this.tm.get(i); if (!previous.isComment()) break; if (previous.getLineBreaksAfter() == 0 && i == index - 1) index = i; if (previous.getLineBreaksBefore() > 0) previous.setWrapPolicy(policy); } } Token token = this.tm.get(index); if (token.getWrapPolicy() != WrapPolicy.DISABLE_WRAP) token.setWrapPolicy(policy); if (this.options.join_wrapped_lines && token.tokenType == TokenNameCOMMENT_BLOCK) { // allow wrap preparator to decide if this comment should be wrapped token.clearLineBreaksBefore(); } }
private void preserveExistingLineBreaks() { // normally n empty lines = n+1 line breaks, but not at the file start and end Token first = this.tm.get(0); int startingBreaks = first.getLineBreaksBefore(); first.clearLineBreaksBefore(); first.putLineBreaksBefore(startingBreaks - 1); this.tm.traverse(0, new TokenTraverser() { boolean join_wrapped_lines = WrapPreparator.this.options.join_wrapped_lines; @Override protected boolean token(Token token, int index) { boolean isBetweenImports = index > WrapPreparator.this.importsStart && index < WrapPreparator.this.importsEnd; int lineBreaks = getLineBreaksToPreserve(getPrevious(), token, isBetweenImports); if (lineBreaks > 1 || (!this.join_wrapped_lines && token.isWrappable()) || index == 0) token.putLineBreaksBefore(lineBreaks); return true; } }); Token last = this.tm.get(this.tm.size() - 1); last.clearLineBreaksAfter(); int endingBreaks = getLineBreaksToPreserve(last, null, false); if (endingBreaks > 0) { last.putLineBreaksAfter(endingBreaks); } else if ((this.kind & (CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.K_MODULE_INFO)) != 0 && this.options.insert_new_line_at_end_of_file_if_missing) { last.breakAfter(); } }
private void preserveExistingLineBreaks() { // normally n empty lines = n+1 line breaks, but not at the file start and end Token first = this.tm.get(0); int startingBreaks = first.getLineBreaksBefore(); first.clearLineBreaksBefore(); first.putLineBreaksBefore(startingBreaks - 1); this.tm.traverse(0, new TokenTraverser() { boolean join_wrapped_lines = WrapPreparator.this.options.join_wrapped_lines; @Override protected boolean token(Token token, int index) { boolean isBetweenImports = index > WrapPreparator.this.importsStart && index < WrapPreparator.this.importsEnd; int lineBreaks = getLineBreaksToPreserve(getPrevious(), token, isBetweenImports); if (lineBreaks > 1 || (!this.join_wrapped_lines && token.isWrappable()) || index == 0) token.putLineBreaksBefore(lineBreaks); return true; } }); Token last = this.tm.get(this.tm.size() - 1); last.clearLineBreaksAfter(); int endingBreaks = getLineBreaksToPreserve(last, null, false); if (endingBreaks > 0) { last.putLineBreaksAfter(endingBreaks); } else if ((this.kind & (CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.K_MODULE_INFO)) != 0 && this.options.insert_new_line_at_end_of_file_if_missing) { last.breakAfter(); } }
first.clearLineBreaksBefore(); first.putLineBreaksBefore(startingBreaks - 1);