A log about Angular LSP configuration inside Nx monorepo, for neovim
Configured neovim to properly use Angular LSP even inside neovim with Nx monorepo. Configuration is in lua.
nvim-lspconfig activates Angular Language Server only when it can detect where it’s root directory is.
In it’s default configuration it will start from the current buffer location, and go up a level until it finds
directory that contains angular.json file. Once it finds it - it sets that as the root directory of our language server and activates it.
Problem with using Nx monorepo is that it does not have angular.json file but instead uses project.json file.
To make both options viable I had to configure angularls root_dir property.
In my specific configuration that meant the following:
-- user/lsp/settings/angularls.lua, configuration override file for angularls
local util = require("lspconfig.util") -- util is from nvim-lspconfig plugin
return {
root_dir = util.root_pattern("project.json", "angular.json")
}
That file would then get picked up by my mason.nvim configuration file while loading language servers:
local servers = {
"angularls"
}
for _, server in pairs(servers) do
opts = {
on_attach = require("user.lsp.handlers").on_attach,
capabilities = require("user.lsp.handlers").capabilities
}
server = vim.split(server, "@")[1]
local require_ok, conf_opts = pcall(require, "user.lsp.settings." .. server)
if require_ok then
-- here we merge configuration from previous snippet with default
-- configuration for each language server
opts = vim.tbl_deep_extend("force", conf_opts, opts)
end
lspconfig[server].setup(opts)
end
Relevant tech: