4

I have a Groovy DSL script which uses classes from libraries, for example org.joda.time.LocalDateTime. In the actual Groovy DSL script I don't have the import to the class, it's added later when executing the script. I wrote a GDSL script for code completion in IntelliJ, but IntelliJ doesn't recognize LocalDateTime class in my script.

When I try to do an import to that class in the GDSL script, I get an error saying that the class cannot be resolved, even though it's on the project's classpath, cause when I run the project's tests for example, it works.

How can I make IntelliJ see classes from external libraries in my DSL script using GDSL script?

mike27
  • 965
  • 3
  • 12
  • 22
  • GDSL has only access to the IDE classpath, not your project classpath. The solution depends on the problem you're trying to solve with this class, please describe the problem. – Peter Gromov Oct 05 '16 at 06:46
  • 1
    My problem is that in my DSL script file I'm using external libraries, like Joda, so I have for example code "new LocalDateTime()", but I don't have the import for the class. So IntelliJ is not able to do code completion for the class. I want to use GDSL script to somehow tell IntelliJ to import the class so code completion works. – mike27 Oct 05 '16 at 10:04
  • I don't understand how LocalDateTime can be related to code completion functionality. GDSL is supposed to add some properties and methods to existing types. Do you need date/time information to generate the names or parameters of those members? Or do you need your GDSL to enhance LocalDateTime type itself, so code completion works in some specific way when you have such objects in your normal Groovy code? In the latter case, why do you need to create LocalDateTime in GDSL? – Peter Gromov Oct 05 '16 at 17:13
  • Let me clear the things up: I have my DSL where I use external libraries, so in there I have for example "new LocalDateTime()". But I don't have and don't want to have imports there. But I want IntelliJ to help me with code completion. That's why I thought I could use a GDSL script to tell IntelliJ that when it sees LocalDateTime in my DSL script it means "org.joda.time.LocalDateTime" so that it can do all the code completion for it like for other classes from java standard library. So in a way I want the GDSL script to dynamically add imports of some classes to DSL script. Can GDSL do that? – mike27 Oct 05 '16 at 20:49
  • 1
    I see now, thanks. No, GDSLs currently can't add imports. A simple plugin could do that, by implementing GrImportContributor extension. – Peter Gromov Oct 06 '16 at 07:52
  • Could you provide a link to an example of how to create a similar plugin? – mike27 Oct 06 '16 at 09:03
  • I'm not aware of examples of very similar plugins. Here's a general introduction: http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started.html. And here, how to create extensions: http://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_extensions_and_extension_points.html – Peter Gromov Oct 06 '16 at 11:24
  • I was able to write a plugin that uses org.jetbrains.plugins.groovy.lang.resolve.DefaultImportContributor to add new imports. But how can I add static imports for methods with it? Also I need to add import just to package for it to work, but I want to add full import like "org.joda.time.DateTime". Any idea how to do that? – mike27 Oct 06 '16 at 17:15
  • Which IDE version are you targeting? Since 2016.2, there should be GrImportContributor supporting all kinds of static/star imports. – Peter Gromov Oct 07 '16 at 10:01
  • Ok. I used 2016.2 and it works great! One more question. In my DSL scripts I also have some code that is not a valid Groovy code, and I simply use AST Transformation at compile time to make it work. But IntelliJ complains in the script that it's not a valid code. Can I use GDSL to make IntelliJ not show it as an error? It looks like this: "myMethod someName {}". So first there is always "myMethod", then there is a string (can have special chars like .[]) and then a closure. Can I make IntelliJ to treat it as if it was "myMethod('someName') {}"? – mike27 Oct 07 '16 at 11:13
  • Unfortunately, custom expression-level AST transforms are not yet supported, not even on plugin level. This is parsed as myMethod(someName({})), so the best you could try is to contribute top-level method myMethod(x) and someName(closure) with GDSL, with such parameter and return types so that it would typecheck as it's parsed. – Peter Gromov Oct 07 '16 at 13:18
  • For anyone coming here from Google, [this](https://stackoverflow.com/questions/71369767/modify-groovydsl-classpath-to-include-3rd-party-libraries) should work – crizzis Apr 03 '22 at 18:01

0 Answers0