Search in sources :

Example 1 with SmartPointerManager

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

the class PyiRelatedItemLineMarkerProvider method createLineMarkerInfo.

@NotNull
private static RelatedItemLineMarkerInfo<PsiElement> createLineMarkerInfo(@NotNull PsiElement element, @NotNull PsiElement relatedElement, @NotNull String itemTitle) {
    final SmartPointerManager pointerManager = SmartPointerManager.getInstance(element.getProject());
    final SmartPsiElementPointer<PsiElement> relatedElementPointer = pointerManager.createSmartPsiElementPointer(relatedElement);
    final String stubFileName = relatedElement.getContainingFile().getName();
    return new RelatedItemLineMarkerInfo<>(element, element.getTextRange(), ICON, Pass.LINE_MARKERS, element1 -> itemTitle + " in " + stubFileName, new GutterIconNavigationHandler<PsiElement>() {

        @Override
        public void navigate(MouseEvent e, PsiElement elt) {
            final PsiElement restoredRelatedElement = relatedElementPointer.getElement();
            if (restoredRelatedElement == null) {
                return;
            }
            final int offset = restoredRelatedElement instanceof PsiFile ? -1 : restoredRelatedElement.getTextOffset();
            final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(restoredRelatedElement);
            if (virtualFile != null && virtualFile.isValid()) {
                new OpenFileDescriptor(restoredRelatedElement.getProject(), virtualFile, offset).navigate(true);
            }
        }
    }, GutterIconRenderer.Alignment.RIGHT, GotoRelatedItem.createItems(Collections.singletonList(relatedElement)));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SmartPointerManager(com.intellij.psi.SmartPointerManager) MouseEvent(java.awt.event.MouseEvent) RelatedItemLineMarkerInfo(com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SmartPointerManager

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

the class UsageViewImpl method disposeSmartPointers.

private void disposeSmartPointers() {
    SmartPointerManager pointerManager = SmartPointerManager.getInstance(getProject());
    for (Usage usage : myUsageNodes.keySet()) {
        if (usage instanceof UsageInfo2UsageAdapter) {
            SmartPsiElementPointer<?> pointer = ((UsageInfo2UsageAdapter) usage).getUsageInfo().getSmartPointer();
            pointerManager.removePointer(pointer);
        }
    }
}
Also used : SmartPointerManager(com.intellij.psi.SmartPointerManager)

Example 3 with SmartPointerManager

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

the class AngularModulesProvider method getDependencies.

@Override
public List<Link> getDependencies(@NotNull PsiFile file) {
    final Project project = file.getProject();
    if (!AngularIndexUtil.hasAngularJS(project))
        return null;
    if (!(file instanceof JSFile))
        return null;
    final SmartPointerManager spm = SmartPointerManager.getInstance(project);
    final List<Link> result = new ArrayList<>();
    final CommonProcessors.CollectProcessor<String> processor = new CommonProcessors.CollectProcessor<>();
    final GlobalSearchScope fileScope = GlobalSearchScope.fileScope(file);
    final int fileId = FileBasedIndex.getFileId(file.getVirtualFile());
    StubIndex.getInstance().processAllKeys(AngularModuleIndex.KEY, processor, fileScope, new IdFilter() {

        @Override
        public boolean containsFileId(int id) {
            return id == fileId;
        }
    });
    for (String key : processor.getResults()) {
        AngularIndexUtil.multiResolve(project, AngularModuleIndex.KEY, key, element -> {
            if (!file.equals(element.getContainingFile()))
                return true;
            final JSCallExpression expression = PsiTreeUtil.getParentOfType(element, JSCallExpression.class);
            if (expression != null) {
                final List<String> dependencies = AngularModuleIndex.findDependenciesInModuleDeclaration(expression);
                if (dependencies != null) {
                    for (String dependency : dependencies) {
                        final JSImplicitElement resolve = AngularIndexUtil.resolve(project, AngularModuleIndex.KEY, dependency);
                        if (resolve != null) {
                            result.add(new Link(spm.createSmartPsiElementPointer(element.getNavigationElement()), spm.createSmartPsiElementPointer(resolve.getNavigationElement()), key, resolve.getName(), AngularJSIcons.AngularJS));
                        }
                    }
                }
            }
            return true;
        });
    }
    return result;
}
Also used : IdFilter(com.intellij.util.indexing.IdFilter) JSCallExpression(com.intellij.lang.javascript.psi.JSCallExpression) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) SmartPointerManager(com.intellij.psi.SmartPointerManager) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JSFile(com.intellij.lang.javascript.psi.JSFile) CommonProcessors(com.intellij.util.CommonProcessors) JSImplicitElement(com.intellij.lang.javascript.psi.stubs.JSImplicitElement)

Example 4 with SmartPointerManager

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

the class ElementCreator method tryCreate.

public PsiElement[] tryCreate(@NotNull final String inputString) {
    if (inputString.length() == 0) {
        Messages.showMessageDialog(myProject, IdeBundle.message("error.name.should.be.specified"), CommonBundle.getErrorTitle(), Messages.getErrorIcon());
        return PsiElement.EMPTY_ARRAY;
    }
    Ref<List<SmartPsiElementPointer>> createdElements = Ref.create();
    Exception exception = executeCommand(getActionName(inputString), () -> {
        PsiElement[] psiElements = create(inputString);
        SmartPointerManager manager = SmartPointerManager.getInstance(myProject);
        createdElements.set(ContainerUtil.map(psiElements, manager::createSmartPsiElementPointer));
    });
    if (exception != null) {
        handleException(exception);
        return PsiElement.EMPTY_ARRAY;
    }
    return ContainerUtil.mapNotNull(createdElements.get(), SmartPsiElementPointer::getElement).toArray(PsiElement.EMPTY_ARRAY);
}
Also used : SmartPointerManager(com.intellij.psi.SmartPointerManager) List(java.util.List) PsiElement(com.intellij.psi.PsiElement)

Example 5 with SmartPointerManager

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

the class UpdateFoldRegionsOperation method addNewRegions.

private List<FoldRegion> addNewRegions(@NotNull EditorFoldingInfo info, @NotNull FoldingModelEx foldingModel, FoldingUpdate.FoldingMap elementsToFold, @NotNull Map<TextRange, Boolean> rangeToExpandStatusMap, @NotNull Map<FoldRegion, Boolean> shouldExpand, @NotNull Map<FoldingGroup, Boolean> groupExpand) {
    List<FoldRegion> newRegions = new ArrayList<>();
    SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
    for (PsiElement element : elementsToFold.keySet()) {
        ProgressManager.checkCanceled();
        final Collection<FoldingDescriptor> descriptors = elementsToFold.get(element);
        for (FoldingDescriptor descriptor : descriptors) {
            FoldingGroup group = descriptor.getGroup();
            TextRange range = descriptor.getRange();
            String placeholder = null;
            try {
                placeholder = descriptor.getPlaceholderText();
            } catch (IndexNotReadyException ignore) {
            }
            if (range.getEndOffset() > myEditor.getDocument().getTextLength()) {
                LOG.error(String.format("Invalid folding descriptor detected (%s). It ends beyond the document range (%d)", descriptor, myEditor.getDocument().getTextLength()));
                continue;
            }
            FoldRegion region = foldingModel.createFoldRegion(range.getStartOffset(), range.getEndOffset(), placeholder == null ? "..." : placeholder, group, descriptor.isNonExpandable());
            if (region == null)
                continue;
            PsiElement psi = descriptor.getElement().getPsi();
            if (psi == null || !psi.isValid() || !foldingModel.addFoldRegion(region) || !myFile.isValid()) {
                region.dispose();
                continue;
            }
            if (descriptor.canBeRemovedWhenCollapsed())
                region.putUserData(CAN_BE_REMOVED_WHEN_COLLAPSED, Boolean.TRUE);
            info.addRegion(region, smartPointerManager.createSmartPsiElementPointer(psi));
            newRegions.add(region);
            boolean expandStatus = !descriptor.isNonExpandable() && shouldExpandNewRegion(element, range, rangeToExpandStatusMap);
            if (group == null) {
                shouldExpand.put(region, expandStatus);
            } else {
                final Boolean alreadyExpanded = groupExpand.get(group);
                groupExpand.put(group, alreadyExpanded == null ? expandStatus : alreadyExpanded.booleanValue() || expandStatus);
            }
        }
    }
    return newRegions;
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) FoldingGroup(com.intellij.openapi.editor.FoldingGroup) FoldRegion(com.intellij.openapi.editor.FoldRegion) TextRange(com.intellij.openapi.util.TextRange) SmartPointerManager(com.intellij.psi.SmartPointerManager) IndexNotReadyException(com.intellij.openapi.project.IndexNotReadyException) PsiElement(com.intellij.psi.PsiElement)

Aggregations

SmartPointerManager (com.intellij.psi.SmartPointerManager)6 PsiElement (com.intellij.psi.PsiElement)4 RelatedItemLineMarkerInfo (com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 JSCallExpression (com.intellij.lang.javascript.psi.JSCallExpression)1 JSFile (com.intellij.lang.javascript.psi.JSFile)1 JSImplicitElement (com.intellij.lang.javascript.psi.stubs.JSImplicitElement)1 FoldRegion (com.intellij.openapi.editor.FoldRegion)1 FoldingGroup (com.intellij.openapi.editor.FoldingGroup)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 IndexNotReadyException (com.intellij.openapi.project.IndexNotReadyException)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 SmartPsiElementPointer (com.intellij.psi.SmartPsiElementPointer)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 CommonProcessors (com.intellij.util.CommonProcessors)1 IdFilter (com.intellij.util.indexing.IdFilter)1 THashSet (gnu.trove.THashSet)1