diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 3d750ab6b..355d54bdc 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -10,6 +10,7 @@ This document provides a high-level view of the changes introduced by release. === 0.43.3 - 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) === 0.43.2 diff --git a/src/main/java/org/asciidoc/intellij/editor/notification/AntoraComponentNameMissingNotificationProvider.java b/src/main/java/org/asciidoc/intellij/editor/notification/AntoraComponentNameMissingNotificationProvider.java new file mode 100644 index 000000000..c7f0109e0 --- /dev/null +++ b/src/main/java/org/asciidoc/intellij/editor/notification/AntoraComponentNameMissingNotificationProvider.java @@ -0,0 +1,68 @@ +package org.asciidoc.intellij.editor.notification; + +import com.intellij.ide.BrowserUtil; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.fileEditor.FileEditor; +import com.intellij.openapi.fileEditor.OpenFileDescriptor; +import com.intellij.openapi.project.DumbAware; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.ui.EditorNotificationPanel; +import com.intellij.ui.EditorNotificationProvider; +import org.asciidoc.intellij.AsciiDocWrapper; +import org.asciidoc.intellij.file.AsciiDocFileType; +import org.asciidoc.intellij.psi.AsciiDocUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import java.util.Map; +import java.util.function.Function; + +import static org.asciidoc.intellij.psi.AsciiDocUtil.ANTORA_YML; + +/** + * Notify a user that an Antora component name is missing, which is a required field. + */ +public class AntoraComponentNameMissingNotificationProvider implements EditorNotificationProvider, DumbAware { + + @Override + public @Nullable Function collectNotificationData(@NotNull Project project, @NotNull VirtualFile file) { + // only in AsciiDoc or Antora files + Map antora; + VirtualFile antoraFile; + if (file.getFileType() == AsciiDocFileType.INSTANCE) { + VirtualFile antoraModuleDir = AsciiDocUtil.findAntoraModuleDir(project, file); + if (antoraModuleDir == null || antoraModuleDir.getParent() == null || antoraModuleDir.getParent().getParent() == null) { + return null; + } + antoraFile = antoraModuleDir.getParent().getParent().findChild(ANTORA_YML); + if (antoraFile == null) { + return null; + } + antora = AsciiDocWrapper.readAntoraYaml(project, antoraFile); + } else if (file.getName().equals(ANTORA_YML)) { + antoraFile = file; + antora = AsciiDocWrapper.readAntoraYaml(project, file); + } else { + return null; + } + + if (antora.get("name") != null) { + return null; + } + + return fileEditor -> { + final EditorNotificationPanel panel = new EditorNotificationPanel(); + panel.setText("The Antora component descriptor 'antora.yml' is missing the attribute 'name'."); + if (!file.equals(antoraFile)) { + panel.createActionLabel("Open 'antora.yml'", () + -> ApplicationManager.getApplication().invokeLater(() + -> new OpenFileDescriptor(project, antoraFile).navigate(true))); + } + panel.createActionLabel("Read more...", () + -> BrowserUtil.browse("https://docs.antora.org/antora/latest/component-version-descriptor/")); + return panel; + }; + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 43e61b4b3..be7ac07e3 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -253,6 +253,8 @@ implementation="org.asciidoc.intellij.editor.notification.FileOutsideCurrentProjectNotificationProvider"/> +