Hello! <@U0CHHN4F4> I was wondering if there is a ...
# scripting
g
Hello! @ilya.chernikov I was wondering if there is a way to have the autocomplete working in a kotlin script with dependencies in IntelliJ/AndroidStudio eg:
Copy code
@file:Repository("<https://repo.maven.apache.org>")
@file:DependsOn("org.apache.commons:commons-text:1.6")
i
Hi! It’s a bit complicated question. What in particular you want to autocomplete and in which scenario? E.g. are your scripts a part of a project sources?
g
hmmm no the script is an easy script that need to be run standalone.. using
kotlin-main-kts.jar
and with some dependencies eg what i described in the message.. i can see that in IntelliJ there is a section in
Preferences -> Languages & Frameworks -> Kotlin -> Kotlin Scripting
where i can find some default stuff like KotlinBuildScript or KotlinInitScript or plain Kotlin Script. and i cannot find a way to add kotlin-main-kts and see the script with autocompletion in the IDE..
will be nice if from intelliJ i'm able to use the autocomplete for a
script.main.kts
with the relatives deps.. that's all 😄
i
Ok, if it is a standalone file, then you need to configure main.kts scripts: open Intellij settings -> Build, Execution, Deployment -> Compiler -> Kotlin compiler. There are two scripting-related fields at the bottom of the setting page. Place the path to
kotlin-main-kts.jar
to the “Script templates classpath” field and
org.jetbrains.kotlin.mainKts.MainKtsScript
to the “Script template classes” field. Then it should work.
You should be able to see main-kts script enabled in the settings -> Languages and Frameworks -> Kotlin -> Kotlin scripting.
g
ok let me try to play with this! i will let you know if it works! 🙂 thank you very much!! i really appreciate
i've setup in Script template classes:
/full/path/to/kotlin-main-kts.jar
Script templates classpath
org.jetbrains.kotlin.mainKts.MainKtsScript
but in Languages and Framework i still cannot see the main.kts script.. i've tried also to restart/invalidate cache.. same 😕 maybe i'm wrong
i
Let me check it myself now…
g
btw i'm using the last intelliJ if relevant 😄
i
It works for me:
image.png
g
yay! i was right as well! was just stuck somehow my intelliJ
is working! this is amazing!!
👍 1
😄 thank you so much!!
one more question.. and what if i want to add another jar to the script classpath? can i do something like:
/path/to/kotlin-main-kts.jar:/path/to/another.jar
?
asking because from command line you can do
-cp /path/to/kotlin-main-kts.jar:/path/to/another.jar
i
Yes, you can do it. Path as in the regular classpath, and class names separated by comma
g
Hello! is me again! @ilya.chernikov 😄 sorry to bother you!! i would like to know if there is a way to make a intellij plugin that adds automatically your custom Script Definitions.. i've extended MainKts and added some more stuff, but i would like to find an easy way (maybe by installing a plugin) to recognize the custom script
i
Hi! Yes, installing a plugin is a preferable method, the one I shown you is created more as a workaround. But we do not have any plugin for the
main-kts
, at least not yet. If you’re willing to make one yourself, you need to implement the extension point
kotlin.script.experimental.intellij.ScriptDefinitionsProvider
, which should be obviously similar to the fields in the settings.
g
I Need to investigate little bit more also because I never made a plugin, but with this infos I have a great start point! Thank you very much @ilya.chernikov very kind of you! Appreciate your help!
i've implemented my plugin with this specs: in plugin.xml
Copy code
<extensions defaultExtensionNs="org.jetbrains.kotlin">
        <scriptDefinitionsProvider
            id="DangerFileScriptDefinitionProvider"
            implementation="com.gianluz.dangerkotlin.intellij.DangerFileScriptDefinitionProvider"/>
        <!-- Add your extensions here -->
    </extensions>
then my implementation here:
Copy code
class DangerFileScriptDefinitionProvider : ScriptDefinitionsProvider {
    private companion object {
        private const val ID = "DangerFileScriptDefinition"
        private const val DANGERFILE_SCRIPT_DEFINITION = "com.danger.kotlin.kts.DangerFileScript"
    }

    override val id: String
        get() = ID

    override fun getDefinitionClasses(): Iterable<String> = listOf(
        DANGERFILE_SCRIPT_DEFINITION
    )

    override fun getDefinitionsClassPath(): Iterable<File> {
        return listOf(
            File("/usr/local/lib/danger/danger-kotlin.jar"),
            File("/usr/local/lib/danger/kotlin-main-kts.jar"),
            File("/usr/local/lib/danger/danger-kotlin-kts.jar")
        )
    }

    override fun useDiscovery() = true
}
but i get an error
Copy code
Caused by: java.lang.NoClassDefFoundError: kotlin/script/experimental/intellij/ScriptDefinitionsProvider
...
am i missing something?? 🤔
i
There is a missing dependency. Could you try to get the full stacktrace or root cause exception?
g
Stacktrace
i
This is still incomplete, the reason for
PluginException
os trimmed. Maybe you can find the full one in the log. I do not know much about this part. Maybe it is better to debug or to ask IntelliJ guys, how to diagnose it.
BTW, live plugin has an implementation of this extension point https://github.com/dkandalov/live-plugin/blob/master/src/main/liveplugin/pluginrunner/kotlin/LivePluginKotlinScriptProvider.kt Maybe you can use it as an example
The author nick here is “dkandalov”.