Hi. Is there currently any way to make IDE "see" c...
# scripting
n
Hi. Is there currently any way to make IDE "see" certain properties/functions as being available in script inside .kts file? To elaborate - when script is run with JSR-223, it will have
val bindings: Bindings
available in its context. However, when editing a .kts file there doesn't seem to be obvious way to inform IDE about that, short of writing
val bindings: Bindings = SimpleBindings()
at the beginning of the file and commenting it out before compiling.
g
Hi. You can use a ScriptTemplateDefinition. IntelliJ can use that to resolve references within the script as if it were an instance of the script template class. We do that here: https://github.com/apollo-rsps/apollo/blob/kotlin-experiments/game/src/main/kotlin/org/apollo/game/plugin/kotlin/KotlinPluginScript.kt and each of our .kts files ends up being a subclass of KotlinPluginScript
Currently you need to configure the classpath and the fully qualified class name in the kotlin settings window of the IDE, but there are changes in the 1.2.50 EAP that makes discovery easier
i
Moreover, in the new scripting API, available for preview starting from 1.2.50, it is possible to define external “environment” variables for scripts, and if appropriate script definition is imported into IDEA, these will be properly resolved too. It will eventually come into JSR 223 wrappers (definitely not in 1.2.50 though). It will be also a solution for related https://youtrack.jetbrains.com/issue/KT-15125. The only problem is that it is all not documented yet. But if you want to try it anyway, start with
kotlin-scripting-*
modules and related samples/tests in the Kotlin project.
s
@ilya.chernikov Will 1.2.50 scripting support allow to import
src/main/resources/*.kts
file in
src/main/kotlin/*.kt
and get
kts
class autocomplete from the
kt
file ? Use case is
*.kts
based configuration.
g
yes, it currently does allow that (and we are currently using this via the mechanism provided above). The changes in 1.2.50 make discovery of *.kts templates easier using a service discovery style loader.
s
Nice
i
@sdeleuze I’m not sure I understand your question completely, but you can have
.kts
and
.kt
files in one sourceset and reference one another. To use a script class from a regular
.kt
you’ll need to know how script class names are generated. What is new with 1.2.50 is that you may have custom scripts in your sourcesets, distinguished by an extension (still should end with
.kts
for a moment though) and automatically recognized by compiler and IDE using discovery mechanism, by just adding appropriate dependency to the project.
n
Thank you!