use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GrCreateFieldForParameterIntention method performRefactoring.
@Override
protected void performRefactoring(Project project, PsiClass targetClass, PsiMethod method, PsiParameter myParameter, PsiType type, String fieldName, boolean methodStatic, boolean isFinal) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (targetClass.findFieldByName(fieldName, false) == null) {
String[] modifiers = getModifiers(methodStatic, isFinal);
GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type);
GrVariableDeclaration inserted = (GrVariableDeclaration) targetClass.add(fieldDeclaration);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
}
GrOpenBlock block = ((GrMethod) method).getBlock();
if (block == null)
return;
GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
GrStatement anchor = getAnchor(block);
GrStatement statement = block.addStatementBefore(assignment, anchor);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class ControlFlowBuilderUtil method isCertainlyReturnStatement.
/**
* check whether statement is return (the statement which provides return value) statement of method or closure.
*
* @param st
* @return
*/
public static boolean isCertainlyReturnStatement(GrStatement st) {
final PsiElement parent = st.getParent();
if (parent instanceof GrOpenBlock) {
if (st != ArrayUtil.getLastElement(((GrOpenBlock) parent).getStatements()))
return false;
PsiElement pparent = parent.getParent();
if (pparent instanceof GrMethod) {
return true;
}
if (pparent instanceof GrBlockStatement || pparent instanceof GrCatchClause || pparent instanceof GrLabeledStatement) {
pparent = pparent.getParent();
}
if (pparent instanceof GrIfStatement || pparent instanceof GrControlStatement || pparent instanceof GrTryCatchStatement) {
return isCertainlyReturnStatement((GrStatement) pparent);
}
} else if (parent instanceof GrClosableBlock) {
return st == ArrayUtil.getLastElement(((GrClosableBlock) parent).getStatements());
} else if (parent instanceof GroovyFileBase) {
return st == ArrayUtil.getLastElement(((GroovyFileBase) parent).getStatements());
} else if (parent instanceof GrForStatement || parent instanceof GrIfStatement && st != ((GrIfStatement) parent).getCondition() || parent instanceof GrSynchronizedStatement && st != ((GrSynchronizedStatement) parent).getMonitor() || parent instanceof GrWhileStatement && st != ((GrWhileStatement) parent).getCondition() || parent instanceof GrConditionalExpression && st != ((GrConditionalExpression) parent).getCondition() || parent instanceof GrElvisExpression) {
return isCertainlyReturnStatement((GrStatement) parent);
} else if (parent instanceof GrCaseSection) {
final GrStatement[] statements = ((GrCaseSection) parent).getStatements();
final GrStatement last = ArrayUtil.getLastElement(statements);
final GrSwitchStatement switchStatement = (GrSwitchStatement) parent.getParent();
if (last instanceof GrBreakStatement && statements.length > 1 && statements[statements.length - 2] == st) {
return isCertainlyReturnStatement(switchStatement);
} else if (st == last) {
if (st instanceof GrBreakStatement || isLastStatementInCaseSection((GrCaseSection) parent, switchStatement)) {
return isCertainlyReturnStatement(switchStatement);
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GroovyConfigSlurperCompletionProvider method getPrefix.
@Nullable
public static List<String> getPrefix(GrReferenceExpression ref) {
List<String> res = new ArrayList<>();
GrExpression qualifier = ref.getQualifierExpression();
while (qualifier != null) {
if (!(qualifier instanceof GrReferenceExpression))
return null;
GrReferenceExpression r = (GrReferenceExpression) qualifier;
String name = r.getReferenceName();
if (name == null)
return null;
res.add(name);
qualifier = r.getQualifierExpression();
}
PsiElement e = ref.getParent();
if (e instanceof GrAssignmentExpression) {
GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) e;
if (assignmentExpression.getLValue() != ref)
return null;
e = assignmentExpression.getParent();
}
while (true) {
if (e instanceof PsiFile) {
break;
} else if (e instanceof GrClosableBlock) {
PsiElement eCall = e.getParent();
if (!(eCall instanceof GrMethodCall))
return null;
GrMethodCall call = (GrMethodCall) eCall;
if (!isPropertyCall(call))
return null;
String name = extractPropertyName(call);
if (name == null)
return null;
res.add(name);
e = call.getParent();
} else if (e instanceof GrBlockStatement || e instanceof GrOpenBlock || e instanceof GrIfStatement || e instanceof GrForStatement || e instanceof GrWhileStatement || e instanceof GrTryCatchStatement) {
e = e.getParent();
} else {
return null;
}
}
Collections.reverse(res);
return res;
}
Aggregations