Creating a LocalInspection for go.mod files

Answered

In our plugin we have created several Code Inspections for different file types.

Example for how we created them:

We need to create an inspector for go.mod files.

What should be used in plugin.xml as a language under localInspection tag?
What is the correct visitor (extending PsiElementVisitor) to be used in this case?

4 comments
Comment actions Permalink

Hi Barb!

First of all, you should refer to the docs related to the GoLand Plugin Development that describes development of the plugin that depends on Go.

 

go.mod file is specific to the Go language and is supported by the Go plugin.

To enable such local inspection for the .mod files, you have to find out the language name first - so having the Go plugin installed in your development IDE, you can use PsiViewer plugin to inspect .mod file - it says that the language name is vgo (not go, which is for .go files).

So we're going to set:

<localInspection language="vgo" ... />

Now we need the Go classes to be available in our classpath. In the Gradle build file, specify:

intellij {
// ...

plugins 'java', 'org.jetbrains.plugins.go:193.5662.53.103'
}

Go versions are listed here: https://plugins.jetbrains.com/plugin/9568-go/versions
You should pick the latest version that matches your target IntelliJ SDK.

Now in Plugin's Manifest file (plugin.xml), add:

<depends>org.jetbrains.plugins.go</depends>
<depends>com.intellij.modules.platform</depends>

Basically that's all - your plugin will be dependent on the Go now.

 

If you want to make the Go dependency optional, modify the go <depends> to:

<depends config-file="with-go.xml" optional="true">org.jetbrains.plugins.go</depends>

And move localInspection to this file:

<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<localInspection language="vgo" ... />
</extensions>
</idea-plugin>
1
Comment actions Permalink

I will also note that you can file an issue in our bug tracker:

https://youtrack.jetbrains.com/issues/GO

Describe what kind of inspection you would like to have for go.mod files. We'll consider your proposal and it might just make to the next EAPs (https://blog.jetbrains.com/go/) or next releases.
Best regards, Ivan Yarkov. GoLand team.

0
Comment actions Permalink

Thanks Jakub! Your explanation helped a lot.

For future purposes (if anyone bumps this thread) I used 'VgoModuleSpec' as the element type in my visitor method.

Ivan, thanks for your suggestion but the inspection I require is kinda unique so no issue here.

0
Comment actions Permalink

It looks like the vgo language disappeared in one of the latest versions of the IntelliJ platform.

Is there another solution for implementing go.mod files custom inspections?

Thanks.

0

Please sign in to leave a comment.