Lsp stuff

Setup lsp server

  • Skim through lspconfig repo to get a general overview of how the config looks/works.

  • Then check server_configurations.md to make sure your language’s lsp server is present there.

  • Create a file in your custom folder. (I would suggest creating plugins dir in custom folder , so /custom/plugins/lspconfig.lua is the config file for this example).

  • Enter the file path in the chadrc’s lspconfig section :

  1. M.plugins = {
  2. options = {
  3. lspconfig = {
  4. setup_lspconf = "custom.plugins.lspconfig",
  5. },
  6. },
  7. }
  8. -- so setup_lspconf = any file but that should be in custom dir!
  • The following file is an example lspconfig file
  1. local M = {}
  2. M.setup_lsp = function(attach, capabilities)
  3. local lspconfig = require "lspconfig"
  4. -- lspservers with default config
  5. local servers = { "html", "cssls", "clangd" }
  6. for _, lsp in ipairs(servers) do
  7. lspconfig[lsp].setup {
  8. on_attach = attach,
  9. capabilities = capabilities,
  10. }
  11. end
  12. end
  13. return M

Overriding on_attach

You can override the default on_attach for example to change the server capabilities:

  1. M.setup_lsp = function(attach, capabilities)
  2. -- [...]
  3. for _, lsp in ipairs(servers) do
  4. lspconfig[lsp].setup {
  5. on_attach = function(client, bufnr)
  6. attach(client, bufnr)
  7. -- change gopls server capabilities
  8. if lsp == "gopls" then
  9. client.resolved_capabilities.document_formatting = true
  10. client.resolved_capabilities.document_range_formatting = true
  11. end
  12. end,
  13. capabilities = capabilities,
  14. }
  15. end
  16. end

Make sure you pass bufnr to the attach function.

lsp-installer

  1. :LspInstall clangd
  • We’ve enabled automatic installation in lsp-installer config, meaning that you dont have to run LspInstall anymore. Any lsp server you configure in your custom lspconfig file, lsp-installer will detect it and install the lsp server automatically!