Writing PyCharm Plugin
the complete plugin project is hosted here
Spent days to get this working, so documenting it here. I reported a PyCharm
issue where I want __property__
to be recognized as property
. The
developer recommend me to write a plugin to fix that. PyCharm plugin must be
written in Java (my choice) or Kotlin in IntelliJ. The official document
recommend using Gradle. However I can't get it
working. Beside what I reported there, I've also bumped into
below difficulties:
- On my work laptop (MacOS), IDEA struggled to find the right JDK to use. I've to download and install JDK 12. This maybe due to JAVA_HOME settings or some sort. Also Gradle struggled to import and build.
- On my personal laptop, IDEA prompt to fix Windows Defender. Gradle also hang at importing.
- In
plugin.xml
,<depends>
complainedcom.intellij.modules.python
cannot be resolved. - Installed PyCharm locally and in IDEA set JDK to PyCharm's to solve
com.intellij.modules.python
, but build fails to findPyKnownDecoratorProvider
.
Here's what works for me and all the tricks.
First install PyCharm locally, so its JDK can be used later in IDEA, which has
PyKnownDecoratorProvider
.
Second, configure project JDKs and libraries to add PyCharm JDK.
In IDEA, create a new project. Select "IntelliJ Platform Plugin" from project templates. NOT the one from "Gradle". I think this is what the official document refers to as "DevKit", which JetBrain does not prefer (but worked for me).
In Project SDK, make sure the PyCharm JDK is selected.
Once project is opened, in plugin.xml
, insert:
<depends>com.intellij.modules.python</depends>
This has auto-complete. If IDEA complained ...modules.python
can't be found,
it means the JDK isn't set up correctly. Once <depends>
is specified
correctly, add
<extensions defaultExtensionNs="Pythonid">
<knownDecoratorProvider implementation="pyprop" />
</extensions>
Relevant official doc is here. However, pay attension to change
defaultExtensionNs
to Pythonid
. Otherwise you don't get autocomplete for
knownDecoratorProvider
and IDEA complains, although later build will pass, PyCharm
will load the plugin, it will NOT instantiate your plugin implementation class.
When you get autocomplete for knownDecoratorProvider
you know you get it
right.
knownDecoratorProvider
is declared in
python-core-common.xml
. Note it's qualified name
begins with Pythonid
that's what we put in defaultExtensionNs
.
Now create the class pyprop
under src
:
import com.jetbrains.python.psi.PyKnownDecoratorProvider;
import org.jetbrains.annotations.Nullable;
public class pyprop implements PyKnownDecoratorProvider {
@Nullable
@Override
public String toKnownDecorator(String decoratorName) {
return decoratorName.equals("__property__") ? "property" : null;
}
}
Build and it show succeed.
Debugging
IntelliJ provides good PyCharm plugin debugging. Just hit "debug plugin", it'll bring up a PyCharm instance with the plugin loaded. You can set breakpoints in plugin implementation to make sure they got hit. If so, you know you get it right.
Deploy
Hit "Build" -> "prepare plgin module for deployment". This will package plugin in a "jar" file which can be installed by PyCharm locally.
Finally, the complete plugin is on github