Skip to content

Instantly share code, notes, and snippets.

@Madoshakalaka
Last active November 28, 2023 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Madoshakalaka/cda4cfc5ac3c0088eb4612e5e85143d9 to your computer and use it in GitHub Desktop.
Save Madoshakalaka/cda4cfc5ac3c0088eb4612e5e85143d9 to your computer and use it in GitHub Desktop.
setting up project-specific feature flags for lsp in neovim (and also html and css completion in rust)

disclaimer: the completion setup is for vsnip but I think you can easily adapt it to other completion engines.

so the nice thing about my setup is that it only sets up html suggestion for specific projects using the power of excr files (.nvim.lua in this case). (it also works for CSS suggestions if you are into css-in-rust stuff)

First you want to have this in your lua config to enable projet-specific config files.

vim.o.exrc = true

Then create ~/.config/nvim/lua/lspatch.lua (it means LSP patch)

-- All glory to this guy, 我的一字之师:
-- https://muniftanjim.dev/blog/neovim-project-local-config-with-exrc-nvim/


local mod = {}

---@param server_name string
---@param settings_patcher fun(settings: table): table
function mod.patch_lsp_settings(server_name, settings_patcher)
  local function patch_settings(client)
    client.config.settings = settings_patcher(client.config.settings)
    client.notify("workspace/didChangeConfiguration", {
      settings = client.config.settings,
    })
  end

  local clients = vim.lsp.get_active_clients({ name = server_name })
  if #clients > 0 then
    patch_settings(clients[1])
    return
  end

  vim.api.nvim_create_autocmd("LspAttach", {
    callback = function(args)
      local client = vim.lsp.get_client_by_id(args.data.client_id)
      if client.name == server_name then
        patch_settings(client)
        return true
      end
    end,
  })
end

return mod

Then in the project directory, add .nvim.lua with this:

-- again, this is for projet-specific feature flags, ignore this if not interested
require("lspatch").patch_lsp_settings("rust_analyzer", function(settings)

	-- somehow both are necessary, one affects syntax highlighting, the other diagnostics it seems
	settings["rust-analyzer"] = {
		check = {
		features = { "you-crate-feature", }
	},
	cargo = {
		features = { "you-crate-feature", }
}

}
  return settings
end)

-- this is for the completion
vim.g.vsnip_filetypes = {
  rust = {'html', 'css'},
}

the extra nice thing about .nvim.lua is that you can also create projet-specific commands in it, like the commands to build/compile your project etc...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment