intellij-ideagroovygroovydoc

Using Groovydoc to document an externally defined global variable


I am writing a snippet of Groovy code that will be executed within the context of a particular application (specifically NoMagic / Cameo Enterprise Architecture).

According to their documentation, they provide certain pre-defined objects that can be referenced as global variables from within my Groovy script.

My problem is that my IDE (IntelliJ IDEA) is not aware of these objects, and is therefore unable to provide any code completion suggestions and also cannot resolve methods properly when referencing these global variables.

I would like to know if there is a way to document the existence of these variables, so that IntelliJ is aware of them. Perhaps there is a way to declare this with Groovydoc?

// I would like to declare that the global variable 'ALH' will be
// defined at runtime as a specific type.

// Perhaps something like the following?
/**
 * @RuntimeValue
 * com.nomagic.magicdraw.simulation.utils.ALH ALH
 */

import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.NamedElement

static void do_stuff(NamedElement e) {
    // Do things...
}

NamedElement my_element = ALH.getCaller() // IntelliJ cannot resolve 'ALH'

do_stuff(my_element)

Solution

  • If that where just a class with static methods, then an import should suffice. But for an instance injected into the script from the caller there is (to my knowledge) no strict groovy way.

    For all suggestions here to work, the classes must be in the class-path, but given NamedElement works in your example, that's most likely the case.

    If you are fine with changing your scripts, you could just write your own global variable at the beginning of the file. E.g.

    com.nomagic.magicdraw.simulation.utils.ALH MY_ALH = ALH
    

    (that should even work using the same name and intentionally shadowing the outside binding).

    If you are not willing to change every file, and you only want to make IDEA behave nicely, your options are:

    Add a GDSL file and make the type known. E.g. put Types.gdsl beside your script:

    contributor(context(scope: scriptScope(), filetypes: ['groovy'])) {
        property name: "ALH", type: "com.nomagic.magicdraw.simulation.utils.ALH"
    }
    

    This file can be checked into source control with your scripts and other collaborators can benefit from it.

    If you really just want to have it work for you, you can use Add Dynamic Property via the light-bulb/alt+enter menu on the error at AHL (alt+enter, choose Add dynamic property, pick the type).