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())));
}
};
}
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());
}
Aggregations