lsp.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. local lsp = require("lsp-zero")
  2. lsp.preset("recommended")
  3. lsp.ensure_installed({
  4. 'tsserver',
  5. 'eslint',
  6. 'sumneko_lua',
  7. 'rust_analyzer',
  8. 'pylsp',
  9. 'nimls',
  10. 'bashls',
  11. 'jsonls',
  12. 'clangd'
  13. })
  14. local cmp = require('cmp')
  15. local luasnip = require('luasnip')
  16. local cmp_select = {behavior = cmp.SelectBehavior.Select}
  17. local cmp_mappings = lsp.defaults.cmp_mappings({
  18. ['K'] = cmp.mapping.select_prev_item(cmp_select),
  19. ['J'] = cmp.mapping.select_next_item(cmp_select),
  20. ['<C-y>'] = cmp.mapping.confirm({select =true}),
  21. ['<C-Space>'] = cmp.mapping.complete(),
  22. -- the next two mappings are for usin Tab and S-Tab for
  23. -- navigating inside of snippets
  24. ['<Tab>'] = cmp.mapping(function(fallback)
  25. if cmp.visible() then
  26. cmp.select_next_item()
  27. elseif luasnip.expand_or_jumpable() then
  28. luasnip.expand_or_jump()
  29. elseif has_words_before() then
  30. cmp.complete()
  31. else
  32. fallback()
  33. end
  34. end, { "i", "s" }),
  35. ["<S-Tab>"] = cmp.mapping(function(fallback)
  36. if cmp.visible() then
  37. cmp.select_prev_item()
  38. elseif luasnip.jumpable(-1) then
  39. luasnip.jump(-1)
  40. else
  41. fallback()
  42. end
  43. end, { "i", "s" }),
  44. })
  45. lsp.set_preferences({
  46. sign_icons = {}
  47. })
  48. lsp.on_attach(function(client, bufnr)
  49. local opts = {buffer = bufnr, remap = false}
  50. vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
  51. vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
  52. vim.keymap.set("n", "gt", function() vim.lsp.buf.type_definition() end, opts)
  53. vim.keymap.set("n", "gi", function() vim.lsp.buf.implementation() end, opts)
  54. vim.keymap.set("n", "<leader>r", function() vim.lsp.buf.rename() end, opts)
  55. end)
  56. -- Enable the custom keymappings
  57. lsp.setup_nvim_cmp({
  58. mapping = cmp_mappings
  59. })
  60. lsp.setup()