use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.
private static boolean isReferringToLocalVariableFromFor(Expression expression) {
ASTNode current = expression;
ASTNode parent = current.getParent();
while (parent != null && !(parent instanceof BodyDeclaration)) {
if (parent instanceof ForStatement) {
ForStatement forStmt = (ForStatement) parent;
if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
List<Expression> initializers = forStmt.initializers();
if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
expression.accept(checker);
if (checker.isReferringToForVariable()) {
return true;
}
}
}
}
current = parent;
parent = current.getParent();
}
return false;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method getEnclosingBodyNode.
private ASTNode getEnclosingBodyNode() throws JavaModelException {
ASTNode node = getSelectedExpression().getAssociatedNode();
// expression must be in a method, lambda or initializer body
// make sure it is not in method or parameter annotation
StructuralPropertyDescriptor location = null;
while (node != null && !(node instanceof BodyDeclaration)) {
location = node.getLocationInParent();
node = node.getParent();
if (node instanceof LambdaExpression) {
break;
}
}
if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
return (ASTNode) node.getStructuralProperty(location);
}
return null;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project flux by eclipse.
the class QuickAssistProcessor method getConvertStringConcatenationProposals.
private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
ASTNode node = context.getCoveringNode();
BodyDeclaration parentDecl = ASTResolving.findParentBodyDeclaration(node);
if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
return false;
AST ast = node.getAST();
// $NON-NLS-1$
ITypeBinding stringBinding = ast.resolveWellKnownType("java.lang.String");
if (node instanceof Expression && !(node instanceof InfixExpression)) {
node = node.getParent();
}
if (node instanceof VariableDeclarationFragment) {
node = ((VariableDeclarationFragment) node).getInitializer();
} else if (node instanceof Assignment) {
node = ((Assignment) node).getRightHandSide();
}
InfixExpression oldInfixExpression = null;
while (node instanceof InfixExpression) {
InfixExpression curr = (InfixExpression) node;
if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) {
// is a infix expression we can use
oldInfixExpression = curr;
} else {
break;
}
node = node.getParent();
}
if (oldInfixExpression == null)
return false;
if (resultingCollections == null) {
return true;
}
LinkedCorrectionProposal stringBufferProposal = getConvertToStringBufferProposal(context, ast, oldInfixExpression);
resultingCollections.add(stringBufferProposal);
ASTRewriteCorrectionProposal messageFormatProposal = getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
if (messageFormatProposal != null)
resultingCollections.add(messageFormatProposal);
return true;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project flux by eclipse.
the class ASTResolving method findParentBodyDeclaration.
public static BodyDeclaration findParentBodyDeclaration(ASTNode node, boolean treatModifiersOutside) {
StructuralPropertyDescriptor lastLocation = null;
while (node != null) {
if (node instanceof BodyDeclaration) {
BodyDeclaration decl = (BodyDeclaration) node;
if (!treatModifiersOutside || lastLocation != decl.getModifiersProperty()) {
return decl;
}
treatModifiersOutside = false;
}
lastLocation = node.getLocationInParent();
node = node.getParent();
}
return (BodyDeclaration) node;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project flux by eclipse.
the class CodeFormatterUtil method format2.
/**
* Creates edits that describe how to format the given string.
* The given node is used to infer the kind to use to format the string.
* Consider to use {@link #format2(int, String, int, String, Map)} if the kind is already known.
* Returns <code>null</code> if the code could not be formatted for the given kind.
*
* @param node
* Use to infer the kind of the code snippet to format.
* @param source
* The source to format
* @param indentationLevel
* The initial indentation level, used to shift left/right the entire source fragment.
* An initial indentation level of zero or below has no effect.
* @param lineSeparator
* The line separator to use in formatted source,
* if set to <code>null</code>, then the platform default one will be used.
* @param options
* The options map to use for formatting with the default code formatter.
* Recognized options are documented on {@link JavaCore#getDefaultOptions()}.
* If set to <code>null</code>, then use the current settings from {@link JavaCore#getOptions()}.
* @return an TextEdit describing the changes required to format source
* @throws IllegalArgumentException
* If the offset and length are not inside the string, a IllegalArgumentException is thrown.
*/
public static TextEdit format2(ASTNode node, String source, int indentationLevel, String lineSeparator, Map<String, String> options) {
int code;
// $NON-NLS-1$
String prefix = "";
// $NON-NLS-1$
String suffix = "";
if (node instanceof Statement) {
code = CodeFormatter.K_STATEMENTS;
if (node.getNodeType() == ASTNode.SWITCH_CASE) {
// $NON-NLS-1$
prefix = "switch(1) {";
// $NON-NLS-1$
suffix = "}";
code = CodeFormatter.K_STATEMENTS;
}
} else if (node instanceof Expression && node.getNodeType() != ASTNode.VARIABLE_DECLARATION_EXPRESSION) {
code = CodeFormatter.K_EXPRESSION;
} else if (node instanceof BodyDeclaration) {
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
} else {
switch(node.getNodeType()) {
case ASTNode.ARRAY_TYPE:
case ASTNode.PARAMETERIZED_TYPE:
case ASTNode.PRIMITIVE_TYPE:
case ASTNode.QUALIFIED_TYPE:
case ASTNode.SIMPLE_TYPE:
// $NON-NLS-1$
suffix = " x;";
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
break;
case ASTNode.WILDCARD_TYPE:
// $NON-NLS-1$
prefix = "A<";
// $NON-NLS-1$
suffix = "> x;";
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
break;
case ASTNode.COMPILATION_UNIT:
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
case ASTNode.SINGLE_VARIABLE_DECLARATION:
// $NON-NLS-1$
suffix = ";";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
// $NON-NLS-1$
prefix = "A ";
// $NON-NLS-1$
suffix = ";";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.PACKAGE_DECLARATION:
case ASTNode.IMPORT_DECLARATION:
// $NON-NLS-1$
suffix = "\nclass A {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.JAVADOC:
// $NON-NLS-1$
suffix = "void foo();";
code = CodeFormatter.K_CLASS_BODY_DECLARATIONS;
break;
case ASTNode.CATCH_CLAUSE:
// $NON-NLS-1$
prefix = "try {}";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
// $NON-NLS-1$
prefix = "new A()";
// $NON-NLS-1$
suffix = ";";
code = CodeFormatter.K_STATEMENTS;
break;
case ASTNode.MEMBER_VALUE_PAIR:
// $NON-NLS-1$
prefix = "@Author(";
// $NON-NLS-1$
suffix = ") class x {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.MODIFIER:
// $NON-NLS-1$
suffix = " class x {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.TYPE_PARAMETER:
// $NON-NLS-1$
prefix = "class X<";
// $NON-NLS-1$
suffix = "> {}";
code = CodeFormatter.K_COMPILATION_UNIT;
break;
case ASTNode.MEMBER_REF:
case ASTNode.METHOD_REF:
case ASTNode.METHOD_REF_PARAMETER:
case ASTNode.TAG_ELEMENT:
case ASTNode.TEXT_ELEMENT:
// Javadoc formatting not yet supported:
return null;
default:
// Assert.isTrue(false, "Node type not covered: " + node.getClass().getName()); //$NON-NLS-1$
return null;
}
}
String concatStr = prefix + source + suffix;
TextEdit edit = format2(code, concatStr, prefix.length(), source.length(), indentationLevel, lineSeparator, options);
if (edit != null && prefix.length() > 0) {
edit.moveTree(-prefix.length());
}
return edit;
}
Aggregations