Replies: 14 comments 23 replies
-
NOTE THIS IS OUTDATED AND JUST A VIMSCRIPT EXAMPLEJust an fyi, I've gone ahead and only kept the pure Lua example up above with some very minimal instructions if you are using a Vimscript based setup. This is mainly for my own sake not having to maintain multiple minimal examples in both Lua and Vimscript. I'll post the last Vimscript based one below with no intent to keep it updated unless something radically changes in the setup. However, it will at leaset give an example: "=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
" These are example settings to use with nvim-metals and the nvim built in
" LSP. Be sure to thoroughly read the the help docs to get an idea of what
" everything does.
"
" The below configuration also makes use of the following plugins besides
" nvim-metals
" - https://github.com/nvim-lua/completion-nvim
"=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
"-----------------------------------------------------------------------------
" nvim-lsp Mappings
"-----------------------------------------------------------------------------
nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>
nnoremap <silent> K <cmd>lua vim.lsp.buf.hover()<CR>
nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>
nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>
nnoremap <silent> gds <cmd>lua vim.lsp.buf.document_symbol()<CR>
nnoremap <silent> gws <cmd>lua vim.lsp.buf.workspace_symbol()<CR>
nnoremap <silent> <leader>rn <cmd>lua vim.lsp.buf.rename()<CR>
nnoremap <silent> <leader>f <cmd>lua vim.lsp.buf.formatting()<CR>
nnoremap <silent> <leader>ca <cmd>lua vim.lsp.buf.code_action()<CR>
nnoremap <silent> <leader>ws <cmd>lua require'metals'.worksheet_hover()<CR>
nnoremap <silent> <leader>a <cmd>lua require'metals'.open_all_diagnostics()<CR>
nnoremap <silent> <space>d <cmd>lua vim.lsp.diagnostic.set_loclist()<CR>
nnoremap <silent> [c <cmd>lua vim.lsp.diagnostic.goto_prev { wrap = false }<CR>
nnoremap <silent> ]c <cmd>lua vim.lsp.diagnostic.goto_next { wrap = false }<CR>
"-----------------------------------------------------------------------------
" nvim-lsp Settings
"-----------------------------------------------------------------------------
" If you just use the latest stable version, then setting this isn't necessary
let g:metals_server_version = '0.9.8+10-334e402e-SNAPSHOT'
"-----------------------------------------------------------------------------
" nvim-metals setup with a few additions such as nvim-completions
"-----------------------------------------------------------------------------
:lua << EOF
metals_config = require'metals'.bare_config()
metals_config.settings = {
showImplicitArguments = true,
excludedPackages = {
"akka.actor.typed.javadsl",
"com.github.swagger.akka.javadsl"
}
}
metals_config.on_attach = function()
require'completion'.on_attach();
end
metals_config.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics, {
virtual_text = {
prefix = '',
}
}
)
EOF
if has('nvim-0.5')
augroup lsp
au!
au FileType scala,sbt lua require('metals').initialize_or_attach(metals_config)
augroup end
endif
"-----------------------------------------------------------------------------
" completion-nvim settings
"-----------------------------------------------------------------------------
" Use <Tab> and <S-Tab> to navigate through popup menu
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
"-----------------------------------------------------------------------------
" Helpful general settings
"-----------------------------------------------------------------------------
" Needed for compltions _only_ if you aren't using completion-nvim
autocmd FileType scala setlocal omnifunc=v:lua.vim.lsp.omnifunc
" Set completeopt to have a better completion experience
set completeopt=menuone,noinsert,noselect
" Avoid showing message extra message when using completion
set shortmess+=c |
Beta Was this translation helpful? Give feedback.
-
Should:
now be:
? |
Beta Was this translation helpful? Give feedback.
-
Most minimal exampleJust as a reminder, if you're trying to reproduce a bug, the best way to do it is to start with the most minimal configuration possible. For that, don't use the full example configuration. The example configuration is meant to show how to set various settings, and even setup vim.cmd([[autocmd FileType scala,sbt lua require("metals").initialize_or_attach({})]]) Then any mappings you need to test stuff. |
Beta Was this translation helpful? Give feedback.
-
Hey @ckipp01 , looks like there's a typo in Thanks for the reference! |
Beta Was this translation helpful? Give feedback.
-
Just started using |
Beta Was this translation helpful? Give feedback.
-
Tried the lua, cmp not found. |
Beta Was this translation helpful? Give feedback.
-
Hi, cmp_nvim_lsp.update_capabilities is deprecated, should use cmp_nvim_lsp.default_capabilities instead. Further more. I tried in nvim according to the minimum configuration. It seems that nvim-metals is not working properly. |
Beta Was this translation helpful? Give feedback.
-
Just updated this. Thanks for pointing this out!
What does your full config look like? What sort of issues are you having? |
Beta Was this translation helpful? Give feedback.
-
Is this a |
Beta Was this translation helpful? Give feedback.
-
Hi. As of Aug 2023, packer is now unmaintained. Wondering if we could use lazy.nvim instead. |
Beta Was this translation helpful? Give feedback.
-
A little update on the default config. I used to use packer.nvim as the example as for a while it was the default choice for many Neovim users. Therefore I'll keep the example that used to be used down below as a reference for people still using packer. Please note that IT WILL NO LONGER BE KEPT UP TO DATE, so there is no guarantee it'll work anymore. The new example will be using lazy.nvim. -------------------------------------------------------------------------------
-- These are example settings to use with nvim-metals and the nvim built-in
-- LSP. Be sure to thoroughly read the `:help nvim-metals` docs to get an
-- idea of what everything does. Again, these are meant to serve as an example,
-- if you just copy pasta them, then should work, but hopefully after time
-- goes on you'll cater them to your own liking especially since some of the stuff
-- in here is just an example, not what you probably want your setup to be.
--
-- Unfamiliar with Lua and Neovim?
-- - Check out https://github.com/nanotee/nvim-lua-guide
--
-- The below configuration also makes use of the following plugins besides
-- nvim-metals, and therefore is a bit opinionated:
--
-- - https://github.com/hrsh7th/nvim-cmp
-- - hrsh7th/cmp-nvim-lsp for lsp completion sources
-- - hrsh7th/cmp-vsnip for snippet sources
-- - hrsh7th/vim-vsnip for snippet sources
--
-- - https://github.com/wbthomason/packer.nvim for package management
-- - https://github.com/mfussenegger/nvim-dap (for debugging)
-------------------------------------------------------------------------------
local api = vim.api
local cmd = vim.cmd
local map = vim.keymap.set
----------------------------------
-- PLUGINS -----------------------
----------------------------------
cmd([[packadd packer.nvim]])
require("packer").startup(function(use)
use({ "wbthomason/packer.nvim", opt = true })
use({
"hrsh7th/nvim-cmp",
requires = {
{ "hrsh7th/cmp-nvim-lsp" },
{ "hrsh7th/cmp-vsnip" },
{ "hrsh7th/vim-vsnip" },
},
})
use({
"scalameta/nvim-metals",
requires = {
"nvim-lua/plenary.nvim",
"mfussenegger/nvim-dap",
},
})
end)
----------------------------------
-- OPTIONS -----------------------
----------------------------------
-- global
vim.opt_global.completeopt = { "menuone", "noinsert", "noselect" }
-- LSP mappings
map("n", "gD", vim.lsp.buf.definition)
map("n", "K", vim.lsp.buf.hover)
map("n", "gi", vim.lsp.buf.implementation)
map("n", "gr", vim.lsp.buf.references)
map("n", "gds", vim.lsp.buf.document_symbol)
map("n", "gws", vim.lsp.buf.workspace_symbol)
map("n", "<leader>cl", vim.lsp.codelens.run)
map("n", "<leader>sh", vim.lsp.buf.signature_help)
map("n", "<leader>rn", vim.lsp.buf.rename)
map("n", "<leader>f", vim.lsp.buf.format)
map("n", "<leader>ca", vim.lsp.buf.code_action)
map("n", "<leader>ws", function()
require("metals").hover_worksheet()
end)
-- all workspace diagnostics
map("n", "<leader>aa", vim.diagnostic.setqflist)
-- all workspace errors
map("n", "<leader>ae", function()
vim.diagnostic.setqflist({ severity = "E" })
end)
-- all workspace warnings
map("n", "<leader>aw", function()
vim.diagnostic.setqflist({ severity = "W" })
end)
-- buffer diagnostics only
map("n", "<leader>d", vim.diagnostic.setloclist)
map("n", "[c", function()
vim.diagnostic.goto_prev({ wrap = false })
end)
map("n", "]c", function()
vim.diagnostic.goto_next({ wrap = false })
end)
-- Example mappings for usage with nvim-dap. If you don't use that, you can
-- skip these
map("n", "<leader>dc", function()
require("dap").continue()
end)
map("n", "<leader>dr", function()
require("dap").repl.toggle()
end)
map("n", "<leader>dK", function()
require("dap.ui.widgets").hover()
end)
map("n", "<leader>dt", function()
require("dap").toggle_breakpoint()
end)
map("n", "<leader>dso", function()
require("dap").step_over()
end)
map("n", "<leader>dsi", function()
require("dap").step_into()
end)
map("n", "<leader>dl", function()
require("dap").run_last()
end)
-- completion related settings
-- This is similiar to what I use
local cmp = require("cmp")
cmp.setup({
sources = {
{ name = "nvim_lsp" },
{ name = "vsnip" },
},
snippet = {
expand = function(args)
-- Comes from vsnip
vim.fn["vsnip#anonymous"](args.body)
end,
},
mapping = cmp.mapping.preset.insert({
-- None of this made sense to me when first looking into this since there
-- is no vim docs, but you can't have select = true here _unless_ you are
-- also using the snippet stuff. So keep in mind that if you remove
-- snippets you need to remove this select
["<CR>"] = cmp.mapping.confirm({ select = true }),
-- I use tabs... some say you should stick to ins-completion but this is just here as an example
["<Tab>"] = function(fallback)
if cmp.visible() then
cmp.select_next_item()
else
fallback()
end
end,
["<S-Tab>"] = function(fallback)
if cmp.visible() then
cmp.select_prev_item()
else
fallback()
end
end,
}),
})
----------------------------------
-- LSP Setup ---------------------
----------------------------------
local metals_config = require("metals").bare_config()
-- Example of settings
metals_config.settings = {
showImplicitArguments = true,
excludedPackages = { "akka.actor.typed.javadsl", "com.github.swagger.akka.javadsl" },
}
-- *READ THIS*
-- I *highly* recommend setting statusBarProvider to true, however if you do,
-- you *have* to have a setting to display this in your statusline or else
-- you'll not see any messages from metals. There is more info in the help
-- docs about this
-- metals_config.init_options.statusBarProvider = "on"
-- Example if you are using cmp how to make sure the correct capabilities for snippets are set
metals_config.capabilities = require("cmp_nvim_lsp").default_capabilities()
-- Debug settings if you're using nvim-dap
local dap = require("dap")
dap.configurations.scala = {
{
type = "scala",
request = "launch",
name = "RunOrTest",
metals = {
runType = "runOrTestFile",
--args = { "firstArg", "secondArg", "thirdArg" }, -- here just as an example
},
},
{
type = "scala",
request = "launch",
name = "Test Target",
metals = {
runType = "testTarget",
},
},
}
metals_config.on_attach = function(client, bufnr)
require("metals").setup_dap()
end
-- Autocmd that will actually be in charging of starting the whole thing
local nvim_metals_group = api.nvim_create_augroup("nvim-metals", { clear = true })
api.nvim_create_autocmd("FileType", {
-- NOTE: You may or may not want java included here. You will need it if you
-- want basic Java support but it may also conflict if you are using
-- something like nvim-jdtls which also works on a java filetype autocmd.
pattern = { "scala", "sbt", "java" },
callback = function()
require("metals").initialize_or_attach(metals_config)
end,
group = nvim_metals_group,
}) |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm working in a project with scala as back-end (play framework) and react (typescript) as front end. My front-end config works fine and now I'm trying to make scala work properly too. So I installed the plugin, and my config looks like this for now:
My keymappings for hovering and everything else are defined in an other file. When I hover one of the models I want to know the type, it displays the message: "No information available". I have done a :MetalsBuild, :MetalInstall, but it does not work neither, do you know why ? However, find references is working, so I am wondering if it's not related to my project, maybe I need to run a specific command ? When I open a scala file in Vim, I see the message: |
Beta Was this translation helpful? Give feedback.
-
I have been a long time vim user but never tweaked my configurations. I only really used it as a simple text editor. I've also been dabbling in Scala for a number of years. Digging into this project from the start. I looked at the dependency list which stated I needed I think we read that Today I made more progress. With I've finally installed As a last step, then I think I need to install the configuration above. Should we install this in ~/.vimrc (underneath the plug configuration?) Finally it's confusing what's required, etc. I had to read through this gitter to have some idea what's going on. I would suggest that if this project wants to be more inclusive to new users, that some effort needs to be made to improve the starting documentation. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
All reactions