use of org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement 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.GrReferenceElement in project intellij-community by JetBrains.
the class GroovyCompletionData method isControlStructure.
private static boolean isControlStructure(PsiElement context) {
final int offset = context.getTextRange().getStartOffset();
PsiElement prevSibling = context.getPrevSibling();
if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
ASTNode node = prevSibling.getNode();
return !TokenSets.DOTS.contains(node.getElementType());
}
if (GroovyCompletionUtil.isNewStatement(context, true)) {
final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context);
if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) {
return true;
}
}
if (context.getParent() != null) {
PsiElement parent = context.getParent();
if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) {
return true;
}
if (parent instanceof GrReferenceExpression) {
PsiElement superParent = parent.getParent();
if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) {
return true;
}
}
return false;
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement in project intellij-community by JetBrains.
the class GroovyCompletionData method suggestPrimitiveTypes.
private static boolean suggestPrimitiveTypes(PsiElement context) {
if (isInfixOperatorPosition(context))
return false;
if (isAfterForParameter(context))
return false;
final PsiElement parent = context.getParent();
if (parent == null)
return false;
PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
PsiElement prevSibling = context.getPrevSibling();
if (prevSibling != null && prevSibling.getNode() != null) {
if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
return true;
}
} else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
return true;
}
}
if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
return true;
if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
return false;
}
if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
return true;
}
if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
PsiElement prevSibling = context.getPrevSibling();
if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
ASTNode node = prevSibling.getNode();
return !TokenSets.DOTS.contains(node.getElementType());
} else {
return true;
}
}
if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
return true;
}
if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
return true;
}
return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
use of org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement in project intellij-community by JetBrains.
the class GrMainCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull final CompletionResultSet result) {
GroovyCompletionData.addGroovyDocKeywords(parameters, result);
PsiElement position = parameters.getPosition();
if (PlatformPatterns.psiElement().inside(false, PlatformPatterns.psiElement(PsiComment.class)).accepts(position)) {
return;
}
GroovyCompletionData.addGroovyKeywords(parameters, result);
addUnfinishedMethodTypeParameters(position, result);
suggestVariableNames(position, result);
GrReferenceElement reference = findGroovyReference(position);
if (reference == null) {
if (parameters.getInvocationCount() >= 2) {
result.stopHere();
addAllClasses(parameters, result.withPrefixMatcher(CompletionUtil.findJavaIdentifierPrefix(parameters)), new JavaCompletionSession(result));
}
return;
}
if (reference.getParent() instanceof GrImportStatement && reference.getQualifier() != null) {
result.addElement(LookupElementBuilder.create("*"));
}
JavaCompletionSession inheritors = new JavaCompletionSession(result);
if (GroovySmartCompletionContributor.AFTER_NEW.accepts(position)) {
GroovySmartCompletionContributor.generateInheritorVariants(parameters, result.getPrefixMatcher(), inheritors);
}
Runnable addSlowVariants = completeReference(parameters, reference, inheritors, result.getPrefixMatcher(), lookupElement -> result.addElement(lookupElement));
if (reference.getQualifier() == null) {
if (!GroovySmartCompletionContributor.AFTER_NEW.accepts(position)) {
GroovySmartCompletionContributor.addExpectedClassMembers(parameters, result);
}
if (isClassNamePossible(position) && JavaCompletionContributor.mayStartClassName(result)) {
result.stopHere();
if (parameters.getInvocationCount() >= 2) {
addAllClasses(parameters, result, inheritors);
} else {
JavaCompletionContributor.advertiseSecondCompletion(position.getProject(), result);
}
}
}
addSlowVariants.run();
}
use of org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement in project intellij-community by JetBrains.
the class ImportOnDemandIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (!(element instanceof GrReferenceElement))
return;
final GrReferenceElement ref = (GrReferenceElement) element;
final PsiElement resolved = ref.resolve();
if (!(resolved instanceof PsiClass))
return;
final String qname = ((PsiClass) resolved).getQualifiedName();
final GrImportStatement importStatement = GroovyPsiElementFactory.getInstance(project).createImportStatementFromText(qname, true, true, null);
final PsiFile containingFile = element.getContainingFile();
if (!(containingFile instanceof GroovyFile))
return;
((GroovyFile) containingFile).addImport(importStatement);
for (PsiReference reference : ReferencesSearch.search(resolved, new LocalSearchScope(containingFile))) {
final PsiElement refElement = reference.getElement();
if (refElement == null)
continue;
final PsiElement parent = refElement.getParent();
if (parent instanceof GrQualifiedReference<?>) {
org.jetbrains.plugins.groovy.codeStyle.GrReferenceAdjuster.shortenReference((GrQualifiedReference<?>) parent);
}
}
}
Aggregations