From 0f7681c9e899180b3f0ef24ea35ca0e494088bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 9 Oct 2023 16:05:48 +0300 Subject: [PATCH] scala --- shared/home/default.nix | 11 +++- shared/home/init.lua | 128 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/shared/home/default.nix b/shared/home/default.nix index 04955c9..ed9fe68 100644 --- a/shared/home/default.nix +++ b/shared/home/default.nix @@ -21,6 +21,7 @@ zigpkgs."0.11.0" scala_2_12 + metals coursier ]) else []; @@ -43,12 +44,20 @@ zig-vim + cmp-nvim-lsp + nvim-cmp nvim-metals plenary-nvim ] else [] ); - extraLuaConfig = builtins.readFile ./init.lua; + extraLuaConfig = + builtins.readFile + (pkgs.substituteAll { + src = ./init.lua; + inherit (pkgs) metals; + }) + .outPath; }; programs.git = { diff --git a/shared/home/init.lua b/shared/home/init.lua index d40e980..29cf45e 100644 --- a/shared/home/init.lua +++ b/shared/home/init.lua @@ -74,3 +74,131 @@ vim.api.nvim_exec([[ " TeX au FileType tex setlocal spell spelllang=en_us ts=2 sw=2 sts=2 ]], false) + +local api = vim.api +local cmd = vim.cmd +local map = vim.keymap.set + +---------------------------------- +-- OPTIONS ----------------------- +---------------------------------- +-- global +vim.opt_global.completeopt = { "menuone", "noinsert", "noselect" } +vim.opt_global.shortmess:remove("F") + +-- 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", "cl", vim.lsp.codelens.run) +map("n", "sh", vim.lsp.buf.signature_help) +map("n", "rn", vim.lsp.buf.rename) +map("n", "f", vim.lsp.buf.format) +map("n", "ca", vim.lsp.buf.code_action) + +map("n", "ws", function() + require("metals").hover_worksheet() +end) + +-- all workspace diagnostics +map("n", "aa", vim.diagnostic.setqflist) + +-- all workspace errors +map("n", "ae", function() + vim.diagnostic.setqflist({ severity = "E" }) +end) + +-- all workspace warnings +map("n", "aw", function() + vim.diagnostic.setqflist({ severity = "W" }) +end) + +-- buffer diagnostics only +map("n", "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) + +-- 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 + [""] = cmp.mapping.confirm({ select = true }), + -- I use tabs... some say you should stick to ins-completion but this is just here as an example + [""] = function(fallback) + if cmp.visible() then + cmp.select_next_item() + else + fallback() + end + end, + [""] = 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" }, + --useGlobalExecutable = true, -- nix addition + metalsBinaryPath = "@metals@/bin/metals", +} + +-- *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() + +-- 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, +})