Hello, I am wondering how to set up a scripting en...
# scripting
Hello, I am wondering how to set up a scripting environment so that every file with a certain extension are wrapped inside a class and has default imports with autocomplete in the ide. I have not been able to find adequate resources for something like this online and have only at most been able to load kts files manually.
I'm not sure I understand your request, there is no support for wrapping script inside a class. But to some extend a custom script definition may provide you some properties of such a construct, namely you can define implicit receivers (sort of wrapping) and default imports, and such definition could be used in the IDE, so autocompletion will work too. You may have a look at these examples - https://github.com/Kotlin/kotlin-script-examples/tree/master/jvm/simple-main-kts
Hello ilya sorry this is 11 days later. I have found 2 jetbrains packages that might help me out but I cannot find any examples of how to use them to get started on achieving what I am asking. Maybe I am not looking in the right spots!
Copy code
More specifically I have been looking at
as it seems to be what I am looking for. Looking at it and
lead me to believe I can achieve what I asked about, assuming it is set up properly with the build system (which I am also new to, advanced building systems like gradle and maven), which would be to say create a custom configuration
that imports certain things by default and only import them by default when inside a file of a certain extension which you can define
For example I have
abstract class Dog { fun woof() { println("woof") }
which I want to set as one of the
and be able to access it's insides in a file with
extension and I can use it at runtime by passing a
to it. I'm not sure if I am explaining this properly, but I know what I am trying to achieve I just don't know how to achieve it
Hi @RayeW47. Sorry, I overlooked your response. Please mention me next time, so I can get the Slack notification. Please have a look at the example I linked above. It is a complete library and test project that demonstrates the things you need (so it seems to me) and many on top. In particular, in this line - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt#L41 it adds an implicit receiver of the type
(which is not a good example indeed, I have to replace it with something more sensible, but it shows the mechanics anyway for now). And in this line - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt#L85 the evaluation configuration defines actual object passed as this receiver (empty string in this case). You may change the configuration dynamically, e.g. you can configure a callback (as in this line - https://github.com/Kotlin/kotlin-script-examples/blob/master/jvm/simple-main-kts/simple-main-kts/src/main/kotlin/org/jetbrains/kotlin/script/examples/simpleMainKts/scriptDef.kt#L86) or you can pass a base evaluation configuration to
Thank you for the links @ilya.chernikov. Sorry, I have not used slack much at all so I didn't understand that you had to mention for a notification. That is exactly what I am looking for, I'm not sure how I didn't find such a thing that is right there on GitHub.
­čĹŹ 1