use of com.perl5.lang.pod.parser.psi.mixin.PodSectionItem in project Perl5-IDEA by Camelcade.
the class PerlDocUtil method renderElement.
@Contract("null->null")
@Nullable
public static String renderElement(@Nullable PodSection podSection) {
if (podSection == null) {
return null;
}
PsiElement run = podSection;
// detecting first section
while (true) {
PsiElement prevSibling = run.getPrevSibling();
if (prevSibling == null) {
break;
}
if (prevSibling instanceof PodSection && ((PodSection) prevSibling).hasContent()) {
break;
}
if (prevSibling instanceof PodTitledSection) {
podSection = (PodTitledSection) prevSibling;
}
run = prevSibling;
}
boolean isTitledSection = podSection instanceof PodTitledSection;
boolean hasContent = podSection.hasContent();
PsiElement lastSection = podSection;
// detecting last section
while (true) {
PsiElement nextSibling = lastSection.getNextSibling();
boolean isNextTitledSection = nextSibling instanceof PodTitledSection && !(nextSibling instanceof PodFormatterX);
if (nextSibling == null || (isTitledSection && isNextTitledSection && hasContent)) {
break;
}
hasContent = hasContent || nextSibling instanceof PodSection && ((PodSection) nextSibling).hasContent();
lastSection = nextSibling;
}
StringBuilder builder = new StringBuilder();
// appending breadcrumbs
List<String> breadCrumbs = new ArrayList<>();
run = podSection.getParent();
while (true) {
if (run instanceof PodLinkTarget) {
String bcLink = ((PodLinkTarget) run).getPodLink();
if (StringUtil.isNotEmpty(bcLink)) {
breadCrumbs.add(0, PodRenderUtil.getHTMLPsiLink(bcLink, ((PodLinkTarget) run).getPodLinkText()));
}
}
if (run instanceof PsiFile) {
break;
}
run = run.getParent();
}
if (!breadCrumbs.isEmpty()) {
builder.append("<p>");
builder.append(StringUtil.join(breadCrumbs, ": "));
builder.append("</p>");
}
String closeTag = "";
if (podSection instanceof PodSectionItem) {
if (((PodSectionItem) podSection).isBulleted()) {
builder.append("<ul style=\"fon-size:200%;\">");
closeTag = "</ul>";
} else {
builder.append("<dl>");
closeTag = "</dl>";
}
}
builder.append(PodRenderUtil.renderPsiRangeAsHTML(podSection, lastSection));
builder.append(closeTag);
return builder.toString();
}
use of com.perl5.lang.pod.parser.psi.mixin.PodSectionItem in project Perl5-IDEA by Camelcade.
the class PodLinkCompletionProvider method addSectionsCompletions.
protected static void addSectionsCompletions(@Nullable PsiFile targetFile, @NotNull PerlCompletionProcessor completionProcessor) {
if (targetFile == null) {
return;
}
Set<String> distinctString = new HashSet<>();
targetFile.accept(new PodStubsAwareRecursiveVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (completionProcessor.result()) {
super.visitElement(element);
}
}
@Override
public void visitTargetableSection(PodTitledSection o) {
String title = cleanItemText(o.getTitleText());
if (completionProcessor.matches(title) && distinctString.add(title)) {
completionProcessor.process(LookupElementBuilder.create(o, escapeTitle(title)).withLookupString(title).withPresentableText(title).withIcon(PerlIcons.POD_FILE).withTypeText(UsageViewUtil.getType(o)));
}
super.visitTargetableSection(o);
}
@Override
public void visitItemSection(@NotNull PsiItemSection o) {
if (((PodSectionItem) o).isTargetable()) {
super.visitItemSection(o);
} else {
visitElement(o);
}
}
@Contract("null->null")
@Nullable
private String cleanItemText(@Nullable String itemText) {
if (itemText == null) {
return null;
}
String trimmed = StringUtil.trimLeading(itemText.trim(), '*');
if (NumberUtils.isNumber(trimmed)) {
return null;
}
return trimItemText(trimmed);
}
@Override
public void visitPodFormatIndex(@NotNull PsiPodFormatIndex o) {
assert o instanceof PodFormatterX;
if (!((PodFormatterX) o).isMeaningful()) {
return;
}
String indexTitle = ((PodFormatterX) o).getTitleText();
if (StringUtil.isEmpty(indexTitle) || !distinctString.add(indexTitle)) {
return;
}
String escapedIndexTitle = escapeTitle(indexTitle);
if (!completionProcessor.matches(indexTitle) && !completionProcessor.matches(escapedIndexTitle)) {
return;
}
PsiElement indexTarget = ((PodFormatterX) o).getIndexTarget();
String targetPresentableText;
if (indexTarget instanceof PodFile) {
targetPresentableText = cleanItemText(((PodFile) indexTarget).getPodLinkText());
} else if (indexTarget instanceof PodCompositeElement) {
targetPresentableText = cleanItemText(((PodCompositeElement) indexTarget).getPresentableText());
} else {
LOG.warn("Unhandled index target: " + indexTarget);
return;
}
String tailText = null;
if (targetPresentableText != null) {
tailText = "(" + targetPresentableText + ")";
}
completionProcessor.process(LookupElementBuilder.create(o, escapedIndexTitle).withLookupString(indexTitle).withPresentableText(indexTitle).withTailText(tailText).withTypeText(UsageViewUtil.getType(o)).withIcon(PerlIcons.POD_FILE));
}
});
}
use of com.perl5.lang.pod.parser.psi.mixin.PodSectionItem in project Perl5-IDEA by Camelcade.
the class PodOverSectionContentMixin method isBulleted.
@Override
public boolean isBulleted() {
if (myIsBulleted == null) {
PodSectionItem firstItem = getFirstItem();
myIsBulleted = firstItem != null && firstItem.isBulleted();
}
return myIsBulleted;
}
Aggregations