Search code examples
intellij-plugin

How to import IntelliJ classes while developing IntelliJ plugin?


TL;DR

How do I import the com.intellij.psi.JavaPsiFacade class in my IntelliJ plugin?

Background Information

I'm trying to develop an IntelliJ plugin. I've been following the getting started guide here and have been using Gradle with a Groovy build script.

I got the "Hello World" example to run. My next step was to try to use the Java PSI. My project will successfully build when I use some classes (e.g. com.intellij.psi.DelegatePsiTarget), but not others (e.g. com.intellij.psi.JavaPsiFacade).

I'm under the impression that to use certain classes I need to add their sources to my build.gradle class. However, I haven't had much luck figuring out how to track down which source would provide it and how to add the source once I find that out.

The idea I've had so far that seems closest is that I need to add a plugins section (as specified here). However, my previous question still stands: how do I figure out what package provides what I want?

Here is my build.gradle file so far:

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

apply plugin: 'org.jetbrains.intellij'
apply plugin: 'idea'
apply plugin: 'java'

version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

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

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

Solution

  • As I was writing this question, I finally ended up discovering my issue.

    Since I was using a Java API, I needed to declare my dependency on the Java plugin, which was done in two parts:

    1. Adding plugins = ['java'] to my intellij object in build.gradle file.
    2. Adding <depends>com.intellij.modules.java</depends> to my plugin.xml file.

    Hopefully this helps someone else avoid the long road I took. If other people have more input, feel free to add additional answers.