Make auto-completion work in Kotlin scripts with context

I tried:

  • cloning the repo
  • opening with IDEA and waiting for indexing, etc
  • gradle assemble in order to build all class files from all modules
  • gradle project reload

and I still get no autocompletion, but a “highlighting temporarily suspended due to internal error” message (see attached screenshot). No errors in the IDEA message tray.

Screenshot from 2022-04-15 11-09-45

The script definition does show up under Language & Frameworks > Kotlin > Kotlin Scripting which is more than what I achieved on my end so I guess I do have some lead to go on!

I’ll try to reproduce importing the script definition in my project and to debug the highlighting error, and then report back - cheers!

PS I am using IDEA 2021.3

I now also started to see

Syntax highlighting has been temporarily turned off [...] because of an internal error

Did you have any luck debugging this? Or @ilya.chernikov would you have any hint at what’s causing this?

I haven’t seen this error before, to be honest.
Please have a look at the intellij logs (on mac: help → show log in finder, should be something similar on other OSes). If there is a stacktrace of some exception related to the problem, that would help to diagnose exceptions. You may also search for the “[KOTLIN_SCRIPTING]” prefix in the log, it may contain some hints too,

Thanks for the hint, but I’m not seeing anything suspicious in the log files. Just some INFO-level entries like

INFO - #o.j.k.i.script - [KOTLIN_SCRIPTING] script definitions aren't loaded yet. They should be loaded by invoking GradleScriptDefinitionsContributor.reloadIfNeeded from KotlinDslSyncListener
INFO - #o.j.k.i.script - [KOTLIN_SCRIPTING] Loading script definitions [org.ossreviewtoolkit.notifier.NotificationsScriptTemplate, org.ossreviewtoolkit.evaluator.RulesScriptTemplate] using classpath: ...
INFO - #o.j.k.i.script - [KOTLIN_SCRIPTING] IllegalStateException loading gradle script templates: Unable to get Gradle home directory

The last one is the only one mentioning an exception, but it seem to be unrelated to my own script definitions (NotificationsScriptTemplate, RulesScriptTemplate).

@sschuberth I did not have any luck debugging your error and I could not reproduce it in my project.

I managed to fix my problem by including the entire classpath instead of trying to figure out where the different JARs were at.

One of the ways I debugged my definitons was to put them in an IDEA plugin (using the right extension point as explained in the KEEP, rather than rely on the auto discovery), set breakpoints, and debug the runIdea task. Might be time consuming though.

I got mine to show up by dropping the jar into the kotlin plugin’s folder, next to the jars that have the other definitions in them. I still had no code completion, but that is the furthest I could push it.

Cloning ort did not work at all, it still asking to include the “default runner” (kotlin-script-runtime.jar), and nothing else.

I found something that sounds like it under “File | Settings | Languages & Frameworks | Kotlin | Kotlin Scripting”, but nothing seems to be pickable.

The script definitions in the docs run, i implemented it supplied it with a script and it worked as expected. Just missing the primary reason why you’d use kotlin for scripting: static typing, with IDE support. I don’t think it even includes anything that can be used to try to make it work.

I’m also getting 2 signals, intellij says that the feature is in beta, the docs call it experimental.

The most stable way I could get any form of scripting to work is with the jsr223 lib, and by keeping my “script” (regular kotlin file) in a gradle module that depends on the libs I need for the syntax highlighting to work.