From d25dcabc9edb9ef7bc7a7795f00f31f2cb6c76d5 Mon Sep 17 00:00:00 2001 From: ksyasuda Date: Thu, 10 Feb 2022 00:43:30 -0800 Subject: [PATCH] update --- nvim/init.vim | 28 +++++--- nvim/plugin-confs/lsp-kind.lua | 0 nvim/plugin-confs/lspconfig.lua | 109 ++++++++++++++++++++++++++++++++ nvim/plugin-confs/null-ls.lua | 73 ++++++++++++--------- nvim/plugin-confs/nvim-cmp.lua | 101 +++++++++++++++++++++++++++++ 5 files changed, 271 insertions(+), 40 deletions(-) create mode 100644 nvim/plugin-confs/lsp-kind.lua create mode 100644 nvim/plugin-confs/nvim-cmp.lua diff --git a/nvim/init.vim b/nvim/init.vim index 2107eb5..d3f62ce 100644 --- a/nvim/init.vim +++ b/nvim/init.vim @@ -1,5 +1,5 @@ -syntax enable -filetype plugin on +" syntax enable +" filetype plugin on set noshowmode "disable default vim insert text at bottom set laststatus=2 set number @@ -63,8 +63,6 @@ Plug 'tpope/vim-surround' Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } Plug 'junegunn/fzf.vim' Plug 'itchyny/lightline.vim' -Plug 'dense-analysis/ale' -Plug 'neoclide/coc.nvim', {'branch': 'release'} Plug 'wakatime/vim-wakatime' Plug 'voldikss/vim-floaterm' Plug 'pechorin/any-jump.vim' @@ -75,6 +73,9 @@ Plug 'sheerun/vim-polyglot' Plug 'maximbaz/lightline-ale' Plug 'osyo-manga/vim-over' +" Plug 'dense-analysis/ale' +" Plug 'neoclide/coc.nvim', {'branch': 'release'} + if has('nvim') Plug 'akinsho/bufferline.nvim' " Plug 'chentau/marks.nvim' @@ -91,6 +92,13 @@ if has('nvim') Plug 'ray-x/lsp_signature.nvim' Plug 'simrat39/symbols-outline.nvim' Plug 'jose-elias-alvarez/null-ls.nvim' + Plug 'hrsh7th/cmp-nvim-lsp' + Plug 'hrsh7th/cmp-buffer' + Plug 'hrsh7th/cmp-path' + Plug 'hrsh7th/nvim-cmp' + Plug 'onsails/lspkind-nvim' + Plug 'saadparwaiz1/cmp_luasnip' + Plug 'L3MON4D3/LuaSnip' Plug 'NTBBloodbath/doom-one.nvim' Plug 'Mofiqul/dracula.nvim' @@ -120,10 +128,13 @@ if has('nvim') source ~/.config/nvim/plugin-confs/treesitter.lua source ~/.config/nvim/plugin-confs/neogit.lua - " source ~/.config/nvim/plugin-confs/lspconfig.lua + set completeopt=menu,menuone,noselect + source ~/.config/nvim/plugin-confs/lspconfig.lua source ~/.config/nvim/plugin-confs/lsp-signature.lua - source ~/.config/nvim/symbols-outline.lua - " source ~/.config/nvim/null-ls.lua + source ~/.config/nvim/plugin-confs/symbols-outline.lua + source ~/.config/nvim/plugin-confs/null-ls.lua + source ~/.config/nvim/plugin-confs/nvim-cmp.lua + source ~/.config/nvim/plugin-confs/lsp-kind.lua source ~/.vim/plugin-confs/fzf.vim source ~/.vim/plugin-confs/vim-closetag.vim @@ -131,9 +142,6 @@ if has('nvim') source ~/.vim/plugin-confs/lightline.vim source ~/.vim/plugin-confs/floaterm.vim - source ~/.vim/plugin-confs/ale.vim - source ~/.vim/plugin-confs/coc.vim - source ~/.config/nvim/plugin-confs/doomone.lua source ~/.config/nvim/plugin-confs/dracula.lua source ~/.config/nvim/plugin-confs/github-theme.lua diff --git a/nvim/plugin-confs/lsp-kind.lua b/nvim/plugin-confs/lsp-kind.lua new file mode 100644 index 0000000..e69de29 diff --git a/nvim/plugin-confs/lspconfig.lua b/nvim/plugin-confs/lspconfig.lua index 741cba6..354a31e 100644 --- a/nvim/plugin-confs/lspconfig.lua +++ b/nvim/plugin-confs/lspconfig.lua @@ -1,3 +1,63 @@ +local opts = { noremap=true, silent=true } +vim.api.nvim_set_keymap('n', 'e', 'lua vim.diagnostic.open_float()', opts) +vim.api.nvim_set_keymap('n', '[d', 'lua vim.diagnostic.goto_prev()', opts) +vim.api.nvim_set_keymap('n', ']d', 'lua vim.diagnostic.goto_next()', opts) +vim.api.nvim_set_keymap('n', 'q', 'lua vim.diagnostic.setloclist()', opts) + +-- Use an on_attach function to only map the following keys +-- after the language server attaches to the current buffer +local on_attach = function(client, bufnr) + -- Enable completion triggered by + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') + + -- Mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.buf.declaration()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', 'lua vim.lsp.buf.definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', 'lua vim.lsp.buf.hover()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', 'lua vim.lsp.buf.implementation()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', '', 'lua vim.lsp.buf.signature_help()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'D', 'lua vim.lsp.buf.type_definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'rn', 'lua vim.lsp.buf.rename()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'ca', 'lua vim.lsp.buf.code_action()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', 'lua vim.lsp.buf.references()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'f', 'lua vim.lsp.buf.formatting()', opts) + if client.resolved_capabilities.document_highlight then + vim.cmd [[ + hi! LspReferenceRead cterm=bold ctermbg=red guibg=LightYellow + hi! LspReferenceText cterm=bold ctermbg=red guibg=LightYellow + hi! LspReferenceWrite cterm=bold ctermbg=red guibg=LightYellow + augroup lsp_document_highlight + autocmd! * + autocmd! CursorHold lua vim.lsp.buf.document_highlight() + autocmd! CursorMoved lua vim.lsp.buf.clear_references() + augroup END + ]] + end +end + +vim.cmd [[autocmd! ColorScheme * highlight NormalFloat guibg=#1f2335]] +vim.cmd [[autocmd! ColorScheme * highlight FloatBorder guifg=white guibg=#1f2335]] + +local border = { + {"🭽", "FloatBorder"}, + {"▔", "FloatBorder"}, + {"🭾", "FloatBorder"}, + {"▕", "FloatBorder"}, + {"🭿", "FloatBorder"}, + {"▁", "FloatBorder"}, + {"🭼", "FloatBorder"}, + {"▏", "FloatBorder"}, +} + +local handlers = { + ["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {border = border}), + ["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {border = border }), +} + local DEFAULT_SETTINGS = { ui = { icons = { @@ -32,6 +92,8 @@ local DEFAULT_SETTINGS = { -- Example: { "--proxy", "https://proxyserver" } install_args = {}, }, + on_attach = on_attach, + -- handlers=handlers, -- Controls to which degree logs are written to the log file. It's useful to set this to vim.log.levels.DEBUG when -- debugging issues with server installations. @@ -42,6 +104,40 @@ local DEFAULT_SETTINGS = { max_concurrent_installers = 4, } +vim.diagnostic.config({ + virtual_text = true, + signs = true, + underline = true, + update_in_insert = false, + severity_sort = false, +}) + +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 = hl }) +end + +function PrintDiagnostics(opts, bufnr, line_nr, client_id) + bufnr = bufnr or 0 + line_nr = line_nr or (vim.api.nvim_win_get_cursor(0)[1] - 1) + opts = opts or {['lnum'] = line_nr} + + local line_diagnostics = vim.diagnostic.get(bufnr, opts) + if vim.tbl_isempty(line_diagnostics) then return end + + local diagnostic_message = "" + for i, diagnostic in ipairs(line_diagnostics) do + diagnostic_message = diagnostic_message .. string.format("%d: %s", i, diagnostic.message or "") + print(diagnostic_message) + if i ~= #line_diagnostics then + diagnostic_message = diagnostic_message .. "\n" + end + end + vim.api.nvim_echo({{diagnostic_message, "Normal"}}, false, {}) +end + +-- vim.cmd [[ autocmd! CursorHold * lua PrintDiagnostics() ]] local lsp_installer = require("nvim-lsp-installer") @@ -60,3 +156,16 @@ lsp_installer.on_server_ready(function(server) -- Refer to https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md server:setup(DEFAULT_SETTINGS) end) + +-- Use a loop to conveniently call 'setup' on multiple servers and +-- map buffer local keybindings when the language server attaches +-- local servers = { 'pyright', 'bashls', 'sqlls' } +-- for _, lsp in pairs(servers) do +-- require('lspconfig')[lsp].setup { +-- on_attach = on_attach, +-- flags = { +-- -- This will be the default in neovim 0.7+ +-- debounce_text_changes = 150, +-- } +-- } +-- end diff --git a/nvim/plugin-confs/null-ls.lua b/nvim/plugin-confs/null-ls.lua index a66ff24..f19140b 100644 --- a/nvim/plugin-confs/null-ls.lua +++ b/nvim/plugin-confs/null-ls.lua @@ -9,7 +9,20 @@ require("null-ls").setup({ require("null-ls").builtins.formatting.shfmt.with({ extra_args = { "-i", "4", "-ci", "-sr" } }), + require("null-ls").builtins.diagnostics.shellcheck.with({ + extra_args = { "-s", "bash", "-o", "check-extra-masked-returns, check-set-e-suppressed, check-unassigned-uppercase, deprecate-which, quote-safe-variables" } + }) }, + on_attach = function(client) + if client.resolved_capabilities.document_formatting then + vim.cmd([[ + augroup LspFormatting + autocmd! * + autocmd BufWritePre lua vim.lsp.buf.formatting_sync() + augroup END + ]]) + end + end, }) local markdownlint = { @@ -50,37 +63,37 @@ local markdownlint = { }), } -local shellcheck = { - method = null_ls.methods.DIAGNOSTICS, - filetypes = { "sh", "bash", "zsh", "fish" }, - generator = null_ls.generator({ - command = "shellcheck", - args = { "-S", "info", "-s", "bash", "-o", "all", "-e", "2250" }, - from_stderr = true, - format = "line", - check_exit_code = function(code, stderr) - local success = code <= 1 +-- local shellcheck = { +-- method = null_ls.methods.DIAGNOSTICS, +-- filetypes = { "sh", "bash", "zsh", "fish" }, +-- generator = null_ls.generator({ +-- command = "shellcheck", +-- args = { "-s", "bash", "-o", "all", "-e", "2250" }, +-- from_stderr = true, +-- format = "line", +-- check_exit_code = function(code, stderr) +-- local success = code <= 1 - if not success then - -- can be noisy for things that run often (e.g. diagnostics), but can - -- be useful for things that run on demand (e.g. formatting) - print(stderr) - end +-- if not success then +-- -- can be noisy for things that run often (e.g. diagnostics), but can +-- -- be useful for things that run on demand (e.g. formatting) +-- print(stderr) +-- end - return success - end, - on_output = helpers.diagnostics.from_patterns({ - { - pattern = [[:(%d+):(%d+) [%w-/]+ (.*)]], - groups = { "row", "col", "message" }, - }, - { - pattern = [[:(%d+) [%w-/]+ (.*)]], - groups = { "row", "message" }, - }, - }), -}), -} +-- return success +-- end, +-- on_output = helpers.diagnostics.from_patterns({ +-- { +-- pattern = [[:(%d+):(%d+) [%w-/]+ (.*)]], +-- groups = { "row", "col", "message" }, +-- }, +-- { +-- pattern = [[:(%d+) [%w-/]+ (.*)]], +-- groups = { "row", "message" }, +-- }, +-- }), +-- }), +-- } null_ls.register(markdownlint) -null_ls.register(shellcheck) +-- null_ls.register(shellcheck) diff --git a/nvim/plugin-confs/nvim-cmp.lua b/nvim/plugin-confs/nvim-cmp.lua new file mode 100644 index 0000000..a569c92 --- /dev/null +++ b/nvim/plugin-confs/nvim-cmp.lua @@ -0,0 +1,101 @@ +-- Setup nvim-cmp. +local cmp = require'cmp' +local lspkind = require('lspkind') + +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) + +-- luasnip setup +local luasnip = require 'luasnip' + +cmp.setup({ + snippet = { + expand = function(args) + -- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. + require('luasnip').lsp_expand(args.body) -- For `luasnip` users. + -- require('snippy').expand_snippet(args.body) -- For `snippy` users. + -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. + end, + }, + capabilities = capabilities, + -- mapping = { + -- [''] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }), + -- [''] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }), + -- [''] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }), + -- [''] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `` mapping. + -- [''] = cmp.mapping({ + -- i = cmp.mapping.abort(), + -- c = cmp.mapping.close(), + -- }), + -- [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + -- }, + mapping = { + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.close(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [''] = function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + else + fallback() + end + end, + [''] = function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, + }, + formatting = { + format = lspkind.cmp_format({ + mode = 'symbol', -- show only symbol annotations + maxwidth = 50, -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters) + + -- The function below will be called before any actual modifications from lspkind + -- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30)) + -- before = function (entry, vim_item) + -- ... + -- return vim_item + -- end + }) + }, + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + -- { name = 'vsnip' }, -- For vsnip users. + { name = 'luasnip' }, -- For luasnip users. + -- { name = 'ultisnips' }, -- For ultisnips users. + -- { name = 'snippy' }, -- For snippy users. + }, { + { name = 'buffer' }, + }) + }) + + + -- Setup lspconfig. + -- local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()) + -- Replace with each lsp server you've enabled. + -- require('lspconfig')['bashls'].setup { + -- capabilities = capabilities + -- } + -- Enable some language servers with the additional completion capabilities offered by nvim-cmp + local lspconfig = require('lspconfig') + local servers = { 'bashls', 'pyright', 'sqlls', 'jsonls', 'yamlls', 'vimls', 'dotls', 'dockerls' } + for _, lsp in ipairs(servers) do + lspconfig[lsp].setup { + -- on_attach = my_custom_on_attach, + capabilities = capabilities, + } + end