use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember in project intellij-community by JetBrains.
the class GroovyRefactoringSupportProvider method isMemberInplaceRenameAvailable.
@Override
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement element, @Nullable PsiElement context) {
if (context == null || context.getContainingFile() instanceof GroovyFile)
return false;
PsiElement parent = context.getParent();
//don't try to inplace rename aliased imported references
if (parent instanceof GrReferenceElement) {
GroovyResolveResult result = ((GrReferenceElement) parent).advancedResolve();
PsiElement fileResolveContext = result.getCurrentFileResolveContext();
if (fileResolveContext instanceof GrImportStatement && ((GrImportStatement) fileResolveContext).isAliasedImport()) {
return false;
}
}
return element instanceof GrMember;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember in project intellij-community by JetBrains.
the class ClosureGenerator method getOwner.
@NonNls
@NotNull
private CharSequence getOwner(@NotNull GrClosableBlock closure) {
final GroovyPsiElement context = PsiTreeUtil.getParentOfType(closure, GrMember.class, GroovyFile.class);
LOG.assertTrue(context != null);
final PsiClass contextClass;
if (context instanceof GroovyFile) {
contextClass = ((GroovyFile) context).getScriptClass();
} else if (context instanceof PsiClass) {
contextClass = (PsiClass) context;
} else if (context instanceof GrMember) {
if (((GrMember) context).hasModifierProperty(PsiModifier.STATIC)) {
//no context class
contextClass = null;
} else {
contextClass = ((GrMember) context).getContainingClass();
}
} else {
contextClass = null;
}
if (contextClass == null)
return "null";
final PsiElement implicitClass = GenerationUtil.getWrappingImplicitClass(closure);
if (implicitClass == null) {
return "this";
} else {
final StringBuilder buffer = new StringBuilder();
GenerationUtil.writeThisReference(contextClass, buffer, this.context);
return buffer;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember 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.typedef.members.GrMember in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method collectReferenceInfoImpl.
private static void collectReferenceInfoImpl(Collection<ReferenceExpressionInfo> infos, PsiElement elem, GrMethod method) {
if (elem instanceof GrReferenceExpression) {
GrReferenceExpression expr = (GrReferenceExpression) elem;
PsiReference ref = expr.getReference();
if (ref != null) {
PsiElement declaration = ref.resolve();
if (declaration instanceof GrMember) {
int offsetInMethod = expr.getTextRange().getStartOffset() - method.getTextRange().getStartOffset();
GrMember member = (GrMember) declaration;
infos.add(new ReferenceExpressionInfo(expr, offsetInMethod, member, member.getContainingClass()));
}
}
}
for (PsiElement element : elem.getChildren()) {
collectReferenceInfoImpl(infos, element, method);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method initializeInMethod.
void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
final PsiElement _scope = myContext.getScope();
final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
final PsiElement place = replaced.get(0);
final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
assert container != null;
final PsiElement anchor;
if (mySettings.removeLocalVar()) {
GrVariable variable = myLocalVariable;
anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
} else {
anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
}
initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
Aggregations