Make auto-completion work in Kotlin scripts with context

I work on a Kotlin project that also uses Kotlin scripts (embedded as resources). The Kotlin script files are evaluated with some context, i.e. with variables bound. As a result, the plain script does not compile / run by itself (Ctrl+Shift+F10 on Windows), and either does auto-completion work.

Is there a way to tell the IDE about the context in which the script is run, i.e. which variables will be bound at runtime, so that the IDE will be able to perform auto-completion when working on the script inside the IDE?

(Sorry for the late answer, seems we overlooked your question here).

I assume that you’re setting the context by using your own script definition, either by having the variables passed in the base class constructor, or passing it in the configuration as provided properties. If it is done differently, please explain how.

To make autocompletion work, you need to supply Intellij with your script definition. The easiest way is to put the jar with the script definition and discovery file to the module’s classpath, and put the script into sources. Or you can write an intellij plugin that supplies the jars to idea - in this case you can have your script in any location. And as a substitution (or workaround) for the latter, you can specify the FQN of your definition class along with the classpath needed to load it in the kotlin compiler settings → “Kotlin scripting” in Intellij.
We are working on the other ways to configure scripting in the IDE too.

I’m setting context (and I mean context in the literal sense, not javax.script.ScriptContext) by two means:

  • binding variables via engine.put()
  • adding preface / postface literal text to the main script to execute

As the project in question is Open Source, let me give you some pointers. This is an example for an embedded script that, if open in the IDE, lights up red like a Christmas tree due to undefined variables:

https://github.com/heremaps/oss-review-toolkit/blob/master/evaluator/src/main/resources/rules/no_gpl_declared.kts

And this is the class that defines the context in which the script is run:

My question is: How can I make syntax checking for no_gpl_declared.kts work in the IDE, and ideally also run the script from the IDE?

What you would need to do is to generate/create mock classes that provide the symbols you expect. You can then mark this mock project/jar as a “provided” or “compileOnly” dependency of the script so that it can work. It may require you to put the script in a proper module though (not in resource).

It is quite unlikely that we’ll be able to support a variant with script text modifications in the IDE any time soon (although there is some functionality in the pipeline that can substitute it at least partially).

But at the current stage, you can achieve the same thing using the different approach. It might be a bit complicated though:

First, you need to create a script definition - a separate jar that describes your script “template”, e.g. similar to the kotlin/libraries/tools/kotlin-main-kts at master · JetBrains/kotlin · GitHub
Your definition may look something like:

@KotlinScript(fileExtension = "custom.ext", compilationConfiguration = ScriptConfiguration::class)
abstract class MyScript(val bindings: Map<String, Any?>) {
    val ortResult = bindings["ortResult"] as OrtResult
    val evalErrors = mutableListOf<OrtIssue>()
}

object ScriptConfiguration : ScriptCompilationConfiguration(
    {
        defaultImports("com.here.ort.model.*", "java.util.*")
        ide {
            acceptedLocations(ScriptAcceptedLocation.Everywhere)
        }
    })

It is a good idea to have a dedicated extension for your scripts (“custom.ext” in the example above), since IDE distinguish scripts by the extension.

Then you’ll need to create your own JSR-223 factory the same way as here - kotlin/KotlinJsr223ScriptEngineFactoryExamples.kt at master · JetBrains/kotlin · GitHub, but use your script definition (MyScript) in place of KotlinStandardJsr223ScriptTemplate. You probably can do it in the same jar. And you need to register it in the services folder, of course.

You’ll still need a postface part in your evaluator though, but it seems not relevant to the IDE.

Then finally you need to supply Intellij with the definition. The simplest ad-hoc way to do it is to specify the FQN of your definition class along with the classpath needed to load it in the kotlin compiler settings → “Kotlin scripting” in Intellij.

3 Likes

I do have a similar problem but I do not have a real “template” to use. My code to invoke a script is:

val result = host.eval(
                    InputStreamReader(`is`).readText().toScriptSource(),
                    ScriptCompilationConfiguration {
                        baseClass(IntegrationConfiguration::class)
                        jvm {
                            //
                            // This is needed as workaround for:
                            //     https://youtrack.jetbrains.com/issue/KT-27497
                            //
                            javaHome(File(javaHome))

                            //
                            // The Kotlin script compiler does not inherit
                            // the classpath by default
                            //
                            dependenciesFromClassloader(wholeClasspath = true)
                        }
                    },
                    ScriptEvaluationConfiguration {
                        //
                        // Arguments used to initialize the script base class
                        //
                        constructorArgs(registry, builder)
                    }
                )

So when I edit my kotlin script, there’s no auto completion even if I add IntegrationConfiguration as script template class.

How can I enable auto completion ?

Full code is here: https://github.com/apache/camel-k/blob/master/runtime/camel-k-runtime-kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt

This looks very interesting, but currently i’m not sure how to implement this. In your example you’re using:

ide {
acceptedLocations(ScriptAcceptedLocation.Everywhere)
}

But this seems not to be in official maven repositories but only in kontlin-dev repository. But i wasn’t able to use it (version 1.3.30-dev-576 from bintray) because of missing dependecies for thist version in the repository (despite the warning that the kotlin-gradle-plugin won’t fit the Intellij Idea version). Earlier versions i’ve tried led me to a 401-Not-Authorized.

But maybe i’m trying all this too naive. I do apologize for this.

Thanks!

@lburgazzoli, you need to move your ScriptCompilationConfiguration into an object is in my sample above and reference it in the @KotlinScriptannotation, with which your IntegrationConfiguration is (should be) annotated. Then if IDEA will pick your template, it should offer appropriate highlighting/completion.
But it makes sense to replace your dependenciesFromClassloader in the configuration with specific dependencies, otherwise, you’ll likely get different classpath on the runtime and in the IDE.
(although taking into account the missing acceptedLocations configuration key in the public Kotlin releases, by default it will work only for the scripts in source roots; so maybe it is better to wait until 1.3.20)

@solatis, this functionality should be part of the 1.3.20. We may publish some valid dev version before that, but I cannot guarantee it yet.

how can I combine host.eval and annotation style set-up ?

The best way is to use val cfg = createJvmCompilationConfigurationFromTemplate<IntegrationConfiguration> and then use it as a second param to eval.

@ilya.chernikov

I finally had time to get my scripting engine working, here the PR: improve kotlin support by lburgazzoli · Pull Request #92 · apache/camel-k-runtime · GitHub.

Thank you very much for the support.

I had a brief look, the implementation looks correct to me. Have fun! :slight_smile:

I am trying to get camel-k kotlin script autocomplete working in intellij but I don’t know how to configure the Script Template Classes and Script templates classpath.

This thread seems to be the only one that covers this! Any help would be appreciated.
I have org.apache.camel.k.loader.kotlin.dsl.IntegrationConfiguration in Compiler → Kotlin Complier → Kotlin Script → Script template classes
and
/Users/dylan/.m2/repository/org/apache/camel/k/camel-k-loader-kotlin/1.0.9/ in Compiler → Kotlin Complier → Kotlin Script → Script template classpath

but autocomplete is still not working.

Is there a way to see error messages from intellij to see what is wrong?

I got it working by updating to use org.apache.camel.k:camel-k-loader-kotlin:1.2.1

Which extension point should be used to supply these jars in an intellij plugin?

Which extension point should be used to supply these jars in an intellij plugin?

This is the EP - https://github.com/JetBrains/kotlin/blob/master/libraries/scripting/intellij/src/kotlin/script/experimental/intellij/scriptDefinitionProvider.kt
And the artefact is named kotlin-scripting-intellij

Compiler → Kotlin Complier → Kotlin Script → Script template classes

I can’t find this setting anymore in IntelliJ IDEA 2021.3. Is it hidden behind these unnamed fields?

1 Like

@sschuberth are you still able to get autocompletion working without setting the options in IDEA? I am trying to achieve the same thing (autocompletion for a custom script inside the IDE).

The main resources I have looked at are this thread, the KEEP, and this PR of yours.

I have added the module with the script definition to the project I’d like to use the script in to the kotlinScriptDef config

dependencies {
    kotlinScriptDef(project(":script"))
}

as well as making sure the script definition project (":script") has been compiled.

I am thinking that I must have missed something – or that how IDEA discovers definitions has changed in recent versions of the kotlin plugin, in particular following your latest post from December '21.

Actually yes.

Interestingly, I’m not using the kotlinScriptDef config at all, and it still works.

You may want to clone GitHub - oss-review-toolkit/ort: A suite of tools to automate software compliance checks., import it into IDEA, and then open up the file https://github.com/oss-review-toolkit/ort/blob/main/examples/evaluator-rules/src/main/resources/example.rules.kts. You should get auto-completion on it out of the box.