Write vscode extensions in Scala.js! This is a collection of examples and templates to get you started, with convenient sbt tasks to build and run your extension.
contains:
- commands from the vscode command palette
- inline completion like github copilot
- language server protocol client
- code actions (when pressing Alt+Enter at a code location)
Requirements:
Run the vscode extension:
-
Clone this project
-
Open the project in VSCode, run the
import buildtask with Metals (it should display a popup automatically). -
run below command, which will open a new VSCode window with the extension loaded(first time it will take some time for scalable typed to convert typescript to scala.js):
sbt openAfter the new VSCode (extension development host) window opens:
- Run the Hello World command from the Command Palette (
⇧⌘P) in the new VSCode window. - Type
helloand selectHello World.- You should see a Notification Hello World!.
To build the project, you need to have sbt installed. You can run the following command in the root directory of the project:
sbt fastOptJSThis will compile the Scala code and generate the JavaScript files in the out directory.
To package the extension to a .vsix file, run the following command in the root directory of the project after running sbt fastOptJS:
cd out && npx vsce packageclick on the Use this template button to create a new repository with the same structure in github.
In your build.sbt add the following:
lazy val vsc = RootProject(uri("https://github.com/doofin/vscode-scalajs-hello.git"))
lazy val root = Project("root", file(".")) dependsOn(vsc)Currently not working due to jitpack missing npm! Welcome to contribute to fix it.
You can use this project as a library in your project by adding the following to your build.sbt:
resolvers += Resolver.bintrayRepo("jitpack", "https://jitpack.io")
libraryDependencies += "com.github.doofin" % "vscode-scalajs-hello" % "master-SNAPSHOT" // might be wrongYou can find the latest version on jitpack.
Note:
- I recommend using the Metals extension for Scala in VSCode.
- If you have any issues, please open an issue on this repository.
The project file structure in src/main/scala is as follows:
src/main/scala
├── extensionMain.scala // main entry point for the extension
├── commands.scala, codeActions.scala,etc // files for different extension features
│ ├── facade // facade for vscode api
│ ├── io // file and network io functionsThe project uses the following tools:
- SBT build tool for building the project
- [Scala.js] for general coding
- Scalably Typed for JavaScript facades
- scalajs-bundler for bundling the JavaScript dependencies
SBT is configured with the build.sbt file. Scala.js, ScalablyTyped and the bundler are SBT plugins. With these, SBT manages your JavaScript npm dependencies. You should never have to run npm directly, simply edit the npmDependencies settings in build.sbt.
In general, javascript functions and classes can be used in the same way as in JS/TS!
If the typechecker disagrees, you can insert casts with .asInstanceOf[Type].
The JS types (like js.Array) are available from
import scala.scalajs.jsThe VSCode classes and functions are available from
import typings.vscode.mod as vscode
vscode.window.showInformationMessage("Hello World!")Some additional types are available in the anon subpackage, for example:
import typings.vscode.anon.Dispose
// register a command. The cast is necessary due to typescript conversion limitations.
vscode.commands.registerCommand(name, fun).asInstanceOf[Dispose]You can find more information and tutorials on the Scala.js website.
- updated from vscode-scalajs-hello with Scala 3.3.3 and sbt.version=1.9.7.
- VSCode Extension Samples repository.
- visualstudio.com/api/get-started in typescript.
- scalablytyped.com for the typing plugin.
- scala js for the scala.js project.
Welcome to contribute to this project! Here are some ideas:
- Add more examples for vscode extension features.
- Add github actions for automatic release
- Add proper test framework and tests for the extension