Skip to content

Instantly share code, notes, and snippets.

@c3r38r170
Last active July 10, 2021 18:50
Show Gist options
  • Save c3r38r170/f7ef389227790adc5ab700648ad8ce2b to your computer and use it in GitHub Desktop.
Save c3r38r170/f7ef389227790adc5ab700648ad8ce2b to your computer and use it in GitHub Desktop.

Kotlin coding with Visual Studio Code

This is an experiment.

I'll try to get as near as possible to a useful Kotlin development environment. I won't use it for important applicationss or my job, just for pocket scripts and apps.

I'll focus on raw Kotlin, not using any build tool.

However, if all goes well, I don't see how I wouldn't be able to. After all, I have succesfully replaced Eclipse as my main Java IDE, except for something about JavaFX's WebView compilation, but runs fine.

First, let's get Kotlin

I'm downloading the command-line compilator (Check for the latest / your favourite version.).

In Windows, it has to be extracted (to a cool location) and then its bin folder has to be referenced in the PATH variable.

Check the installation for your OS online, it can be done from the command line.

Second, the extensions:

DISCLAIMER:

There's a pack with this extensions and some more for Gradle. I won't be using Gradle for now.

Kotlin

Kotlin Language

Code Runner

The last one is to run inline code.

However, I'll use it for debugging. Unfortunately, the "Kotlin" extension doesn't support debug for simple Kotlin. Looking forward to it I guess.

It supposedly works with Gradle projects.

For executing Kotlin with Code Runner, we need the folloring settings:

"code-runner.executorMap": {
	"kotlin":"kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar",
	"kotlinscript":"kotlinc -script $fileName"
},
"code-runner.languageIdToFileExtensionMap": {
	"kotlin":".kt",
	"kotlinscript":".kts"
}

Each of them along with the other default options, of course. Go to File > Preferences > Settings, optionally filter by "Code Runner", the settings are "Executor Map" and "Language ID To File Extension Map".

Third, let's Hello World

Ctrl + N (New Untitled File)

Write:

fun main() = println("Hello, world!")

Ctrl + Alt + J (Command Pallete > Run By Language)

kotlin + Enter

You should be able to see the output in the console.

That's it for now

You can run code! And compile it to jar (when you run it; proper task, exe and kexe coming soon).

Ahead-of-time limitations

I haven't found how to import libraries without using Gradle... yet.

But I've tested a simple script importing Java and it works.

The kotlin-compiler file has some libraries inside apparently, but I haven't tested them. Maybe we can put new libraries in the directory those included libs are.

Sources

Kotlin command-line compiler

Kotlin Development in VS Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment