Looking to confirm my config structure is sound

Hello Everyone,

I’m hoping that someone with a more experience than I with lua/neovim might confirm that my approach to configuring neovim/lsp is sound.

There are essentially 4 phases to the process

  1. usermod settings for handles and capabilities (“global” context)

  2. init.vim that reads in series of configurations in a sequence-dependent-manner

    • first activate plugins that aren’t related to lsp (directly anyway): e.g.,
      require("nvim-treesitter.configs").setup({ ... })
      
    • following by my nvim-lspconfig.lua config file. Here I might have a list of lsps that may or may not have custom setups. They are need access to the usermod settings (see below)
  3. run the lsp setups for tools that provide “lsp+” capabilities

  4. run my legacy .vim configurations and plugin activations

Snippets from my nvim-lspconfig.lua file

  local servers = {
   "cssls",
   "dockerls",
   "docker_compose_language_service",
   ...
}
require("mason-lspconfig").setup({
   ensure_installed = servers,
})

-- create local setup, attach to a lua table
local eslint = { ... }
local sqlls = { ... }

--------------------------------------------------------------------------------
-- activate each lsp in the list
--------------------------------------------------------------------------------
for _, lsp in ipairs(servers) do
    local generic_opts = {
       capabilities = capabilities,
       on_attach = handlers.on_attach,
       flags = { debounce_text_changes = 150 },
    }
    local specialized_opts = option_lookup[lsp]
    if specialized_opts == nil then
       lspconfig[lsp].setup(generic_opts)
    else
       local opts = vim.tbl_deep_extend(
           "force",
           generic_opts,
           specialized_opts
        )
        lspconfig[lsp].setup(opts)
    end
end

The third group configs called by init.vim. Here is one of them.

-- depends on `lspconfig`
--------------------------------------------------------------------------------
-- rust-tools - Augmented lsp languages
--            - includes options sent to rust_analyzer
--------------------------------------------------------------------------------
-- Use this to access the extra information provided by the rust
-- language server (rust_analyzer)
-- tools: see https://github.com/simrat39/rust-tools.nvim#configuration
--------------------------------------------------------------------------------
local status_ok, _ = pcall(require, "lspconfig")
if not status_ok then
    return
end
--------------------------------------------------------------------------------
local lspconfig = require("lspconfig")
--------------------------------------------------------------------------------
local handlers = require("usermod.nvim_handlers")
local capabilities = require("usermod.env")
--------------------------------------------------------------------------------
local util = require("lspconfig/util")

-- rust-tools depends on lspconfig plugin; so presumably sets-up
-- the connection between this setup and the lsp (not the case for tsserver)
require("rust-tools").setup({
    tools = {
        -- rust-tools options
        autoSetHints = true,
        inlay_hints = {
            ...
        },
    },
    -- all the options to send to nvim-lspconfig
    -- these override the defaults set by rust-tools.nvim
    -- see https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#rust_analyzer
    server = {
        -- The callback following when the language server attaches to the buffer
        on_attach = handlers.on_attach, -- RHS set in usermod
        capabilities = capabilities, -- RHS set in usermod
        settings = {
            ["rust-analyzer"] = { 
                    ...
                },
            },
        },
    },
})

Finally, I’m also using the $HOME/.config/nvim/after/ftplugin/<filetype>.lua approach to host the file-type specific configs outside the lsp-related content.

Am I doing what I think I’m doing? In other words, correctly connecting the dots :)) Comments and suggestions are welcome.

Thank you!

1 Like