Advertisement
TornaxO7

Untitled

Nov 30th, 2022
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | Software | 0 0
  1. Hm... I don't have any ideas how to use CoC, so here's my guide on how to setup it with `nvim-lspconfig`:
  2. If I'm seeing it right, you've already setup the lsp. All you wan now is
  3. basically just auto-completion, right?
  4.  
  5. You need an auto-completion plugin for this! `nvim-lspconfig` just simplifies
  6. the lsp-configuration for you. You can see a list of auto-completion plugins [here](https://github.com/rockerBOO/awesome-neovim#completion).
  7.  
  8. To simplify things I provide a quick guide how I set it up:
  9. 1. Prepare some directories first (I like to separate the settings for each
  10. plugin):
  11. ```bash
  12. mkdir -p ~/.config/nvim/lua/nvim_cmp_settings/
  13. mkdir -p ~/.config/nvim/lua/luasnip_settings/
  14. ```
  15.  
  16. 2. Add the following to your [plugin list](https://github.com/hungLink/dotfiles-debian/blob/master/.config/nvim/lua/plugins.lua#L15-L20):
  17. ```lua
  18. -- the auto-completion plugin
  19. use({
  20. "hrsh7th/nvim-cmp",
  21. config = function()
  22. require("nvim_cmp_settings.settings")
  23. end,
  24. requires = {
  25. "hrsh7th/cmp-buffer",
  26. "hrsh7th/cmp-nvim-lsp",
  27. "hrsh7th/cmp-path",
  28. "hrsh7th/cmp-nvim-lua",
  29. "saadparwaiz1/cmp_luasnip",
  30. "onsails/lspkind-nvim",
  31. "hrsh7th/cmp-nvim-lsp-signature-help",
  32. "lukas-reineke/cmp-rg",
  33. },
  34. })
  35.  
  36. -- the snippet plugin. You can choose another snippet plugin if you want.
  37. -- Here's a list of them: https://github.com/rockerBOO/awesome-neovim#snippet
  38. use({
  39. "L3MON4D3/LuaSnip",
  40. config = function()
  41. require("luasnip_settings.settings")
  42. end,
  43. })
  44. ```
  45.  
  46. 3. Execute inside neovim `:PackerSync` wait until it finished and exit it
  47. afterwards.
  48.  
  49. 4. Put the following into `~/.config/nvim/lua/nvim_cmp_settings/settings.lua`:
  50. ```lua
  51. local cmp = require("cmp")
  52. local lspkind = require("lspkind")
  53. local luasnip = require("luasnip")
  54.  
  55. local has_words_before = function()
  56. local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  57. return col ~= 0 and
  58. vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col,
  59. col)
  60. :match("%s") == nil
  61. end
  62.  
  63. cmp.setup({
  64. preselect = cmp.PreselectMode.None,
  65. snippet = {
  66. expand = function(args) require('luasnip').lsp_expand(args.body) end
  67. },
  68. window = {
  69. completion = cmp.config.window.bordered(),
  70. documentation = cmp.config.window.bordered(),
  71. },
  72. mapping = {
  73. ['<C-Space>'] = cmp.mapping.complete(),
  74. ['<C-c>'] = cmp.mapping.close(),
  75. ['<CR>'] = cmp.mapping.confirm({select = false}),
  76. ["<Tab>"] = cmp.mapping(function(fallback)
  77. if cmp.visible() then
  78. cmp.select_next_item()
  79. elseif luasnip.expand_or_jumpable() then
  80. luasnip.expand_or_jump()
  81. elseif has_words_before() then
  82. cmp.complete()
  83. else
  84. fallback()
  85. end
  86. end, {"i", "s"}),
  87. ["<S-Tab>"] = cmp.mapping(function(fallback)
  88. if cmp.visible() then
  89. cmp.select_prev_item()
  90. elseif luasnip.jumpable(-1) then
  91. luasnip.jump(-1)
  92. else
  93. fallback()
  94. end
  95. end, { "i", "s" }),
  96. },
  97. formatting = {
  98. format = lspkind.cmp_format({
  99. with_text = false,
  100. maxwidth = 50,
  101.  
  102. -- The function below will be called before any actual modifications from lspkind
  103. -- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30))
  104. before = function(entry, vim_item) return vim_item end
  105. })
  106. },
  107. sources = {
  108. -- {name = "rg", prority = 8},
  109. {name = 'nvim_lsp', priority = 10},
  110. {name = 'path', priority = 2},
  111. {name = 'buffer', priority = 4},
  112. {name = 'luasnip', priority = 3},
  113. -- {name = 'nvim_lsp_signature_help', priority = 11}
  114. },
  115. })
  116. ```
  117.  
  118. 5. Put the following into `~/.config/nvim/lua/luasnip_settings/settings.lua`
  119. ```lua
  120. local function prequire(...)
  121. local status, lib = pcall(require, ...)
  122. if (status) then return lib end
  123. return nil
  124. end
  125.  
  126. local luasnip = prequire('luasnip')
  127. local cmp = prequire("cmp")
  128.  
  129. local t = function(str)
  130. return vim.api.nvim_replace_termcodes(str, true, true, true)
  131. end
  132.  
  133. local check_back_space = function()
  134. local col = vim.fn.col('.') - 1
  135. if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
  136. return true
  137. else
  138. return false
  139. end
  140. end
  141.  
  142. _G.tab_complete = function()
  143. if cmp and cmp.visible() then
  144. cmp.select_next_item()
  145. elseif luasnip and luasnip.expand_or_jumpable() then
  146. return t("<Plug>luasnip-expand-or-jump")
  147. elseif check_back_space() then
  148. return t "<Tab>"
  149. else
  150. cmp.complete()
  151. end
  152. return ""
  153. end
  154. _G.s_tab_complete = function()
  155. if cmp and cmp.visible() then
  156. cmp.select_prev_item()
  157. elseif luasnip and luasnip.jumpable(-1) then
  158. return t("<Plug>luasnip-jump-prev")
  159. else
  160. return t "<S-Tab>"
  161. end
  162. return ""
  163. end
  164.  
  165. vim.api.nvim_set_keymap("i", "<Tab>", "v:lua.tab_complete()", {expr = true})
  166. vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", {expr = true})
  167. vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
  168. vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
  169. vim.api.nvim_set_keymap("i", "<C-E>", "<Plug>luasnip-next-choice", {})
  170. vim.api.nvim_set_keymap("s", "<C-E>", "<Plug>luasnip-next-choice", {})
  171.  
  172. require("luasnip.loaders.from_lua").lazy_load({
  173. paths = "./lua/luasnip_settings/snippets"
  174. })
  175. ```
  176.  
  177. Now you should have autocompletion.
  178. If it's too much for you, there's also a list of
  179. [neovim-"distributions"](https://github.com/rockerBOO/awesome-neovim#preconfigured-configuration)
  180. which basically preconfigured a lot for you. (Some of my friends are praising
  181. [LunarVim](https://github.com/LunarVim/LunarVim))
  182. They are mostly giving you an out-of-the-box experience as in VSCode and other
  183. editors/IDEs.
  184.  
  185. Maybe [my configs](https://github.com/TornaxO7/my_configs/tree/master/nvim) can
  186. inspire you at some points?
  187.  
Tags: nvim
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement