Advertisement
Guest User

Untitled

a guest
Nov 5th, 2021
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.46 KB | None | 0 0
  1. -- vim:fileencoding=utf-8:ft=lua:foldmethod=marker
  2.  
  3. -- TODO: explore codelens support
  4. -- See: https://github.com/neovim/neovim/pull/13165
  5.  
  6. local lspconfig = require "lspconfig"
  7.  
  8. -- {{{ on_attach
  9. local on_attach = function(client, bufnr)
  10.   require("lsp_signature").on_attach() -- NOTE: add in lsp client
  11.   require("illuminate").on_attach(client) -- NOTE: add in lsp client
  12.  
  13.   vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
  14.  
  15.   local opts = { noremap = true, silent = true }
  16.   vim.api.nvim_buf_set_keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
  17.   vim.api.nvim_buf_set_keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
  18.   vim.api.nvim_buf_set_keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
  19.   vim.api.nvim_buf_set_keymap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
  20.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
  21.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
  22.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
  23.   vim.api.nvim_buf_set_keymap(
  24.     bufnr,
  25.     "n",
  26.     "<leader>wl",
  27.     "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>",
  28.     opts
  29.   )
  30.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
  31.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
  32.   vim.api.nvim_buf_set_keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
  33.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ca", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
  34.   -- vim.api.nvim_buf_set_keymap(bufnr, 'v', '<leader>ca', '<cmd>lua vim.lsp.buf.range_code_action()<CR>', opts)
  35.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
  36.   vim.api.nvim_buf_set_keymap(bufnr, "n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
  37.   vim.api.nvim_buf_set_keymap(bufnr, "n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
  38.   vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>q", "<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>", opts)
  39.   vim.api.nvim_buf_set_keymap(
  40.     bufnr,
  41.     "n",
  42.     "<leader>so",
  43.     [[<cmd>lua require('telescope.builtin').lsp_document_symbols()<CR>]],
  44.     opts
  45.   )
  46.  
  47.   -- if the language server has formatting capabilities format the buffer
  48.   if client.resolved_capabilities.document_formatting then
  49.     -- vim.lsp.buf.formatting() -- asynchronous formatting
  50.     -- vim.lsp.buf.formatting_sync() -- synchronous formatting, better to avoid desync problems
  51.     vim.cmd "autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()"
  52.   end
  53. end
  54. -- }}}
  55.  
  56. -- {{{ Available servers
  57. local servers = {
  58.   "sumneko_lua",
  59.   "tsserver", -- javascript, jsx, typescript and tsx
  60.   "html",
  61.   "pyright",
  62.   "tailwindcss",
  63.   "pyright",
  64.   "null-ls",
  65.   "bashls",
  66.   "emmet_ls", -- completion is working but it's kinda strange
  67.   "vimls",
  68.   "eslint", -- TODO: learn more about eslint lsp configuration options
  69. }
  70. -- }}}
  71.  
  72. local capabilities = vim.lsp.protocol.make_client_capabilities()
  73.  
  74. -- default config
  75. local function default_config()
  76.   return {
  77.     capabilities = capabilities,
  78.     on_attach = on_attach,
  79.   }
  80. end
  81.  
  82. -- {{{ Enable and configure servers
  83. for _, server in ipairs(servers) do
  84.   local config = default_config()
  85.  
  86.   -- {{{ sumneko_lua
  87.   if server == "sumneko_lua" then
  88.     local system_name
  89.  
  90.     if vim.fn.has "mac" == 1 then
  91.       system_name = "macOS"
  92.     elseif vim.fn.has "unix" == 1 then
  93.       system_name = "Linux"
  94.     elseif vim.fn.has "win32" == 1 then
  95.       system_name = "Windows"
  96.     else
  97.       print "Unsupported system for sumneko"
  98.     end
  99.  
  100.     -- set the path to the sumneko installation
  101.     local sumneko_root_path = vim.fn.getenv "HOME" .. "/.local/share/nvim/nvim-lsp-language-servers/lua-language-server"
  102.     local sumneko_binary = sumneko_root_path .. "/bin/" .. system_name .. "/lua-language-server"
  103.  
  104.     local lua_settings = {
  105.       Lua = {
  106.         runtime = {
  107.           -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
  108.           version = "LuaJIT",
  109.           -- Setup your lua path
  110.           path = vim.split(package.path, ";"),
  111.         },
  112.         diagnostics = {
  113.           -- Get the language server to recognize the `vim` global
  114.           globals = { "vim" },
  115.         },
  116.         workspace = {
  117.           -- Make the server aware of Neovim runtime files
  118.           library = {
  119.             [vim.fn.expand "$VIMRUNTIME/lua"] = true,
  120.             [vim.fn.expand "$VIMRUNTIME/lua/vim/lsp"] = true,
  121.           },
  122.         },
  123.         -- Do not send telemetry data containing a randomized but unique identifier
  124.         telemetry = {
  125.           enable = false,
  126.         },
  127.       },
  128.     }
  129.  
  130.     config.cmd = { sumneko_binary, "-E", sumneko_root_path .. "/main.lua" }
  131.     config.filetypes = { "lua" }
  132.     config.log_level = 2
  133.     config.settings = lua_settings
  134.   end
  135.   -- }}}
  136.  
  137.   -- {{{ tsserver
  138.  
  139.   if server == "tsserver" then
  140.     config.on_attach = function(client, bufnr)
  141.       -- disable default tsserver formatting in favor of formatting via null-ls
  142.       client.resolved_capabilities.document_formatting = false
  143.       client.resolved_capabilities.document_range_formatting = false
  144.  
  145.       local ts_utils = require "nvim-lsp-ts-utils"
  146.  
  147.       -- stylua: ignore
  148.       ts_utils.setup {
  149.         debug = false,
  150.         disable_commands = false,
  151.         enable_import_on_completion = false, -- applicable ONLY when using omnicompletion
  152.                                              -- coq_nvim supports it
  153.                                              -- nvim_cmp supports it too
  154.         -- import all
  155.         import_all_timeout = 5000, -- ms
  156.         import_all_priorities = {
  157.           buffers = 4, -- loaded buffer names
  158.           buffer_content = 3, -- loaded buffer content
  159.           local_files = 2, -- git files or files with relative path markers
  160.           same_file = 1, -- add to existing import statement
  161.         },
  162.         import_all_scan_buffers = 100,
  163.         import_all_select_source = false,
  164.  
  165.         -- eslint
  166.         -- TODO: disable because eslint lsp is being used instead
  167.         eslint_enable_code_actions = false,
  168.         eslint_enable_disable_comments = false,
  169.         eslint_bin = "eslint",
  170.         eslint_enable_diagnostics = false,
  171.         eslint_opts = {},
  172.  
  173.         -- formatting
  174.         enable_formatting = true,
  175.         formatter = "prettierd",
  176.         formatter_opts = {},
  177.  
  178.         -- update imports on file move
  179.         update_imports_on_move = true,
  180.         require_confirmation_on_move = false,
  181.         watch_dir = nil,
  182.  
  183.         -- filter diagnostics
  184.         filter_out_diagnostics_by_severity = {},
  185.         filter_out_diagnostics_by_code = {},
  186.       }
  187.  
  188.       -- required to fix code action ranges and filter diagnostics
  189.       ts_utils.setup_client(client)
  190.  
  191.       -- no default maps, so you may want to define some here
  192.       local opts = { silent = true }
  193.  
  194.       vim.api.nvim_buf_set_keymap(bufnr, "n", "gso", ":TSLspOrganize<CR>", opts)
  195.       vim.api.nvim_buf_set_keymap(bufnr, "n", "gsr", ":TSLspRenameFile<CR>", opts)
  196.       vim.api.nvim_buf_set_keymap(bufnr, "n", "gsi", ":TSLspImportAll<CR>", opts)
  197.     end
  198.   end
  199.   -- }}}
  200.  
  201.   -- {{{ null ls
  202.   if server == "null-ls" then
  203.     local null_ls = require(server)
  204.     local sources = {
  205.       null_ls.builtins.diagnostics.vint, -- viml
  206.       null_ls.builtins.diagnostics.codespell,
  207.       null_ls.builtins.diagnostics.vale, -- TODO: add proselint rules emulated through vale, see https://github.com/jose-elias-alvarez/null-ls.nvim/pull/241
  208.       null_ls.builtins.diagnostics.selene,
  209.       null_ls.builtins.diagnostics.shellcheck,
  210.       -- null_ls.builtins.diagnostics.pkgbuildlint, -- TODO: make pkgbuild shellcheck lint work
  211.       null_ls.builtins.diagnostics.flake8,
  212.  
  213.       null_ls.builtins.formatting.rustywind,
  214.       null_ls.builtins.formatting.stylua,
  215.       -- null_ls.builtins.formatting.shellharden, -- BUG: something is wrong with its formatting
  216.  
  217.       null_ls.builtins.formatting.shfmt.with {
  218.         extra_args = {
  219.           "-i", -- uses n spaces instead of the tab default
  220.           vim.bo.shiftwidth, -- uses shiftwidth as arg to -i,
  221.           "-ci", -- switch cases will be indented
  222.           "-s", -- simplify the code
  223.           "-bn", -- binary ops like && and | may start a line
  224.         },
  225.       },
  226.  
  227.       null_ls.builtins.formatting.prettierd.with {
  228.         filetypes = {
  229.           "html",
  230.           "json",
  231.           "yaml",
  232.           "markdown",
  233.           "javascript",
  234.           "javascriptreact",
  235.           "typescript",
  236.           "typescriptreact",
  237.         },
  238.       },
  239.  
  240.       null_ls.builtins.code_actions.gitsigns,
  241.  
  242.       null_ls.builtins.formatting.codespell,
  243.       -- null_ls.builtins.diagnostics.tidy, -- TODO: add support for it
  244.     }
  245.     null_ls.config {
  246.       sources = sources,
  247.     }
  248.   end
  249.   -- }}}
  250.  
  251.   -- {{{ tailwindcss
  252.  
  253.   if server == "tailwindcss" then
  254.     -- TODO: add color support to tailwindcss server
  255.  
  256.     -- BUG: TailwindCSS server is dumb and freaks out trying to attach to markdown files, why?
  257.     -- BUG: TailwindCSS server tries to attach to almost any project
  258.     -- so root_dir is defined with files that must be present in a tailwind project
  259.     config.root_dir = lspconfig.util.root_pattern("tailwind.config.js", "tailwind.config.ts")
  260.   end
  261.  
  262.   -- }}}
  263.  
  264.   -- {{{ html langserver
  265.  
  266.   if server == "html" then
  267.     -- Enable (broadcasting) snippet capability for completion
  268.     config.capabilities.textDocument.completion.completionItem.snippetSupport = true
  269.  
  270.     -- using npm install -g vscode-langservers-extracted
  271.   end
  272.  
  273.   -- }}}
  274.  
  275.   if packer_plugins["coq_nvim"] and packer_plugins["coq_nvim"].loaded then
  276.     -- generic config for setting up the servers using coq nvim
  277.     local coq = require "coq"
  278.     lspconfig[server].setup(coq.lsp_ensure_capabilities(config))
  279.   elseif packer_plugins["nvim-cmp"] and packer_plugins["nvim-cmp"].loaded then
  280.     -- generic config for setting up the servers using nvim-cmp
  281.     config.capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())
  282.     lspconfig[server].setup(config)
  283.   else -- just regular omnicompletion
  284.     lspconfig[server].setup(config)
  285.   end
  286. end
  287. -- }}}
  288.  
  289. -- {{{ Customizing how diagnostics are displayed
  290. -- See: help on_publish_diagnostics for more advanced customization options.
  291.  
  292. -- TODO: configure show diagnostics on cursor
  293. -- stylua: ignore
  294. vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  295.   -- Alternativaly, diagnostics can be seen using Lspsaga or Trouble.nvim
  296.   virtual_text = false, -- Virtual text can be REALLY distracting
  297.   signs = false, -- shows column signs for diagnostics
  298.   underline = true,
  299.   update_in_insert = false, -- updates diagnosting while on insert mode
  300. })                          -- nvim-ts-autotag, set to 'true' if https://github.com/windwp/nvim-ts-autotag/issues/19 happens
  301.  
  302. -- signs column icons when signs is set to true above
  303. local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " }
  304.  
  305. for type, icon in pairs(signs) do
  306.   local hl = "DiagnosticSign" .. type
  307.   vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
  308. end
  309.  
  310. -- }}}
  311.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement