使用 IntelliJ 2019.3.1 为 Clion 开发插件。如何为我的插件项目添加 C++ 语言支持?

Using IntelliJ 2019.3.1 for plugin development for Clion. How to add c++ language support for my plugin project?

我目前正在学习为 IntelliJ IDE 编写插件。我特别感兴趣 为 CLion 开发插件。因此我想在我的插件项目中添加对 c++ 的支持。

我的问题:

我在文档中阅读了如何为 C++ 开发插件。但是它描述的方式对我不起作用。 IntelliJ 无法解析我要添加的依赖项。

我目前知道的:

1. This 部分文档解释了插件可能依赖于其他插件, 以及如何添加依赖项。

Plugin dependencies

Your plugin may depend on classes from other plugins. In this case, plugins can be either bundled, third-party or even your own. For instructions on how to express the dependencies, refer to Plugin Dependencies.

Your plugin should specify which product or products it will be compatible with (all IntelliJ-based IDEs, CLion only, or some subset). You can do that by declaring module dependencies with the tag in plugin.xml (see Plugin Compatibility with IntelliJ Products).

2. This 文档的一部分解释了哪个功能在哪个插件中。每种特定语言似乎都是一个插件。所以开发一个要解析c++的插件,就要依赖c++插件了。

Modules Specific to Functionality More specialized functionality is also delivered via modules and plugins in IntelliJ Platform-based products. For example, the com.intellij.modules.python module supports the Python language-specific functionality. If a plugin uses functionality from this module, such as Python-specific inspections and refactoring, it must declare a dependency on this module.

...

The following table lists(1) modules or built-in plugins that provide specific functionality, and the products that currently ship with them.

根据上面提到的table我需要添加<depends>com.intellij.modules.cidr.lang</depends>作为c++的依赖。但是,当我将此行添加到我的 plugins.xml 文件时,无法识别 cidr.lang 部分。

3. Whosebug问题中,关于java插件开发的问题,有人回答说最近有变化,现在也有变化必须将所需的插件添加到 build.gradle。此外,java 语言支持现在是一个内置插件。

我猜要做什么

我猜 c++ 支持现在也是一个内置插件?但是如何添加呢?

所以不用

intellij {
    version '2019.2'
    plugins 'java'
}

也许我需要这样的东西?

intellij {
    version '2019.2'
    plugins 'c++' // or maybe cidr??
}

附录

我的javaclass:

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
//import com.jetbrains.cidr.lang.psi.OCFunctionDefinition; <-- This import does not work 
public class HelloAction extends AnAction {
    public HelloAction() {
        super("Hello");
    }

    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {
        Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR);
        PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE);
        if (editor == null || psiFile == null) return;
        int offset = editor.getCaretModel().getOffset();

        final StringBuilder infoBuilder = new StringBuilder();
        PsiElement element = psiFile.findElementAt(offset);
        infoBuilder.append("Element at caret: ").append(element).append("\n");
        Messages.showMessageDialog(anActionEvent.getProject(), infoBuilder.toString(), "PSI Info", null);

        psiFile.accept(new PsiRecursiveElementWalkingVisitor(){
            @Override
            public void visitElement(PsiElement element) {
                super.visitElement(element);
                infoBuilder.append(element.getText()).append("\n");

            }
        });
    }

    @Override
    public void update(AnActionEvent e) {
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
        e.getPresentation().setEnabled(editor != null && psiFile != null);
    }
}

我的build.gradle文件

plugins {
    id 'java'
    id 'org.jetbrains.intellij' version '0.4.15'
}

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
    version 'LATEST-EAP-SNAPSHOT'
    type 'CL'
}
patchPluginXml {
    changeNotes """
      Add change notes here.<br>
      <em>most HTML tags may be used</em>"""
}

我的plugin.xml

<idea-plugin>
    <id>org.helloplugin</id>
    <name>Hello Action Project</name>
    <version>0.0.1</version>
    <vendor email="dummy" url="dummy">dummy</vendor>

    <depends>com.intellij.modules.lang</depends>
    <depends>com.intellij.modules.cidr.lang</depends> // cannot be resolved

    <extensions defaultExtensionNs="com.intellij">
    </extensions>

    <actions>
        <group id="MyPlugin.SampleMenu" text="Greeting" description="Greeting menu">
            <add-to-group group-id="MainMenu" anchor="last"/>
            <action id="Myplugin.Textboxes" class="HelloAction" text="Hello" description="Says hello"/>
        </group>
    </actions>
</idea-plugin>

我在文档中找到了答案。我只需要重新导入项目。

Exploring Module and Plugin APIs

Once the dependency on a module or plugin is declared in plugin.xml, it’s useful to explore the packages and classes available in that dependency. The section below gives some recommended procedures for discovering what’s available in a module or plugin on which a project depends. These procedures assume a project has the build.gradle and plugin.xml dependencies configured correctly. Exploring APIs as a Consumer

Exploring the available packages and classes in a plugin or module utilizes features in the IntelliJ IDEA IDE.

If the project is not up to date, Reimport the Gradle project as a first step. Reimporting the project will automatically update the dependencies.

所以对于c++语言的支持,没有必要在build.gradle

中添加一些东西

我的plugin.xml

<idea-plugin>
    <id>org.helloplugin</id>
    <name>Hello Action Project</name>
    <version>0.0.1</version>
    <vendor email="dummy" url="dummy">dummy</vendor>

    <depends>com.intellij.modules.platform</depends>
    <depends>com.intellij.modules.lang</depends>
    <depends>com.intellij.modules.cidr.lang</depends>


    <extensions defaultExtensionNs="com.intellij">
    </extensions>

    <actions>
        <group id="MyPlugin.SampleMenu" text="Greeting" description="Greeting menu">
            <add-to-group group-id="MainMenu" anchor="last"/>
            <action id="Myplugin.Textboxes" class="HelloAction" text="Hello" description="Says hello"/>
        </group>
    </actions> </idea-plugin>