List of usage examples for org.eclipse.jdt.internal.compiler.parser ScannerHelper isWhitespace
public static boolean isWhitespace(char c)
From source file:com.bsiag.eclipse.jdt.java.formatter.CommentsPreparator.java
License:Open Source License
private List<Token> tokenizeLineComment(Token commentToken) { List<Token> fragments = commentToken.getInternalStructure(); if (fragments == null) { fragments = Arrays.asList(commentToken); }//from w w w . ja v a 2s . c o m ArrayList<Token> result = new ArrayList<>(); for (int i = 0; i < fragments.size(); i++) { Token token = fragments.get(i); if (token.hasNLSTag()) { if (ScannerHelper.isWhitespace(this.tm.charAt(token.originalStart - 1))) token.spaceBefore(); result.add(token); continue; } int sourcePosition = token.originalStart; if (sourcePosition == commentToken.originalStart) { // separate starting slashes while (sourcePosition <= token.originalEnd && this.tm.charAt(sourcePosition) == '/') sourcePosition++; result.add(new Token(commentToken.originalStart, sourcePosition - 1, TokenNameCOMMENT_LINE)); } int tokenStart = sourcePosition; while (sourcePosition <= token.originalEnd + 1) { if (sourcePosition == token.originalEnd + 1 || ScannerHelper.isWhitespace(this.tm.charAt(sourcePosition))) { if (tokenStart < sourcePosition) { Token outputToken = new Token(tokenStart, sourcePosition - 1, TokenNameCOMMENT_LINE); outputToken.spaceBefore(); result.add(outputToken); } tokenStart = sourcePosition + 1; } sourcePosition++; } } if (this.tm.getSource().startsWith("$FALL-THROUGH$", result.get(0).originalEnd + 1)) { //$NON-NLS-1$ result.get(1).clearSpaceBefore(); } return result; }
From source file:com.bsiag.eclipse.jdt.java.formatter.CommentsPreparator.java
License:Open Source License
@Override public boolean visit(TagElement node) { String tagName = node.getTagName(); if (tagName == null || tagName.length() <= 1) return true; int startIndex = tokenStartingAt(node.getStartPosition()); int nodeEnd = node.getStartPosition() + node.getLength() - 1; while (ScannerHelper.isWhitespace(this.ctm.charAt(nodeEnd))) nodeEnd--;// w ww . j av a2 s .com int endIndex = tokenEndingAt(nodeEnd); this.ctm.get(startIndex + 1).setWrapPolicy(WrapPolicy.DISABLE_WRAP); if (node.getParent() instanceof Javadoc) { assert this.ctm.toString(startIndex).startsWith(tagName); boolean isParamTag = PARAM_TAGS.contains(tagName); if (isParamTag && this.options.comment_insert_new_line_for_parameter && startIndex < endIndex) { Token token = this.ctm.get(startIndex + 2); token.breakBefore(); } if (this.options.comment_indent_root_tags) { int indent = this.ctm.getLength(this.ctm.get(startIndex), 0) + 1; if (isParamTag && this.options.comment_indent_parameter_description) indent += this.options.indentation_size; for (int i = startIndex + 1; i <= endIndex; i++) { Token token = this.ctm.get(i); token.setIndent(indent); // indent is used temporarily, tokens that are actually first in line // will have this changed to align (indent is reserved for code inside <pre> tags) } } Token startTokeen = this.ctm.get(startIndex); if (startIndex > 1) startTokeen.breakBefore(); int firstTagIndex; if (this.firstTagToken == null || (firstTagIndex = this.ctm.indexOf(this.firstTagToken)) < 0 || startIndex < firstTagIndex) this.firstTagToken = startTokeen; handleHtml(node); } if (node.isNested()) { substituteWrapIfTouching(startIndex); substituteWrapIfTouching(endIndex + 1); if (IMMUTABLE_TAGS.contains(tagName) && startIndex < endIndex) disableFormatting(startIndex, endIndex); noSubstituteWrapping(node.getStartPosition(), nodeEnd); } return true; }
From source file:com.bsiag.eclipse.jdt.java.formatter.CommentsPreparator.java
License:Open Source License
private int findCommentLineIndent(int commentFragmentIndex) { int position = this.ctm.get(commentFragmentIndex).originalStart; int lastNonWhitespace = position; while (--position > 0) { char c = this.ctm.charAt(position); if (c == '\r' || c == '\n') break; if (!ScannerHelper.isWhitespace(c)) lastNonWhitespace = position; }/*from ww w . ja v a 2 s . com*/ if (lastNonWhitespace > 0 && this.ctm.charAt(lastNonWhitespace - 1) == ' ') lastNonWhitespace--; return this.ctm.getLength(position, lastNonWhitespace - 1, 0); }
From source file:com.bsiag.eclipse.jdt.java.formatter.CommentsPreparator.java
License:Open Source License
private boolean tokenizeMultilineComment(Token commentToken) { if (this.noSubstituteWrapping == null || this.noSubstituteWrapping.length < commentToken.countChars()) { this.noSubstituteWrapping = new boolean[commentToken.countChars()]; } else {/*from w w w . ja v a2s . co m*/ Arrays.fill(this.noSubstituteWrapping, 0, commentToken.countChars(), false); } final boolean cleanBlankLines = commentToken.tokenType == TokenNameCOMMENT_JAVADOC ? this.options.comment_clear_blank_lines_in_javadoc_comment : this.options.comment_clear_blank_lines_in_block_comment; List<Token> structure = new ArrayList<>(); int firstTokenEnd = commentToken.originalStart + 1; while (firstTokenEnd < commentToken.originalEnd - 1 && this.tm.charAt(firstTokenEnd + 1) == '*') firstTokenEnd++; Token first = new Token(commentToken.originalStart, firstTokenEnd, commentToken.tokenType); first.spaceAfter(); structure.add(first); int lastTokenStart = commentToken.originalEnd - 1; while (lastTokenStart - 1 > firstTokenEnd && this.tm.charAt(lastTokenStart - 1) == '*') lastTokenStart--; int position = firstTokenEnd + 1; int lineBreaks = 0; while (position <= commentToken.originalEnd) { // find line start for (int i = position; i < lastTokenStart; i++) { char c = this.tm.charAt(i); if (c == '\r' || c == '\n') { lineBreaks++; char c2 = this.tm.charAt(i + 1); if ((c2 == '\r' || c2 == '\n') && c2 != c) i++; position = i + 1; } else if (!ScannerHelper.isWhitespace(c)) { while (this.tm.charAt(i) == '*' && lineBreaks > 0) i++; position = i; break; } } int tokenStart = position; while (position <= commentToken.originalEnd + 1) { char c = 0; if (position == commentToken.originalEnd + 1 || position == lastTokenStart || ScannerHelper.isWhitespace(c = this.tm.charAt(position))) { if (tokenStart < position) { Token outputToken = new Token(tokenStart, position - 1, commentToken.tokenType); outputToken.spaceBefore(); if (lineBreaks > 0) { if (cleanBlankLines) lineBreaks = 1; if (lineBreaks > 1 || !this.options.join_lines_in_comments) outputToken.putLineBreaksBefore(lineBreaks); } if (this.tm.charAt(tokenStart) == '@') { outputToken.setWrapPolicy(WrapPolicy.DISABLE_WRAP); if (commentToken.tokenType == TokenNameCOMMENT_BLOCK && lineBreaks == 1 && structure.size() > 1) outputToken.putLineBreaksBefore(cleanBlankLines ? 1 : 2); if (this.tm.charAt(tokenStart + 1) == '@' && lineBreaks > 0 && this.firstTagToken == null) { // Commons Attributes annotation, see bug 237051 this.firstTagToken = outputToken; } } structure.add(outputToken); lineBreaks = 0; } if (c == '\r' || c == '\n') break; tokenStart = position == lastTokenStart ? position : position + 1; } position++; } } Token last = structure.get(structure.size() - 1); boolean newLinesAtBoundries = commentToken.tokenType == TokenNameCOMMENT_JAVADOC ? this.options.comment_new_lines_at_javadoc_boundaries : this.options.comment_new_lines_at_block_boundaries; if (newLinesAtBoundries && this.tm.countLineBreaksBetween(first, last) > 0) { first.breakAfter(); last.breakBefore(); last.setAlign(1); } if (structure.size() == 2) return false; commentToken.setInternalStructure(structure); return true; }
From source file:com.bsiag.eclipse.jdt.java.formatter.CommentsPreparator.java
License:Open Source License
private void getCodeToFormat(int startPos, int endPos, StringBuilder sb, int[] posMapping) { int position = 0; // original source position (minus startPos) // skip excessive line break at the beginning char c, c2;//from w w w. j a va 2 s . co m if ((c = this.ctm.charAt(position + startPos)) == '\r' || c == '\n') { posMapping[position++] = sb.length() - 1; if (((c2 = this.ctm.charAt(position + startPos)) == '\r' || c2 == '\n') && c2 != c) posMapping[position++] = sb.length() - 1; } while (position + startPos <= endPos) { int lineStart = position + startPos; for (int i = lineStart;; i++) { c = this.ctm.charAt(i); if (c == '\r' || c == '\n') { sb.append(c); lineStart = i + 1; } else if (!ScannerHelper.isWhitespace(c)) { if (c == '*') lineStart = (this.ctm.charAt(i + 1) == ' ') ? i + 2 : i + 1; break; } } int lineEnd = endPos + 1; for (int i = lineStart; i <= endPos; i++) { c = this.ctm.charAt(i); if (c == '\r' || c == '\n') { lineEnd = i; break; } } while (position + startPos < lineStart) { posMapping[position++] = sb.length() - 1; } int htmlEntityStart = -1; for (int i = lineStart; i < lineEnd; i++) { c = this.ctm.charAt(i); sb.append(c); posMapping[position++] = sb.length() - 1; if (c == '&') { htmlEntityStart = i; } else if (c == ';' && htmlEntityStart >= 0) { char replacementChar = getHtmlEntityChar( this.ctm.getSource().substring(htmlEntityStart, i + 1)); if (replacementChar != 0) { sb.setLength(sb.length() - (i + 1 - htmlEntityStart)); sb.append(replacementChar); for (int k = position - (i + 1 - htmlEntityStart); k < position; k++) posMapping[k] = sb.length() - 1; } htmlEntityStart = -1; } } } // remove last line if empty while (sb.length() > 0 && ((c = sb.charAt(sb.length() - 1)) == ' ' || c == '\t')) sb.deleteCharAt(sb.length() - 1); if (sb.length() > 0 && ((c = sb.charAt(sb.length() - 1)) == '\r' || c == '\n')) { sb.deleteCharAt(sb.length() - 1); if (sb.length() > 0 && ((c2 = sb.charAt(sb.length() - 1)) == '\r' || c2 == '\n') && c2 != c) sb.deleteCharAt(sb.length() - 1); } }
From source file:com.bsiag.eclipse.jdt.java.formatter.TextEditsBuilder.java
License:Open Source License
private void bufferLineSeparator(Token token, boolean emptyLine) { if (this.parent == null) { this.buffer.append(this.options.line_separator); return;/*from ww w. j a v a2 s . c om*/ } this.parent.counter = this.counter; this.parent.bufferLineSeparator(null, false); this.counter = this.parent.counter; bufferIndent(this.parent.tm.get(this.parentTokenIndex), -1); if (token != null && token.tokenType == TokenNameNotAToken) return; // this is an unformatted block comment, don't force asterisk if (getNext() == null && !emptyLine) return; // this is the last token of block comment, asterisk is included boolean asteriskFound = false; int searchLimit = token != null ? token.originalStart : this.sourceLimit; for (int i = this.counter; i < searchLimit; i++) { char c = this.source.charAt(i); if (c == '*') { this.buffer.append(' '); flushBuffer(i); while (i + 1 < this.sourceLimit && this.source.charAt(i + 1) == '*') i++; this.counter = i + 1; c = this.source.charAt(i + 1); if ((c != '\r' && c != '\n') || !emptyLine) this.buffer.append(' '); asteriskFound = true; break; } if (!ScannerHelper.isWhitespace(c)) break; } if (!asteriskFound) this.buffer.append(" * "); //$NON-NLS-1$ }
From source file:com.bsiag.eclipse.jdt.java.formatter.TextEditsBuilder.java
License:Open Source License
private boolean isOnlyWhitespace(String text) { for (int i = 0; i < text.length(); i++) if (!ScannerHelper.isWhitespace(text.charAt(i))) return false; return true;//from w w w . j a v a 2s.com }
From source file:com.bsiag.eclipse.jdt.java.formatter.TextEditsBuilder.java
License:Open Source License
private String adaptReplaceText(String text, int breaksToPreserve, boolean isRegionEnd, int regionEdge) { int i = isRegionEnd ? 0 : text.length() - 1; int direction = isRegionEnd ? 1 : -1; int preservedBreaks = 0; for (; i >= 0 && i < text.length(); i += direction) { assert ScannerHelper.isWhitespace(text.charAt(i)); char c1 = text.charAt(i); if (c1 == '\r' || c1 == '\n') { if (preservedBreaks >= breaksToPreserve) break; preservedBreaks++;//from w w w .j av a2s .c o m int i2 = i + direction; if (i2 >= 0 && i2 < text.length()) { char c2 = text.charAt(i2); if ((c2 == '\r' || c2 == '\n') && c2 != c1) i = i2; } } } text = isRegionEnd ? text.substring(0, i) : text.substring(i + 1); // cut out text if the source outside region is a matching whitespace int textPos = isRegionEnd ? text.length() - 1 : 0; int sourcePos = regionEdge; theLoop: while (textPos >= 0 && textPos < text.length() && sourcePos >= 0 && sourcePos < this.source.length()) { char c1 = text.charAt(textPos); char c2 = this.source.charAt(sourcePos); if (c1 == c2 && (c1 == ' ' || c1 == '\t')) { textPos -= direction; sourcePos += direction; } else if (c1 == '\t' && c2 == ' ') { for (i = 0; i < this.options.tab_size; i++) { sourcePos += direction; if (i < this.options.tab_size - 1 && (sourcePos < 0 || sourcePos >= this.source.length() || this.source.charAt(sourcePos) != ' ')) continue theLoop; } textPos -= direction; } else if (c2 == '\t' && c1 == ' ') { for (i = 0; i < this.options.tab_size; i++) { textPos -= direction; if (i < this.options.tab_size - 1 && (textPos < 0 || textPos >= text.length() || text.charAt(textPos) != ' ')) continue theLoop; } sourcePos += direction; } else { break; } } if (isRegionEnd) { text = text.substring(0, textPos + 1); } else { text = text.substring(textPos); } return text; }
From source file:com.ibm.research.tours.content.url.Signature.java
License:Open Source License
private static int checkName(char[] name, char[] typeName, int pos, int length) { if (CharOperation.fragmentEquals(name, typeName, pos, true)) { pos += name.length;/*from w w w .java 2 s. c o m*/ if (pos == length) return pos; char currentChar = typeName[pos]; switch (currentChar) { case ' ': case '.': case '<': case '>': case '[': case ',': return pos; default: if (ScannerHelper.isWhitespace(currentChar)) return pos; } } return -1; }
From source file:com.ibm.research.tours.content.url.Signature.java
License:Open Source License
private static int encodeQualifiedName(char[] typeName, int pos, int length, StringBuffer buffer) { int count = 0; char lastAppendedChar = 0; nameLoop: while (pos < length) { char currentChar = typeName[pos]; switch (currentChar) { case '<': case '>': case '[': case ',': break nameLoop; case '.': buffer.append(C_DOT);/*from ww w . ja v a 2s . c o m*/ lastAppendedChar = C_DOT; count++; break; default: if (currentChar == ' ' || ScannerHelper.isWhitespace(currentChar)) { if (lastAppendedChar == C_DOT) { // allow spaces after a dot pos = consumeWhitespace(typeName, pos, length) - 1; // will be incremented break; } // allow spaces before a dot int checkPos = checkNextChar(typeName, '.', pos, length, true); if (checkPos > 0) { buffer.append(C_DOT); // process dot immediately to avoid one iteration lastAppendedChar = C_DOT; count++; pos = checkPos; break; } break nameLoop; } buffer.append(currentChar); lastAppendedChar = currentChar; count++; break; } pos++; } if (count == 0) throw new IllegalArgumentException(new String(typeName)); return pos; }