Search in sources :

Example 1 with NullableComputable

use of com.intellij.openapi.util.NullableComputable in project intellij-community by JetBrains.

the class MoveInnerDialog method getTargetContainer.

@Nullable
private PsiElement getTargetContainer() {
    if (myTargetContainer instanceof PsiDirectory) {
        final PsiDirectory psiDirectory = (PsiDirectory) myTargetContainer;
        PsiPackage oldPackage = getTargetPackage();
        String name = oldPackage == null ? "" : oldPackage.getQualifiedName();
        final String targetName = myPackageNameField.getText();
        if (!Comparing.equal(name, targetName)) {
            final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
            final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(myProject);
            final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(myProject), targetName);
            final VirtualFile targetSourceRoot;
            if (contentSourceRoots.size() > 1) {
                PsiPackage targetPackage = JavaPsiFacade.getInstance(myProject).findPackage(targetName);
                PsiDirectory initialDir = null;
                if (targetPackage != null) {
                    final PsiDirectory[] directories = targetPackage.getDirectories();
                    final VirtualFile root = projectRootManager.getFileIndex().getSourceRootForFile(psiDirectory.getVirtualFile());
                    for (PsiDirectory dir : directories) {
                        if (Comparing.equal(projectRootManager.getFileIndex().getSourceRootForFile(dir.getVirtualFile()), root)) {
                            initialDir = dir;
                            break;
                        }
                    }
                }
                final VirtualFile sourceRoot = MoveClassesOrPackagesUtil.chooseSourceRoot(newPackage, contentSourceRoots, initialDir);
                if (sourceRoot == null)
                    return null;
                targetSourceRoot = sourceRoot;
            } else {
                targetSourceRoot = contentSourceRoots.get(0);
            }
            PsiDirectory dir = RefactoringUtil.findPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
            if (dir == null) {
                dir = ApplicationManager.getApplication().runWriteAction(new NullableComputable<PsiDirectory>() {

                    public PsiDirectory compute() {
                        try {
                            return RefactoringUtil.createPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
                        } catch (IncorrectOperationException e) {
                            return null;
                        }
                    }
                });
            }
            return dir;
        }
    }
    return myTargetContainer;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NullableComputable(com.intellij.openapi.util.NullableComputable) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) PackageWrapper(com.intellij.refactoring.PackageWrapper) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with NullableComputable

use of com.intellij.openapi.util.NullableComputable in project intellij-plugins by JetBrains.

the class CucumberJavaAllFeaturesInFolderRunConfigurationProducer method getStepsGlue.

@Override
protected NullableComputable<String> getStepsGlue(@NotNull final PsiElement element) {
    final Set<String> glues = getHookGlue(element);
    if (element instanceof PsiDirectory) {
        final PsiDirectory dir = (PsiDirectory) element;
        final CucumberJvmExtensionPoint[] extensions = Extensions.getExtensions(CucumberJvmExtensionPoint.EP_NAME);
        return new NullableComputable<String>() {

            @Nullable
            @Override
            public String compute() {
                dir.accept(new PsiElementVisitor() {

                    @Override
                    public void visitFile(final PsiFile file) {
                        if (file instanceof GherkinFile) {
                            for (CucumberJvmExtensionPoint extension : extensions) {
                                extension.getGlues((GherkinFile) file, glues);
                            }
                        }
                    }

                    @Override
                    public void visitDirectory(PsiDirectory dir) {
                        for (PsiDirectory subDir : dir.getSubdirectories()) {
                            subDir.accept(this);
                        }
                        for (PsiFile file : dir.getFiles()) {
                            file.accept(this);
                        }
                    }
                });
                return StringUtil.join(glues, " ");
            }
        };
    }
    return null;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) CucumberJvmExtensionPoint(org.jetbrains.plugins.cucumber.CucumberJvmExtensionPoint) NullableComputable(com.intellij.openapi.util.NullableComputable) PsiFile(com.intellij.psi.PsiFile) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) GherkinFile(org.jetbrains.plugins.cucumber.psi.GherkinFile)

Example 3 with NullableComputable

use of com.intellij.openapi.util.NullableComputable in project intellij-plugins by JetBrains.

the class StrutsConstantHelper method getActionExtensions.

/**
   * Returns the current action extension(s) ("{@code .action}").
   *
   * @param psiElement Invocation element.
   * @return empty list on configuration problems.
   */
@NotNull
public static List<String> getActionExtensions(@NotNull final PsiElement psiElement) {
    final PsiFile psiFile = psiElement.getContainingFile().getOriginalFile();
    CachedValue<AtomicNotNullLazyValue<List<String>>> extensions = psiFile.getUserData(KEY_ACTION_EXTENSIONS);
    if (extensions == null) {
        final Project project = psiElement.getProject();
        extensions = CachedValuesManager.getManager(project).createCachedValue(() -> {
            final AtomicNotNullLazyValue<List<String>> lazyValue = new AtomicNotNullLazyValue<List<String>>() {

                @NotNull
                @Override
                protected List<String> compute() {
                    final List<String> extensions1 = ApplicationManager.getApplication().runReadAction((NullableComputable<List<String>>) () -> StrutsConstantManager.getInstance(project).getConvertedValue(psiFile, StrutsCoreConstantContributor.ACTION_EXTENSION));
                    if (extensions1 == null) {
                        return Collections.emptyList();
                    }
                    return ContainerUtil.map(extensions1, DOT_PATH_FUNCTION);
                }
            };
            return CachedValueProvider.Result.create(lazyValue, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
        }, false);
        psiFile.putUserData(KEY_ACTION_EXTENSIONS, extensions);
    }
    return extensions.getValue().getValue();
}
Also used : Project(com.intellij.openapi.project.Project) AtomicNotNullLazyValue(com.intellij.openapi.util.AtomicNotNullLazyValue) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) NullableComputable(com.intellij.openapi.util.NullableComputable) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with NullableComputable

use of com.intellij.openapi.util.NullableComputable in project kotlin by JetBrains.

the class MoveKotlinNestedClassesToUpperLevelDialog method getTargetContainer.

@Nullable
private PsiElement getTargetContainer() {
    if (targetContainer instanceof PsiDirectory) {
        PsiDirectory psiDirectory = (PsiDirectory) targetContainer;
        FqName oldPackageFqName = getTargetPackageFqName();
        String targetName = packageNameField.getText();
        if (!Comparing.equal(oldPackageFqName != null ? oldPackageFqName.asString() : null, targetName)) {
            ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
            List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
            final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(project), targetName);
            final VirtualFile targetSourceRoot;
            if (contentSourceRoots.size() > 1) {
                PsiDirectory initialDir = null;
                PsiPackage oldPackage = oldPackageFqName != null ? JavaPsiFacade.getInstance(project).findPackage(oldPackageFqName.asString()) : null;
                if (oldPackage != null) {
                    PsiDirectory[] directories = oldPackage.getDirectories();
                    VirtualFile root = projectRootManager.getFileIndex().getContentRootForFile(psiDirectory.getVirtualFile());
                    for (PsiDirectory dir : directories) {
                        if (Comparing.equal(projectRootManager.getFileIndex().getContentRootForFile(dir.getVirtualFile()), root)) {
                            initialDir = dir;
                        }
                    }
                }
                VirtualFile sourceRoot = MoveClassesOrPackagesUtil.chooseSourceRoot(newPackage, contentSourceRoots, initialDir);
                if (sourceRoot == null)
                    return null;
                targetSourceRoot = sourceRoot;
            } else {
                targetSourceRoot = contentSourceRoots.get(0);
            }
            PsiDirectory dir = RefactoringUtil.findPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
            if (dir == null) {
                dir = ApplicationManager.getApplication().runWriteAction(new NullableComputable<PsiDirectory>() {

                    @Override
                    public PsiDirectory compute() {
                        try {
                            return RefactoringUtil.createPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
                        } catch (IncorrectOperationException e) {
                            return null;
                        }
                    }
                });
            }
            return dir;
        }
        return targetContainer;
    }
    if (targetContainer instanceof KtFile || targetContainer instanceof KtClassOrObject)
        return targetContainer;
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FqName(org.jetbrains.kotlin.name.FqName) NullableComputable(com.intellij.openapi.util.NullableComputable) PackageWrapper(com.intellij.refactoring.PackageWrapper) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with NullableComputable

use of com.intellij.openapi.util.NullableComputable in project intellij-community by JetBrains.

the class DomSemContributor method registerSemProviders.

@Override
public void registerSemProviders(SemRegistrar registrar) {
    registrar.registerSemElementProvider(DomManagerImpl.FILE_DESCRIPTION_KEY, xmlFile(), xmlFile -> {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        return new FileDescriptionCachedValueProvider(DomManagerImpl.getDomManager(xmlFile.getProject()), xmlFile);
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_HANDLER_KEY, xmlTag().withParent(psiElement(XmlElementType.XML_DOCUMENT).withParent(xmlFile())), xmlTag -> {
        final FileDescriptionCachedValueProvider provider = mySemService.getSemElement(DomManagerImpl.FILE_DESCRIPTION_KEY, xmlTag.getContainingFile());
        assert provider != null;
        final DomFileElementImpl element = provider.getFileElement();
        if (element != null) {
            final DomRootInvocationHandler handler = element.getRootHandler();
            if (handler.getXmlTag() == xmlTag) {
                return handler;
            }
        }
        return null;
    });
    final ElementPattern<XmlTag> nonRootTag = xmlTag().withParent(or(xmlTag(), xmlEntityRef().withParent(xmlTag())));
    registrar.registerSemElementProvider(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, nonRootTag, tag -> {
        final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
        assert parentTag != null;
        DomInvocationHandler parent = getParentDom(parentTag);
        if (parent == null)
            return null;
        final String localName = tag.getLocalName();
        final String namespace = tag.getNamespace();
        final DomFixedChildDescription description = findChildrenDescription(parent.getGenericInfo().getFixedChildrenDescriptions(), tag, parent);
        if (description != null) {
            final int totalCount = description.getCount();
            int index = 0;
            PsiElement current = tag;
            while (true) {
                current = current.getPrevSibling();
                if (current == null) {
                    break;
                }
                if (current instanceof XmlTag) {
                    final XmlTag xmlTag = (XmlTag) current;
                    if (localName.equals(xmlTag.getLocalName()) && namespace.equals(xmlTag.getNamespace())) {
                        index++;
                        if (index >= totalCount) {
                            return null;
                        }
                    }
                }
            }
            final DomManagerImpl myDomManager = parent.getManager();
            return new IndexedElementInvocationHandler(parent.createEvaluatedXmlName(description.getXmlName()), (FixedChildDescriptionImpl) description, index, new PhysicalDomParentStrategy(tag, myDomManager), myDomManager, null);
        }
        return null;
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_COLLECTION_HANDLER_KEY, nonRootTag, tag -> {
        final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
        assert parentTag != null;
        DomInvocationHandler parent = getParentDom(parentTag);
        if (parent == null)
            return null;
        final DomCollectionChildDescription description = findChildrenDescription(parent.getGenericInfo().getCollectionChildrenDescriptions(), tag, parent);
        if (description != null) {
            DomStub parentStub = parent.getStub();
            if (parentStub != null) {
                int index = ArrayUtil.indexOf(parentTag.findSubTags(tag.getName(), tag.getNamespace()), tag);
                ElementStub stub = parentStub.getElementStub(tag.getLocalName(), index);
                if (stub != null) {
                    XmlName name = description.getXmlName();
                    EvaluatedXmlNameImpl evaluatedXmlName = EvaluatedXmlNameImpl.createEvaluatedXmlName(name, name.getNamespaceKey(), true);
                    return new CollectionElementInvocationHandler(evaluatedXmlName, (AbstractDomChildDescriptionImpl) description, parent.getManager(), stub);
                }
            }
            return new CollectionElementInvocationHandler(description.getType(), tag, (AbstractCollectionChildDescription) description, parent, null);
        }
        return null;
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_CUSTOM_HANDLER_KEY, nonRootTag, new NullableFunction<XmlTag, CollectionElementInvocationHandler>() {

        private final RecursionGuard myGuard = RecursionManager.createGuard("customDomParent");

        @Override
        public CollectionElementInvocationHandler fun(XmlTag tag) {
            if (StringUtil.isEmpty(tag.getName()))
                return null;
            final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
            assert parentTag != null;
            DomInvocationHandler parent = myGuard.doPreventingRecursion(tag, true, (NullableComputable<DomInvocationHandler>) () -> getParentDom(parentTag));
            if (parent == null)
                return null;
            DomGenericInfoEx info = parent.getGenericInfo();
            final List<? extends CustomDomChildrenDescription> customs = info.getCustomNameChildrenDescription();
            if (customs.isEmpty())
                return null;
            if (mySemService.getSemElement(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, tag) == null && mySemService.getSemElement(DomManagerImpl.DOM_COLLECTION_HANDLER_KEY, tag) == null) {
                String localName = tag.getLocalName();
                XmlFile file = parent.getFile();
                for (final DomFixedChildDescription description : info.getFixedChildrenDescriptions()) {
                    XmlName xmlName = description.getXmlName();
                    if (localName.equals(xmlName.getLocalName()) && DomImplUtil.isNameSuitable(xmlName, tag, parent, file)) {
                        return null;
                    }
                }
                for (CustomDomChildrenDescription description : customs) {
                    if (description.getTagNameDescriptor() != null) {
                        AbstractCollectionChildDescription desc = (AbstractCollectionChildDescription) description;
                        Type type = description.getType();
                        return new CollectionElementInvocationHandler(type, tag, desc, parent, null);
                    }
                }
            }
            return null;
        }
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_ATTRIBUTE_HANDLER_KEY, xmlAttribute(), attribute -> {
        final XmlTag tag = PhysicalDomParentStrategy.getParentTag(attribute);
        final DomInvocationHandler handler = tag == null ? null : getParentDom(tag);
        if (handler == null)
            return null;
        final String localName = attribute.getLocalName();
        final Ref<AttributeChildInvocationHandler> result = Ref.create(null);
        handler.getGenericInfo().processAttributeChildrenDescriptions(description -> {
            if (description.getXmlName().getLocalName().equals(localName)) {
                final EvaluatedXmlName evaluatedXmlName = handler.createEvaluatedXmlName(description.getXmlName());
                final String ns = evaluatedXmlName.getNamespace(tag, handler.getFile());
                if (ns.equals(tag.getNamespace()) && localName.equals(attribute.getName()) || ns.equals(attribute.getNamespace())) {
                    final DomManagerImpl myDomManager = handler.getManager();
                    final AttributeChildInvocationHandler attributeHandler = new AttributeChildInvocationHandler(evaluatedXmlName, description, myDomManager, new PhysicalDomParentStrategy(attribute, myDomManager), null);
                    result.set(attributeHandler);
                    return false;
                }
            }
            return true;
        });
        return result.get();
    });
}
Also used : DomStub(com.intellij.util.xml.stubs.DomStub) CustomDomChildrenDescription(com.intellij.util.xml.reflect.CustomDomChildrenDescription) EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) XmlName(com.intellij.util.xml.XmlName) PsiElement(com.intellij.psi.PsiElement) EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) XmlFile(com.intellij.psi.xml.XmlFile) RecursionGuard(com.intellij.openapi.util.RecursionGuard) NullableComputable(com.intellij.openapi.util.NullableComputable) EvaluatedXmlNameImpl(com.intellij.util.xml.EvaluatedXmlNameImpl) XmlElementType(com.intellij.psi.xml.XmlElementType) Type(java.lang.reflect.Type) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomFixedChildDescription(com.intellij.util.xml.reflect.DomFixedChildDescription) ElementStub(com.intellij.util.xml.stubs.ElementStub) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

NullableComputable (com.intellij.openapi.util.NullableComputable)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Nullable (org.jetbrains.annotations.Nullable)3 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)2 PsiFile (com.intellij.psi.PsiFile)2 PackageWrapper (com.intellij.refactoring.PackageWrapper)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 Project (com.intellij.openapi.project.Project)1 AtomicNotNullLazyValue (com.intellij.openapi.util.AtomicNotNullLazyValue)1 RecursionGuard (com.intellij.openapi.util.RecursionGuard)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 XmlElementType (com.intellij.psi.xml.XmlElementType)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 EvaluatedXmlName (com.intellij.util.xml.EvaluatedXmlName)1 EvaluatedXmlNameImpl (com.intellij.util.xml.EvaluatedXmlNameImpl)1 XmlName (com.intellij.util.xml.XmlName)1 CustomDomChildrenDescription (com.intellij.util.xml.reflect.CustomDomChildrenDescription)1