diff options
-rw-r--r-- | lua/diagnostics.lua | 55 | ||||
-rw-r--r-- | lua/lsp.lua | 208 | ||||
-rw-r--r-- | lua/treesitter.lua | 45 |
3 files changed, 308 insertions, 0 deletions
diff --git a/lua/diagnostics.lua b/lua/diagnostics.lua new file mode 100644 index 0000000..f5f7b9d --- /dev/null +++ b/lua/diagnostics.lua @@ -0,0 +1,55 @@ +-- Diagnostics +require("trouble").setup({ + position = "bottom", -- position of the list can be: bottom, top, left, right + height = 10, -- height of the trouble list when position is top or bottom + width = 50, -- width of the list when position is left or right + icons = true, -- use devicons for filenames + mode = "workspace_diagnostics", -- "workspace_diagnostics", "document_diagnostics", "quickfix", "lsp_references", "loclist" + fold_open = "", -- icon used for open folds + fold_closed = "", -- icon used for closed folds + group = true, -- group results by file + padding = true, -- add an extra new line on top of the list + action_keys = { -- key mappings for actions in the trouble list + -- map to {} to remove a mapping, for example: + -- close = {}, + close = "q", -- close the list + cancel = "<esc>", -- cancel the preview and get back to your last window / buffer / cursor + refresh = "r", -- manually refresh + jump = { "<cr>", "<tab>" }, -- jump to the diagnostic or open / close folds + open_split = { "<c-x>" }, -- open buffer in new split + open_vsplit = { "<c-v>" }, -- open buffer in new vsplit + open_tab = { "<c-t>" }, -- open buffer in new tab + jump_close = { "o" }, -- jump to the diagnostic and close the list + toggle_mode = "m", -- toggle between "workspace" and "document" diagnostics mode + toggle_preview = "P", -- toggle auto_preview + hover = "K", -- opens a small popup with the full multiline message + preview = "p", -- preview the diagnostic location + close_folds = { "zM", "zm" }, -- close all folds + open_folds = { "zR", "zr" }, -- open all folds + toggle_fold = { "zA", "za" }, -- toggle fold of current file + previous = "k", -- preview item + next = "j", -- next item + }, + indent_lines = true, -- add an indent guide below the fold icons + auto_open = false, -- automatically open the list when you have diagnostics + auto_close = false, -- automatically close the list when you have no diagnostics + auto_preview = true, -- automatically preview the location of the diagnostic. <esc> to close preview and go back to last window + auto_fold = false, -- automatically fold a file trouble list at creation + auto_jump = { "lsp_definitions" }, -- for the given modes, automatically jump if there is only a single result + signs = { + -- icons / text used for a diagnostic + error = "", + warning = "", + hint = "", + information = "", + other = "", + }, + use_diagnostic_signs = false, -- enabling this will use the signs defined in your lsp client +}) + +-- Mappings +vim.api.nvim_set_keymap("n", "<Leader>xx", "<Cmd>Trouble<CR>", { silent = true, noremap = true }) +vim.api.nvim_set_keymap("n", "<Leader>xw", "<Cmd>Trouble workspace_diagnostics<CR>", { silent = true, noremap = true }) +vim.api.nvim_set_keymap("n", "<Leader>xd", "<Cmd>Trouble document_diagnostics<CR>", { silent = true, noremap = true }) +vim.api.nvim_set_keymap("n", "<Leader>xl", "<Cmd>Trouble loclist<CR>", { silent = true, noremap = true }) +vim.api.nvim_set_keymap("n", "<Leader>xq", "<Cmd>Trouble quickfix<CR>", { silent = true, noremap = true }) diff --git a/lua/lsp.lua b/lua/lsp.lua new file mode 100644 index 0000000..ac086e4 --- /dev/null +++ b/lua/lsp.lua @@ -0,0 +1,208 @@ +-- CiderLSP +local vim = assert(vim) + +-- 1. Configure CiderLSP +-- Set desired filetypes from go/ciderlsp#supported +-- To list all filetype names, see https://vi.stackexchange.com/a/14990 +local nvim_lsp = require("lspconfig") +local configs = require("lspconfig.configs") +configs.ciderlsp = { + default_config = { + cmd = { "/google/bin/releases/cider/ciderlsp/ciderlsp", "-merge_diagnostic_layers=true", "-tooltag=nvim-cmp" }, + filetypes = { "java", "kotlin", "objc", "proto", "textproto", "go", "python", "BUILD" }, + root_dir = nvim_lsp.util.root_pattern("BUILD"), + settings = {}, + }, +} + +configs.lualsp = { + default_config = { + cmd = { "/usr/local/google/home/rahm/Projects/lua-language-server/bin/lua-language-server" }, + filetypes = { "lua" }, + settings = {}, + root_dir = nvim_lsp.util.root_pattern(""), + }, +} + +configs.clangd = { + default_config = { + cmd = { "clangd" }, + filetypes = { "c", "cpp" }, + settings = {}, + root_dir = nvim_lsp.util.root_pattern(""), + }, +} + +configs.hls = { + default_config = { + cmd = { "haskell-language-server-wrapper", "--lsp" }, + filetypes = { "haskell" }, + settings = {}, + root_dir = nvim_lsp.util.root_pattern("package.yaml"), + } +} + +local has_words_before = function() + unpack = unpack or table.unpack + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil +end + +local feedkey = function(key, mode) + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) +end + +-- 2. Configure CMP +vim.opt.completeopt = { "menu", "menuone", "noselect" } + +-- Don't show matching +vim.opt.shortmess:append("c") + +local lspkind = require("lspkind") lspkind.init() + +local cmp = require("cmp") + +cmp.setup({ + mapping = { + ["<C-d>"] = cmp.mapping.scroll_docs(-4), + ["<C-u>"] = cmp.mapping.scroll_docs(4), + ["<C-e>"] = cmp.mapping.close(), + ["<C-Space>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }), + ["<C-m>"] = cmp.mapping.confirm({ select = true }), + ["<C-n>"] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + else + fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`. + end + end, { "i", "s" }), + ["<C-p>"] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`. + end + end, { "i", "s" }), + + ["<Tab>"] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.mapping.confirm({ select = true }) + elseif vim.fn["vsnip#available"](1) == 1 then + feedkey("<Plug>(vsnip-expand-or-jump)", "") + elseif has_words_before() then + cmp.complete() + else + fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`. + end + end, { "i", "s" }), + + ["<S-Tab>"] = cmp.mapping(function() + if cmp.visible() then + cmp.select_prev_item() + elseif vim.fn["vsnip#jumpable"](-1) == 1 then + feedkey("<Plug>(vsnip-jump-prev)", "") + end + end, { "i", "s" }), + }, + + sources = { + { name = "nvim_lsp" }, + { name = "path" }, + { name = "vim_vsnip" }, + { name = "buffer", keyword_length = 5 }, + }, + + sorting = { + comparators = {}, -- We stop all sorting to let the lsp do the sorting + }, + + snippet = { + expand = function(args) + vim.fn["vsnip#anonymous"](args.body) + end, + }, + + formatting = { + format = lspkind.cmp_format({ + with_text = true, + maxwidth = 40, -- half max width + menu = { + buffer = "[buffer]", + nvim_lsp = "[LSP]", + nvim_lua = "[API]", + path = "[path]", + vim_vsnip = "[snip]", + }, + }), + }, + + experimental = { + native_menu = false, + ghost_text = true, + }, +}) + +vim.cmd([[ + augroup CmpZsh + au! + autocmd Filetype zsh lua require'cmp'.setup.buffer { sources = { { name = "zsh" }, } } + augroup END +]]) + +-- 3. Set up CiderLSP +local on_attach = function(client, bufnr) + vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") + if vim.lsp.formatexpr then -- Neovim v0.6.0+ only. + vim.api.nvim_buf_set_option(bufnr, "formatexpr", "v:lua.vim.lsp.formatexpr") + end + if vim.lsp.tagfunc then + vim.api.nvim_buf_set_option(bufnr, "tagfunc", "v:lua.vim.lsp.tagfunc") + end + + local opts = { noremap = true, silent = true } + vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "<M-k>", "<cmd>lua vim.lsp.buf.hover()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "g0", "<cmd>lua vim.lsp.buf.document_symbol()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "gW", "<cmd>lua vim.lsp.buf.workspace_symbol()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "gt", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opts) + vim.api.nvim_buf_set_keymap(bufnr, "n", "]d", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts) + + vim.api.nvim_command("augroup LSP") + vim.api.nvim_command("autocmd!") + if client.server_capabilities.documentHighlightProvider then + vim.api.nvim_command("autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()") + vim.api.nvim_command("autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()") + vim.api.nvim_command("autocmd CursorMoved <buffer> lua vim.lsp.util.buf_clear_references()") + end + vim.api.nvim_command("augroup END") +end + +nvim_lsp.ciderlsp.setup({ + capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities()), + on_attach = on_attach, +}) + +nvim_lsp.lualsp.setup({ + capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities()), + on_attach = on_attach, +}) + +nvim_lsp.clangd.setup({ + capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities()), + on_attach = on_attach, +}) + +nvim_lsp.hls.setup({ + capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities()), + on_attach = on_attach, +}) + +vim.cmd[[hi DiagnosticUnderlineError gui=undercurl guisp=salmon]] +vim.cmd[[hi DiagnosticUnderlineWarn gui=undercurl guisp=gold]] diff --git a/lua/treesitter.lua b/lua/treesitter.lua new file mode 100644 index 0000000..0a44cc7 --- /dev/null +++ b/lua/treesitter.lua @@ -0,0 +1,45 @@ +require'nvim-treesitter.configs'.setup { + -- A list of parser names, or "all" (the four listed parsers should always be installed) + ensure_installed = { "all" }, + + -- Install parsers synchronously (only applied to `ensure_installed`) + sync_install = false, + + -- Automatically install missing parsers when entering buffer + -- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally + auto_install = true, + + -- List of parsers to ignore installing (for "all") + ignore_install = { "javascript" }, + + ---- If you need to change the installation directory of the parsers (see -> Advanced Setup) + -- parser_install_dir = "/some/path/to/store/parsers", -- Remember to run vim.opt.runtimepath:append("/some/path/to/store/parsers")! + + highlight = { + -- `false` will disable the whole extension + enable = true, + + -- NOTE: these are the names of the parsers and not the filetype. (for example if you want to + -- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is + -- the name of the parser) + -- list of language that will be disabled + -- disable = { "c", "rust" }, + -- Or use a function for more flexibility, e.g. to disable slow treesitter highlight for large files + disable = function(lang, buf) + if lang == "vim" or lang == "lua" then + return true + end + local max_filesize = 100 * 1024 -- 100 KB + local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf)) + if ok and stats and stats.size > max_filesize then + return true + end + end, + + -- Setting this to true will run `:h syntax` and tree-sitter at the same time. + -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation). + -- Using this option may slow down your editor, and you may see some duplicate highlights. + -- Instead of true it can also be a list of languages + additional_vim_regex_highlighting = false, + }, +} |