aboutsummaryrefslogtreecommitdiffstats
path: root/lsp/angularls.lua
diff options
context:
space:
mode:
authorHyBer <59885141+BBboy01@users.noreply.github.com>2025-04-18 22:53:37 +0800
committerGitHub <noreply@github.com>2025-04-18 07:53:37 -0700
commit4692f3a14f05bbc1f75e887c838c81c07f1ba91f (patch)
tree4dc5e908a6a003200e6e5568715198178b162900 /lsp/angularls.lua
parentdocs: update configs.md (diff)
downloadnvim-lspconfig-4692f3a14f05bbc1f75e887c838c81c07f1ba91f.tar
nvim-lspconfig-4692f3a14f05bbc1f75e887c838c81c07f1ba91f.tar.gz
nvim-lspconfig-4692f3a14f05bbc1f75e887c838c81c07f1ba91f.tar.bz2
nvim-lspconfig-4692f3a14f05bbc1f75e887c838c81c07f1ba91f.tar.lz
nvim-lspconfig-4692f3a14f05bbc1f75e887c838c81c07f1ba91f.tar.xz
nvim-lspconfig-4692f3a14f05bbc1f75e887c838c81c07f1ba91f.tar.zst
nvim-lspconfig-4692f3a14f05bbc1f75e887c838c81c07f1ba91f.zip
feat: angularls #3746
Diffstat (limited to 'lsp/angularls.lua')
-rw-r--r--lsp/angularls.lua87
1 files changed, 87 insertions, 0 deletions
diff --git a/lsp/angularls.lua b/lsp/angularls.lua
new file mode 100644
index 00000000..776affff
--- /dev/null
+++ b/lsp/angularls.lua
@@ -0,0 +1,87 @@
+-- Angular requires a node_modules directory to probe for @angular/language-service and typescript
+-- in order to use your projects configured versions.
+local root_dir = vim.fn.getcwd()
+local project_root = vim.fs.dirname(vim.fs.find('node_modules', { path = root_dir, upward = true })[1])
+
+local function get_probe_dir()
+ return project_root and (project_root .. '/node_modules') or ''
+end
+
+local function get_angular_core_version()
+ 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()
+local default_angular_core_version = get_angular_core_version()
+
+-- structure should be like
+-- - $EXTENSION_PATH
+-- - @angular
+-- - language-server
+-- - bin
+-- - ngserver
+-- - typescript
+local ngserver_path = vim.fs.dirname(vim.uv.fs_realpath(vim.fn.exepath('ngserver')))
+local extension_path = vim.fs.normalize(vim.fs.joinpath(ngserver_path, '../../../'))
+
+-- angularls will get module by `require.resolve(PROBE_PATH, MODULE_NAME)` of nodejs
+local ts_probe_dirs = vim.iter({ extension_path, default_probe_dir }):join(',')
+local ng_probe_dirs = vim
+ .iter({ extension_path, default_probe_dir })
+ :map(function(p)
+ return vim.fs.joinpath(p, '/@angular/language-server/node_modules')
+ end)
+ :join(',')
+
+---@brief
+---
+-- https://github.com/angular/vscode-ng-language-service
+-- `angular-language-server` can be installed via npm `npm install -g @angular/language-server`.
+--
+-- Note, that if you override the default `cmd`, you must also update `on_new_config` to set `new_config.cmd` during startup.
+--
+-- ```lua
+-- local project_library_path = "/path/to/project/lib"
+-- local cmd = {"ngserver", "--stdio", "--tsProbeLocations", project_library_path , "--ngProbeLocations", project_library_path}
+--
+-- require'lspconfig'.angularls.setup{
+-- cmd = cmd,
+-- on_new_config = function(new_config,new_root_dir)
+-- new_config.cmd = cmd
+-- end,
+-- }
+-- ```
+
+return {
+ cmd = {
+ 'ngserver',
+ '--stdio',
+ '--tsProbeLocations',
+ ts_probe_dirs,
+ '--ngProbeLocations',
+ ng_probe_dirs,
+ '--angularCoreVersion',
+ default_angular_core_version,
+ },
+ filetypes = { 'typescript', 'html', 'typescriptreact', 'typescript.tsx', 'htmlangular' },
+ root_markers = { 'angular.json', 'nx.json' },
+}