Skip to content
Federico Igne edited this page Aug 28, 2020 · 3 revisions

Note: Metals provides a bunch of additional helpers on top of the standard LSP functionalities. These are not necessarely integrated in vim-lsp (yet?). At the time of writing full Metals support is provided by coc.nvim along with coc-metals.

Installing server

Follow the instructions on the Metals-Vim Integration webpage, on how to generate Metals binaries.

Make sure the executable generated in the process is available in your $PATH.

Registering the server with vim-lsp

Include the following in your .vimrc

if executable('metals-vim')
   au User lsp_setup call lsp#register_server({
      \ 'name': 'metals',
      \ 'cmd': {server_info->['metals-vim']},
      \ 'initialization_options': { 'rootPatterns': 'build.sbt' },
      \ 'whitelist': [ 'scala', 'sbt' ],
      \ })
endif

Importing a build

The first time you open a workspace, you might want to import the build. This can be manually done by sending a request to Metals with the custom command build-import.

Inside of vim, issue the following command:

call lsp#send_request('metals', { 'method': 'workspace/executeCommand', 'params': { 'command': 'build-import' }})

You can also define a custom command, including this in your .vimrc:

command LspMetalsBuildImport call lsp#send_request('metals', { 'method': 'workspace/executeCommand', 'params': { 'command': 'build-import' }})

Formatting on save

Setting up document formatting involves custom Metals commands. Metals uses Scalafmt to format code but vim-lsp does not currently prompt for setting it up.

To manually configure formatting and trigger the installation of Scalafmt, you need to create a .scalafmt.conf file containing:

version="<version>"

where <version> is the Scalafmt version to use for the project (the latest version can be found here).

Metals will look for this file in the project root directory. Additionally, a custom path can be set during server registration with the config option metals.scalafmtConfigPath.

Note: the first time running the command LspDocumentFormat or LspDocumentFormatSync will be slow because Metals needs to download the appropriate version of Scalafmt.

Once Scalafmt is set up, you can configure vim-lsp to format buffers on save as explained in the plugin docs.