How do I make use of the javascript psi?

Hi,

I am trying to write a javascript code generation plugin. I can do most of what I need by modifying the file's text directly, but I would like to use the javascript psi to make it more robust. Is this possible?

Having had a look around I can see there is a com.intellij.lang.javascript pacakage, but when I try to use it I can only see com.intellij.lang.javascript.boilerplate. From searching around I can see there should be a com.intellij.lang.javascript.psi. Do I need to include another jar? Is there another way to use the psi? I am on IntelliJ 12.

Thanks for your help.

Steve

8 comments

The JavaScript PSI classes are in the com.intellij.lang.javascript.psi package, inside the JavaScriptLanguage.jar and in the JavaScript plugin.

0

Great, thanks. It is probably worth adding this to the plugin how to. Knowing that I just needed that jar would have saved me a good few hours.

For anyone else who is looking to work with the javascript psi, the exact plugin you need is 'JavaScriptLanguage' and you just need to add the following folder to jar to your project.

<idea installation directory>\plugins\JavaScriptLanguage\lib\JavascriptLanguage.jar

0

I can now see the package, but I am still not sure how to create new JS elements, methods, fields etc. I thought JSElementFactory would have methods to create these from strings (like com.intellij.psi.PsiElementFactory) does. How do I need to go about creating a JS psi element?

Thanks.

0

I have managed to create elements using the following code, but is there a better way?

FileType jsType = FileTypeManager.getInstance().getFileTypeByExtension("js");
PsiFile file =  PsiFileFactory.getInstance(e.getProject()).createFileFromText("dummy.js", jsType, code);

0

Cheers! This helped me out a lot.

Make sure to add the `JavaScriptLanguage.jar` in the IntelliJ SDK classpath (not a dependency in the module settings dialog). It should not be distributed with the plugin.

Plugins each have their own class loaders. If you include this jar in your plugin you will have two different versions of the classes (one for the JS plugin and one for your plugin) which will cause cryptic problems such as:

`java.lang.ClassCastException: com.intellij.lang.javascript.psi.ecmal4.impl.JSClassImpl cannot be cast to com.intellij.lang.javascript.psi.ecmal4.impl.JSClassImpl`

Adding the line `<depends>JavaScript</depends>` to `META-INF/plugin.xml` tells IJ to look for the classes in the existing JS plugin.

:)

Updated answer from Dmitry's response.

0

Plugin jars need to be added to the classpath of the IntelliJ IDEA SDK, not as library dependencies of any kind.

0

Where do I find the Javascript plugin with the javascript.jar. I have no clue but been searching around...

Thought it might be listed here, but no :(

https://github.com/JetBrains/intellij-plugins

Please advice. Thanks!

Seems there is no (or very hard to find and lacking!) documentation on how to write Web plugins such as for javascript...

0

The JavaScript plugin is part of IntelliJ IDEA Ultimate. It is not open-source.

0

Please sign in to leave a comment.