private void prepareComments() { CommentsPreparator commentsPreparator = new CommentsPreparator(this.tokenManager, this.workingOptions, this.sourceLevel); List<Comment> comments = ((CompilationUnit) this.astRoot.getRoot()).getCommentList(); for (Comment comment : comments) { comment.accept(commentsPreparator); } commentsPreparator.finishUp(); }
@Override public void endVisit(Javadoc node) { if (this.ctm == null) return; if (this.options.comment_insert_empty_line_before_root_tags && this.firstTagToken != null && this.ctm.indexOf(this.firstTagToken) > 1) { this.firstTagToken.putLineBreaksBefore(2); } addSubstituteWraps(); }
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 handleNoFormatTag(int start, int end, boolean isOpeningTag) { if (isOpeningTag) { if (this.noFormatTagOpenStart < 0) this.noFormatTagOpenStart = start; } else if (this.noFormatTagOpenStart >= 0) { int openingTagIndex = tokenStartingAt(this.noFormatTagOpenStart); int closingTagIndex = tokenEndingAt(end); if (openingTagIndex < closingTagIndex) { disableFormatting(openingTagIndex, closingTagIndex); closingTagIndex = tokenEndingAt(end); } cleanupHTMLElement(openingTagIndex, closingTagIndex, false); this.noFormatTagOpenStart = -1; } }
private void handleStringLiterals(String text, int textStartPosition) { Matcher matcher = STRING_LITERAL_PATTERN.matcher(text); while (matcher.find()) { int startPosition = textStartPosition + matcher.start(); int startIndex = this.ctm.findIndex(startPosition, -1, false); int endPosition = textStartPosition + matcher.end() - 1; int endIndex = this.ctm.findIndex(endPosition, -1, false); if (startIndex != endIndex) { startIndex = tokenStartingAt(startPosition); endIndex = tokenEndingAt(endPosition); disableFormatting(startIndex, endIndex); } noSubstituteWrapping(startPosition, endPosition); } }
public void handleBlockComment(int commentIndex) { Token commentToken = this.tm.get(commentIndex); boolean isFirstColumn = handleWhitespaceAround(commentIndex); if (handleFormatOnOffTags(commentToken)) return; boolean isHeader = this.tm.isInHeader(commentIndex); boolean formattingEnabled = (this.options.comment_format_block_comment && !isHeader) || (this.options.comment_format_header && isHeader); formattingEnabled = formattingEnabled && this.tm.charAt(commentToken.originalStart + 2) != '-'; if (formattingEnabled && tokenizeMultilineComment(commentToken)) { this.commentStructure = commentToken.getInternalStructure(); this.ctm = new TokenManager(this.commentStructure, this.tm); handleStringLiterals(this.tm.toString(commentToken), commentToken.originalStart); addSubstituteWraps(); } else { commentToken.setInternalStructure(commentToLines(commentToken, -1)); } if (this.options.never_indent_block_comments_on_first_column && isFirstColumn) { commentToken.setIndent(0); commentToken.setWrapPolicy(null); } }
private void handleFormatCodeTag(int startPos, int endPos, boolean isOpeningTag) { if (!this.options.comment_format_source) { handleNoFormatTag(startPos, endPos, isOpeningTag); return; } // add empty lines before opening and after closing token handleSeparateLineTag(startPos, endPos); if (isOpeningTag) { int startIndex = tokenStartingAt(startPos); if (startIndex > 1) this.ctm.get(startIndex).putLineBreaksBefore(2); if (this.formatCodeTagOpenEnd < 0) this.formatCodeTagOpenEnd = endPos; } else if (this.formatCodeTagOpenEnd >= 0) { int endTagIndex = tokenEndingAt(endPos); if (endTagIndex < this.ctm.size() - 2) this.ctm.get(endTagIndex).putLineBreaksAfter(2); formatCode(startPos, endPos); this.formatCodeTagOpenEnd = -1; this.lastFormatCodeClosingTagIndex = this.ctm.findIndex(startPos, -1, true); } }
@Override public boolean visit(Javadoc node) { this.noFormatTagOpenStart = -1; this.formatCodeTagOpenEnd = -1; this.lastFormatCodeClosingTagIndex = -1; this.firstTagToken = null; this.ctm = null; int commentIndex = this.tm.firstIndexIn(node, TokenNameCOMMENT_JAVADOC); Token commentToken = this.tm.get(commentIndex); if (node.getParent() == null) { // not a proper javadoc, treat as block comment handleWhitespaceAround(commentIndex); } if (commentIndex < this.tm.size() - 1) commentToken.breakAfter(); if (handleFormatOnOffTags(commentToken)) return false; boolean isHeader = this.tm.isInHeader(commentIndex); boolean formattingEnabled = (this.options.comment_format_javadoc_comment && !isHeader) || (this.options.comment_format_header && isHeader); if (!formattingEnabled || !tokenizeMultilineComment(commentToken)) { commentToken.setInternalStructure(commentToLines(commentToken, -1)); return false; } this.commentStructure = commentToken.getInternalStructure(); this.commentIndent = this.tm.toIndent(commentToken.getIndent(), true); this.ctm = new TokenManager(commentToken.getInternalStructure(), this.tm); handleJavadocTagAlignment(node); return true; }
return true; int startIndex = tokenStartingAt(node.getStartPosition()); int nodeEnd = node.getStartPosition() + node.getLength() - 1; while (ScannerHelper.isWhitespace(this.ctm.charAt(nodeEnd))) nodeEnd--; int endIndex = tokenEndingAt(nodeEnd); this.firstTagToken = startTokeen; handleHtml(node); disableFormatting(startIndex, endIndex);
return result; CommentsPreparator commentsPreparator = new CommentsPreparator(this.tokenManager, this.workingOptions, this.sourceLevel); CommentWrapExecutor commentWrapper = new CommentWrapExecutor(this.tokenManager, this.workingOptions); Token token = this.tokens.get(i); if (token.tokenType == TokenNameCOMMENT_BLOCK) { commentsPreparator.handleBlockComment(i); int startPosition = this.tokenManager.findSourcePositionInLine(token.originalStart); commentWrapper.wrapMultiLineComment(token, startPosition, false, false); Token token = this.tokens.get(i); if (token.tokenType == TokenNameCOMMENT_LINE) { commentsPreparator.handleLineComment(i); if (i >= this.tokens.size() || this.tokens.get(i) != token) {
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 handleNLSTags(Token comment, int commentIndex) { List<Token> stringLiterals = findStringLiteralsInLine(commentIndex); if (stringLiterals.isEmpty()) return;
public void handleBlockComment(int commentIndex) { Token commentToken = this.tm.get(commentIndex); boolean isFirstColumn = handleWhitespaceAround(commentIndex); if (handleFormatOnOffTags(commentToken)) return; boolean isHeader = this.tm.isInHeader(commentIndex); boolean formattingEnabled = (this.options.comment_format_block_comment && !isHeader) || (this.options.comment_format_header && isHeader); formattingEnabled = formattingEnabled && this.tm.charAt(commentToken.originalStart + 2) != '-'; if (formattingEnabled && tokenizeMultilineComment(commentToken)) { this.commentStructure = commentToken.getInternalStructure(); this.ctm = new TokenManager(this.commentStructure, this.tm); handleStringLiterals(this.tm.toString(commentToken), commentToken.originalStart); addSubstituteWraps(); } else { commentToken.setInternalStructure(commentToLines(commentToken, -1)); } if (this.options.never_indent_block_comments_on_first_column && isFirstColumn) { commentToken.setIndent(0); commentToken.setWrapPolicy(WrapPolicy.FORCE_FIRST_COLUMN); } }
private void handleFormatCodeTag(int startPos, int endPos, boolean isOpeningTag) { if (!this.options.comment_format_source) { handleNoFormatTag(startPos, endPos, isOpeningTag); return; } // add empty lines before opening and after closing token handleSeparateLineTag(startPos, endPos); if (isOpeningTag) { int startIndex = tokenStartingAt(startPos); if (startIndex > 1) this.ctm.get(startIndex).putLineBreaksBefore(2); if (this.formatCodeTagOpenEnd < 0) this.formatCodeTagOpenEnd = endPos; } else if (this.formatCodeTagOpenEnd >= 0) { int endTagIndex = tokenEndingAt(endPos); if (endTagIndex < this.ctm.size() - 2) this.ctm.get(endTagIndex).putLineBreaksAfter(2); formatCode(startPos, endPos); this.formatCodeTagOpenEnd = -1; this.lastFormatCodeClosingTagIndex = this.ctm.findIndex(startPos, -1, true); } }
@Override public boolean visit(Javadoc node) { this.noFormatTagOpenStart = -1; this.formatCodeTagOpenEnd = -1; this.lastFormatCodeClosingTagIndex = -1; this.firstTagToken = null; this.ctm = null; int commentIndex = this.tm.firstIndexIn(node, TokenNameCOMMENT_JAVADOC); Token commentToken = this.tm.get(commentIndex); if (node.getParent() == null) { // not a proper javadoc, treat as block comment handleWhitespaceAround(commentIndex); } if (commentIndex < this.tm.size() - 1) commentToken.breakAfter(); if (handleFormatOnOffTags(commentToken)) return false; boolean isHeader = this.tm.isInHeader(commentIndex); boolean formattingEnabled = (this.options.comment_format_javadoc_comment && !isHeader) || (this.options.comment_format_header && isHeader); if (!formattingEnabled || !tokenizeMultilineComment(commentToken)) { commentToken.setInternalStructure(commentToLines(commentToken, -1)); return false; } this.commentStructure = commentToken.getInternalStructure(); this.commentIndent = this.tm.toIndent(commentToken.getIndent(), true); this.ctm = new TokenManager(commentToken.getInternalStructure(), this.tm); return true; }
private void handleStringLiterals(String text, int textStartPosition) { Matcher matcher = STRING_LITERAL_PATTERN.matcher(text); while (matcher.find()) { int startPosition = textStartPosition + matcher.start(); int startIndex = this.ctm.findIndex(startPosition, -1, false); int endPosition = textStartPosition + matcher.end() - 1; int endIndex = this.ctm.findIndex(endPosition, -1, false); if (startIndex != endIndex) { startIndex = tokenStartingAt(startPosition); endIndex = tokenEndingAt(endPosition); disableFormatting(startIndex, endIndex); } noSubstituteWrapping(startPosition, endPosition); } }
private void handleNoFormatTag(int start, int end, boolean isOpeningTag) { if (isOpeningTag) { if (this.noFormatTagOpenStart < 0) this.noFormatTagOpenStart = start; } else if (this.noFormatTagOpenStart >= 0) { int openingTagIndex = tokenStartingAt(this.noFormatTagOpenStart); int closingTagIndex = tokenEndingAt(end); if (openingTagIndex < closingTagIndex) { disableFormatting(openingTagIndex, closingTagIndex); closingTagIndex = tokenEndingAt(end); } cleanupHTMLElement(openingTagIndex, closingTagIndex, false); this.noFormatTagOpenStart = -1; } }
return true; int startIndex = tokenStartingAt(node.getStartPosition()); int nodeEnd = node.getStartPosition() + node.getLength() - 1; while (ScannerHelper.isWhitespace(this.ctm.charAt(nodeEnd))) nodeEnd--; int endIndex = tokenEndingAt(nodeEnd); this.firstTagToken = startTokeen; handleHtml(node); disableFormatting(startIndex, endIndex);
return result; CommentsPreparator commentsPreparator = new CommentsPreparator(this.tokenManager, this.workingOptions, this.sourceLevel); CommentWrapExecutor commentWrapper = new CommentWrapExecutor(this.tokenManager, this.workingOptions); Token token = this.tokens.get(i); if (token.tokenType == TokenNameCOMMENT_BLOCK) { commentsPreparator.handleBlockComment(i); int startPosition = this.tokenManager.findSourcePositionInLine(token.originalStart); commentWrapper.wrapMultiLineComment(token, startPosition, false, false); Token token = this.tokens.get(i); if (token.tokenType == TokenNameCOMMENT_LINE) { commentsPreparator.handleLineComment(i); if (i >= this.tokens.size() || this.tokens.get(i) != token) {
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(); } } }