|
| 1 | +----------------------- hrsh7th/nvim-cmp ---------------------------- |
| 2 | +local cmp = require 'cmp' |
| 3 | + |
| 4 | +cmp.setup({ |
| 5 | + snippet = { |
| 6 | + -- REQUIRED - you must specify a snippet engine |
| 7 | + expand = function(args) |
| 8 | + -- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. |
| 9 | + -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users. |
| 10 | + -- require('snippy').expand_snippet(args.body) -- For `snippy` users. |
| 11 | + vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. |
| 12 | + end, |
| 13 | + }, |
| 14 | + window = { |
| 15 | + completion = cmp.config.window.bordered(), |
| 16 | + documentation = cmp.config.window.bordered(), |
| 17 | + }, |
| 18 | + mapping = cmp.mapping.preset.insert({ |
| 19 | + ['<C-b>'] = cmp.mapping.scroll_docs(-4), |
| 20 | + ['<C-f>'] = cmp.mapping.scroll_docs(4), |
| 21 | + ['<C-Space>'] = cmp.mapping.complete(), |
| 22 | + ['<C-e>'] = cmp.mapping.abort(), |
| 23 | + ['<tab>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. |
| 24 | + }), |
| 25 | + sources = cmp.config.sources({ |
| 26 | + { name = 'nvim_lsp' }, |
| 27 | + -- { name = 'vsnip' }, -- For vsnip users. |
| 28 | + -- { name = 'luasnip' }, -- For luasnip users. |
| 29 | + { name = 'ultisnips' }, -- For ultisnips users. |
| 30 | + -- { name = 'snippy' }, -- For snippy users. |
| 31 | + }, { |
| 32 | + { name = 'buffer' }, |
| 33 | + }) |
| 34 | +}) |
| 35 | + |
| 36 | +-- Set configuration for specific filetype. |
| 37 | +cmp.setup.filetype('gitcommit', { |
| 38 | + sources = cmp.config.sources({ |
| 39 | + { name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it. |
| 40 | + }, { |
| 41 | + { name = 'buffer' }, |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +-- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore). |
| 46 | +cmp.setup.cmdline({ '/', '?' }, { |
| 47 | + mapping = cmp.mapping.preset.cmdline(), |
| 48 | + sources = { |
| 49 | + { name = 'buffer' } |
| 50 | + } |
| 51 | +}) |
| 52 | + |
| 53 | +-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). |
| 54 | +cmp.setup.cmdline(':', { |
| 55 | + mapping = cmp.mapping.preset.cmdline(), |
| 56 | + sources = cmp.config.sources({ |
| 57 | + { name = 'path' } |
| 58 | + }, { |
| 59 | + { name = 'cmdline' } |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +-- Set up lspconfig. |
| 64 | +local capabilities = require('cmp_nvim_lsp').default_capabilities() |
| 65 | +-- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled. |
| 66 | +-- require('lspconfig')['<YOUR_LSP_SERVER>'].setup { |
| 67 | +-- capabilities = capabilities, |
| 68 | +-- } |
| 69 | + |
| 70 | +--------------------------------------------------------------------- |
| 71 | + |
| 72 | +-- See: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md |
| 73 | +vim.diagnostic.config { |
| 74 | + virtual_text = true, |
| 75 | + signs = true, |
| 76 | +} |
| 77 | + |
| 78 | +-- Use an on_attach function to only map the following keys |
| 79 | +-- after the language server attaches to the current buffer |
| 80 | +local on_attach = function(client, bufnr) |
| 81 | + -- Enable completion triggered by <c-x><c-o> |
| 82 | + vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") |
| 83 | + |
| 84 | + -- Mappings. |
| 85 | + -- See `:help vim.lsp.*` for documentation on any of the below functions |
| 86 | + local bufopts = { noremap = true, silent = true, buffer = bufnr } |
| 87 | + vim.keymap.set("n", "<leader>gD", vim.lsp.buf.declaration, bufopts) |
| 88 | + vim.keymap.set("n", "<leader>gd", vim.lsp.buf.definition, bufopts) |
| 89 | + vim.keymap.set("n", "<leader>K", vim.lsp.buf.hover, bufopts) |
| 90 | + vim.keymap.set("n", "<leader>gi", vim.lsp.buf.implementation, bufopts) |
| 91 | +-- vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, bufopts) |
| 92 | +-- vim.keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, bufopts) |
| 93 | +-- vim.keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, bufopts) |
| 94 | +-- vim.keymap.set("n", "<space>wl", function() |
| 95 | +-- print(vim.inspect(vim.lsp.buf.list_workspace_folders())) |
| 96 | +-- end, bufopts) |
| 97 | + vim.keymap.set("n", "<leader>gt", vim.lsp.buf.type_definition, bufopts) |
| 98 | + vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, bufopts) |
| 99 | + vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, bufopts) |
| 100 | + vim.keymap.set("n", "<leader>gr", vim.lsp.buf.references, bufopts) |
| 101 | + vim.keymap.set("n", "<leader>fmt", function() |
| 102 | + vim.lsp.buf.format { async = true } |
| 103 | + end, bufopts) |
| 104 | +end |
| 105 | + |
| 106 | +require("clangd_extensions").setup { |
| 107 | + server = { |
| 108 | + -- options to pass to nvim-lspconfig |
| 109 | + -- i.e. the arguments to require("lspconfig").clangd.setup({}) |
| 110 | + capabilities = capabilities, |
| 111 | + cmd = { |
| 112 | + "clangd", |
| 113 | + "--clang-tidy", |
| 114 | + "--compile-commands-dir=_build/debug", |
| 115 | + "--pretty", |
| 116 | + "--cross-file-rename", |
| 117 | + "--inlay-hints=true", |
| 118 | + "--background-index", |
| 119 | + "--suggest-missing-includes=true", |
| 120 | + }, |
| 121 | + filetypes = { "c", "cpp", "objc", "objcpp" }, |
| 122 | + on_attach = on_attach |
| 123 | + }, |
| 124 | + extensions = { |
| 125 | + -- defaults: |
| 126 | + -- Automatically set inlay hints (type hints) |
| 127 | + autoSetHints = true, |
| 128 | + -- These apply to the default ClangdSetInlayHints command |
| 129 | + inlay_hints = { |
| 130 | + -- Only show inlay hints for the current line |
| 131 | + only_current_line = false, |
| 132 | + -- Event which triggers a refersh of the inlay hints. |
| 133 | + -- You can make this "CursorMoved" or "CursorMoved,CursorMovedI" but |
| 134 | + -- not that this may cause higher CPU usage. |
| 135 | + -- This option is only respected when only_current_line and |
| 136 | + -- autoSetHints both are true. |
| 137 | + only_current_line_autocmd = "CursorHold", |
| 138 | + -- whether to show parameter hints with the inlay hints or not |
| 139 | + show_parameter_hints = true, |
| 140 | + -- prefix for parameter hints |
| 141 | + parameter_hints_prefix = "<- ", |
| 142 | + -- prefix for all the other hints (type, chaining) |
| 143 | + other_hints_prefix = "=> ", |
| 144 | + -- whether to align to the length of the longest line in the file |
| 145 | + max_len_align = false, |
| 146 | + -- padding from the left if max_len_align is true |
| 147 | + max_len_align_padding = 1, |
| 148 | + -- whether to align to the extreme right or not |
| 149 | + right_align = false, |
| 150 | + -- padding from the right if right_align is true |
| 151 | + right_align_padding = 7, |
| 152 | + -- The color of the hints |
| 153 | + highlight = "Comment", |
| 154 | + -- The highlight group priority for extmark |
| 155 | + priority = 100, |
| 156 | + }, |
| 157 | + ast = { |
| 158 | + -- These are unicode, should be available in any font |
| 159 | + role_icons = { |
| 160 | + type = "🄣", |
| 161 | + declaration = "🄓", |
| 162 | + expression = "🄔", |
| 163 | + statement = ";", |
| 164 | + specifier = "🄢", |
| 165 | + ["template argument"] = "🆃", |
| 166 | + }, |
| 167 | + kind_icons = { |
| 168 | + Compound = "🄲", |
| 169 | + Recovery = "🅁", |
| 170 | + TranslationUnit = "🅄", |
| 171 | + PackExpansion = "🄿", |
| 172 | + TemplateTypeParm = "🅃", |
| 173 | + TemplateTemplateParm = "🅃", |
| 174 | + TemplateParamObject = "🅃", |
| 175 | + }, |
| 176 | + highlights = { |
| 177 | + detail = "Comment", |
| 178 | + }, |
| 179 | + }, |
| 180 | + memory_usage = { |
| 181 | + border = "none", |
| 182 | + }, |
| 183 | + symbol_info = { |
| 184 | + border = "none", |
| 185 | + }, |
| 186 | + }, |
| 187 | +} |
| 188 | + |
| 189 | +require 'lspconfig'.lua_ls.setup { |
| 190 | + capabilities = capabilities, |
| 191 | + on_attach = on_attach, |
| 192 | + settings = { |
| 193 | + Lua = { |
| 194 | + runtime = { |
| 195 | + -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) |
| 196 | + version = 'LuaJIT', |
| 197 | + }, |
| 198 | + format = { |
| 199 | + enable = true, |
| 200 | + -- Put format options here |
| 201 | + -- NOTE: the value should be STRING!! |
| 202 | + defaultConfig = { |
| 203 | + indent_style = "tab", |
| 204 | + indent_size = "2", |
| 205 | + } |
| 206 | + }, |
| 207 | + diagnostics = { |
| 208 | + -- Get the language server to recognize the `vim` global |
| 209 | + globals = { 'vim' }, |
| 210 | + }, |
| 211 | + workspace = { |
| 212 | + -- Make the server aware of Neovim runtime files |
| 213 | + library = vim.api.nvim_get_runtime_file("", true), |
| 214 | + }, |
| 215 | + -- Do not send telemetry data containing a randomized but unique identifier |
| 216 | + telemetry = { |
| 217 | + enable = false, |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | +} |
| 222 | + |
| 223 | +-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#jsonls |
| 224 | +-- npm i -g vscode-langservers-extracted |
| 225 | +require 'lspconfig'.jsonls.setup { |
| 226 | + capabilities = capabilities, |
| 227 | + on_attach = on_attach, |
| 228 | +} |
| 229 | + |
| 230 | +cmp.setup { |
| 231 | + -- ... rest of your cmp setup ... |
| 232 | + sorting = { |
| 233 | + comparators = { |
| 234 | + cmp.config.compare.offset, |
| 235 | + cmp.config.compare.exact, |
| 236 | + cmp.config.compare.recently_used, |
| 237 | + require("clangd_extensions.cmp_scores"), |
| 238 | + cmp.config.compare.kind, |
| 239 | + cmp.config.compare.sort_text, |
| 240 | + cmp.config.compare.length, |
| 241 | + cmp.config.compare.order, |
| 242 | + }, |
| 243 | + }, |
| 244 | +} |
0 commit comments