Search in sources :

Example 11 with PodTitledSection

use of com.perl5.lang.pod.parser.psi.PodTitledSection in project Perl5-IDEA by Camelcade.

the class PodUsagesHighlightingFactory method createHighlightUsagesHandler.

@Override
@Nullable
public HighlightUsagesHandlerBase<?> createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement target) {
    if (!target.getLanguage().isKindOf(PodLanguage.INSTANCE)) {
        return null;
    }
    PsiElement targetElement = TargetElementUtil.findTargetElement(editor, REFERENCED_ELEMENT_ACCEPTED | ELEMENT_NAME_ACCEPTED);
    if (!(targetElement instanceof PodTitledSection)) {
        return null;
    }
    return new HighlightUsagesHandlerBase<>(editor, file) {

        @Override
        @NotNull
        public List<PsiElement> getTargets() {
            return Collections.singletonList(targetElement);
        }

        @Override
        protected void selectTargets(@NotNull List<? extends PsiElement> targets, @NotNull Consumer<? super List<? extends PsiElement>> selectionConsumer) {
            selectionConsumer.consume(targets);
        }

        @Override
        public void computeUsages(@NotNull List<? extends PsiElement> targets) {
            List<PodTitledSection> allTargetSections = PodLinkToSectionReference.getAllSynonymousSections((PodTitledSection) targetElement);
            if (allTargetSections.isEmpty()) {
                return;
            }
            if (Objects.equals(PsiUtilCore.getVirtualFile(targetElement), PsiUtilCore.getVirtualFile(file))) {
                List<TextRange> writeUsages = getWriteUsages();
                for (PodTitledSection titledSection : allTargetSections) {
                    PsiElement titleElement = titledSection.getTitleElement();
                    if (titleElement != null) {
                        writeUsages.add(titleElement.getTextRange());
                    }
                }
            }
            List<TextRange> readUsages = getReadUsages();
            ReferencesSearch.search(allTargetSections.get(0), new LocalSearchScope(file)).forEach((PsiReference it) -> readUsages.add(it.getRangeInElement().shiftRight(it.getElement().getTextRange().getStartOffset())));
        }
    };
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) Consumer(com.intellij.util.Consumer) PsiReference(com.intellij.psi.PsiReference) List(java.util.List) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) HighlightUsagesHandlerBase(com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with PodTitledSection

use of com.perl5.lang.pod.parser.psi.PodTitledSection in project Perl5-IDEA by Camelcade.

the class PodTitleCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    PsiElement elementParent = element.getParent();
    if (PsiUtilCore.getElementType(element) != POD_IDENTIFIER || element.getPrevSibling() != null || !(elementParent instanceof PodSectionTitle)) {
        return;
    }
    IElementType grandparentElementType = PsiUtilCore.getElementType(elementParent.getParent());
    PerlSimpleCompletionProcessor completionProcessor = new PerlSimpleCompletionProcessor(parameters, result, element);
    if (grandparentElementType == HEAD_1_SECTION) {
        for (String sectionTitle : DEFAULT_POD_SECTIONS) {
            if (completionProcessor.matches(sectionTitle) && !completionProcessor.process(LookupElementBuilder.create(sectionTitle))) {
                break;
            }
        }
    }
    final PsiFile elementFile = element.getContainingFile().getOriginalFile();
    final PsiFile perlFile = PodFileUtil.getTargetPerlFile(elementFile);
    if (perlFile == null) {
        return;
    }
    Set<PerlSubElement> possibleTargets = new HashSet<>();
    PerlPsiUtil.processSubElements(perlFile, possibleTargets::add);
    elementFile.accept(new PodRecursiveVisitor() {

        @Override
        public void visitTargetableSection(PodTitledSection o) {
            processSection(o);
            super.visitTargetableSection(o);
        }

        private void processSection(@NotNull PodTitledSection o) {
            PsiElement titleBlock = o.getTitleElement();
            if (titleBlock == null) {
                return;
            }
            PsiElement firstChild = titleBlock.getFirstChild();
            if (firstChild == null) {
                return;
            }
            // noinspection SuspiciousMethodCalls
            Arrays.stream(firstChild.getReferences()).filter(it -> it instanceof PodSubReference).flatMap(it -> Arrays.stream(((PodSubReference) it).multiResolve(false))).map(ResolveResult::getElement).forEach(possibleTargets::remove);
        }
    });
    for (PerlSubElement subElement : possibleTargets) {
        String lookupString = StringUtil.notNullize(subElement.getPresentableName());
        if (completionProcessor.matches(lookupString) && !completionProcessor.process(LookupElementBuilder.create(subElement, lookupString).withIcon(subElement.getIcon(0)))) {
            break;
        }
    }
    completionProcessor.logStatus(getClass());
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) PodFileUtil(com.perl5.lang.pod.parser.psi.util.PodFileUtil) java.util(java.util) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) IElementType(com.intellij.psi.tree.IElementType) PodRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodRecursiveVisitor) PodSectionTitle(com.perl5.lang.pod.parser.psi.PodSectionTitle) StringUtil(com.intellij.openapi.util.text.StringUtil) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) PerlSimpleCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor) PerlPsiUtil(com.perl5.lang.perl.psi.utils.PerlPsiUtil) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PodSubReference(com.perl5.lang.pod.parser.psi.references.PodSubReference) PodElementPatterns(com.perl5.lang.pod.parser.PodElementPatterns) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) ResolveResult(com.intellij.psi.ResolveResult) CompletionProvider(com.intellij.codeInsight.completion.CompletionProvider) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PodSectionTitle(com.perl5.lang.pod.parser.psi.PodSectionTitle) PodSubReference(com.perl5.lang.pod.parser.psi.references.PodSubReference) PodTitledSection(com.perl5.lang.pod.parser.psi.PodTitledSection) PerlSimpleCompletionProcessor(com.perl5.lang.perl.idea.completion.providers.processors.PerlSimpleCompletionProcessor) PodRecursiveVisitor(com.perl5.lang.pod.parser.psi.PodRecursiveVisitor) IElementType(com.intellij.psi.tree.IElementType) PerlSubElement(com.perl5.lang.perl.psi.PerlSubElement) PsiFile(com.intellij.psi.PsiFile) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PodTitledSection (com.perl5.lang.pod.parser.psi.PodTitledSection)11 PsiElement (com.intellij.psi.PsiElement)9 Nullable (org.jetbrains.annotations.Nullable)7 NotNull (org.jetbrains.annotations.NotNull)4 PsiFile (com.intellij.psi.PsiFile)3 PodStubsAwareRecursiveVisitor (com.perl5.lang.pod.parser.psi.PodStubsAwareRecursiveVisitor)3 ArrayList (java.util.ArrayList)3 TextRange (com.intellij.openapi.util.TextRange)2 IElementType (com.intellij.psi.tree.IElementType)2 PodRecursiveVisitor (com.perl5.lang.pod.parser.psi.PodRecursiveVisitor)2 PodFormatterX (com.perl5.lang.pod.parser.psi.mixin.PodFormatterX)2 Contract (org.jetbrains.annotations.Contract)2 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 CompletionProvider (com.intellij.codeInsight.completion.CompletionProvider)1 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 HighlightUsagesHandlerBase (com.intellij.codeInsight.highlighting.HighlightUsagesHandlerBase)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1 PsiReference (com.intellij.psi.PsiReference)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1