use of com.perl5.lang.pod.parser.psi.PodRecursiveVisitor in project Perl5-IDEA by Camelcade.
the class PodTitledSection method getTitleTextWithoutIndexes.
/**
* @return a text of the section title with all formatting codes but indexes, or null if there is no title section
*/
@Nullable
default String getTitleTextWithoutIndexes() {
PsiElement titleElement = getTitleElement();
if (titleElement == null) {
return null;
}
StringBuilder sb = new StringBuilder();
titleElement.accept(new PodRecursiveVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (element.getChildren().length > 0) {
super.visitElement(element);
} else {
sb.append(element.getNode().getChars());
}
}
@Override
public void visitPodFormatIndex(@NotNull PsiPodFormatIndex o) {
}
});
return sb.toString().trim();
}
use of com.perl5.lang.pod.parser.psi.PodRecursiveVisitor in project Perl5-IDEA by Camelcade.
the class UpdatePerlPackageFileCopyright method getCopyrightRangesInPod.
/**
* @return text ranges to remove block and text range to replace block
*/
@Nullable
private Pair<TextRange, TextRange> getCopyrightRangesInPod() {
Pattern pattern = getSearchPattern();
if (pattern == null) {
return null;
}
PsiFile podPsiFile = getPodFile();
if (podPsiFile == null) {
return null;
}
Ref<PodTitledSection> copyrightSectionRef = Ref.create();
podPsiFile.accept(new PodRecursiveVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (copyrightSectionRef.isNull()) {
super.visitElement(element);
}
}
@Override
public void visitTargetableSection(PodTitledSection o) {
String titleText = o.getTitleText();
if (StringUtil.isNotEmpty(titleText) && pattern.matcher(titleText).find()) {
copyrightSectionRef.set(o);
return;
}
super.visitTargetableSection(o);
}
});
PodTitledSection copyrightSection = copyrightSectionRef.get();
if (copyrightSection == null) {
return null;
}
int[] offsetAdjustments = new int[] { -1, -1 };
copyrightSection.accept(new PodRecursiveVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (offsetAdjustments[1] > -1) {
return;
}
IElementType elementType = PsiUtilCore.getElementType(element);
if (elementType == CUT_SECTION) {
offsetAdjustments[1] = element.getTextOffset() - 1;
if (PsiUtilCore.getElementType(PsiTreeUtil.nextLeaf(element)) == POD_OUTER) {
offsetAdjustments[0] = element.getTextRange().getEndOffset() + 1;
} else {
offsetAdjustments[0] = offsetAdjustments[1];
}
return;
}
super.visitElement(element);
}
});
PsiElement sectionNextSibling = copyrightSection.getNextSibling();
while (sectionNextSibling instanceof PsiWhiteSpace) {
sectionNextSibling = sectionNextSibling.getNextSibling();
}
TextRange copyrightSectionTextRange = copyrightSection.getTextRange();
TextRange copyrightSectionRange = sectionNextSibling == null ? copyrightSectionTextRange : TextRange.create(copyrightSectionTextRange.getStartOffset(), sectionNextSibling.getTextRange().getStartOffset());
PsiElement copyrightBlock = copyrightSection.getContentBlock();
TextRange copyrightBlockRange = copyrightBlock == null ? TextRange.EMPTY_RANGE : copyrightBlock.getTextRange();
if (offsetAdjustments[0] < 0) {
return Pair.create(copyrightSectionRange, copyrightBlockRange);
}
return Pair.create(TextRange.create(copyrightSectionRange.getStartOffset(), offsetAdjustments[0]), TextRange.create(copyrightBlockRange.getStartOffset(), offsetAdjustments[1]));
}
use of com.perl5.lang.pod.parser.psi.PodRecursiveVisitor 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());
}
use of com.perl5.lang.pod.parser.psi.PodRecursiveVisitor in project Perl5-IDEA by Camelcade.
the class PodTitledSectionMixin method setName.
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
PsiElement nameIdentifier = getNameIdentifier();
if (nameIdentifier == null) {
return this;
}
StringBuilder indexes = new StringBuilder();
nameIdentifier.accept(new PodRecursiveVisitor() {
@Override
public void visitPodFormatIndex(@NotNull PsiPodFormatIndex o) {
indexes.append(o.getText());
}
});
if (indexes.length() > 0) {
name = name + "\n" + indexes;
}
ElementManipulators.getManipulator(nameIdentifier).handleContentChange(nameIdentifier, name);
return this;
}
Aggregations