-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an intent to add the option to disable validation on individua…
…l code blocks (#830)
- Loading branch information
Showing
23 changed files
with
356 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...org/asciidoc/intellij/injection/AsciiDocCodeFenceErrorHighlightingIntentionThisBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package org.asciidoc.intellij.injection; | ||
|
||
import com.intellij.codeInsight.intention.IntentionAction; | ||
import com.intellij.lang.injection.InjectedLanguageManager; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.Pair; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.asciidoc.intellij.AsciiDocBundle; | ||
import org.asciidoc.intellij.file.AsciiDocFileType; | ||
import org.asciidoc.intellij.psi.AsciiDocAttributeInBrackets; | ||
import org.asciidoc.intellij.psi.AsciiDocBlockAttributes; | ||
import org.asciidoc.intellij.psi.AsciiDocElementWithLanguage; | ||
import org.asciidoc.intellij.psi.AsciiDocFile; | ||
import org.asciidoc.intellij.psi.AsciiDocUtil; | ||
import org.asciidoc.intellij.settings.AsciiDocApplicationSettings; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
public class AsciiDocCodeFenceErrorHighlightingIntentionThisBlock implements IntentionAction { | ||
@Nls(capitalization = Nls.Capitalization.Sentence) | ||
@NotNull | ||
@Override | ||
public String getText() { | ||
return AsciiDocBundle.message("asciidoc.hide.errors.thisBlock.intention.text"); | ||
} | ||
|
||
@Nls(capitalization = Nls.Capitalization.Sentence) | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return getText(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* This action is only available if there is an active code injection at the active element. | ||
*/ | ||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (file.getFileType() != AsciiDocFileType.INSTANCE || AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().isHideErrorsInSourceBlocks()) { | ||
return false; | ||
} | ||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); | ||
if (element == null) { | ||
return false; | ||
} | ||
AsciiDocElementWithLanguage elementWithLanguage = PsiTreeUtil.getParentOfType(element, AsciiDocElementWithLanguage.class); | ||
if (elementWithLanguage == null) { | ||
return false; | ||
} | ||
if (!elementWithLanguage.validateContent()) { | ||
return false; | ||
} | ||
String language = elementWithLanguage.getFenceLanguage(); | ||
if (language != null) { | ||
if (AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().getDisabledInjectionsByLanguageAsList().contains(language.substring(CodeFenceHighlightInfoFilter.SOURCE_PREFIX.length())) | ||
|| AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().getHiddenErrorsByLanguageAsList().contains(language.substring(CodeFenceHighlightInfoFilter.SOURCE_PREFIX.length()))) { | ||
return false; | ||
} | ||
} | ||
List<Pair<PsiElement, TextRange>> injectedPsiFiles = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(elementWithLanguage); | ||
return injectedPsiFiles != null && injectedPsiFiles.size() > 0; | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); | ||
if (element == null) { | ||
return; | ||
} | ||
AsciiDocElementWithLanguage elementWithLanguage = PsiTreeUtil.getParentOfType(element, AsciiDocElementWithLanguage.class); | ||
|
||
AsciiDocBlockAttributes blockAttributes = PsiTreeUtil.findChildOfType(elementWithLanguage, AsciiDocBlockAttributes.class); | ||
|
||
if (blockAttributes == null) { | ||
return; | ||
|
||
} | ||
PsiElement child = blockAttributes.getFirstChild(); | ||
boolean found = false; | ||
while (child != null) { | ||
if (child instanceof AsciiDocAttributeInBrackets a) { | ||
if (a.getAttrName().equals("opts")) { | ||
String value = a.getAttrValueUnresolved(); | ||
if (value == null || value.isEmpty()) { | ||
value = "novalidate"; | ||
} else { | ||
value = value + ",novalidate"; | ||
} | ||
PsiElement newBlock = createAttributeBlock(project, value); | ||
found = true; | ||
child.replace(newBlock); | ||
break; | ||
} | ||
} | ||
child = child.getNextSibling(); | ||
} | ||
|
||
if (!found) { | ||
PsiElement newBlock = createAttributeBlock(project, "novalidate"); | ||
blockAttributes.addBefore(newBlock.getPrevSibling(), blockAttributes.getLastChild()); | ||
blockAttributes.addBefore(newBlock, blockAttributes.getLastChild()); | ||
} | ||
} | ||
|
||
private AsciiDocAttributeInBrackets createAttributeBlock(@NotNull Project project, String text) { | ||
if (text.contains(",")) { | ||
text = '"' + text + '"'; | ||
} | ||
AsciiDocFile fileFromText = AsciiDocUtil.createFileFromText(project, "[,opts=" + text + "]\n----\n----\n"); | ||
return PsiTreeUtil.findChildOfType(fileFromText, AsciiDocAttributeInBrackets.class); | ||
} | ||
|
||
@Override | ||
public boolean startInWriteAction() { | ||
return true; | ||
} | ||
|
||
} |
122 changes: 122 additions & 0 deletions
122
.../asciidoc/intellij/injection/AsciiDocCodeFenceErrorHighlightingIntentionThisLanguage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package org.asciidoc.intellij.injection; | ||
|
||
import com.intellij.codeInsight.intention.IntentionAction; | ||
import com.intellij.lang.injection.InjectedLanguageManager; | ||
import com.intellij.notification.Notification; | ||
import com.intellij.notification.NotificationAction; | ||
import com.intellij.notification.NotificationType; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.Pair; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.asciidoc.intellij.AsciiDocBundle; | ||
import org.asciidoc.intellij.file.AsciiDocFileType; | ||
import org.asciidoc.intellij.psi.AsciiDocElementWithLanguage; | ||
import org.asciidoc.intellij.settings.AsciiDocApplicationSettings; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
public class AsciiDocCodeFenceErrorHighlightingIntentionThisLanguage implements IntentionAction { | ||
@Nls(capitalization = Nls.Capitalization.Sentence) | ||
@NotNull | ||
@Override | ||
public String getText() { | ||
return AsciiDocBundle.message("asciidoc.hide.errors.thisLanguage.intention.text"); | ||
} | ||
|
||
@Nls(capitalization = Nls.Capitalization.Sentence) | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return getText(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* This action is only available if there is an active code injection at the active element. | ||
*/ | ||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (file.getFileType() != AsciiDocFileType.INSTANCE || AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().isHideErrorsInSourceBlocks()) { | ||
return false; | ||
} | ||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); | ||
if (element == null) { | ||
return false; | ||
} | ||
AsciiDocElementWithLanguage elementWithLanguage = PsiTreeUtil.getParentOfType(element, AsciiDocElementWithLanguage.class); | ||
if (elementWithLanguage == null) { | ||
return false; | ||
} | ||
if (!elementWithLanguage.validateContent()) { | ||
return false; | ||
} | ||
String language = elementWithLanguage.getFenceLanguage(); | ||
if (language != null) { | ||
if (AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().getDisabledInjectionsByLanguageAsList().contains(language.substring(CodeFenceHighlightInfoFilter.SOURCE_PREFIX.length())) | ||
|| AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().getHiddenErrorsByLanguageAsList().contains(language.substring(CodeFenceHighlightInfoFilter.SOURCE_PREFIX.length()))) { | ||
return false; | ||
} | ||
} | ||
List<Pair<PsiElement, TextRange>> injectedPsiFiles = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(elementWithLanguage); | ||
return injectedPsiFiles != null && injectedPsiFiles.size() > 0; | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); | ||
if (element == null) { | ||
return; | ||
} | ||
AsciiDocElementWithLanguage elementWithLanguage = PsiTreeUtil.getParentOfType(element, AsciiDocElementWithLanguage.class); | ||
if (elementWithLanguage == null) { | ||
return; | ||
} | ||
if (!elementWithLanguage.validateContent()) { | ||
return; | ||
} | ||
String language = elementWithLanguage.getFenceLanguage().substring(CodeFenceHighlightInfoFilter.SOURCE_PREFIX.length()); | ||
setHideErrors(true, language); | ||
|
||
Notification notification = new Notification("asciidoctor", AsciiDocBundle.message("asciidoc.hide.errors.notification.title"), | ||
AsciiDocBundle.message("asciidoc.hide.errors.notification.content"), NotificationType.INFORMATION); | ||
notification.addAction(new NotificationAction(AsciiDocBundle.message("asciidoc.hide.errors.notification.rollback.action.text")) { | ||
@Override | ||
public void actionPerformed(@NotNull AnActionEvent e, @NotNull Notification notification) { | ||
setHideErrors(false, language); | ||
notification.expire(); | ||
} | ||
}); | ||
|
||
notification.notify(project); | ||
} | ||
|
||
private void setHideErrors(boolean hideErrors, String language) { | ||
List<String> languages = AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().getHiddenErrorsByLanguageAsList(); | ||
if (hideErrors && !languages.contains(language)) { | ||
languages.add(language); | ||
} else { | ||
languages.remove(language); | ||
} | ||
|
||
AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().setHideErrorsByLanguage(String.join(";", languages)); | ||
|
||
ApplicationManager.getApplication().getMessageBus() | ||
.syncPublisher(AsciiDocApplicationSettings.SettingsChangedListener.TOPIC) | ||
.onSettingsChange(AsciiDocApplicationSettings.getInstance()); | ||
} | ||
|
||
@Override | ||
public boolean startInWriteAction() { | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...rorHighlightingIntention/description.html → ...ingIntentionAllLanguages/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ntionDescriptions/AsciiDocCodeFenceErrorHighlightingIntentionThisBlock/after.txt.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Errors highlighting is disabled |
1 change: 1 addition & 0 deletions
1
...tionDescriptions/AsciiDocCodeFenceErrorHighlightingIntentionThisBlock/before.txt.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Errors are highlighted in code fences |
Oops, something went wrong.