From 499c4553cc3c2973c820c5e7c74ea18dae284341 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Sun, 25 Feb 2024 19:17:35 +0100 Subject: [PATCH] feat(nvim): add coc --- .config/nvim/coc-settings.json | 7 + .config/nvim/init.lua | 8 +- .config/nvim/lua/packer-plugins.lua | 81 ++++++++--- .config/nvim/plugin/coc.lua | 184 ++++++++++++++++++++++++ .config/nvim/plugin/lsp.lua | 124 ++++++++++++++-- .config/nvim/plugin/neogen.lua | 5 + .config/nvim/plugin/packer_compiled.lua | 25 ++-- 7 files changed, 395 insertions(+), 39 deletions(-) create mode 100644 .config/nvim/coc-settings.json mode change 100644 => 100755 .config/nvim/init.lua create mode 100644 .config/nvim/plugin/coc.lua create mode 100755 .config/nvim/plugin/neogen.lua diff --git a/.config/nvim/coc-settings.json b/.config/nvim/coc-settings.json new file mode 100644 index 0000000..13441de --- /dev/null +++ b/.config/nvim/coc-settings.json @@ -0,0 +1,7 @@ +"languageserver": { + "clangd": { + "command": "clangd", + "rootPatterns": ["compile_flags.txt", "compile_commands.json"], + "filetypes": ["c", "cc", "cpp", "c++", "objc", "objcpp"] + } +} diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua old mode 100644 new mode 100755 index 7f59eb0..160c89d --- a/.config/nvim/init.lua +++ b/.config/nvim/init.lua @@ -6,7 +6,13 @@ vim.o.softtabstop = 4 vim.o.shiftwidth = 4 vim.o.expandtab = true vim.o.smartindent = true -vim.o.autoread = true + +-- Sync clipboard between OS and Neovim. +-- See `:help 'clipboard'` +vim.o.clipboard = 'unnamedplus' + +-- Save undo history +vim.o.undofile = true --vim.keymap.set({"n", "v", "i"}, "", "", {noremap = true}) diff --git a/.config/nvim/lua/packer-plugins.lua b/.config/nvim/lua/packer-plugins.lua index ab6566e..2e87b53 100644 --- a/.config/nvim/lua/packer-plugins.lua +++ b/.config/nvim/lua/packer-plugins.lua @@ -3,12 +3,13 @@ return require('packer').startup(function() use 'sonph/onehalf' use 'navarasu/onedark.nvim' use { - 'nvim-telescope/telescope.nvim', tag = '0.1.1', + 'nvim-telescope/telescope.nvim', tag = '0.1.4', -- or , branch = '0.1.x', requires = { {'nvim-lua/plenary.nvim'} } } + require('onedark').setup { - style = 'darker' + style = 'darker' } require('onedark').load() -- use({ @@ -18,24 +19,38 @@ return require('packer').startup(function() -- vim.cmd('colorscheme rose-pine') -- end -- }) - use('nvim-treesitter/nvim-treesitter', {run = ':TSUpdate'}) + use { - 'VonHeikemen/lsp-zero.nvim', - branch = 'v2.x', - requires = { - -- LSP Support - {'neovim/nvim-lspconfig'}, - {'williamboman/mason.nvim', run = function() - pcall(vim.cmd, 'MasonUpdate') - end,}, - {'williamboman/mason-lspconfig.nvim'}, -- Optional - - -- Autocompletion - {'hrsh7th/nvim-cmp'}, -- Required - {'hrsh7th/cmp-nvim-lsp'}, -- Required - {'L3MON4D3/LuaSnip'}, -- Required - } + "williamboman/mason.nvim", + "williamboman/mason-lspconfig.nvim", + 'neovim/nvim-lspconfig' } + use 'hrsh7th/nvim-cmp' -- Autocompletion plugin + use 'hrsh7th/cmp-nvim-lsp' -- Autocompletion with LSPs + use 'hrsh7th/cmp-buffer' + use 'hrsh7th/cmp-path' + + use {'neoclide/coc.nvim', branch = 'release'} + + use('nvim-treesitter/nvim-treesitter', {run = ':TSUpdate'}) + + --use { + -- 'VonHeikemen/lsp-zero.nvim', + -- branch = 'v2.x', + -- requires = { + -- -- LSP Support + -- {'neovim/nvim-lspconfig'}, + -- {'williamboman/mason.nvim', run = function() + -- pcall(vim.cmd, 'MasonUpdate') + -- end,}, + -- {'williamboman/mason-lspconfig.nvim'}, -- Optional + + -- -- Autocompletion + -- {'hrsh7th/nvim-cmp'}, -- Required + -- {'hrsh7th/cmp-nvim-lsp'}, -- Required + -- {'L3MON4D3/LuaSnip'}, -- Required + -- } + --} -- Auto pairs use { @@ -45,4 +60,34 @@ return require('packer').startup(function() --harpoon use("theprimeagen/harpoon") + + ---- neogen + --local i = require("neogen.types.template").item + --local annotation = { + -- { nil, "/**", { no_results = true, type = { "func", "file", "class" } } }, + -- { nil, "** \\file", { no_results = true, type = { "file" } } }, + -- { nil, "** \\brief $1", { no_results = true, type = { "func", "file", "class" } } }, + -- { nil, "*/", { no_results = true, type = { "func", "file", "class" } } }, + -- { nil, "", { no_results = true, type = { "file" } } }, + + -- { nil, "/**", { type = { "func", "class", "type" } } }, + -- { i.ClassName, "** @class %s", { type = { "class" } } }, + -- { i.Type, "** @typedef %s", { type = { "type" } } }, + -- { nil, "** \\brief $1", { type = { "func", "class", "type" } } }, + -- { nil, "**", { type = { "func", "class", "type" } } }, + -- { i.Tparam, "** \\tparam %s $1" }, + -- { i.Parameter, "** \\param %s $1" }, + -- { i.Return, "** \\return $1" }, + -- { nil, "*/", { type = { "func", "class", "type" } } }, + --} + --require("neogen").setup({ + -- languages = { + -- c = { + -- template = { + -- annotation_convention = "my_annotation", + -- my_annotation = annotation + -- } + -- } + -- } + --}) end) diff --git a/.config/nvim/plugin/coc.lua b/.config/nvim/plugin/coc.lua new file mode 100644 index 0000000..f34bd2f --- /dev/null +++ b/.config/nvim/plugin/coc.lua @@ -0,0 +1,184 @@ +-- Some servers have issues with backup files, see #649 +vim.opt.backup = false +vim.opt.writebackup = false + +-- Having longer updatetime (default is 4000 ms = 4s) leads to noticeable +-- delays and poor user experience +vim.opt.updatetime = 300 + +-- Always show the signcolumn, otherwise it would shift the text each time +-- diagnostics appeared/became resolved +vim.opt.signcolumn = "yes" + +local keyset = vim.keymap.set +-- Autocomplete +function _G.check_back_space() + local col = vim.fn.col('.') - 1 + return col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') ~= nil +end + +-- Use Tab for trigger completion with characters ahead and navigate +-- NOTE: There's always a completion item selected by default, you may want to enable +-- no select by setting `"suggest.noselect": true` in your configuration file +-- NOTE: Use command ':verbose imap ' to make sure Tab is not mapped by +-- other plugins before putting this into your config +local opts = {silent = true, noremap = true, expr = true, replace_keycodes = false} +keyset("i", "", 'coc#pum#visible() ? coc#pum#next(1) : v:lua.check_back_space() ? "" : coc#refresh()', opts) +keyset("i", "", [[coc#pum#visible() ? coc#pum#prev(1) : "\"]], opts) + +-- Make to accept selected completion item or notify coc.nvim to format +-- u breaks current undo, please make your own choice +keyset("i", "", [[coc#pum#visible() ? coc#pum#confirm() : "\u\\=coc#on_enter()\"]], opts) + +-- Use to trigger snippets +keyset("i", "", "(coc-snippets-expand-jump)") +-- Use to trigger completion +keyset("i", "", "coc#refresh()", {silent = true, expr = true}) + +-- Use `[g` and `]g` to navigate diagnostics +-- Use `:CocDiagnostics` to get all diagnostics of current buffer in location list +keyset("n", "[g", "(coc-diagnostic-prev)", {silent = true}) +keyset("n", "]g", "(coc-diagnostic-next)", {silent = true}) + +-- GoTo code navigation +keyset("n", "gd", "(coc-definition)", {silent = true}) +keyset("n", "gy", "(coc-type-definition)", {silent = true}) +keyset("n", "gi", "(coc-implementation)", {silent = true}) +keyset("n", "gr", "(coc-references)", {silent = true}) + + +-- Use K to show documentation in preview window +function _G.show_docs() + local cw = vim.fn.expand('') + if vim.fn.index({'vim', 'help'}, vim.bo.filetype) >= 0 then + vim.api.nvim_command('h ' .. cw) + elseif vim.api.nvim_eval('coc#rpc#ready()') then + vim.fn.CocActionAsync('doHover') + else + vim.api.nvim_command('!' .. vim.o.keywordprg .. ' ' .. cw) + end +end +keyset("n", "K", 'lua _G.show_docs()', {silent = true}) + + +-- Highlight the symbol and its references on a CursorHold event(cursor is idle) +vim.api.nvim_create_augroup("CocGroup", {}) +vim.api.nvim_create_autocmd("CursorHold", { + group = "CocGroup", + command = "silent call CocActionAsync('highlight')", + desc = "Highlight symbol under cursor on CursorHold" +}) + + +-- Symbol renaming +keyset("n", "rn", "(coc-rename)", {silent = true}) + + +-- Formatting selected code +keyset("x", "f", "(coc-format-selected)", {silent = true}) +keyset("n", "f", "(coc-format-selected)", {silent = true}) + + +-- Setup formatexpr specified filetype(s) +vim.api.nvim_create_autocmd("FileType", { + group = "CocGroup", + pattern = "typescript,json", + command = "setl formatexpr=CocAction('formatSelected')", + desc = "Setup formatexpr specified filetype(s)." +}) + +-- Update signature help on jump placeholder +vim.api.nvim_create_autocmd("User", { + group = "CocGroup", + pattern = "CocJumpPlaceholder", + command = "call CocActionAsync('showSignatureHelp')", + desc = "Update signature help on jump placeholder" +}) + +-- Apply codeAction to the selected region +-- Example: `aap` for current paragraph +local opts = {silent = true, nowait = true} +keyset("x", "a", "(coc-codeaction-selected)", opts) +keyset("n", "a", "(coc-codeaction-selected)", opts) + +-- Remap keys for apply code actions at the cursor position. +keyset("n", "ac", "(coc-codeaction-cursor)", opts) +-- Remap keys for apply source code actions for current file. +keyset("n", "as", "(coc-codeaction-source)", opts) +-- Apply the most preferred quickfix action on the current line. +keyset("n", "qf", "(coc-fix-current)", opts) + +-- Remap keys for apply refactor code actions. +keyset("n", "re", "(coc-codeaction-refactor)", { silent = true }) +keyset("x", "r", "(coc-codeaction-refactor-selected)", { silent = true }) +keyset("n", "r", "(coc-codeaction-refactor-selected)", { silent = true }) + +-- Run the Code Lens actions on the current line +keyset("n", "cl", "(coc-codelens-action)", opts) + + +-- Map function and class text objects +-- NOTE: Requires 'textDocument.documentSymbol' support from the language server +keyset("x", "if", "(coc-funcobj-i)", opts) +keyset("o", "if", "(coc-funcobj-i)", opts) +keyset("x", "af", "(coc-funcobj-a)", opts) +keyset("o", "af", "(coc-funcobj-a)", opts) +keyset("x", "ic", "(coc-classobj-i)", opts) +keyset("o", "ic", "(coc-classobj-i)", opts) +keyset("x", "ac", "(coc-classobj-a)", opts) +keyset("o", "ac", "(coc-classobj-a)", opts) + + +-- Remap and to scroll float windows/popups +---@diagnostic disable-next-line: redefined-local +local opts = {silent = true, nowait = true, expr = true} +keyset("n", "", 'coc#float#has_scroll() ? coc#float#scroll(1) : ""', opts) +keyset("n", "", 'coc#float#has_scroll() ? coc#float#scroll(0) : ""', opts) +keyset("i", "", + 'coc#float#has_scroll() ? "=coc#float#scroll(1)" : ""', opts) +keyset("i", "", + 'coc#float#has_scroll() ? "=coc#float#scroll(0)" : ""', opts) +keyset("v", "", 'coc#float#has_scroll() ? coc#float#scroll(1) : ""', opts) +keyset("v", "", 'coc#float#has_scroll() ? coc#float#scroll(0) : ""', opts) + + +-- Use CTRL-S for selections ranges +-- Requires 'textDocument/selectionRange' support of language server +keyset("n", "", "(coc-range-select)", {silent = true}) +keyset("x", "", "(coc-range-select)", {silent = true}) + + +-- Add `:Format` command to format current buffer +vim.api.nvim_create_user_command("Format", "call CocAction('format')", {}) + +-- " Add `:Fold` command to fold current buffer +vim.api.nvim_create_user_command("Fold", "call CocAction('fold', )", {nargs = '?'}) + +-- Add `:OR` command for organize imports of the current buffer +vim.api.nvim_create_user_command("OR", "call CocActionAsync('runCommand', 'editor.action.organizeImport')", {}) + +-- Add (Neo)Vim's native statusline support +-- NOTE: Please see `:h coc-status` for integrations with external plugins that +-- provide custom statusline: lightline.vim, vim-airline +vim.opt.statusline:prepend("%{coc#status()}%{get(b:,'coc_current_function','')}") + +-- Mappings for CoCList +-- code actions and coc stuff +---@diagnostic disable-next-line: redefined-local +local opts = {silent = true, nowait = true} +-- Show all diagnostics +keyset("n", "a", ":CocList diagnostics", opts) +-- Manage extensions +keyset("n", "e", ":CocList extensions", opts) +-- Show commands +keyset("n", "c", ":CocList commands", opts) +-- Find symbol of current document +keyset("n", "o", ":CocList outline", opts) +-- Search workspace symbols +keyset("n", "s", ":CocList -I symbols", opts) +-- Do default action for next item +keyset("n", "j", ":CocNext", opts) +-- Do default action for previous item +keyset("n", "k", ":CocPrev", opts) +-- Resume latest coc list +keyset("n", "p", ":CocListResume", opts) diff --git a/.config/nvim/plugin/lsp.lua b/.config/nvim/plugin/lsp.lua index da1f967..1099264 100644 --- a/.config/nvim/plugin/lsp.lua +++ b/.config/nvim/plugin/lsp.lua @@ -1,13 +1,117 @@ -local lsp = require('lsp-zero').preset({}) +-- Initialize Mason and specify which language servers to install +--require("mason").setup() +--require("mason-lspconfig").setup({ +-- ensured_installed = { "rust_analyzer" } +--}) +-- +---- Define lspconfig settings for rust_analyzer +--local on_attach = function(_, _) +-- local opts = { buffer = bufnr, remap = false } +-- vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts) +--end +-- +--local capabilities = require('cmp_nvim_lsp').default_capabilities() +-- +---- Configure autocomplete using nvim-cmp +--local cmp = require("cmp") +--cmp.setup({ +-- 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.confirm({ select = true }), +-- [""] = cmp.mapping.confirm({ select = true }), +-- }, +-- sources = { +-- { name = 'nvim_lsp' }, +-- { name = 'buffer' }, +-- { name = 'path' }, +-- }, +-- experimental = { ghost_text = false }, +-- formatting = { fields = { 'menu', 'abbr', 'kind' } }, +-- confirm_opts = { behavior = cmp.ConfirmBehavior.Replace, select = false }, +-- window = { +-- --completion = cmp.config.window.bordered(), +-- --documentation = cmp.config.window.bordered(), +-- }, +-- capabilities = capabilities, +--}) +-- +---- Set up Rust Analyzer +--local lspconfig = require('lspconfig') +--lspconfig.rust_analyzer.setup { +-- capabilities = capabilities, +-- on_attach = on_attach +--} +--vim.keymap.set('n', 'e', vim.diagnostic.open_float) +--vim.keymap.set('n', '[d', vim.diagnostic.goto_prev) +--vim.keymap.set('n', ']d', vim.diagnostic.goto_next) +---- vim.keymap.set('n', 'q', vim.diagnostic.setloclist) - lsp.on_attach(function(client, bufnr) - local opts = {buffer = bufnr, remap = false} - vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts) - end) +-- old +--require("mason").setup() +--require("mason-lspconfig").setup({ +-- ensure_installed = { "rust_analyzer" } +--}) +-- +--local on_attach = function(_, _) +-- local opts = {buffer = bufnr, remap = false} +-- vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts) +--end +-- +--local capabilities = require('cmp_nvim_lsp').default_capabilities() +--require("configs.nvim-cmp") +--require('nvim-cmp').setup{} +--require('cmp').setup({ +-- 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.confirm({ select = true }), +-- }, +-- sources = { +-- { name = 'nvim_lsp' }, +-- { name = 'buffer' }, +-- { name = 'path' }, +-- }, +-- experimental = { ghost_text = false }, +-- formatting = { fields = { 'menu', 'abbr', 'kind' } }, +-- confirm_opts = { behavior = cmp.ConfirmBehavior.Replace, select = false }, +-- window = { +-- completion = cmp.config.window.bordered(), +-- documentation = cmp.config.window.bordered(), +-- }, +-- capabilities = capabilities, +--}) +-- +-- +--require("lspconfig").rust_analyzer.setup { +-- capabilities = capabilities +-- on_attach = on_attach +--} -lsp.ensure_installed({ - 'tsserver', - 'eslint', -}) -lsp.setup() +--local lsp = require('lsp-zero').preset({}) +-- +-- lsp.on_attach(function(client, bufnr) +-- local opts = {buffer = bufnr, remap = false} +-- +-- vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts) +-- end) +-- +----lsp['rust_analyzer'].setup({ +---- capabilities = capabilities, +---- on_attach = on_attach +----}) +-- +--lsp.ensure_installed({ +-- 'tsserver', +-- 'eslint', +-- 'rust_analyzer' +--}) +--lsp.setup() +----lsp.rust_analyzer.setup() diff --git a/.config/nvim/plugin/neogen.lua b/.config/nvim/plugin/neogen.lua new file mode 100755 index 0000000..fe113a9 --- /dev/null +++ b/.config/nvim/plugin/neogen.lua @@ -0,0 +1,5 @@ +--require('neogen').generate() +--local opts = { noremap = true, silent = true } +-- +--vim.api.nvim_set_keymap("n", "nf", ":lua require('neogen').generate()", opts) + diff --git a/.config/nvim/plugin/packer_compiled.lua b/.config/nvim/plugin/packer_compiled.lua index f954b22..f86cd30 100644 --- a/.config/nvim/plugin/packer_compiled.lua +++ b/.config/nvim/plugin/packer_compiled.lua @@ -49,8 +49,8 @@ local function save_profiles(threshold) end time([[Luarocks path setup]], true) -local package_path_str = "/home/grimhilt/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?.lua;/home/grimhilt/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?/init.lua;/home/grimhilt/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?.lua;/home/grimhilt/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?/init.lua" -local install_cpath_pattern = "/home/grimhilt/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/?.so" +local package_path_str = "/home/grimhilt/.cache/nvim/packer_hererocks/2.1.1693350652/share/lua/5.1/?.lua;/home/grimhilt/.cache/nvim/packer_hererocks/2.1.1693350652/share/lua/5.1/?/init.lua;/home/grimhilt/.cache/nvim/packer_hererocks/2.1.1693350652/lib/luarocks/rocks-5.1/?.lua;/home/grimhilt/.cache/nvim/packer_hererocks/2.1.1693350652/lib/luarocks/rocks-5.1/?/init.lua" +local install_cpath_pattern = "/home/grimhilt/.cache/nvim/packer_hererocks/2.1.1693350652/lib/lua/5.1/?.so" if not string.find(package.path, package_path_str, 1, true) then package.path = package.path .. ';' .. package_path_str end @@ -74,26 +74,31 @@ end time([[try_loadstring definition]], false) time([[Defining packer_plugins]], true) _G.packer_plugins = { - LuaSnip = { + ["cmp-buffer"] = { loaded = true, - path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/LuaSnip", - url = "https://github.com/L3MON4D3/LuaSnip" + path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/cmp-buffer", + url = "https://github.com/hrsh7th/cmp-buffer" }, ["cmp-nvim-lsp"] = { loaded = true, path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/cmp-nvim-lsp", url = "https://github.com/hrsh7th/cmp-nvim-lsp" }, + ["cmp-path"] = { + loaded = true, + path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/cmp-path", + url = "https://github.com/hrsh7th/cmp-path" + }, + ["coc.nvim"] = { + loaded = true, + path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/coc.nvim", + url = "https://github.com/neoclide/coc.nvim" + }, harpoon = { loaded = true, path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/harpoon", url = "https://github.com/theprimeagen/harpoon" }, - ["lsp-zero.nvim"] = { - loaded = true, - path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/lsp-zero.nvim", - url = "https://github.com/VonHeikemen/lsp-zero.nvim" - }, ["mason-lspconfig.nvim"] = { loaded = true, path = "/home/grimhilt/.local/share/nvim/site/pack/packer/start/mason-lspconfig.nvim",