From f03a8e53b88d7db5f5b52b2d5481cdad4fa40bf0 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sun, 26 May 2024 00:45:53 -0400 Subject: NOW THE LSP IS COMFY!!! :) --- lua/core/init.lua | 25 +++++--- lua/core/langmap.lua | 5 ++ lua/plugins/cmp.lua | 38 ++++++++++++ lua/plugins/colorscheme.lua | 1 + lua/plugins/fugitive.lua | 19 +++--- lua/plugins/harpoon.lua | 5 +- lua/plugins/lsp.lua | 143 +++++++++++++++++++++++++++++++++++--------- lua/plugins/telescope.lua | 22 +++---- lua/plugins/whichkey.lua | 13 ++++ 9 files changed, 214 insertions(+), 57 deletions(-) create mode 100644 lua/core/langmap.lua create mode 100644 lua/plugins/cmp.lua create mode 100644 lua/plugins/whichkey.lua (limited to 'lua') diff --git a/lua/core/init.lua b/lua/core/init.lua index a4b31de..729c3e7 100644 --- a/lua/core/init.lua +++ b/lua/core/init.lua @@ -4,8 +4,9 @@ vim.opt.wildmenu=true -- Show mathes with tab-completion vim.opt.number=true vim.opt.relativenumber=true vim.opt.ruler=true --- vim.opt.laststatus=0 -vim.opt.laststatus=3 +vim.opt.lazyredraw=true +vim.opt.laststatus=0 +vim.opt.showmode=false vim.opt.splitbelow=true vim.opt.splitright=true vim.opt.tabstop=2 @@ -13,6 +14,7 @@ vim.opt.shiftwidth=2 vim.opt.expandtab=true vim.opt.foldmethod="marker" --- For `{{{` & `}}}` folding vim.opt.complete:append("kspell") +vim.opt.inccommand="split" vim.opt.spelllang="fr" -- why does french exist... vim.api.nvim_set_keymap("n","Y","y$", {}) -- What should have been `Y` vim.api.nvim_set_keymap("t", "", "", {}) -- terminal mode Esc @@ -25,11 +27,18 @@ vim.g.netrw_liststyle = 3 vim.g.netrw_banner = 0 vim.g.netrw_bufsettings = "noma nomod nu nobl nowrap ro" --- Navigate splits -vim.keymap.set("n", "", "h") -vim.keymap.set("n", "", "j") -vim.keymap.set("n", "", "k") -vim.keymap.set("n", "", "l") - -- Clear search highlights vim.keymap.set("n", "", ":nohlsearch") + +-- Don't mess with pasted text +vim.keymap.set("i", "+","+") + +-- Remove trailing whitespace (exept current line to avoid moving cursor) +vim.api.nvim_create_autocmd({ "BufWritePre" }, { + pattern = {"*"}, + callback = function() + local save_cursor = vim.fn.getpos(".") + pcall(function() vim.cmd [[%s/\s\+$//e]] end) + vim.fn.setpos(".", save_cursor) + end, +}) diff --git a/lua/core/langmap.lua b/lua/core/langmap.lua new file mode 100644 index 0000000..5b8c04d --- /dev/null +++ b/lua/core/langmap.lua @@ -0,0 +1,5 @@ +vim.api.nvim_command("set langmap+=é/") +vim.api.nvim_command("set langmap+=É?") +vim.api.nvim_command("set langmap+=¸]") +vim.api.nvim_command("set langmap+=^[") +vim.api.nvim_command("set langmap+=`'") diff --git a/lua/plugins/cmp.lua b/lua/plugins/cmp.lua new file mode 100644 index 0000000..c6f2d40 --- /dev/null +++ b/lua/plugins/cmp.lua @@ -0,0 +1,38 @@ +return { + "hrsh7th/nvim-cmp", + dependencies = { + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-path", + "L3MON4D3/LuaSnip", + "saadparwaiz1/cmp_luasnip", + }, + config = function() + local cmp = require("cmp") + local luasnip = require("luasnip") + + cmp.setup({ + completion = { + completeopt = "menu,menuone,noselect", + }, + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm({ select = true }), -- accept item under cursor when pressing enter + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + { name = 'buffer' }, + { name = 'path' } + }) + }) + end +} diff --git a/lua/plugins/colorscheme.lua b/lua/plugins/colorscheme.lua index b088a30..1b08ce3 100644 --- a/lua/plugins/colorscheme.lua +++ b/lua/plugins/colorscheme.lua @@ -37,6 +37,7 @@ return { -- No nonsense, very stoic -- vim.api.nvim_set_hl(0,"Visual",{bg="#171717"}) -- vim.api.nvim_set_hl(0,"Visual",{bg="#829e9c"}) vim.api.nvim_set_hl(0,"Visual",{bg="#84493b"}) + vim.api.nvim_set_hl(0,"Folded",{bg="none"}) vim.api.nvim_set_hl(0,"VertSplit",{bg="none",fg="#727272"}) vim.api.nvim_set_hl(0,"WinSeparator",{bg="none",fg="#727272"}) vim.api.nvim_set_hl(0,"StatusLine",{bg="none",fg="#727272"}) diff --git a/lua/plugins/fugitive.lua b/lua/plugins/fugitive.lua index ce5e173..bbb5ef8 100644 --- a/lua/plugins/fugitive.lua +++ b/lua/plugins/fugitive.lua @@ -4,14 +4,17 @@ return { dependencies = "tpope/vim-rhubarb", config = function() - local handle = io.popen('git rev-parse --is-inside-work-tree 2> /dev/null') - if handle then - local result = handle:read('*a') - handle:close() - if result and result:match('true') then - vim.cmd([[Gcd]]) - end - end + -- The following seems to break harpoon + -- it's supposed to auto-cd to the git root on vim startup + + -- local handle = io.popen('git rev-parse --is-inside-work-tree 2> /dev/null') + -- if handle then + -- local result = handle:read('*a') + -- handle:close() + -- if result and result:match('true') then + -- vim.cmd([[Gcd]]) + -- end + -- end end, -- Only load when using one of the following commands: diff --git a/lua/plugins/harpoon.lua b/lua/plugins/harpoon.lua index 9a8942e..9d061cf 100644 --- a/lua/plugins/harpoon.lua +++ b/lua/plugins/harpoon.lua @@ -7,7 +7,8 @@ return { }, config = function() local harpoon = require("harpoon") - harpoon:setup({}) + + harpoon:setup() -- -- basic telescope config -- local conf = require("telescope.config").values @@ -39,6 +40,6 @@ return { vim.keymap.set("n", "", function() harpoon:list():select(5)end ) vim.keymap.set("n", "", function() harpoon:list():prev() end) - vim.keymap.set("n", "", function() harpoon:list():next() end) + vim.keymap.set("n", "", function() harpoon:list():next() end) end, } diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index aafd16e..ccd25a0 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -1,34 +1,119 @@ return { - "neovim/nvim-lspconfig", - dependencies = { - { - "nvim-telescope/telescope.nvim" + { + "williamboman/mason.nvim", + dependencies = { + "williamboman/mason-lspconfig.nvim", }, - { - "hrsh7th/nvim-cmp", - dependencies = { - "hrsh7th/cmp-nvim-lsp", - "hrsh7th/cmp-buffer", - }, - } + config = function() + local mason = require('mason') + local mlsp = require('mason-lspconfig') + mason.setup({ + ui = { + icons = { + package_installed = "", + package_not_installed = "", + package_pending = "", + } + } + }) + -- Global so it can be used by both mason and lspconfig ;) + local myServers = { + "gopls", + "lua_ls", + "bashls", + "graphql", + "rust_analyzer", + } + mlsp.setup({ + ensure_installed = myServers, + automatic_installation = false, + }) + end, }, - lazy=false, - config = function() - telescope = require('telescope') - require("lspconfig").gopls.setup{ - on_attach = function() - vim.keymap.set("n", "K", vim.lsp.buf.hover, {buffer=0}) - vim.keymap.set("n", "r", vim.lsp.buf.rename, {buffer=0}) - vim.keymap.set("n", "gd", vim.lsp.buf.definition, {buffer=0}) - vim.keymap.set("n", "gt", vim.lsp.buf.type_definition, {buffer=0}) - vim.keymap.set("n", "gi", vim.lsp.buf.implementation, {buffer=0}) - vim.keymap.set("n", "fa", vim.lsp.buf.code_action, {buffer=0}) - vim.keymap.set("n", "fn", vim.diagnostic.goto_next, {buffer=0}) - vim.keymap.set("n", "fp", vim.diagnostic.goto_prev, {buffer=0}) - - -- like "K" but for diagnostics - vim.keymap.set("n", "S", vim.diagnostic.open_float, {buffer=0}) + { + "neovim/nvim-lspconfig", + dependencies = { + "hrsh7th/nvim-cmp", + "hrsh7th/cmp-nvim-lsp", + "nvim-telescope/telescope.nvim", + }, + config = function() + + -- Use icons in the sidebar + local signs = { Error="", Warn ="", Hint ="󰈈", Info ="" } + for type, icon in pairs(signs) do + local hl = "DiagnosticSign" .. type + vim.fn.sign_define(hl, {text = icon, texthl = hl, numhl = ""}) end - } - end, + -- Hide inline virtual text and use only the icons for debbuging/tips + vim.diagnostic.config({ + virtual_text = false, + signs = true, + underline = true + }) + + local lsp = require('lspconfig') + local lspCaps = require('cmp_nvim_lsp').default_capabilities() + local lspMaps = function() + + vim.keymap.set("n", "K", vim.lsp.buf.hover, {buffer=0, + desc = "Show object description on hover"}) + + vim.keymap.set("n", "r", vim.lsp.buf.rename, {buffer=0, + desc = "Rename object across all occurences"}) + + vim.keymap.set("n", "gd", vim.lsp.buf.definition, {buffer=0, + desc = "Go to the location where the object is defined"}) + + vim.keymap.set("n", "gt", vim.lsp.buf.type_definition, {buffer=0, + desc = "Go to the definition of the objects type"}) + + vim.keymap.set("n", "gi", vim.lsp.buf.implementation, {buffer=0, + desc = "Go to the method implementation"}) + + vim.keymap.set("n", "fa", vim.lsp.buf.code_action, {buffer=0, + desc = ""}) + vim.keymap.set("n", "]d", vim.diagnostic.goto_next, {buffer=0, + desc = "Go to the next diagnostic/issue"}) + + vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, {buffer=0, + desc = "Go to the previous diagnostic/issue"}) + + vim.keymap.set("n", "H", vim.diagnostic.open_float, {buffer=0, + desc = "View diagnostics information in a floating window"}) + + end + + -- Configure every lsp installed and managed by mason + -- TODO: automate this with a global myServers table + lsp["gopls"].setup({ capabilities = lspCaps, on_attach = lspMaps }) + lsp["bashls"].setup({ capabilities = lspCaps, on_attach = lspMaps }) + lsp["graphql"].setup({ capabilities = lspCaps, on_attach = lspMaps }) + lsp["rust_analyzer"].setup({ capabilities = lspCaps, on_attach = lspMaps }) + -- Non standard language settings: + lsp.lua_ls.setup({ + capabilities = lspCaps, + on_attach = lspMaps, + settings = { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using + -- -- (most likely LuaJIT in the case of Neovim) + version = 'LuaJIT' + }, + diagnostic = { + -- Get the language server to recognize the `vim` global + globals = { + 'vim', + 'require' + } + }, + workspace = { + library = vim.api.nvim_get_runtime_file("", true), + }, + } + } + }) + end, + }, } diff --git a/lua/plugins/telescope.lua b/lua/plugins/telescope.lua index 4acf424..ddf8a5e 100644 --- a/lua/plugins/telescope.lua +++ b/lua/plugins/telescope.lua @@ -2,28 +2,30 @@ return { "nvim-telescope/telescope.nvim", branch = "0.1.x", dependencies = { - "nvim-lua/plenary.nvim", + { "nvim-lua/plenary.nvim" }, + { 'nvim-telescope/telescope-fzf-native.nvim', build = 'make' }, + { "stevearc/dressing.nvim" }, }, - keys = { -- LazyLoad telescope when it's actually needed - + config = function() + local telescope = require("telescope") + local actions = require("telescope.actions") + telescope.load_extension("fzf") + end, + keys = { -- P.roject F.iles { "pv", 'lua require("telescope.builtin").find_files()' }, - -- P.roject S.earch { "ps", 'lua require("telescope.builtin").grep_string({search = vim.fn.input("Grep > ")})' }, - - -- F.ind Q.uickfix { "qf", 'lua require("telescope.builtin").quickfix()' }, - -- F.ind G.it (files known to git / not ignored or untracked) { "", 'lua require("telescope.builtin").git_files()' }, - -- F.ind B.ranch { "fb", 'lua require("telescope.builtin").grep_branches()' }, - -- F.ix L.ist - { "fl", "Telescope diagnostics" } + { "fl", "Telescope diagnostics" }, + -- F.ind R.eferences + { "fr", "Telescope lsp_references" } }, cmd = { "Telescope", diff --git a/lua/plugins/whichkey.lua b/lua/plugins/whichkey.lua new file mode 100644 index 0000000..6f820e0 --- /dev/null +++ b/lua/plugins/whichkey.lua @@ -0,0 +1,13 @@ +return { + "folke/which-key.nvim", + event = "VeryLazy", + init = function() + vim.o.timeout = true + vim.o.timeoutlen = 300 + end, + opts = { + -- your configuration comes here + -- or leave it empty to use the default settings + -- refer to the configuration section below + } +} -- cgit v1.2.3