use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrMainCompletionProvider method completeReference.
@NotNull
static Runnable completeReference(final CompletionParameters parameters, final GrReferenceElement reference, final JavaCompletionSession inheritorsHolder, final PrefixMatcher matcher, final Consumer<LookupElement> _consumer) {
final Consumer<LookupElement> consumer = new Consumer<LookupElement>() {
final Set<LookupElement> added = ContainerUtil.newHashSet();
@Override
public void consume(LookupElement element) {
if (added.add(element)) {
_consumer.consume(element);
}
}
};
final Map<PsiModifierListOwner, LookupElement> staticMembers = ContainerUtil.newHashMap();
final PsiElement qualifier = reference.getQualifier();
final PsiType qualifierType = GroovyCompletionUtil.getQualifierType(qualifier);
if (reference instanceof GrReferenceExpression && (qualifier instanceof GrExpression || qualifier == null)) {
for (String string : CompleteReferencesWithSameQualifier.getVariantsWithSameQualifier((GrReferenceExpression) reference, matcher, (GrExpression) qualifier)) {
consumer.consume(LookupElementBuilder.create(string).withItemTextUnderlined(true));
}
if (parameters.getInvocationCount() < 2 && qualifier != null && qualifierType == null && !(qualifier instanceof GrReferenceExpression && ((GrReferenceExpression) qualifier).resolve() instanceof PsiPackage)) {
if (parameters.getInvocationCount() == 1) {
showInfo();
}
return EmptyRunnable.INSTANCE;
}
}
final List<LookupElement> zeroPriority = ContainerUtil.newArrayList();
PsiClass qualifierClass = com.intellij.psi.util.PsiUtil.resolveClassInClassTypeOnly(qualifierType);
final boolean honorExcludes = qualifierClass == null || !JavaCompletionUtil.isInExcludedPackage(qualifierClass, false);
GroovyCompletionUtil.processVariants(reference, matcher, parameters, lookupElement -> {
Object object = lookupElement.getObject();
if (object instanceof GroovyResolveResult) {
object = ((GroovyResolveResult) object).getElement();
}
if (isLightElementDeclaredDuringCompletion(object)) {
return;
}
if (!(lookupElement instanceof LookupElementBuilder) && inheritorsHolder.alreadyProcessed(lookupElement)) {
return;
}
if (honorExcludes && object instanceof PsiMember && JavaCompletionUtil.isInExcludedPackage((PsiMember) object, true)) {
return;
}
if (!(object instanceof PsiClass)) {
int priority = assignPriority(lookupElement, qualifierType);
lookupElement = JavaCompletionUtil.highlightIfNeeded(qualifierType, PrioritizedLookupElement.withPriority(lookupElement, priority), object, reference);
}
if ((object instanceof PsiMethod || object instanceof PsiField) && ((PsiModifierListOwner) object).hasModifierProperty(PsiModifier.STATIC)) {
if (lookupElement.getLookupString().equals(((PsiMember) object).getName())) {
staticMembers.put(CompletionUtil.getOriginalOrSelf((PsiModifierListOwner) object), lookupElement);
}
}
PrioritizedLookupElement prio = lookupElement.as(PrioritizedLookupElement.CLASS_CONDITION_KEY);
if (prio == null || prio.getPriority() == 0) {
zeroPriority.add(lookupElement);
} else {
consumer.consume(lookupElement);
}
});
for (LookupElement element : zeroPriority) {
consumer.consume(element);
}
if (qualifier == null) {
return addStaticMembers(parameters, matcher, staticMembers, consumer);
}
return EmptyRunnable.INSTANCE;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrIfConditionFixer method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
if (psiElement instanceof GrIfStatement) {
final Document doc = editor.getDocument();
final GrIfStatement ifStatement = (GrIfStatement) psiElement;
final PsiElement rParen = ifStatement.getRParenth();
final PsiElement lParen = ifStatement.getLParenth();
final GrExpression condition = ifStatement.getCondition();
if (condition == null) {
if (lParen == null || rParen == null) {
int stopOffset = doc.getLineEndOffset(doc.getLineNumber(ifStatement.getTextRange().getStartOffset()));
final GrStatement then = ifStatement.getThenBranch();
if (then != null) {
stopOffset = Math.min(stopOffset, then.getTextRange().getStartOffset());
}
stopOffset = Math.min(stopOffset, ifStatement.getTextRange().getEndOffset());
doc.replaceString(ifStatement.getTextRange().getStartOffset(), stopOffset, "if ()");
processor.registerUnresolvedError(ifStatement.getTextRange().getStartOffset() + "if (".length());
} else {
processor.registerUnresolvedError(lParen.getTextRange().getEndOffset());
}
} else if (rParen == null) {
doc.insertString(condition.getTextRange().getEndOffset(), ")");
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrMethodCallFixer method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
final GrArgumentList argList = psiElement instanceof GrCall ? ((GrCall) psiElement).getArgumentList() : null;
if (argList == null || argList instanceof GrCommandArgumentList)
return;
GrCall call = (GrCall) psiElement;
PsiElement parenth = argList.getLastChild();
if (parenth != null && ")".equals(parenth.getText()) || PsiImplUtil.hasClosureArguments(call))
return;
int endOffset = -1;
for (PsiElement child = argList.getFirstChild(); child != null; child = child.getNextSibling()) {
if (!(child instanceof PsiErrorElement))
continue;
final PsiErrorElement errorElement = (PsiErrorElement) child;
if (errorElement.getErrorDescription().contains("')'")) {
endOffset = errorElement.getTextRange().getStartOffset();
break;
}
}
if (endOffset == -1) {
endOffset = argList.getTextRange().getEndOffset();
}
final GrExpression[] params = argList.getExpressionArguments();
if (params.length > 0 && GrForBodyFixer.startLine(editor.getDocument(), argList) != GrForBodyFixer.startLine(editor.getDocument(), params[0])) {
endOffset = argList.getTextRange().getStartOffset() + 1;
}
endOffset = CharArrayUtil.shiftBackward(editor.getDocument().getCharsSequence(), endOffset - 1, " \t\n") + 1;
editor.getDocument().insertString(endOffset, ")");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrCallReferenceProcessor method process.
@Override
public boolean process(@NotNull PsiReference reference, @NotNull JavaCallHierarchyData data) {
PsiClass originalClass = data.getOriginalClass();
PsiMethod method = data.getMethod();
Set<PsiMethod> methodsToFind = data.getMethodsToFind();
PsiMethod methodToFind = data.getMethodToFind();
PsiClassType originalType = data.getOriginalType();
Map<PsiMember, NodeDescriptor> methodToDescriptorMap = data.getResultMap();
Project project = data.getProject();
if (reference instanceof GrReferenceExpression) {
final GrExpression qualifier = ((GrReferenceExpression) reference).getQualifierExpression();
if (org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isSuperReference(qualifier)) {
// filter super.foo() call inside foo() and similar cases (bug 8411)
assert qualifier != null;
final PsiClass superClass = PsiUtil.resolveClassInType(qualifier.getType());
if (originalClass == null || originalClass.isInheritor(superClass, true)) {
return true;
}
}
if (qualifier != null && !methodToFind.hasModifierProperty(PsiModifier.STATIC)) {
final PsiType qualifierType = qualifier.getType();
if (qualifierType instanceof PsiClassType && !TypeConversionUtil.isAssignable(qualifierType, originalType) && methodToFind != method) {
final PsiClass psiClass = ((PsiClassType) qualifierType).resolve();
if (psiClass != null) {
final PsiMethod callee = psiClass.findMethodBySignature(methodToFind, true);
if (callee != null && !methodsToFind.contains(callee)) {
// skip sibling methods
return true;
}
}
}
}
} else {
if (!(reference instanceof PsiElement)) {
return true;
}
final PsiElement parent = ((PsiElement) reference).getParent();
if (parent instanceof PsiNewExpression) {
if (((PsiNewExpression) parent).getClassReference() != reference) {
return true;
}
} else if (parent instanceof GrAnonymousClassDefinition) {
if (((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy() != reference) {
return true;
}
} else {
return true;
}
}
final PsiElement element = reference.getElement();
final PsiMember key = CallHierarchyNodeDescriptor.getEnclosingElement(element);
synchronized (methodToDescriptorMap) {
CallHierarchyNodeDescriptor d = (CallHierarchyNodeDescriptor) methodToDescriptorMap.get(key);
if (d == null) {
d = new CallHierarchyNodeDescriptor(project, (CallHierarchyNodeDescriptor) data.getNodeDescriptor(), element, false, true);
methodToDescriptorMap.put(key, d);
} else if (!d.hasReference(reference)) {
d.incrementUsageCount();
}
d.addReference(reference);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class ConvertConcatenationToGstringIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final PsiFile file = element.getContainingFile();
final int offset = editor.getCaretModel().getOffset();
final AccessToken accessToken = ReadAction.start();
final List<GrExpression> expressions;
try {
expressions = collectExpressions(file, offset);
} finally {
accessToken.finish();
}
final Document document = editor.getDocument();
if (expressions.size() == 1) {
invokeImpl(expressions.get(0), document);
} else if (!expressions.isEmpty()) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
invokeImpl(expressions.get(expressions.size() - 1), document);
return;
}
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {
@Override
public void pass(final GrExpression selectedValue) {
invokeImpl(selectedValue, document);
}
}, grExpression -> grExpression.getText());
}
}
Aggregations