if (nlsNumber > 0 && nlsNumber <= stringLiterals.size()) { if (matcher.start() > previousMatcherEnd) { Token fragment = new Token(comment.originalStart + previousMatcherEnd, comment.originalStart + matcher.start() - 1, TokenNameCOMMENT_LINE); commentFragments.add(fragment); Token nlsTag = new Token(comment.originalStart + matcher.start(), comment.originalStart + matcher.end() - 1, TokenNameCOMMENT_LINE); stringLiterals.get(nlsNumber - 1).setNLSTag(nlsTag); comment.setInternalStructure(commentFragments); if (comment.originalStart + previousMatcherEnd <= comment.originalEnd) { Token fragment = new Token(comment.originalStart + previousMatcherEnd, comment.originalEnd, TokenNameCOMMENT_LINE); commentFragments.add(fragment);
public static Token fromCurrent(Scanner scanner, int currentToken) { int start = scanner.getCurrentTokenStartPosition(); int end = scanner.getCurrentTokenEndPosition(); if (currentToken == TokenNameCOMMENT_LINE) { // don't include line separator while(end >= start) { char c = scanner.source[end]; if (c != '\r' && c != '\n') break; end--; } } Token token = new Token(start, end, currentToken); return token; }
public static Token fromCurrent(Scanner scanner, int currentToken) { int start = scanner.getCurrentTokenStartPosition(); int end = scanner.getCurrentTokenEndPosition(); if (currentToken == TokenNameCOMMENT_LINE) { // don't include line separator while(end >= start) { char c = scanner.source[end]; if (c != '\r' && c != '\n') break; end--; } } Token token = new Token(start, end, currentToken); return token; }
public static Token fromCurrent(Scanner scanner, int currentToken) { int start = scanner.getCurrentTokenStartPosition(); int end = scanner.getCurrentTokenEndPosition(); if (currentToken == TokenNameCOMMENT_LINE) { // don't include line separator while(end >= start) { char c = scanner.source[end]; if (c != '\r' && c != '\n') break; end--; } } Token token = new Token(start, end, currentToken); return token; }
private void preserveWhitespace(Token commentToken, int commentIndex) { if (this.options.comment_preserve_white_space_between_code_and_line_comments && commentToken.getLineBreaksBefore() == 0 && commentIndex > 0) { commentToken.clearSpaceBefore(); List<Token> structure = commentToken.getInternalStructure(); if (structure != null && !structure.isEmpty()) structure.get(0).clearSpaceBefore(); Token previous = this.tm.get(commentIndex - 1); previous.clearSpaceAfter(); if (previous.originalEnd + 1 >= commentToken.originalStart) return; if (structure == null || structure.isEmpty()) { structure = new ArrayList<>(); structure.add(new Token(previous.originalEnd + 1, commentToken.originalEnd, TokenNameCOMMENT_LINE)); commentToken.setInternalStructure(structure); } else { structure.add(0, new Token(previous.originalEnd + 1, commentToken.originalStart - 1, TokenNameWHITESPACE)); } } }
private void preserveWhitespace(Token commentToken, int commentIndex) { if (this.options.comment_preserve_white_space_between_code_and_line_comments && commentToken.getLineBreaksBefore() == 0 && commentIndex > 0) { commentToken.clearSpaceBefore(); List<Token> structure = commentToken.getInternalStructure(); if (structure != null && !structure.isEmpty()) structure.get(0).clearSpaceBefore(); Token previous = this.tm.get(commentIndex - 1); previous.clearSpaceAfter(); if (previous.originalEnd + 1 >= commentToken.originalStart) return; if (structure == null || structure.isEmpty()) { structure = new ArrayList<>(); structure.add(new Token(previous.originalEnd + 1, commentToken.originalEnd, TokenNameCOMMENT_LINE)); commentToken.setInternalStructure(structure); } else { structure.add(0, new Token(previous.originalEnd + 1, commentToken.originalStart - 1, TokenNameWHITESPACE)); } } }
private void preserveWhitespace(Token commentToken, int commentIndex) { if (this.options.comment_preserve_white_space_between_code_and_line_comments && commentToken.getLineBreaksBefore() == 0 && commentIndex > 0) { commentToken.clearSpaceBefore(); List<Token> structure = commentToken.getInternalStructure(); if (structure != null && !structure.isEmpty()) structure.get(0).clearSpaceBefore(); Token previous = this.tm.get(commentIndex - 1); previous.clearSpaceAfter(); if (previous.originalEnd + 1 >= commentToken.originalStart) return; if (structure == null || structure.isEmpty()) { structure = new ArrayList<>(); structure.add(new Token(previous.originalEnd + 1, commentToken.originalEnd, TokenNameCOMMENT_LINE)); commentToken.setInternalStructure(structure); } else { structure.add(0, new Token(previous.originalEnd + 1, commentToken.originalStart - 1, TokenNameWHITESPACE)); } } }
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 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 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 splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
private void disableFormatting(int startIndex, int endIndex) { Token startToken = this.ctm.get(startIndex), endToken = this.ctm.get(endIndex); Token noFormatToken = new Token(startToken.originalStart, endToken.originalEnd, TokenNameCOMMENT_JAVADOC); List<Token> tokensToReplace = this.commentStructure.subList(startIndex, endIndex + 1); if (this.ctm.countLineBreaksBetween(startToken, endToken) == 0) { tokensToReplace.clear(); tokensToReplace.add(noFormatToken); } else { int commentStart = findCommentLineIndent(startIndex); tokensToReplace.clear(); tokensToReplace.addAll(commentToLines(noFormatToken, commentStart)); } Token first = tokensToReplace.get(0), last = tokensToReplace.get(tokensToReplace.size() - 1); if (startToken.isSpaceBefore()) first.spaceBefore(); first.putLineBreaksBefore(startToken.getLineBreaksBefore()); first.setWrapPolicy(startToken.getWrapPolicy()); if (endToken.isSpaceAfter()) last.spaceAfter(); last.putLineBreaksAfter(endToken.getLineBreaksAfter()); for (Token token : tokensToReplace) if (token.tokenType == TokenNameCOMMENT_JAVADOC) token.setIndent(startToken.getIndent()); }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
private void disableFormatting(int startIndex, int endIndex) { Token startToken = this.ctm.get(startIndex), endToken = this.ctm.get(endIndex); Token noFormatToken = new Token(startToken.originalStart, endToken.originalEnd, TokenNameCOMMENT_JAVADOC); List<Token> tokensToReplace = this.commentStructure.subList(startIndex, endIndex + 1); if (this.ctm.countLineBreaksBetween(startToken, endToken) == 0) { tokensToReplace.clear(); tokensToReplace.add(noFormatToken); } else { int commentStart = findCommentLineIndent(startIndex); tokensToReplace.clear(); tokensToReplace.addAll(commentToLines(noFormatToken, commentStart)); } Token first = tokensToReplace.get(0), last = tokensToReplace.get(tokensToReplace.size() - 1); if (startToken.isSpaceBefore()) first.spaceBefore(); first.putLineBreaksBefore(startToken.getLineBreaksBefore()); first.setWrapPolicy(startToken.getWrapPolicy()); if (endToken.isSpaceAfter()) last.spaceAfter(); last.putLineBreaksAfter(endToken.getLineBreaksAfter()); for (Token token : tokensToReplace) if (token.tokenType == TokenNameCOMMENT_JAVADOC) token.setIndent(startToken.getIndent()); }
private void splitToken(Token token, int tokenIndex, int splitPosition) { assert splitPosition > token.originalStart && splitPosition <= token.originalEnd; Token part1 = new Token(token.originalStart, splitPosition - 1, token.tokenType); Token part2 = new Token(splitPosition, token.originalEnd, token.tokenType); if (token.isSpaceBefore()) part1.spaceBefore(); part1.putLineBreaksBefore(token.getLineBreaksBefore()); if (token.isSpaceAfter()) part2.spaceAfter(); part2.putLineBreaksAfter(token.getLineBreaksAfter()); part1.setIndent(token.getIndent()); part2.setIndent(token.getIndent()); part1.setWrapPolicy(token.getWrapPolicy()); this.commentStructure.set(tokenIndex, part1); this.commentStructure.add(tokenIndex + 1, part2); }
private void disableFormatting(int startIndex, int endIndex) { Token startToken = this.ctm.get(startIndex), endToken = this.ctm.get(endIndex); Token noFormatToken = new Token(startToken.originalStart, endToken.originalEnd, TokenNameCOMMENT_JAVADOC); List<Token> tokensToReplace = this.commentStructure.subList(startIndex, endIndex + 1); if (this.ctm.countLineBreaksBetween(startToken, endToken) == 0) { tokensToReplace.clear(); tokensToReplace.add(noFormatToken); } else { int commentStart = findCommentLineIndent(startIndex); tokensToReplace.clear(); tokensToReplace.addAll(commentToLines(noFormatToken, commentStart)); } Token first = tokensToReplace.get(0), last = tokensToReplace.get(tokensToReplace.size() - 1); if (startToken.isSpaceBefore()) first.spaceBefore(); first.putLineBreaksBefore(startToken.getLineBreaksBefore()); first.setWrapPolicy(startToken.getWrapPolicy()); if (endToken.isSpaceAfter()) last.spaceAfter(); last.putLineBreaksAfter(endToken.getLineBreaksAfter()); for (Token token : tokensToReplace) if (token.tokenType == TokenNameCOMMENT_JAVADOC) token.setIndent(startToken.getIndent()); }
public void applyFormatOff() { if (this.formatOffTagPairs == null) return; for (Token[] pair : this.formatOffTagPairs) { int index1 = findIndex(pair[0].originalStart, -1, false); int index2 = findIndex(pair[1].originalEnd, -1, false); pair[0] = get(index1); pair[1] = get(index2); Token unformatted = new Token(pair[0].originalStart, pair[1].originalEnd, TokenNameWHITESPACE); unformatted.setIndent(Math.min(pair[0].getIndent(), findSourcePositionInLine(pair[0].originalStart))); unformatted.putLineBreaksBefore(pair[0].getLineBreaksBefore()); if (pair[0].isSpaceBefore()) unformatted.spaceBefore(); unformatted.putLineBreaksAfter(pair[1].getLineBreaksAfter()); if (pair[1].isSpaceAfter()) unformatted.spaceAfter(); this.tokens.set(index1, unformatted); this.tokens.subList(index1 + 1, index2 + 1).clear(); } } }
private void handleTokenAfter(ASTNode node, int tokenType, boolean spaceBefore, boolean spaceAfter) { if (spaceBefore || spaceAfter) { if (tokenType == TokenNameGREATER) { // there could be ">>" or ">>>" instead, get rid of them int index = this.tm.lastIndexIn(node, -1); for (int i = index; i < index + 2; i++) { Token token = this.tm.get(i); if (token.tokenType == TokenNameRIGHT_SHIFT || token.tokenType == TokenNameUNSIGNED_RIGHT_SHIFT) { this.tm.remove(i); for (int j = 0; j < (token.tokenType == TokenNameRIGHT_SHIFT ? 2 : 3); j++) { this.tm.insert(i + j, new Token(token.originalStart + j, token.originalStart + j, TokenNameGREATER)); } } } } Token token = this.tm.firstTokenAfter(node, tokenType); handleToken(token, spaceBefore, spaceAfter); } }
private void handleTokenAfter(ASTNode node, int tokenType, boolean spaceBefore, boolean spaceAfter) { if (tokenType == TokenNameGREATER) { // there could be ">>" or ">>>" instead, get rid of them int index = this.tm.lastIndexIn(node, -1); for (int i = index; i < index + 2; i++) { Token token = this.tm.get(i); if (token.tokenType == TokenNameRIGHT_SHIFT || token.tokenType == TokenNameUNSIGNED_RIGHT_SHIFT) { this.tm.remove(i); for (int j = 0; j < (token.tokenType == TokenNameRIGHT_SHIFT ? 2 : 3); j++) { this.tm.insert(i + j, new Token(token.originalStart + j, token.originalStart + j, TokenNameGREATER)); } } } } if (spaceBefore || spaceAfter) { Token token = this.tm.firstTokenAfter(node, tokenType); handleToken(token, spaceBefore, spaceAfter); } }
private void handleTokenAfter(ASTNode node, int tokenType, boolean spaceBefore, boolean spaceAfter) { if (tokenType == TokenNameGREATER) { // there could be ">>" or ">>>" instead, get rid of them int index = this.tm.lastIndexIn(node, -1); for (int i = index; i < index + 2; i++) { Token token = this.tm.get(i); if (token.tokenType == TokenNameRIGHT_SHIFT || token.tokenType == TokenNameUNSIGNED_RIGHT_SHIFT) { this.tm.remove(i); for (int j = 0; j < (token.tokenType == TokenNameRIGHT_SHIFT ? 2 : 3); j++) { this.tm.insert(i + j, new Token(token.originalStart + j, token.originalStart + j, TokenNameGREATER)); } } } } if (spaceBefore || spaceAfter) { Token token = this.tm.firstTokenAfter(node, tokenType); handleToken(token, spaceBefore, spaceAfter); } }