From ac27fcf4df1751d77b872aaf1d29a960b87c3a5a Mon Sep 17 00:00:00 2001 From: Radvil Date: Fri, 22 Mar 2024 21:22:18 +0800 Subject: [PATCH] feat(extras): Add extra for angular lspconfig Since treesitter has better support for angular parser recently that works for the legacy and the latest version of it, I am happy to suggest this extra configuration that works for well for me without any issue. And since lack of references about how to config the angular lsp inside of neovim out there, this will work fine as a starting point. --- lua/lazyvim/plugins/extras/lang/angular.lua | 79 +++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lua/lazyvim/plugins/extras/lang/angular.lua diff --git a/lua/lazyvim/plugins/extras/lang/angular.lua b/lua/lazyvim/plugins/extras/lang/angular.lua new file mode 100644 index 00000000..fca2ab3c --- /dev/null +++ b/lua/lazyvim/plugins/extras/lang/angular.lua @@ -0,0 +1,79 @@ +local goto_template_file = function() + local params = vim.lsp.util.make_position_params(0) + vim.lsp.buf_request(0, "angular/getTemplateLocationForComponent", params, function(_, result) + if result then + vim.lsp.util.jump_to_location(result, "utf-8") + end + end) +end + +local goto_component_file = function() + local params = vim.lsp.util.make_position_params(0) + vim.lsp.buf_request(0, "angular/getComponentsWithTemplateFile", params, function(_, result) + if result then + if #result == 1 then + vim.lsp.util.jump_to_location(result[1], "utf-8") + else + vim.fn.setqflist({}, " ", { + title = "Angular Language Server", + items = vim.lsp.util.locations_to_items(result, "utf-8"), + }) + vim.cmd.copen() + end + end + end) +end + +---@param id string +---@param cmd string | function +---@param desc string +local create_cmd = function(id, cmd, desc) + vim.api.nvim_create_user_command(string.format("Ng%s", id), cmd, { + desc = string.format("Angular ยป %s", desc), + }) +end + +return { + { + "nvim-treesitter", + optional = true, + opts = function(_, opts) + if type(opts.ensure_installed) == "table" then + vim.list_extend(opts.ensure_installed, { "angular" }) + end + end, + }, + + { + "nvim-lspconfig", + opts = { + servers = { + angularls = { + root_dir = function(fname) + local util = require("lspconfig").util + local original = util.root_pattern("angular.json", "project.json")(fname) + -- set fallback for nx workspace support + local nx_fallback = util.root_pattern("nx.json", "workspace.json")(fname) + local git_fallback = util.root_pattern(".git")(fname) + return original or nx_fallback or git_fallback + end, + }, + }, + setup = { + angularls = function() + require("lazyvim.util").lsp.on_attach(function(client) + if client.name == "angularls" then + -- disable lsp rename to not conflicting with tsserver + client.server_capabilities.renameProvider = false + -- run :Ngc to go to the component of the current template buffer + create_cmd("c", goto_component_file, "Go to component file") + -- run :Ngt to go to the template of the current component + create_cmd("t", goto_template_file, "Go to template file") + end + end) + return false + end, + }, + }, + }, +}