From cc53010b2ec8e9dd4a69dd8fa6134a02eedf942b Mon Sep 17 00:00:00 2001 From: Andre Schaf Date: Thu, 12 Jun 2025 12:01:00 +0200 Subject: [PATCH] add memory limit to angular node lsp process --- lua/lspconf/angular.lua | 58 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/lua/lspconf/angular.lua b/lua/lspconf/angular.lua index fb86039..193d635 100644 --- a/lua/lspconf/angular.lua +++ b/lua/lspconf/angular.lua @@ -1,8 +1,63 @@ local lspconfig = require 'lspconfig' +local configs = require 'lspconfig.configs' + +-- Angular requires a node_modules directory to probe for @angular/language-service and typescript +-- in order to use your projects configured versions. +-- This defaults to the vim cwd, but will get overwritten by the resolved root of the file. +local function get_probe_dir(root_dir) + local project_root = vim.fs.dirname(vim.fs.find('node_modules', { path = root_dir, upward = true })[1]) + + return project_root and (project_root .. '/node_modules') or '' +end + +local function get_angular_core_version(root_dir) + local project_root = vim.fs.dirname(vim.fs.find('node_modules', { path = root_dir, upward = true })[1]) + + if not project_root then + return '' + end + + local package_json = project_root .. '/package.json' + if not vim.uv.fs_stat(package_json) then + return '' + end + + local contents = io.open(package_json):read '*a' + local json = vim.json.decode(contents) + if not json.dependencies then + return '' + end + + local angular_core_version = json.dependencies['@angular/core'] + + angular_core_version = angular_core_version and angular_core_version:match('%d+%.%d+%.%d+') + + return angular_core_version +end + +local default_probe_dir = get_probe_dir(vim.fn.getcwd()) +local default_angular_core_version = get_angular_core_version(vim.fn.getcwd()) + +local cmd = { + 'node', + '--max-old-space-size=8192', + vim.fn.exepath('ngserver'), + '--stdio', + '--tsProbeLocations', + default_probe_dir, + '--ngProbeLocations', + default_probe_dir, + '--angularCoreVersion', + default_angular_core_version, +} lspconfig.angularls.setup({ + cmd = cmd, + on_new_config = function(new_config,new_root_dir) + new_config.cmd = cmd + end, on_attach = function() - for _, server in ipairs(vim.lsp.buf_get_clients()) do + for _, server in ipairs(vim.lsp.get_clients()) do if server.name == "tsserver" then local tsserver = vim.lsp.get_client_by_id(server.id) tsserver.server_capabilities.renameProvider = false @@ -10,3 +65,4 @@ lspconfig.angularls.setup({ end end }) +