Skip to content

Commit

Permalink
Allow disabling validation on individual code blocks (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahus1 committed Nov 3, 2024
1 parent 012c4b4 commit 38a7670
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This document provides a high-level view of the changes introduced by release.

- Clear lock for JCEF preview to prevent a blank preview every time you open an AsciiDoc file (#1610)
- Notify user when an Antora component descriptor misses the name (#1728)
- Allow disabling validation on individual code blocks (#830)

=== 0.43.2

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ To do this, all files need to be in one project.

If the IDE opens a single file and not a project with all related files, it might show validation errors where it shouldn't.

== Validation of source code snippets

For source code with a specified language, the IDE will inject the language context and will show validation errors.

[,java]
----
System.out.println("Hello, world!");
----

In the plugin's settings, a user can hide errors in source blocks for all languages, or for specific languages.

image::hide-errors-in-source-blocks.png[]

To prevent this on individual code blocks, add the option `novalidate` to the source block.
This option is a non-standard AsciiDoc option and specific to the AsciiDoc plugin for IntelliJ.

.Adding the `novalidate` option to the beginning
[source,asciidoc]
-----
[%novalidate,java]
----
System.out.println("Hello, world!");
----
-----

.Adding the `novalidate` option to the `opts` attributes
[source,asciidoc]
-----
[,java,opts=novalidate]
----
System.out.println("Hello, world!");
----
-----

== How to edit files as a project

The user selects the menu item menu:File[Open...], then selects the folder and opens it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ public boolean accept(@NotNull HighlightInfo highlightInfo, @Nullable PsiFile fi
PsiLanguageInjectionHost injectionHost = manager.getInjectionHost(file);
PsiFile topLevelFile = manager.getTopLevelFile(file);
if (topLevelFile.getFileType() == AsciiDocFileType.INSTANCE
&& injectionHost instanceof AsciiDocElementWithLanguage) {
&& injectionHost instanceof AsciiDocElementWithLanguage host) {
if (!SEVERITIES.contains(highlightInfo.getSeverity())) {
if (!host.validateContent()) {
return false;
}
if (AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().isHideErrorsInSourceBlocks()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ public interface AsciiDocElementWithLanguage extends PsiLanguageInjectionHost {
String getFenceLanguage();

TextRange getContentTextRange();

default boolean validateContent() {
return true;
}
}
56 changes: 20 additions & 36 deletions src/main/java/org/asciidoc/intellij/psi/AsciiDocListing.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.asciidoc.intellij.psi;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.LiteralTextEscaper;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.tree.IElementType;
Expand All @@ -26,47 +25,32 @@ public void accept(@NotNull PsiElementVisitor visitor) {
super.accept(visitor);
}

@Override
public Type getType() {
return Type.LISTING;
}

@Override
public String getDefaultTitle() {
return "Listing";
}

@Override
public TextRange getContentTextRange() {
return getContentTextRange(AsciiDocTokenTypes.LISTING_BLOCK_DELIMITER, AsciiDocTokenTypes.LITERAL_BLOCK_DELIMITER);
}

@NotNull
@Override
public LiteralTextEscaper<? extends AsciiDocElementWithLanguage> createLiteralTextEscaper() {
return new LiteralTextEscaper<AsciiDocElementWithLanguage>(this) {
@Override
public boolean decode(@NotNull TextRange rangeInsideHost, @NotNull StringBuilder outChars) {
outChars.append(rangeInsideHost.substring(myHost.getText()));
return true;
}

@Override
public int getOffsetInHost(int offsetInDecoded, @NotNull TextRange rangeInsideHost) {
return rangeInsideHost.getStartOffset() + offsetInDecoded;
}

@NotNull
@Override
public TextRange getRelevantTextRange() {
return myHost.getContentTextRange();
}

@Override
public boolean isOneLine() {
return false;
public boolean validateContent() {
final PsiElement element = findPsiChildByType(AsciiDocElementTypes.BLOCK_ATTRIBUTES);
if (element != null) {
AsciiDocBlockAttributes blockAttributes = PsiTreeUtil.findChildOfType(this, AsciiDocBlockAttributes.class);
if (blockAttributes != null) {
String[] attr = blockAttributes.getAttributes();
if (attr.length > 0 && attr[0].contains("%novalidate")) {
return false;
}
String opts = blockAttributes.getAttribute("opts");
if (opts != null) {
for (String opt : opts.split(",")) {
if (opt.trim().equals("novalidate")) {
return false;
}
}
}
}
};
}
return true;
}

@Override
Expand All @@ -82,7 +66,7 @@ public String getFenceLanguage() {
return null;
} else if (attr.length >= 1) {
firstAttr = attr[0];
int locationOfPercent = firstAttr.indexOf("%"); // this handles for example "plantuml%interactive"
int locationOfPercent = firstAttr.indexOf("%"); // this handles, for example, "plantuml%interactive"
if (locationOfPercent != -1) {
firstAttr = firstAttr.substring(0, locationOfPercent);
}
Expand Down

0 comments on commit 38a7670

Please sign in to comment.