Search in sources :

Example 36 with PsiClass

use of com.intellij.psi.PsiClass in project intellij-community by JetBrains.

the class QualifiedMethodInsertHandler method handleInsert.

@Override
public void handleInsert(InsertionContext context, JavaGlobalMemberLookupElement item) {
    GroovyInsertHandler.INSTANCE.handleInsert(context, item);
    final PsiClass containingClass = item.getContainingClass();
    context.getDocument().insertString(context.getStartOffset(), containingClass.getName() + ".");
    PsiDocumentManager.getInstance(containingClass.getProject()).commitDocument(context.getDocument());
    final GrReferenceExpression ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GrReferenceExpression.class, false);
    if (ref != null) {
        ref.bindToElement(containingClass);
    }
}
Also used : PsiClass(com.intellij.psi.PsiClass) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 37 with PsiClass

use of com.intellij.psi.PsiClass 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);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GrQualifiedReference(org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference)

Example 38 with PsiClass

use of com.intellij.psi.PsiClass in project intellij-community by JetBrains.

the class GrMethodBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrMethod))
        return;
    GrMethod method = (GrMethod) psiElement;
    final PsiClass aClass = method.getContainingClass();
    if (aClass != null && aClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT))
        return;
    final GrCodeBlock body = method.getBlock();
    final Document doc = editor.getDocument();
    if (body != null) {
        // See IDEADEV-1093. This is quite hacky heuristic but it seem to be best we can do.
        String bodyText = body.getText();
        if (StringUtil.startsWithChar(bodyText, '{')) {
            final GrStatement[] statements = body.getStatements();
            if (statements.length > 0) {
            //          [todo]
            //          if (statements[0] instanceof PsiDeclarationStatement) {
            //            if (PsiTreeUtil.getDeepestLast(statements[0]) instanceof PsiErrorElement) {
            //              if (method.getContainingClass().getRBrace() == null) {
            //                doc.insertString(body.getTextRange().getStartOffset() + 1, "\n}");
            //              }
            //            }
            //          }
            }
        }
        return;
    }
    int endOffset = method.getTextRange().getEndOffset();
    if (StringUtil.endsWithChar(method.getText(), ';')) {
        doc.deleteString(endOffset - 1, endOffset);
        endOffset--;
    }
    doc.insertString(endOffset, "{\n}");
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) Document(com.intellij.openapi.editor.Document) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 39 with PsiClass

use of com.intellij.psi.PsiClass in project intellij-community by JetBrains.

the class SceneBuilderImpl method prepareCustomComponents.

@NotNull
private Collection<CustomComponent> prepareCustomComponents(Collection<PsiClass> psiClasses) {
    final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(myProject);
    final GlobalSearchScope scope = GlobalSearchScope.allScope(myProject);
    final Map<String, BuiltinComponent> builtinComponents = loadBuiltinComponents(className -> psiFacade.findClass(className, scope) != null);
    final List<CustomComponent> customComponents = new ArrayList<>();
    for (PsiClass psiClass : psiClasses) {
        final String qualifiedName = psiClass.getQualifiedName();
        final String name = psiClass.getName();
        if (qualifiedName != null && name != null) {
            BuiltinComponent parentComponent = null;
            for (PsiClass aClass = psiClass; aClass != null; aClass = aClass.getSuperClass()) {
                final BuiltinComponent component = builtinComponents.get(aClass.getQualifiedName());
                if (component != null) {
                    parentComponent = component;
                    break;
                }
            }
            final String moduleName = getComponentModuleName(psiClass);
            final Map<String, String> attributes = parentComponent != null ? parentComponent.getAttributes() : Collections.emptyMap();
            customComponents.add(new CustomComponent(name, qualifiedName, moduleName, attributes));
        }
    }
    return customComponents;
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass) NotNull(org.jetbrains.annotations.NotNull)

Example 40 with PsiClass

use of com.intellij.psi.PsiClass in project intellij-community by JetBrains.

the class SceneBuilderImpl method collectCustomComponents.

private Collection<CustomComponent> collectCustomComponents() {
    if (myProject.isDisposed()) {
        return Collections.emptyList();
    }
    final PsiClass nodeClass = JavaPsiFacade.getInstance(myProject).findClass(JavaFxCommonNames.JAVAFX_SCENE_NODE, GlobalSearchScope.allScope(myProject));
    if (nodeClass == null) {
        return Collections.emptyList();
    }
    final Collection<PsiClass> psiClasses = CachedValuesManager.getCachedValue(nodeClass, () -> {
        // Take custom components from libraries, but not from the project modules, because SceneBuilder instantiates the components' classes.
        // Modules might be not compiled or may change since last compile, it's too expensive to keep track of that.
        final GlobalSearchScope scope = ProjectScope.getLibrariesScope(nodeClass.getProject());
        final String ideJdkVersion = Object.class.getPackage().getSpecificationVersion();
        final LanguageLevel ideLanguageLevel = LanguageLevel.parse(ideJdkVersion);
        final Query<PsiClass> query = ClassInheritorsSearch.search(nodeClass, scope, true, true, false);
        final Set<PsiClass> result = new THashSet<>();
        query.forEach(psiClass -> {
            if (psiClass.hasModifierProperty(PsiModifier.PUBLIC) && !psiClass.hasModifierProperty(PsiModifier.ABSTRACT) && !isBuiltInComponent(psiClass) && isCompatibleLanguageLevel(psiClass, ideLanguageLevel)) {
                result.add(psiClass);
            }
        });
        return CachedValueProvider.Result.create(result, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT);
    });
    if (psiClasses.isEmpty()) {
        return Collections.emptyList();
    }
    return prepareCustomComponents(psiClasses);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) LanguageLevel(com.intellij.pom.java.LanguageLevel) PsiClass(com.intellij.psi.PsiClass) THashSet(gnu.trove.THashSet)

Aggregations

PsiClass (com.intellij.psi.PsiClass)598 PsiElement (com.intellij.psi.PsiElement)113 PsiMethod (com.intellij.psi.PsiMethod)97 Nullable (org.jetbrains.annotations.Nullable)75 NotNull (org.jetbrains.annotations.NotNull)60 Project (com.intellij.openapi.project.Project)59 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)57 Module (com.intellij.openapi.module.Module)55 PsiFile (com.intellij.psi.PsiFile)49 VirtualFile (com.intellij.openapi.vfs.VirtualFile)47 ArrayList (java.util.ArrayList)37 PsiField (com.intellij.psi.PsiField)36 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)25 Location (com.intellij.execution.Location)20 File (java.io.File)16 HashSet (java.util.HashSet)16 PsiClassType (com.intellij.psi.PsiClassType)15 PsiPackage (com.intellij.psi.PsiPackage)15 List (java.util.List)15 PsiType (com.intellij.psi.PsiType)13