diff --git a/CHANGELOG.md b/CHANGELOG.md index 01249e9..3fb8e04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ ## [Unreleased] + +### Changed +- Make compatible with IntelliJ IDEA 2023.2 +- Add support for absolute paths in file-matcher +- Fixes & improvements to `README.md` +- Add name of file being analysed to transient status message +- Log an error if `spectral` execution fails due to misconfiguration. This is not ideal but at least offers some user feedback. + ## [2.0.0] - 2023-07-24 ### Changed diff --git a/README.md b/README.md index 60551ff..a48be15 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# jetbrains-plugin-template +# Spectral Linter Plugin for JetBrains -![Build](https://github.com/markbrockhoff/jetbrains-plugin-template/workflows/Build/badge.svg) +![Build](https://github.com/SchwarzIT/spectral-intellij-plugin/workflows/Build/badge.svg) [![Version](https://img.shields.io/jetbrains/plugin/v/com.schwarzit.spectral-intellij-plugin.svg)](https://plugins.jetbrains.com/plugin/com.schwarzit.spectral-intellij-plugin) [![Downloads](https://img.shields.io/jetbrains/plugin/d/com.schwarzit.spectral-intellij-plugin.svg)](https://plugins.jetbrains.com/plugin/com.schwarzit.spectral-intellij-plugin) @@ -17,7 +17,7 @@ OpenApi schemas. - Manually: - Download the [latest release](https://github.com/markbrockhoff/jetbrains-plugin-template/releases/latest) and install + Download the [latest release](https://github.com/SchwarzIT/spectral-intellij-plugin/releases/latest) and install it manually using Settings/Preferences > Plugins > ⚙️ > Install plugin from disk... @@ -33,29 +33,36 @@ _Note: The CLI needs to be in your path and executable from the command line._ Automatic linting of your OpenApi specifications and highlighting in your editor -### Customizable Ruleset +### Configurable Ruleset Specify your own [ruleset](https://meta.stoplight.io/docs/spectral/ZG9jOjYyMDc0NA-rulesets) in the plugins settings, under Preferences -> Tools -> Spectral -> Ruleset. + There you can specify a file on your local machine or just paste the URL of a ruleset available on the internet e.g.: [Schwarz IT API linting rules](https://github.com/SchwarzIT/api-linter-rules). Examples: -- Link to a hosted ruleset: https://raw.githubusercontent.com/SchwarzIT/api-linter-rules/main/spectral-api.yml -- Local ruleset inside the project: ".spectral.json" +- Link to a hosted ruleset: `https://raw.githubusercontent.com/SchwarzIT/api-linter-rules/main/spectral-api.yml` +- Local ruleset relative to Project base-path: `.spectral.json` +- Fully-qualified path: `/Users/mick/.spectral.yaml` + +### Configurable Included path patterns -### Customizable file matching +Select the files that will be linted. By default, every file called "openapi.json", "openapi.yml" or "openapi.yaml" +within the Project root will be matched for linting by the plugin when it's opened. -Select the files that will be linted. By default, every file called "openapi.json" or "openapi.yml" will be linted by -the plugin when it's opened. -You can adjust this in the settings under Preferences -> Tools -> Spectral -> Included files. All paths are relative -to the projects working directory. +You can adjust this in the settings under Preferences -> Tools -> Spectral -> Included path patterns. All paths are relative +to the project's root directory unless absolute. Examples: -- openapi.json: Matches the file called "openapi.json" inside the root directory of the project -- components/**.json: Matches all files inside the directory "components" that end with ".json" +- `openapi.json`: Matches the file called "openapi.json" inside the root directory of the project +- `components/**.yaml`: Matches all files inside the project subdirectory "components" that end with ".yaml" +- `/Users/mick/code/**/openapi*.yaml`: Matches all YAML files within the absolute path "/Users/mick/code" that start with "openapi" and end with ".yaml" + +**Note:** Each file must also be recognised by the IDE as a JSON or YAML file - that is with a suitable File Type association. +If it is detected as a plain text (or any other type) it will be ignored. diff --git a/gradle.properties b/gradle.properties index d5b6eda..c60f056 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,10 +3,10 @@ pluginGroup=com.schwarzit.spectral-intellij-plugin pluginName=Spectral pluginRepositoryUrl=https://github.com/SchwarzIT/spectral-intellij-plugin # SemVer format -> https://semver.org -pluginVersion=2.0.0 +pluginVersion=2.1.0 # Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html pluginSinceBuild=222 -pluginUntilBuild=231.* +#pluginUntilBuild=231.* # IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension platformType=IC platformVersion=2022.2 diff --git a/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt b/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt index 8c3f863..791d723 100644 --- a/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt +++ b/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt @@ -16,13 +16,14 @@ import com.intellij.psi.PsiFile import com.schwarzit.spectralIntellijPlugin.settings.ProjectSettingsState import org.jetbrains.yaml.psi.YAMLFile import java.nio.file.FileSystems +import java.nio.file.Paths -class SpectralExternalAnnotator : ExternalAnnotator>() { +class SpectralExternalAnnotator : ExternalAnnotator, List>() { companion object { val logger = getLogger() } - override fun collectInformation(file: PsiFile, editor: Editor, hasErrors: Boolean): Editor? { + override fun collectInformation(file: PsiFile, editor: Editor, hasErrors: Boolean): Pair? { if (file !is JsonFile && file !is YAMLFile) return null try { @@ -34,21 +35,29 @@ class SpectralExternalAnnotator : ExternalAnnotator> return null } - return editor + return Pair(file, editor) } private fun isFileIncluded(file: PsiFile, includedFiles: List): Boolean { - val matcherPattern = "glob:" + file.project.basePath + "/{" + includedFiles.joinToString(",") + "}" + var matcherPattern = "glob:{" + val iterator = includedFiles.iterator() + while (iterator.hasNext()) { + val pathPattern = iterator.next() + if (!Paths.get(pathPattern).isAbsolute) matcherPattern += file.project.basePath + "/" + matcherPattern += pathPattern + matcherPattern += if (iterator.hasNext()) "," else "}" + } + logger.trace(matcherPattern) val fileMatcher = FileSystems.getDefault().getPathMatcher(matcherPattern) return fileMatcher.matches(file.virtualFile.toNioPath()) } - override fun doAnnotate(editor: Editor): List { + override fun doAnnotate(info: Pair): List { val progressManager = ProgressManager.getInstance() - val computable = Computable { lintFile(editor) } + val computable = Computable { lintFile(info.second) } val indicator = BackgroundableProcessIndicator( - editor.project, - "Spectral: analyzing OpenAPI spec...", + info.second.project, + "Spectral: analyzing OpenAPI specification '${info.first.name}' ...", "Stop", "Stop file analysis", false @@ -70,7 +79,7 @@ class SpectralExternalAnnotator : ExternalAnnotator> val issues = linter.run(editor.document.text) issues } catch (e: Throwable) { - logger.debug(e) + logger.error(e) emptyList() } } diff --git a/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/ProjectSettingsState.kt b/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/ProjectSettingsState.kt index 3be12cb..ec53914 100644 --- a/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/ProjectSettingsState.kt +++ b/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/ProjectSettingsState.kt @@ -15,6 +15,7 @@ class ProjectSettingsState : PersistentStateComponent { var includedFiles: String = """ **openapi.json **openapi.yml + **openapi.yaml """.trimIndent() override fun getState(): ProjectSettingsState { diff --git a/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/SettingsComponent.kt b/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/SettingsComponent.kt index 3934fdb..fab4a05 100644 --- a/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/SettingsComponent.kt +++ b/src/main/kotlin/com/schwarzit/spectralIntellijPlugin/settings/SettingsComponent.kt @@ -16,7 +16,7 @@ class SettingsComponent { mainPanel = FormBuilder.createFormBuilder() .addLabeledComponent(JBLabel("Ruleset"), rulesetInput) - .addLabeledComponent(JBLabel("Included files"), includedFilesInput) + .addLabeledComponent(JBLabel("Included path patterns"), includedFilesInput) .addComponentFillVertically(JPanel(), 0) .panel }