use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyReferenceCopyPasteProcessor method restoreReferences.
@Override
protected void restoreReferences(ReferenceData[] referenceData, GrReferenceElement[] refs) {
for (int i = 0; i < refs.length; i++) {
GrReferenceElement reference = refs[i];
if (reference == null)
continue;
try {
PsiManager manager = reference.getManager();
ReferenceData refData = referenceData[i];
PsiClass refClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(refData.qClassName, reference.getResolveScope());
if (refClass != null) {
if (refData.staticMemberName == null) {
reference.bindToElement(refClass);
} else {
LOG.assertTrue(reference instanceof GrReferenceExpression);
PsiMember member = findMember(refData, refClass);
if (member != null) {
((GrReferenceExpression) reference).bindToElementViaStaticImport(member);
}
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyGStringSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
final List<TextRange> ranges = ContainerUtil.newArrayList();
final PsiElement parent = e.getParent();
if (parent instanceof GrStringContent && parent.getParent() instanceof GrString) {
TextRange range = getLineTextRange(parent, cursorOffset);
ranges.add(range);
} else if (parent instanceof GrString) {
PsiElement firstChild = parent.getFirstChild();
PsiElement lastChild = parent.getLastChild();
if (firstChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_BEGIN) {
firstChild = firstChild.getNextSibling();
}
if (lastChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_END) {
lastChild = lastChild.getPrevSibling();
}
if (firstChild != null && lastChild != null) {
TextRange range = new TextRange(firstChild.getTextOffset(), lastChild.getTextOffset() + lastChild.getTextLength());
ranges.add(range);
}
ranges.add(parent.getTextRange());
} else if (parent instanceof GrStringInjection) {
if (e instanceof GrReferenceExpression) {
List<TextRange> r = new ArrayList<>(2);
SelectWordUtil.addWordSelection(editor.getSettings().isCamelWords(), editorText, cursorOffset, r);
for (TextRange textRange : r) {
if (editorText.charAt(textRange.getStartOffset()) == '$') {
textRange = new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset());
}
ranges.add(textRange);
}
}
ranges.add(parent.getTextRange());
ranges.add(e.getTextRange());
}
return ranges;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method findConflicts.
private static boolean findConflicts(InitialInfo info) {
//new ConflictsDialog()
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final PsiElement declarationOwner = info.getContext().getParent();
GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
}
}
//skip 'print' and 'println'
private boolean skipResult(GroovyResolveResult result) {
PsiElement element = result.getElement();
if (element instanceof PsiMethod) {
String name = ((PsiMethod) element).getName();
if (!name.startsWith("print"))
return false;
if (element instanceof GrGdkMethod)
element = ((GrGdkMethod) element).getStaticMethod();
PsiClass aClass = ((PsiMethod) element).getContainingClass();
if (aClass != null) {
String qname = aClass.getQualifiedName();
return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
}
}
return false;
}
};
GrStatement[] statements = info.getStatements();
for (GrStatement statement : statements) {
statement.accept(visitor);
}
if (conflicts.isEmpty())
return false;
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
}
ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
dialog.show();
return !dialog.isOK();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrVariableInliner method getConflicts.
@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
GrExpression expr = (GrExpression) reference.getElement();
if (expr.getParent() instanceof GrAssignmentExpression) {
GrAssignmentExpression parent = (GrAssignmentExpression) expr.getParent();
if (expr.equals(parent.getLValue())) {
conflicts.putValue(expr, GroovyRefactoringBundle.message("local.varaible.is.lvalue"));
}
}
if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && expr instanceof GrReferenceExpression) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expr).advancedResolve();
if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
final PsiElement parent = expr.getParent();
if (!(parent instanceof GrCall && parent instanceof GrExpression)) {
conflicts.putValue(expr, GroovyRefactoringBundle.message("reference.to.accessor.0.is.used", CommonRefactoringUtil.htmlEmphasize(PsiFormatUtil.formatMethod((GrAccessorMethod) resolveResult.getElement(), PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE))));
}
}
}
return conflicts;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyInlineLocalProcessor method collectRefs.
private static void collectRefs(final GrVariable variable, Instruction[] flow, final List<BitSet> writes, final int writeInstructionNumber, final ArrayList<UsageInfo> toInline) {
for (Instruction instruction : flow) {
final PsiElement element = instruction.getElement();
if (instruction instanceof ReadWriteVariableInstruction) {
if (((ReadWriteVariableInstruction) instruction).isWrite())
continue;
if (element instanceof GrVariable && element != variable)
continue;
if (!(element instanceof GrReferenceExpression))
continue;
final GrReferenceExpression ref = (GrReferenceExpression) element;
if (ref.isQualified() || ref.resolve() != variable)
continue;
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber)) {
toInline.add(new UsageInfo(ref));
} else if (writeInstructionNumber == -1 && prev.cardinality() == 0) {
toInline.add(new ClosureUsage(ref));
}
} else if (element instanceof GrClosableBlock) {
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
final Instruction[] closureFlow = ((GrClosableBlock) element).getControlFlow();
collectRefs(variable, closureFlow, ControlFlowUtils.inferWriteAccessMap(closureFlow, variable), -1, toInline);
}
} else if (element instanceof GrAnonymousClassDefinition) {
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
((GrAnonymousClassDefinition) element).acceptChildren(new GroovyRecursiveElementVisitor() {
@Override
public void visitField(@NotNull GrField field) {
GrExpression initializer = field.getInitializerGroovy();
if (initializer != null) {
Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
}
@Override
public void visitMethod(@NotNull GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block != null) {
Instruction[] flow = block.getControlFlow();
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
}
@Override
public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
GrOpenBlock block = initializer.getBlock();
Instruction[] flow = block.getControlFlow();
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
});
}
}
}
}
Aggregations