Search in sources :

Example 36 with GroovyFile

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

the class GrAccessibilityChecker method buildFixes.

static GroovyFix[] buildFixes(PsiElement location, GroovyResolveResult resolveResult) {
    final PsiElement element = resolveResult.getElement();
    if (!(element instanceof PsiMember))
        return GroovyFix.EMPTY_ARRAY;
    final PsiMember refElement = (PsiMember) element;
    if (refElement instanceof PsiCompiledElement)
        return GroovyFix.EMPTY_ARRAY;
    PsiModifierList modifierList = refElement.getModifierList();
    if (modifierList == null)
        return GroovyFix.EMPTY_ARRAY;
    List<GroovyFix> fixes = new ArrayList<>();
    try {
        Project project = refElement.getProject();
        JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
        PsiModifierList modifierListCopy = facade.getElementFactory().createFieldFromText("int a;", null).getModifierList();
        assert modifierListCopy != null;
        modifierListCopy.setModifierProperty(PsiModifier.STATIC, modifierList.hasModifierProperty(PsiModifier.STATIC));
        String minModifier = PsiModifier.PROTECTED;
        if (refElement.hasModifierProperty(PsiModifier.PROTECTED)) {
            minModifier = PsiModifier.PUBLIC;
        }
        String[] modifiers = { PsiModifier.PROTECTED, PsiModifier.PUBLIC, PsiModifier.PACKAGE_LOCAL };
        PsiClass accessObjectClass = PsiTreeUtil.getParentOfType(location, PsiClass.class, false);
        if (accessObjectClass == null) {
            final PsiFile file = location.getContainingFile();
            if (!(file instanceof GroovyFile))
                return GroovyFix.EMPTY_ARRAY;
            accessObjectClass = ((GroovyFile) file).getScriptClass();
        }
        for (int i = ArrayUtil.indexOf(modifiers, minModifier); i < modifiers.length; i++) {
            String modifier = modifiers[i];
            modifierListCopy.setModifierProperty(modifier, true);
            if (facade.getResolveHelper().isAccessible(refElement, modifierListCopy, location, accessObjectClass, null)) {
                fixes.add(new GrModifierFix(refElement, modifier, true, true, GrModifierFix.MODIFIER_LIST_OWNER));
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return fixes.toArray(new GroovyFix[fixes.size()]);
}
Also used : ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 37 with GroovyFile

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

the class AnnotatedMembersSearcher method getAnnotatedMemberCandidates.

@NotNull
private static List<PsiModifierListOwner> getAnnotatedMemberCandidates(final PsiClass clazz, final GlobalSearchScope scope) {
    final String name = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Override
        public String compute() {
            return clazz.getName();
        }
    });
    if (name == null)
        return Collections.emptyList();
    final Collection<PsiElement> members = ApplicationManager.getApplication().runReadAction(new Computable<Collection<PsiElement>>() {

        @Override
        public Collection<PsiElement> compute() {
            return StubIndex.getElements(GrAnnotatedMemberIndex.KEY, name, clazz.getProject(), scope, PsiElement.class);
        }
    });
    if (members.isEmpty()) {
        return Collections.emptyList();
    }
    final List<PsiModifierListOwner> result = new ArrayList<>();
    for (final PsiElement element : members) {
        ApplicationManager.getApplication().runReadAction(() -> {
            PsiElement e = element instanceof GroovyFile ? ((GroovyFile) element).getPackageDefinition() : element;
            if (e instanceof PsiModifierListOwner) {
                result.add((PsiModifierListOwner) e);
            }
        });
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) NotNull(org.jetbrains.annotations.NotNull)

Example 38 with GroovyFile

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

the class GroovyFoldingBuilder method isSingleHighLevelClassBody.

private static boolean isSingleHighLevelClassBody(PsiElement element) {
    if (!(element instanceof GrTypeDefinitionBody))
        return false;
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrTypeDefinition))
        return false;
    final GrTypeDefinition clazz = (GrTypeDefinition) parent;
    if (clazz.isAnonymous() || clazz.getContainingClass() != null)
        return false;
    final PsiFile file = element.getContainingFile();
    return file instanceof GroovyFile && ((GroovyFile) file).getClasses().length == 1;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 39 with GroovyFile

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

the class GroovyFoldingBuilder method appendDescriptors.

private void appendDescriptors(PsiElement element, List<FoldingDescriptor> descriptors, Set<PsiElement> usedComments) {
    ASTNode node = element.getNode();
    if (node == null)
        return;
    IElementType type = node.getElementType();
    if (TokenSets.BLOCK_SET.contains(type) && !isSingleHighLevelClassBody(element) || type == GroovyElementTypes.CLOSABLE_BLOCK) {
        if (isMultiline(element)) {
            collapseBlock(descriptors, element);
        }
    }
    // comments
    if ((type.equals(GroovyTokenTypes.mML_COMMENT) || type.equals(GroovyDocElementTypes.GROOVY_DOC_COMMENT)) && isMultiline(element) && isWellEndedComment(element)) {
        descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
    }
    if (type.equals(GroovyTokenTypes.mSL_COMMENT) && !usedComments.contains(element)) {
        boolean containsCustomRegionMarker = isCustomRegionElement(element);
        usedComments.add(element);
        PsiElement end = null;
        for (PsiElement current = element.getNextSibling(); current != null; current = current.getNextSibling()) {
            if (PsiImplUtil.isWhiteSpaceOrNls(current))
                continue;
            IElementType elementType = current.getNode().getElementType();
            if (elementType == GroovyTokenTypes.mSL_COMMENT) {
                end = current;
                usedComments.add(current);
                containsCustomRegionMarker |= isCustomRegionElement(current);
                continue;
            }
            break;
        }
        if (end != null && !containsCustomRegionMarker) {
            final TextRange range = new TextRange(element.getTextRange().getStartOffset(), end.getTextRange().getEndOffset());
            descriptors.add(new FoldingDescriptor(element, range));
        }
    }
    //multiline strings
    addFoldingForStrings(descriptors, node);
    Set<PsiElement> newUsedComments = new HashSet<>();
    for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        appendDescriptors(child, descriptors, newUsedComments);
    }
    if (element instanceof GroovyFile) {
        processImports(descriptors, ((GroovyFile) element).getImportStatements());
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) NamedFoldingDescriptor(com.intellij.lang.folding.NamedFoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) HashSet(com.intellij.util.containers.hash.HashSet)

Example 40 with GroovyFile

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

the class ControlFlowBuilderUtil method findClassByText.

private static boolean findClassByText(GrReferenceExpression ref) {
    final String text = ref.getText();
    final int i = text.indexOf('<');
    String className = i == -1 ? text : text.substring(0, i);
    PsiClass[] names = PsiShortNamesCache.getInstance(ref.getProject()).getClassesByName(className, ref.getResolveScope());
    if (names.length > 0)
        return true;
    PsiFile file = ref.getContainingFile();
    if (file instanceof GroovyFile) {
        GrImportStatement[] imports = ((GroovyFile) file).getImportStatements();
        for (GrImportStatement anImport : imports) {
            if (className.equals(anImport.getImportedName()))
                return true;
        }
    }
    return false;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5