Java Code Examples for org.eclipse.jdt.core.dom.Expression#getLength()

The following examples show how to use org.eclipse.jdt.core.dom.Expression#getLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InvertBooleanUtility.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) {
	if (expression.getStartPosition() + expression.getLength() <= operatorOffset) {
		// add to the left
		res[0] = combineOperands(rewrite, res[0], expression, removeParentheses, operator);
		return;
	}
	if (operatorOffset <= expression.getStartPosition()) {
		// add to the right
		res[1] = combineOperands(rewrite, res[1], expression, removeParentheses, operator);
		return;
	}
	if (!(expression instanceof InfixExpression)) {
		throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$
	}
	InfixExpression infixExpression = (InfixExpression) expression;
	if (infixExpression.getOperator() != operator) {
		throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$
	}
	breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res);
	breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res);

	List<Expression> extended = infixExpression.extendedOperands();
	for (int i = 0; i < extended.size(); i++) {
		breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res);
	}
}
 
Example 2
Source File: IntroduceParameterRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeSelectedExpression(CompilationUnitRewrite cuRewrite) throws JavaModelException {
	IASTFragment fragment= ASTFragmentFactory.createFragmentForSourceRange(
			new SourceRange(fSelectionStart, fSelectionLength), cuRewrite.getRoot(), cuRewrite.getCu());

	if (! (fragment instanceof IExpressionFragment))
		return;

	//TODO: doesn't handle selection of partial Expressions
	Expression expression= ((IExpressionFragment) fragment).getAssociatedExpression();
	if (fragment.getStartPosition() != expression.getStartPosition()
			|| fragment.getLength() != expression.getLength())
		return;

	if (Checks.isInsideJavadoc(expression))
		return;
	//TODO: exclude invalid selections
	if (Checks.isEnumCase(expression.getParent()))
		return;

	fSelectedExpression= expression;
}
 
Example 3
Source File: AdvancedQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) {
	if (expression.getStartPosition() + expression.getLength() <= operatorOffset) {
		// add to the left
		res[0]= combineOperands(rewrite, res[0], expression, removeParentheses, operator);
		return;
	}
	if (operatorOffset <= expression.getStartPosition()) {
		// add to the right
		res[1]= combineOperands(rewrite, res[1], expression, removeParentheses, operator);
		return;
	}
	if (!(expression instanceof InfixExpression)) {
		throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$
	}
	InfixExpression infixExpression= (InfixExpression) expression;
	if (infixExpression.getOperator() != operator) {
		throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$
	}
	breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res);
	breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res);

	List<Expression> extended= infixExpression.extendedOperands();
	for (int i= 0; i < extended.size(); i++) {
		breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res);
	}
}
 
Example 4
Source File: PreconditionExaminer.java    From JDeodorant with MIT License 5 votes vote down vote up
private boolean isInsideDifference(SimpleName simpleName, Expression difference) {
	int startOffset = simpleName.getStartPosition();
	int endOffset = simpleName.getStartPosition() + simpleName.getLength();
	int differenceStartOffset = difference.getStartPosition();
	int differenceEndOffset = difference.getStartPosition() + difference.getLength();
	if(startOffset >= differenceStartOffset && endOffset <= differenceEndOffset)
		return true;
	return false;
}
 
Example 5
Source File: AssociativeInfixExpressionFragment.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static ISourceRange getRangeOfOperands(List<Expression> operands) {
	Expression first = operands.get(0);
	Expression last = operands.get(operands.size() - 1);
	return new SourceRange(first.getStartPosition(), last.getStartPosition() + last.getLength() - first.getStartPosition());
}
 
Example 6
Source File: ChangeTypeRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void setSelectionRanges(Expression exp){
	fEffectiveSelectionStart= exp.getStartPosition();
	fEffectiveSelectionLength= exp.getLength();
	fSelectionBinding= ExpressionVariable.resolveBinding(exp);
	setOriginalType(exp.resolveTypeBinding());
}
 
Example 7
Source File: AssociativeInfixExpressionFragment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ISourceRange getRangeOfOperands(List<Expression> operands) {
	Expression first= operands.get(0);
	Expression last= operands.get(operands.size() - 1);
	return new SourceRange(first.getStartPosition(), last.getStartPosition() + last.getLength() - first.getStartPosition());
}