Search in sources :

Example 11 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GroovyFindJarFix method getFqns.

@Override
protected Collection<String> getFqns(@NotNull GrReferenceElement ref) {
    GrImportStatement importStatement = PsiTreeUtil.getParentOfType(ref.getElement(), GrImportStatement.class);
    //from static imports
    if (importStatement != null) {
        GrCodeReferenceElement reference = importStatement.getImportReference();
        if (reference != null) {
            return Collections.singleton(reference.getText());
        }
        return Collections.emptyList();
    }
    if (ref.getQualifier() != null)
        return Collections.emptyList();
    final String className = ref.getReferenceName();
    if (className == null)
        return Collections.emptyList();
    PsiFile file = ref.getContainingFile().getOriginalFile();
    if (!(file instanceof GroovyFile))
        return Collections.emptyList();
    GrImportStatement[] importList = ((GroovyFile) file).getImportStatements();
    for (GrImportStatement imp : importList) {
        if (className.equals(imp.getImportedName())) {
            GrCodeReferenceElement importReference = imp.getImportReference();
            if (importReference == null)
                return Collections.emptyList();
            return Collections.singleton(importReference.getText());
        }
    }
    return Collections.emptyList();
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 12 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GroovyDocCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    final PsiElement position = parameters.getPosition();
    GrDocMemberReference reference = PsiTreeUtil.getParentOfType(position, GrDocMemberReference.class);
    if (reference == null)
        return;
    GrDocReferenceElement holder = reference.getReferenceHolder();
    PsiElement resolved;
    if (holder != null) {
        GrCodeReferenceElement referenceElement = holder.getReferenceElement();
        resolved = referenceElement != null ? referenceElement.resolve() : null;
    } else {
        resolved = PsiUtil.getContextClass(reference);
    }
    if (resolved instanceof PsiClass) {
        ResolverProcessor propertyProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
        resolved.processDeclarations(propertyProcessor, ResolveState.initial(), null, reference);
        PsiElement[] propertyCandidates = ResolveUtil.mapToElements(propertyProcessor.getCandidates());
        ResolverProcessor methodProcessor = CompletionProcessor.createPropertyCompletionProcessor(reference);
        resolved.processDeclarations(methodProcessor, ResolveState.initial(), null, reference);
        PsiElement[] methodCandidates = ResolveUtil.mapToElements(methodProcessor.getCandidates());
        PsiElement[] elements = ArrayUtil.mergeArrays(propertyCandidates, methodCandidates);
        for (PsiElement psiElement : elements) {
            LookupElement element = GroovyCompletionUtil.createLookupElement((PsiNamedElement) psiElement);
            if (psiElement instanceof PsiMethod) {
                element = ((LookupElementBuilder) element).withInsertHandler(new GroovyMethodSignatureInsertHandler());
            }
            result.addElement(element);
        }
    }
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor) GroovyMethodSignatureInsertHandler(org.jetbrains.plugins.groovy.lang.completion.handlers.GroovyMethodSignatureInsertHandler) GrDocReferenceElement(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement) GrDocMemberReference(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocMemberReference) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 13 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GroovyInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    @NonNls Object obj = item.getObject();
    PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
    if (obj instanceof GroovyResolveResult) {
        substitutor = ((GroovyResolveResult) obj).getSubstitutor();
        obj = ((GroovyResolveResult) obj).getElement();
    }
    if (obj instanceof PsiMethod) {
        final PsiMethod method = (PsiMethod) obj;
        PsiParameter[] parameters = method.getParameterList().getParameters();
        Editor editor = context.getEditor();
        Document document = editor.getDocument();
        if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
            handleOverwrite(editor.getCaretModel().getOffset(), document);
        }
        CaretModel caretModel = editor.getCaretModel();
        int offset = context.getTailOffset();
        PsiFile file = context.getFile();
        PsiElement elementAt = file.findElementAt(context.getStartOffset());
        assert elementAt != null;
        PsiElement parent = elementAt.getParent();
        if (parent instanceof GrReferenceExpression && ((GrReferenceExpression) parent).getDotTokenType() == GroovyTokenTypes.mMEMBER_POINTER) {
            return;
        }
        CharSequence charsSequence = document.getCharsSequence();
        if (isAnnotationNameValuePair(obj, parent)) {
            int endOffset = offset;
            if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
                endOffset = CharArrayUtil.shiftForward(charsSequence, offset, " \t");
                if (charsSequence.length() > endOffset && charsSequence.charAt(endOffset) == '=') {
                    endOffset++;
                    endOffset = CharArrayUtil.shiftForward(charsSequence, endOffset, " \t");
                }
            }
            document.replaceString(offset, endOffset, " = ");
            caretModel.moveToOffset(offset + 3);
            return;
        }
        if (PsiTreeUtil.getParentOfType(elementAt, GrImportStatement.class) != null)
            return;
        if (parameters.length == 1) {
            if ((context.getCompletionChar() != '(' && context.getCompletionChar() != ' ') && TypesUtil.isClassType(parameters[0].getType(), GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
                int afterBrace;
                final int nonWs = CharArrayUtil.shiftForward(charsSequence, offset, " \t");
                if (nonWs < document.getTextLength() && charsSequence.charAt(nonWs) == '{') {
                    afterBrace = nonWs + 1;
                } else {
                    if (isSpaceBeforeClosure(file)) {
                        document.insertString(offset, " ");
                        offset++;
                    }
                    if (ClosureCompleter.runClosureCompletion(context, method, substitutor, document, offset, parent))
                        return;
                    if (context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
                        //smart enter invoked
                        document.insertString(offset, "{\n}");
                        //position caret before '{' for smart enter
                        afterBrace = offset + 1;
                        context.setTailOffset(afterBrace);
                    } else {
                        document.insertString(offset, "{}");
                        afterBrace = offset + 1;
                    }
                }
                caretModel.moveToOffset(afterBrace);
                return;
            }
        }
        context.commitDocument();
        if (context.getCompletionChar() == ' ' && MethodParenthesesHandler.hasParams(item, context.getElements(), true, method)) {
            return;
        }
        CommonCodeStyleSettings settings = context.getCodeStyleSettings();
        ParenthesesInsertHandler.getInstance(MethodParenthesesHandler.hasParams(item, context.getElements(), true, method), settings.SPACE_BEFORE_METHOD_CALL_PARENTHESES, settings.SPACE_WITHIN_METHOD_CALL_PARENTHESES, true, true).handleInsert(context, item);
        AutoPopupController.getInstance(context.getProject()).autoPopupParameterInfo(editor, method);
        return;
    }
    if (obj instanceof PsiClass) {
        final PsiClass clazz = (PsiClass) obj;
        Editor editor = context.getEditor();
        Document document = editor.getDocument();
        PsiFile file = PsiDocumentManager.getInstance(clazz.getProject()).getPsiFile(document);
        assert file != null;
        PsiElement elementAt = file.findElementAt(context.getStartOffset());
        assert elementAt != null;
        CaretModel caretModel = editor.getCaretModel();
        int offset = context.getStartOffset() + elementAt.getTextLength();
        final String text = document.getText();
        final PsiElement parent = elementAt.getParent();
        if (parent instanceof GrCodeReferenceElement && parent.getParent() instanceof GrNewExpression && (offset == text.length() || !text.substring(offset).trim().startsWith("("))) {
            document.insertString(offset, "()");
            if (GroovyCompletionUtil.hasConstructorParameters(clazz, parent)) {
                caretModel.moveToOffset(offset + 1);
                return;
            }
            caretModel.moveToOffset(offset + 2);
            return;
        }
    }
    if (context.getCompletionChar() == '=') {
        context.setAddCompletionChar(false);
        TailType.EQ.processTail(context.getEditor(), context.getTailOffset());
        return;
    }
    if (obj instanceof PsiPackage) {
        AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CaretModel(com.intellij.openapi.editor.CaretModel) Document(com.intellij.openapi.editor.Document) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) Editor(com.intellij.openapi.editor.Editor)

Example 14 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GroovyCodeStyleManagerImpl method getPackageEntryIdx.

protected static int getPackageEntryIdx(@NotNull PackageEntry[] entries, @NotNull GrImportStatement statement) {
    final GrCodeReferenceElement reference = statement.getImportReference();
    if (reference == null)
        return -1;
    final String packageName = StringUtil.getPackageName(reference.getCanonicalText());
    final boolean isStatic = statement.isStatic();
    int best = -1;
    int allOtherStatic = -1;
    int allOther = -1;
    PackageEntry bestEntry = null;
    for (int i = 0, length = entries.length; i < length; i++) {
        PackageEntry entry = entries[i];
        if (entry.isBetterMatchForPackageThan(bestEntry, packageName, isStatic)) {
            best = i;
            bestEntry = entry;
        } else if (entry == PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY) {
            allOtherStatic = i;
        } else if (entry == PackageEntry.ALL_OTHER_IMPORTS_ENTRY) {
            allOther = i;
        }
    }
    if (best >= 0)
        return best;
    if (isStatic && allOtherStatic != -1)
        return allOtherStatic;
    return allOther;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) PackageEntry(com.intellij.psi.codeStyle.PackageEntry)

Example 15 with GrCodeReferenceElement

use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.

the class GroovyShellLanguageConsoleView method importToString.

@Nullable
private static String importToString(@NotNull GrImportStatement anImport) {
    StringBuilder buffer = new StringBuilder();
    GrCodeReferenceElement reference = anImport.getImportReference();
    if (reference == null)
        return null;
    String qname = reference.getClassNameText();
    buffer.append(qname);
    if (!anImport.isOnDemand()) {
        String importedName = anImport.getImportedName();
        buffer.append(":").append(importedName);
    }
    return buffer.toString();
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)71 NotNull (org.jetbrains.annotations.NotNull)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 PsiElement (com.intellij.psi.PsiElement)12 Nullable (org.jetbrains.annotations.Nullable)12 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)11 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)10 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)7 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)5 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)5 PsiClass (com.intellij.psi.PsiClass)4 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)4 GrTypeArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)4 Editor (com.intellij.openapi.editor.Editor)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 GrAnnotation (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation)3 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)3