Skip to content

Commit

Permalink
Small improvements and support for absolute paths (#84)
Browse files Browse the repository at this point in the history
* Remove pluginUntilBuild from gradle.properties to prevent plugin breaking in newer JetBrains IDEs

Add '**openapi.yaml' to default list of included files

Fixes to broken some URLs in README.md - improved some formatting and included details on using Absolute as well as relative paths for ruleset and included files. Added explanation that files must also be detected as JSON/YAML to be included.

Changed field name for included path patterns

Improved SpectralExternalAnnotator.kt isFileIncluded to also support absolute paths.

Modified collectInformation and doAnnotate to also pass the PsiFile so that activity messace van iunclude the filename.

Made failure to successfully run lintFile() log a warning message rather than debug so that use can see something in the IDE logs. Otherwise it fails silently.

* Update src/main/kotlin/com/schwarzit/spectralIntellijPlugin/SpectralExternalAnnotator.kt

Co-authored-by: Mark Brockhoff <95471369+markbrockhoff@users.noreply.github.com>

* - Fixed wording in `README.md`
- Changed failed execution of `spectral` subprocess to log an error instead of a warning
- Updated version to 2.1.0 as per owner's request
- Added a `CHANGELOG.md` entry as per owner's request

* Update CHANGELOG.md

Co-authored-by: Mark Brockhoff <95471369+markbrockhoff@users.noreply.github.com>

---------

Co-authored-by: Mark Brockhoff <95471369+markbrockhoff@users.noreply.github.com>
  • Loading branch information
noelos and markbrockhoff authored Aug 2, 2023
1 parent 46b24ff commit 44536b9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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
<kbd>Settings/Preferences</kbd> > <kbd>Plugins</kbd> > <kbd>⚙️</kbd> > <kbd>Install plugin from disk...</kbd>

Expand All @@ -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.

<!-- Plugin description end -->

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Editor, List<SpectralIssue>>() {
class SpectralExternalAnnotator : ExternalAnnotator<Pair<PsiFile,Editor>, List<SpectralIssue>>() {
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<PsiFile,Editor>? {
if (file !is JsonFile && file !is YAMLFile) return null

try {
Expand All @@ -34,21 +35,29 @@ class SpectralExternalAnnotator : ExternalAnnotator<Editor, List<SpectralIssue>>
return null
}

return editor
return Pair(file, editor)
}

private fun isFileIncluded(file: PsiFile, includedFiles: List<String>): 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<SpectralIssue> {
override fun doAnnotate(info: Pair<PsiFile,Editor>): List<SpectralIssue> {
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
Expand All @@ -70,7 +79,7 @@ class SpectralExternalAnnotator : ExternalAnnotator<Editor, List<SpectralIssue>>
val issues = linter.run(editor.document.text)
issues
} catch (e: Throwable) {
logger.debug(e)
logger.error(e)
emptyList()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ProjectSettingsState : PersistentStateComponent<ProjectSettingsState> {
var includedFiles: String = """
**openapi.json
**openapi.yml
**openapi.yaml
""".trimIndent()

override fun getState(): ProjectSettingsState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 44536b9

Please sign in to comment.