use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (element instanceof GrVariableDeclaration) {
GrVariableDeclaration decl = (GrVariableDeclaration) element;
GrVariable[] variables = decl.getVariables();
if (variables.length > 1 && PsiUtil.isLocalVariable(variables[0])) {
if (!decl.isTuple() || decl.getTupleInitializer() instanceof GrListOrMap) {
myText = GroovyIntentionsBundle.message("split.into.separate.declaration");
} else {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
}
return true;
} else if (variables.length == 1 && PsiUtil.isLocalVariable(variables[0]) && variables[0].getInitializerGroovy() != null) {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
return true;
}
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GroovySmartCompletionContributor method inferDiamond.
@Nullable
private static PsiType inferDiamond(PsiElement place) {
if (!GroovyConfigUtils.getInstance().isVersionAtLeast(place, GroovyConfigUtils.GROOVY1_8)) {
return null;
}
final PsiElement parent = place.getParent().getParent();
if (!(parent instanceof GrNewExpression))
return null;
final PsiElement pparent = parent.getParent();
if (pparent instanceof GrVariable) {
return ((GrVariable) pparent).getDeclaredType();
} else if (pparent instanceof GrAssignmentExpression) {
GrAssignmentExpression assignment = (GrAssignmentExpression) pparent;
IElementType optoken = assignment.getOperationTokenType();
GrExpression lvalue = assignment.getLValue();
GrExpression rvalue = assignment.getRValue();
if (parent == rvalue && optoken == GroovyTokenTypes.mASSIGN) {
return lvalue.getNominalType();
}
} else if (pparent instanceof GrApplicationStatement) {
PsiElement ppparent = pparent.getParent();
if (ppparent instanceof GrAssignmentExpression) {
GrAssignmentExpression assignment = (GrAssignmentExpression) ppparent;
IElementType optoken = assignment.getOperationTokenType();
GrExpression lvalue = assignment.getLValue();
GrExpression rvalue = assignment.getRValue();
if (pparent == rvalue && optoken == GroovyTokenTypes.mASSIGN) {
return lvalue.getNominalType();
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ExtractClosureFromClosureProcessor method findUsages.
@NotNull
@Override
protected UsageInfo[] findUsages() {
final GrVariable var = (GrVariable) myHelper.getToSearchFor();
if (var != null) {
final List<UsageInfo> result = new ArrayList<>();
for (PsiReference ref : ReferencesSearch.search(var)) {
final PsiElement element = ref.getElement();
if (element.getLanguage() != GroovyLanguage.INSTANCE) {
result.add(new OtherLanguageUsageInfo(ref));
continue;
}
final GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
if (call == null)
continue;
result.add(new ExternalUsageInfo(element));
}
return result.toArray(new UsageInfo[result.size()]);
}
return UsageInfo.EMPTY_ARRAY;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GroovyInlineLocalHandler method createSettings.
/**
* Returns Settings object for referenced definition in case of local variable
*/
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
final String localName = variable.getName();
final Project project = variable.getProject();
GrExpression initializer = null;
Instruction writeInstr = null;
Instruction[] flow = null;
//search for initializer to inline
if (invokedOnReference) {
LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
final PsiReference ref = TargetElementUtil.findReference(editor);
LOG.assertTrue(ref != null);
PsiElement cur = ref.getElement();
if (cur instanceof GrReferenceExpression) {
GrControlFlowOwner controlFlowOwner;
do {
controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
if (controlFlowOwner == null)
break;
flow = controlFlowOwner.getControlFlow();
final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
final PsiElement finalCur = cur;
Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
LOG.assertTrue(instruction != null);
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1) {
writeInstr = flow[prev.nextSetBit(0)];
final PsiElement element = writeInstr.getElement();
if (element instanceof GrVariable) {
initializer = ((GrVariable) element).getInitializerGroovy();
} else if (element instanceof GrReferenceExpression) {
initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
}
}
PsiElement old_cur = cur;
if (controlFlowOwner instanceof GrClosableBlock) {
cur = controlFlowOwner;
} else {
PsiElement parent = controlFlowOwner.getParent();
if (parent instanceof GrMember)
cur = ((GrMember) parent).getContainingClass();
}
if (cur == old_cur)
break;
} while (initializer == null);
}
} else {
flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
initializer = variable.getInitializerGroovy();
writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
}
if (initializer == null || writeInstr == null) {
String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
return null;
}
int writeInstructionNumber = writeInstr.num();
if (ApplicationManager.getApplication().isUnitTestMode()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
if (dialog.showAndGet()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrAbstractInplaceIntroducer method getVariable.
@Override
protected GrVariable getVariable() {
if (myVarMarker == null)
return null;
int offset = myVarMarker.getStartOffset();
PsiElement at = myFile.findElementAt(offset);
GrVariable var = PsiTreeUtil.getParentOfType(at, GrVariable.class);
return var;
}
Aggregations