public static int getTokenEndOffsetFixed(TokenScanner scanner, int token, int startOffset, Object domNode) throws CoreException { boolean isGenerated = false; try { isGenerated = (Boolean) domNode.getClass().getField("$isGenerated").get(domNode); } catch (Exception e) { // If this fails, better to break some refactor scripts than to crash eclipse. } if (isGenerated) return -1; return scanner.getTokenEndOffset(token, startOffset); }
private void replaceOperation(int posBeforeOperation, String newOperation, TextEditGroup editGroup) { try { getScanner().readNext(posBeforeOperation, true); doTextReplace(getScanner().getCurrentStartOffset(), getScanner().getCurrentLength(), newOperation, editGroup); } catch (CoreException e) { handleException(e); } }
if (event != null && event.getChangeKind() != RewriteEvent.UNCHANGED) { try { int pos= getScanner().getTokenEndOffset(TerminalTokens.TokenNameimport, node.getStartPosition()); boolean wasStatic= ((Boolean) event.getOriginalValue()).booleanValue(); if (wasStatic) { int endPos= getScanner().getTokenEndOffset(TerminalTokens.TokenNamestatic, pos); doTextRemove(pos, endPos - pos, getEditGroup(event)); } else { } else { try { int endPos= getScanner().getTokenStartOffset(TerminalTokens.TokenNameSEMICOLON, pos); doTextRemove(pos, endPos - pos, getEditGroup(event)); } catch (CoreException e) {
int tok= getScanner().readNext(pos, true); // after the closing parent pos= (tok == TerminalTokens.TokenNameRPAREN) ? getScanner().getCurrentEndOffset() : getScanner().getCurrentStartOffset(); if (elseStatement != null) { ASTNode thenStatement = (ASTNode) thenEvent.getOriginalValue(); endPos= getScanner().getTokenStartOffset(TerminalTokens.TokenNameelse, thenStatement.getStartPosition() + thenStatement.getLength()); // else keyword
/** * Reads the next token from the given offset and returns the offset after the token. * @param offset The offset to start reading from. * @param ignoreComments If set, comments will be overread * @return Returns the start position of the next token. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int getNextEndOffset(int offset, boolean ignoreComments) throws CoreException { readNext(offset, ignoreComments); return getCurrentEndOffset(); }
/** * Reads the next token. * @param ignoreComments If set, comments will be overread * @return Return the token id. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int readNext(boolean ignoreComments) throws CoreException { int curr= 0; do { try { curr= this.scanner.getNextToken(); if (curr == TerminalTokens.TokenNameEOF) { throw new CoreException(createError(END_OF_FILE, "End Of File", null)); //$NON-NLS-1$ } } catch (InvalidInputException e) { throw new CoreException(createError(LEXICAL_ERROR, e.getMessage(), e)); } } while (ignoreComments && isComment(curr)); return curr; }
/** * @return Returns the length of the current token */ public int getCurrentLength() { return getCurrentEndOffset() - getCurrentStartOffset(); }
private void ensureSpaceAfterReplace(ASTNode node, ChildPropertyDescriptor desc) { if (getChangeKind(node, desc) == RewriteEvent.REPLACED) { int leftOperandEnd= getExtendedEnd((ASTNode) getOriginalValue(node, desc)); try { int offset= getScanner().getNextStartOffset(leftOperandEnd, true); // instanceof if (offset == leftOperandEnd) { doTextInsert(offset, String.valueOf(' '), getEditGroup(node, desc)); } } catch (CoreException e) { handleException(e); } } }
public boolean visit(AssertStatement node) { try { this.beforeRequiredSpaceIndex = getScanner().getNextEndOffset(node.getStartPosition(), true); if (!hasChildrenChanges(node)) { return doVisitUnchangedChildren(node); } ensureSpaceBeforeReplace(node); int offset= rewriteRequiredNode(node, AssertStatement.EXPRESSION_PROPERTY); rewriteNode(node, AssertStatement.MESSAGE_PROPERTY, offset, ASTRewriteFormatter.ASSERT_COMMENT); } catch (CoreException e) { handleException(e); } return false; }
private int getPreviousTokenEndOffset(ASTNode node, int token) { int offset = -1; ASTNode prev = getPreviousNode(node); if (prev != null) { offset = prev.getStartPosition() + prev.getLength(); try { offset = getScanner().getPreviousTokenEndOffset(token, offset); } catch (CoreException e1) { e1.printStackTrace(); } } return offset; }
if (isChanged(node, ClassInstanceCreation.TYPE_ARGUMENTS_PROPERTY)) { try { pos= getScanner().getTokenEndOffset(TerminalTokens.TokenNamenew, pos); //after 'new' rewriteOptionalTypeParameters(node, ClassInstanceCreation.TYPE_ARGUMENTS_PROPERTY, pos, " ", true, true); //$NON-NLS-1$ } catch (CoreException e) { int startpos= getScanner().getTokenEndOffset(TerminalTokens.TokenNameLPAREN, pos); rewriteNodeList(node, ClassInstanceCreation.ARGUMENTS_PROPERTY, startpos, Util.EMPTY_STRING, ", "); //$NON-NLS-1$ } catch (CoreException e) { if (kind == RewriteEvent.REMOVED) { try { pos= getScanner().getPreviousTokenEndOffset(TerminalTokens.TokenNameLBRACE, pos); } catch (CoreException e) { handleException(e);
/** * Reads from the given offset until a token is reached and returns the start offset of the token. * @param token The token to be found. * @param startOffset The offset to start reading from. * @return Returns the start position of the found token. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int getTokenStartOffset(int token, int startOffset) throws CoreException { readToToken(token, startOffset); return getCurrentStartOffset(); }
/** * Reads from the given offset until a token is reached and returns the offset after the token. * @param token The token to be found. * @param startOffset Offset to start reading from * @return Returns the end position of the found token. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int getTokenEndOffset(int token, int startOffset) throws CoreException { readToToken(token, startOffset); return getCurrentEndOffset(); }
/** * Reads the next token from the given offset and returns the start offset of the token. * @param offset The offset to start reading from. * @param ignoreComments If set, comments will be overread * @return Returns the start position of the next token. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int getNextStartOffset(int offset, boolean ignoreComments) throws CoreException { readNext(offset, ignoreComments); return getCurrentStartOffset(); }
this.tokenScanner= new TokenScanner(scanner);
boolean fakeInModule = getScanner().getScanner().fakeInModule; try { boolean wasOpen= ((Boolean) event.getOriginalValue()).booleanValue(); if (wasOpen) { this.tokenScanner.getScanner().fakeInModule = true; int endPos= getScanner().getTokenStartOffset(TerminalTokens.TokenNamemodule, pos); doTextRemove(pos, endPos - pos, getEditGroup(event)); } else { handleException(e); } finally { this.tokenScanner.getScanner().fakeInModule = fakeInModule; int startPos = getPosAfterLeftBrace(pos); int startIndent= getIndent(node.getStartPosition()) + 1; boolean fakeInModule = this.tokenScanner.getScanner().fakeInModule; this.tokenScanner.getScanner().fakeInModule = true; rewriteParagraphList(node, ModuleDeclaration.MODULE_DIRECTIVES_PROPERTY, startPos, startIndent, 0, 1); this.tokenScanner.getScanner().fakeInModule = fakeInModule; return false;
private void replaceOperation(int posBeforeOperation, String newOperation, TextEditGroup editGroup) { try { getScanner().readNext(posBeforeOperation, true); doTextReplace(getScanner().getCurrentStartOffset(), getScanner().getCurrentLength(), newOperation, editGroup); } catch (CoreException e) { handleException(e); } }
public boolean visit(DoStatement node) { // do statement while expression if (!hasChildrenChanges(node)) { return doVisitUnchangedChildren(node); } int pos= node.getStartPosition(); try { RewriteEvent event= getEvent(node, DoStatement.BODY_PROPERTY); if (event != null && event.getChangeKind() == RewriteEvent.REPLACED) { int startOffset= getScanner().getTokenEndOffset(TerminalTokens.TokenNamedo, pos); ASTNode body= (ASTNode) event.getOriginalValue(); int bodyEnd= body.getStartPosition() + body.getLength(); int endPos= getScanner().getTokenStartOffset(TerminalTokens.TokenNamewhile, bodyEnd); rewriteBodyNode(node, DoStatement.BODY_PROPERTY, startOffset, endPos, getIndent(node.getStartPosition()), this.formatter.DO_BLOCK); // body } else { voidVisit(node, DoStatement.BODY_PROPERTY); } } catch (CoreException e) { handleException(e); } rewriteRequiredNode(node, DoStatement.EXPRESSION_PROPERTY); return false; }
/** * Reads the next token from the given offset and returns the offset after the token. * @param offset The offset to start reading from. * @param ignoreComments If set, comments will be overread * @return Returns the start position of the next token. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int getNextEndOffset(int offset, boolean ignoreComments) throws CoreException { readNext(offset, ignoreComments); return getCurrentEndOffset(); }
/** * Reads the next token. * @param ignoreComments If set, comments will be overread * @return Return the token id. * @exception CoreException Thrown when the end of the file has been reached (code END_OF_FILE) * or a lexical error was detected while scanning (code LEXICAL_ERROR) */ public int readNext(boolean ignoreComments) throws CoreException { int curr= 0; do { try { curr= this.scanner.getNextToken(); if (curr == TerminalTokens.TokenNameEOF) { throw new CoreException(createError(END_OF_FILE, "End Of File", null)); //$NON-NLS-1$ } } catch (InvalidInputException e) { throw new CoreException(createError(LEXICAL_ERROR, e.getMessage(), e)); } } while (ignoreComments && isComment(curr)); return curr; }