Configuring VSCode to work with Scala







The article is intended for beginners in programming in Scala, which I myself am, and just for those who want to start writing program code in VSCode.



It so happened that the only guide on working with Scala in Visial Studio Code was this video on the DevInsideYou YouTube channel. It describes in great detail the process of installing and setting up the environment for VS Code and Sublime Text.



This article is an adaptation of the video with some additions from me. You can go to watching the video or continue reading this article (or do both), in which case, I hope it will be useful to you.



Installation



The first thing we need to do is to install Scalu itself, as well as the SBT project build system, if you have not already done so.









To work with Scala in VSCode there is a Metals extension.



Metals Features





Only the basic functionality is listed here, follow the documentation for a more detailed description.



The following is a snippet from a DevInsideYou video that demonstrates how Scala interacts with code editors:









Language server protocol - used between the client tool (IDE) and servers that provide functions specific to a programming language (auto-completion, transition to definition, etc.).



Metals = Meta (from Scalameta) + LS (from Language Server - the standard language server protocol).



LSP reduces m-times-n complexity by providing a high level of support for any programming language in any editor, IDE or client endpoint, to the simpler m-plus-n task. LSP was created by Microsoft to define a common language that programming language analyzers can speak. Microsoft Visual Studio Code supports this protocol out of the box.



LSP allows language communities to focus on one high-performance language server that can provide code completion, tooltips, go to definition, search for links and much more, while editors and client communities can focus on creating a single, high-performance, intuitive and an idiomatic extension that can communicate with any language server to instantly provide deep language support.



Project Initialization



1 way



Initialization via scala file . To do this, open the project folder and create the following default structure in it:









Sample output after the first start of Metals:











2 way



The second way involves using mill . To do this, create the build.sc file in the project folder and open it.



Sample output after the first start of Metals:









3 way (final)



Initialization with SBT . Create a build.sbt file in the project folder with the following contents:



name := "scala-vscode-example" version := "0.1" scalaVersion := "2.13.1" triggeredMessage := Watched.clearWhenTriggered autoStartServer := false scalacOptions ++= Seq( "-feature", "-deprecation", "-language:implicitConversions", "-language:higherKinds" ) addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.10.3")
      
      





Sample output after the first start of Metals:









When Metals detects an sbt workspace for which there is no project / build.properties , a pop-up notification appears recommending that you upgrade to 0.13.7:









They recommend using sbt version 1.2.8. To change the sbt version, make the following changes to the project / build.properties file :









To check the sbt version, use the command:



 $ sbt sbtVersion
      
      





Initialize the sbt working environment in the project folder:



 $ sbt
      
      





Import project



To import a project, click the Import build button in the notification that appears or enter the command:









After making changes to any scala, the Metals file will automatically compile it:









Doctor



Used to fix potential build configuration issues. To start use:









Output:









Bloop



Bloop is a build server and CLI tool for Scala that works with SBT and has experimental support for other build tools such as Maven , Gradle, and Mill . If your workspace contains a .bloop directory with Bloop JSON files, Metals will automatically connect to it.



Bloop Benefits:





The following snippet from the video “Happy Life With Scala Metals” shows the process of building the application:









Note : there are some inaccuracies in the figure. Gabriele Petronella points to them in his comment under the video:

In the figure, the lower BSP arrows are technically incorrect. As you (correctly) pointed out, if the build tool speaks BSP, it can integrate directly with metals without going through Bloop. Moving from a build tool to a Bloop is a way to add BSP integration to the build tool itself, and it's a special step that doesn't use BSP. More information can be found here .
Bloop integrates with IDEs and text editors, providing a short feedback loop and reliable compiler diagnostics.



Bloop is used in Metals in two possible ways:

  1. If Bloop is installed on your computer, Metals simply connects to an existing build server (recommended);
  2. If Bloop is not installed, the Metals server will download it and run it in native mode.


SemanticDB is a data model for semantic information, such as characters and types, about programs in Scala and other languages. After compiling the project, sbt-metals sends the information to the local semanticdb-scala database.



Bloop installation



To get started, download the latest version :



 $ curl -L https://github.com/scalacenter/bloop/releases/download/v1.3.2/install.py | python
      
      





Next, you need to create a copy of the bloop service for the current user:



 $ cp ~/.bloop/systemd/bloop.service ~/.config/systemd/user/
      
      





Also, in order not to constantly write the path to the script, you can copy bloop to / usr / bin :



 $ sudo cp /.bloop/bloop /usr/bin/
      
      





Reboot the systemctl daemon:



 $ systemctl --user daemon-reload
      
      





Adding a bloop to startup:



 $ systemctl --user enable bloop
      
      





However, if you are not going to work with Scala constantly, I do not recommend removing this service from startup. You can disable autorun with the following command:



 $ systemctl --user disable bloop
      
      





Running bloop:



 $ systemctl --user start bloop
      
      





Display information about the current connection:







Application launch



When the bloop service is running, you can run your compiled program using this command in the project directory:



 $ bloop run <proj-name>
      
      





You can also add a hotkey to ~ / .config / Code / User / keybindings.json :



  {    "key": "meta+r",    "command": "workbench.action.terminal.sendSequence",    "args": {      "text": "bloop run ${workspaceFolderBasename}\u000D"    } }
      
      





If you do not have a bloop service running, you can start the application directly via sbt, however, in this case, the application will be recompiled each time, which will affect the program’s launch time:



 $ sbt run
      
      





Initial data



A small test case of the scala project lies in this repository on github. If you will also keep your projects, be sure to add the following list of exceptions to the .gitignore file:



 *.class *.log project/ target/ .bloop/ .metals/
      
      






All Articles