Skip to content

Commit

Permalink
ifdef/ifndef/endif body references attributes in PSI asciidoctor#275
Browse files Browse the repository at this point in the history
  • Loading branch information
ahus1 committed Jul 12, 2019
1 parent fdd2833 commit 57d1e7d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ public Object[] getVariants() {
String attributeName = declaration.getAttributeName();
variants.add(LookupElementBuilder.create(attributeName)
.withIcon(AsciiDocIcons.ASCIIDOC_ICON)
.withPresentableText(attributeName + value)
.withTailText(value, true)
.withTypeText(declaration.getContainingFile().getName())
.withInsertHandler((insertionContext, item) -> {
// the finalizing } hasn't been entered yet, autocomplete it here
int offset = insertionContext.getStartOffset();
PsiElement element = insertionContext.getFile().findElementAt(offset);
if (element != null && element.getNode() != null && element.getNode().getElementType() != AsciiDocTokenTypes.ATTRIBUTE_REF) {
if (element != null && element.getNode() != null
&& element.getNode().getElementType() != AsciiDocTokenTypes.ATTRIBUTE_REF
&& element.getNode().getElementType() != AsciiDocTokenTypes.BLOCK_MACRO_BODY) {
offset += attributeName.length();
insertionContext.getDocument().insertString(offset, "}");
offset += 1;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/asciidoc/intellij/psi/AsciiDocBlockMacro.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
public class AsciiDocBlockMacro extends AsciiDocStandardBlock {
private static final Set<String> HAS_FILE_AS_BODY = new HashSet<>();
private static final Set<String> HAS_ATTRIBUTE_BODY = new HashSet<>();

static {
HAS_FILE_AS_BODY.addAll(Arrays.asList(
Expand All @@ -33,6 +34,10 @@ public class AsciiDocBlockMacro extends AsciiDocStandardBlock {
"nwdiag", "packetdiag", "plantuml", "rackdiag", "seqdiag", "shaape", "svgbob",
"syntrax", "umlet", "vega", "vegalite", "wavedrom"
));
HAS_ATTRIBUTE_BODY.addAll(Arrays.asList(
// standard asciidoctor
"ifdef", "ifndef"
));
}

public AsciiDocBlockMacro(@NotNull ASTNode node) {
Expand Down Expand Up @@ -65,6 +70,29 @@ public PsiReference[] getReferences() {
);
return references.toArray(new PsiReference[0]);
}
} else if (HAS_ATTRIBUTE_BODY.contains(getMacroName())) {
ASTNode bodyNode = getNode().findChildByType(AsciiDocTokenTypes.BLOCK_MACRO_BODY);
if (bodyNode != null) {
String file = bodyNode.getText();
ArrayList<PsiReference> references = new ArrayList<>();
int start = 0;
for (int i = 0; i < file.length(); ++i) {
if (file.charAt(i) == ',' || file.charAt(i) == '+') {
references.add(
new AsciiDocAttributeDeclarationReference(this,
TextRange.create(bodyNode.getPsi().getStartOffsetInParent() + start, bodyNode.getPsi().getStartOffsetInParent() + i)
)
);
start = i + 1;
}
}
references.add(
new AsciiDocAttributeDeclarationReference(this,
TextRange.create(bodyNode.getPsi().getStartOffsetInParent() + start, bodyNode.getPsi().getStartOffsetInParent() + file.length())
)
);
return references.toArray(new PsiReference[0]);
}
}
return super.getReferences();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public Object[] getVariants() {
final Icon icon = result.getElement().getIcon(Iconable.ICON_FLAG_READ_STATUS | Iconable.ICON_FLAG_VISIBILITY);
additionalItems.add(
FileInfoManager.getFileLookupItem(result.getElement(), "{" + decl.getAttributeName() + "}", icon)
.withTypeText(decl.getAttributeValue())
.withTailText(" (" + decl.getAttributeValue() + ")", true)
.withTypeText(decl.getContainingFile().getName())
);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/asciidoc/intellij/psi/AsciiDocPsiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,20 @@ public void testImageBlockMacroWithReferences() {
assertEquals(1, children.length);
AsciiDocBlockMacro blockMacro = (AsciiDocBlockMacro) children[0];
assertEquals(2, blockMacro.getReferences().length);
assertEquals(AsciiDocFileReference.class, blockMacro.getReferences()[0].getClass());
assertEquals(2, blockMacro.getReferences()[0].getVariants().length);
}

public void testIfdefBlockMacroWithReferences() {
PsiFile psiFile = configureByAsciiDoc(":hi: ho\nifdef::hi,ho+hu[]");
PsiElement[] children = psiFile.getChildren();
assertEquals(3, children.length);
AsciiDocBlockMacro blockMacro = (AsciiDocBlockMacro) children[2];
assertEquals(3, blockMacro.getReferences().length);
assertEquals(AsciiDocAttributeDeclarationReference.class, blockMacro.getReferences()[0].getClass());
assertEquals(1, blockMacro.getReferences()[0].getVariants().length);
}

public void testIncompleteBlockMacroWithAttributesInNewLine() {
PsiFile psiFile = configureByAsciiDoc("image::foo.png\n[hi]");
PsiElement[] children = psiFile.getChildren();
Expand Down

0 comments on commit 57d1e7d

Please sign in to comment.