/** * Whether the given token can be replaced or not. It's identical to whether * the token can be removed. */ public boolean canReplaceToken(TokenItem token) { return canRemoveToken(token); }
/** Replace the given token with the new token. * @param originalToken original token to be replaced. * @param tokenID token-id of the new token-item * @param tokenContextPath token-context-path of the new token-item * @param tokenImage token-text of the new token-item */ public void replaceToken(TokenItem originalToken, TokenID tokenID, TokenContextPath tokenContextPath, String tokenImage) { if (!canReplaceToken(originalToken)) { throw new IllegalStateException("Cannot insert token into chain"); // NOI18N } TokenItem next = originalToken.getNext(); removeToken(originalToken); insertToken(next, tokenID, tokenContextPath, tokenImage); }
/** Get the indentation of the line. * @param formatTokenPosition any position on the line. * It doesn't have to be the first one. * @param zeroForWSLine If set to true the method will return zero * in case the line consist of whitespace only. If false * the method will return the indentation even for whitespace * lines. */ public int getLineIndent(FormatTokenPosition pos, boolean zeroForWSLine) { FormatTokenPosition firstNWS = findLineFirstNonWhitespace(pos); if (firstNWS == null) { // no non-WS char on the line if (zeroForWSLine) { return 0; } else { // return indent even for WS lines firstNWS = findLineEnd(pos); } } return getVisualColumnOffset(firstNWS); }
/** * Check whether there are only the whitespace tokens on the given line. * * @param token * any token on the line. It doesn't have to be the first one. */ public boolean isLineWhite(FormatTokenPosition pos) { FormatTokenPosition lineStart = findLineStart(pos); return findLineEndWhitespace(pos).equals(lineStart); }
/** Check whether there are no tokens except the ending EOL * on the given line. * @param pos any position on the line */ public boolean isLineEmpty(FormatTokenPosition pos) { return findLineStart(pos).equals(findLineEnd(pos)); }
pos = findLineStart(pos); // go to line begining String indentString = getIndentString(indent); int indentStringLen = indentString.length(); int indentStringInd = 0; // current index in the indentString token = insertToken(null, getValidWhitespaceTokenID(), getValidWhitespaceTokenContextPath(), indentString); if (canModifyWhitespace(token)) { insertString(token, offset, indentString.substring(indentStringInd)); if (isWhitespace(token, offset) || offset > 0) { throw new IllegalStateException( "Cannot modify token=" + token); // NOI18N insertToken(token, getValidWhitespaceTokenID(), getValidWhitespaceTokenContextPath(), indentString.substring(indentStringInd) ); return getPosition(token, 0); token = token.getNext(); if (token == null) { // was last token, insert WS token token = insertToken(null, getValidWhitespaceTokenID(), getValidWhitespaceTokenContextPath(), indentString.substring(indentStringInd) ); return getPosition(token, 0);
pos = findLineEnd(pos); if (isChainStartPosition(pos)) { // empty first line return pos; pos = getPreviousPosition(pos); int textLen = text.length(); while (offset >= 0) { if (offset < textLen && ((text.charAt(offset) == '\n') || !isWhitespace(token, offset))) { return getNextPosition(token, offset); return getPosition(token, 0);
public FormatTokenPosition getLastPosition() { TokenItem lt = findNonEmptyToken(getLastToken(), true); return (lt == null) ? null : getPosition(lt, lt.getImage().length() - 1); }
/** Return the first non-whitespace character on the line * or null if there is no non-WS char on the line. */ public FormatTokenPosition findLineFirstNonWhitespace(FormatTokenPosition pos) { pos = findLineStart(pos); TokenItem token = pos.getToken(); int offset = pos.getOffset(); if (token == null) { // no line start, no WS return null; } while (true) { String text = token.getImage(); int textLen = text.length(); while (offset < textLen) { if (text.charAt(offset) == '\n') { return null; } if (!isWhitespace(token, offset)) { return getPosition(token, offset); } offset++; } if (token.getNext() == null) { return null; } token = token.getNext(); offset = 0; } }
public void insertString(FormatTokenPosition pos, String text) { TokenItem token = pos.getToken(); int offset = pos.getOffset(); if (token == null) { // ending position token = getLastToken(); if (token == null) { throw new IllegalStateException("Cannot insert string. No tokens."); // NOI18N } offset = token.getImage().length(); } insertString(token, offset, text); }
/** Get the previous token or last token if the argument is null. */ public TokenItem getPreviousToken(TokenItem token) { return (token == null) ? getLastToken() : token.getPrevious(); }
int targetOffset = pos.getOffset(); FormatTokenPosition lineStart = findLineStart(pos); TokenItem token = lineStart.getToken(); int offset = lineStart.getOffset();
pos = findLineStart(pos); // go to line begining String indentString = getIndentString(indent); int indentStringLen = indentString.length(); int indentStringInd = 0; // current index in the indentString token = insertToken(null, getValidWhitespaceTokenID(), getValidWhitespaceTokenContextPath(), indentString); if (canModifyWhitespace(token)) { insertString(token, offset, indentString.substring(indentStringInd)); if (isWhitespace(token, offset) || offset > 0) { throw new IllegalStateException("Cannot modify token=" + token); // NOI18N insertToken(token, getValidWhitespaceTokenID(), getValidWhitespaceTokenContextPath(), indentString.substring(indentStringInd)); return getPosition(token, 0); token = token.getNext(); if (token == null) { // was last token, insert WS token token = insertToken(null, getValidWhitespaceTokenID(), getValidWhitespaceTokenContextPath(), indentString.substring(indentStringInd)); return getPosition(token, 0); if (!isWhitespace(token, offset) || text.charAt(offset) == '\n') { if (removeInd >= 0) { remove(token, removeInd, offset - removeInd); offset = removeInd; return getPosition(token, offset);
pos = findLineEnd(pos); if (isChainStartPosition(pos)) { // empty first line return pos; pos = getPreviousPosition(pos); while (offset >= 0) { if (offset < textLen && ((text.charAt(offset) == '\n') || !isWhitespace(token, offset)) ) { return getNextPosition(token, offset); return getPosition(token, 0);
public FormatTokenPosition getLastPosition() { TokenItem lt = findNonEmptyToken(getLastToken(), true); return (lt == null) ? null : getPosition(lt, lt.getImage().length() - 1); }
/** * Return the first non-whitespace character on the line or null if there is * no non-WS char on the line. */ public FormatTokenPosition findLineFirstNonWhitespace(FormatTokenPosition pos) { pos = findLineStart(pos); TokenItem token = pos.getToken(); int offset = pos.getOffset(); if (token == null) { // no line start, no WS return null; } while (true) { String text = token.getImage(); int textLen = text.length(); while (offset < textLen) { if (text.charAt(offset) == '\n') { return null; } if (!isWhitespace(token, offset)) { return getPosition(token, offset); } offset++; } if (token.getNext() == null) { return null; } token = token.getNext(); offset = 0; } }
/** Check whether there are only the whitespace tokens * on the given line. * @param token any token on the line. It doesn't have to be the first one. */ public boolean isLineWhite(FormatTokenPosition pos) { FormatTokenPosition lineStart = findLineStart(pos); return findLineEndWhitespace(pos).equals(lineStart); }
/** * Check whether there are no tokens except the ending EOL on the given * line. * * @param pos * any position on the line */ public boolean isLineEmpty(FormatTokenPosition pos) { return findLineStart(pos).equals(findLineEnd(pos)); }
public void insertString(FormatTokenPosition pos, String text) { TokenItem token = pos.getToken(); int offset = pos.getOffset(); if (token == null) { // ending position token = getLastToken(); if (token == null) { throw new IllegalStateException("Cannot insert string. No tokens."); } offset = token.getImage().length(); } insertString(token, offset, text); }
/** Get the previous token or last token if the argument is null. */ public TokenItem getPreviousToken(TokenItem token) { return (token == null) ? getLastToken() : token.getPrevious(); }