Search in sources :

Example 31 with PsiNamedElement

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

the class FileReferenceCompletionImpl method getFileReferenceCompletionVariants.

@NotNull
@Override
public Object[] getFileReferenceCompletionVariants(final FileReference reference) {
    final String s = reference.getText();
    if (s != null && s.equals("/")) {
        return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
    final CommonProcessors.CollectUniquesProcessor<PsiFileSystemItem> collector = new CommonProcessors.CollectUniquesProcessor<>();
    final PsiElementProcessor<PsiFileSystemItem> processor = new PsiElementProcessor<PsiFileSystemItem>() {

        @Override
        public boolean execute(@NotNull PsiFileSystemItem fileSystemItem) {
            return new FilteringProcessor<>(reference.getFileReferenceSet().getReferenceCompletionFilter(), collector).process(FileReference.getOriginalFile(fileSystemItem));
        }
    };
    List<Object> additionalItems = ContainerUtil.newArrayList();
    for (PsiFileSystemItem context : reference.getContexts()) {
        for (final PsiElement child : context.getChildren()) {
            if (child instanceof PsiFileSystemItem) {
                processor.execute((PsiFileSystemItem) child);
            }
        }
        if (context instanceof FileReferenceResolver) {
            additionalItems.addAll(((FileReferenceResolver) context).getVariants(reference));
        }
    }
    final FileType[] types = reference.getFileReferenceSet().getSuitableFileTypes();
    final THashSet<PsiElement> set = new THashSet<>(collector.getResults(), VARIANTS_HASHING_STRATEGY);
    final PsiElement[] candidates = PsiUtilCore.toPsiElementArray(set);
    final Object[] variants = new Object[candidates.length + additionalItems.size()];
    for (int i = 0; i < candidates.length; i++) {
        PsiElement candidate = candidates[i];
        Object item = reference.createLookupItem(candidate);
        if (item == null) {
            item = FileInfoManager.getFileLookupItem(candidate);
        }
        if (candidate instanceof PsiFile && item instanceof LookupElement && types.length > 0 && ArrayUtil.contains(((PsiFile) candidate).getFileType(), types)) {
            item = PrioritizedLookupElement.withPriority((LookupElement) item, Double.MAX_VALUE);
        }
        variants[i] = item;
    }
    for (int i = 0; i < additionalItems.size(); i++) {
        variants[i + candidates.length] = additionalItems.get(i);
    }
    if (!reference.getFileReferenceSet().isUrlEncoded()) {
        return variants;
    }
    List<Object> encodedVariants = new ArrayList<>(variants.length + additionalItems.size());
    for (int i = 0; i < candidates.length; i++) {
        final PsiElement element = candidates[i];
        if (element instanceof PsiNamedElement) {
            final PsiNamedElement psiElement = (PsiNamedElement) element;
            String name = psiElement.getName();
            final String encoded = reference.encode(name, psiElement);
            if (encoded == null)
                continue;
            if (!encoded.equals(name)) {
                final Icon icon = psiElement.getIcon(Iconable.ICON_FLAG_READ_STATUS | Iconable.ICON_FLAG_VISIBILITY);
                LookupElementBuilder item = FileInfoManager.getFileLookupItem(candidates[i], encoded, icon);
                encodedVariants.add(item.withTailText(" (" + name + ")"));
            } else {
                encodedVariants.add(variants[i]);
            }
        }
    }
    encodedVariants.addAll(additionalItems);
    return ArrayUtil.toObjectArray(encodedVariants);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiFile(com.intellij.psi.PsiFile) CommonProcessors(com.intellij.util.CommonProcessors) PsiElement(com.intellij.psi.PsiElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) THashSet(gnu.trove.THashSet) FileType(com.intellij.openapi.fileTypes.FileType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with PsiNamedElement

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

the class JavaTestFinder method findTestsForClass.

@NotNull
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
    PsiClass klass = findSourceElement(element);
    if (klass == null)
        return Collections.emptySet();
    List<Pair<? extends PsiNamedElement, Integer>> classesWithProximities = new ArrayList<>();
    Processor<Pair<? extends PsiNamedElement, Integer>> processor = Processors.cancelableCollectProcessor(classesWithProximities);
    collectTests(klass, processor);
    return TestFinderHelper.getSortedElements(classesWithProximities, true);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with PsiNamedElement

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

the class AddMethodQualifierTest method doTest.

private void doTest(final String... candidatesNames) {
    myFixture.configureByFile(getTestName(false) + ".java");
    final AddMethodQualifierFix addMethodQualifierFix = getQuickFix();
    if (candidatesNames.length == 0) {
        assertNull(addMethodQualifierFix);
        return;
    }
    assertNotNull(addMethodQualifierFix);
    final Set<String> actualCandidatesNames = new TreeSet<>(ContainerUtil.map(addMethodQualifierFix.getCandidates(), new Function<PsiNamedElement, String>() {

        @Override
        public String fun(final PsiNamedElement psiNamedElement) {
            final String name = psiNamedElement.getName();
            assertNotNull(name);
            return name;
        }
    }));
    final Set<String> expectedCandidatesNames = new TreeSet<>(ContainerUtil.list(candidatesNames));
    assertEquals(expectedCandidatesNames, actualCandidatesNames);
}
Also used : AddMethodQualifierFix(com.intellij.codeInsight.daemon.impl.quickfix.AddMethodQualifierFix) Function(com.intellij.util.Function) PsiNamedElement(com.intellij.psi.PsiNamedElement)

Example 34 with PsiNamedElement

use of com.intellij.psi.PsiNamedElement in project intellij-plugins by JetBrains.

the class GherkinInplaceRenamer method finish.

@Override
public void finish(boolean success) {
    super.finish(success);
    if (success) {
        final PsiNamedElement newVariable = getVariable();
        if (newVariable != null) {
            final GherkinScenarioOutline scenario = PsiTreeUtil.getParentOfType(newVariable, GherkinScenarioOutline.class);
            if (scenario != null) {
                final CodeStyleManager csManager = CodeStyleManager.getInstance(newVariable.getProject());
                csManager.reformat(scenario);
            }
        }
    }
}
Also used : CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) PsiNamedElement(com.intellij.psi.PsiNamedElement) GherkinScenarioOutline(org.jetbrains.plugins.cucumber.psi.GherkinScenarioOutline)

Example 35 with PsiNamedElement

use of com.intellij.psi.PsiNamedElement in project intellij-plugins by JetBrains.

the class FlexUnitTestFinder method findTestsForClass.

@NotNull
public Collection<PsiElement> findTestsForClass(@NotNull final PsiElement element) {
    final VirtualFile file = element.getContainingFile().getVirtualFile();
    final JSClass jsClass = findSourceElement(element);
    final String className = jsClass == null ? null : jsClass.getName();
    final Pair<Module, FlexUnitSupport> moduleAndSupport = FlexUnitSupport.getModuleAndSupport(element);
    final Module module = moduleAndSupport == null ? null : moduleAndSupport.first;
    final FlexUnitSupport flexUnitSupport = moduleAndSupport == null ? null : moduleAndSupport.second;
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex();
    if (className == null || module == null || flexUnitSupport == null || (file != null && fileIndex.isInTestSourceContent(file))) {
        return Collections.emptyList();
    }
    final Collection<String> allNames = StubIndex.getInstance().getAllKeys(JSNameIndex.KEY, element.getProject());
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependentsScope(module);
    final List<Pair<? extends PsiNamedElement, Integer>> classesWithProximity = new ArrayList<>();
    for (final String possibleTestName : allNames) {
        if (possibleTestName.contains(className)) {
            for (final JSQualifiedNamedElement jsElement : JSResolveUtil.findElementsByName(possibleTestName, element.getProject(), scope)) {
                final VirtualFile f = jsElement.getContainingFile().getVirtualFile();
                final boolean inTestSource = f != null && fileIndex.isInTestSourceContent(f);
                if (jsElement instanceof JSClass && jsElement != jsClass && ((inTestSource && possibleTestName.contains("Test")) || flexUnitSupport.isTestClass((JSClass) jsElement, true))) {
                    classesWithProximity.add(Pair.create(jsElement, TestFinderHelper.calcTestNameProximity(className, jsElement.getName())));
                }
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithProximity, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PsiNamedElement (com.intellij.psi.PsiNamedElement)65 PsiElement (com.intellij.psi.PsiElement)29 NotNull (org.jetbrains.annotations.NotNull)16 ArrayList (java.util.ArrayList)14 PsiFile (com.intellij.psi.PsiFile)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)7 Pair (com.intellij.openapi.util.Pair)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Project (com.intellij.openapi.project.Project)4 UsageInfo (com.intellij.usageView.UsageInfo)4 Nullable (org.jetbrains.annotations.Nullable)4 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PsiReference (com.intellij.psi.PsiReference)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)2