| local util = require("lspconfig.util") | |
| local utils = require("lsp.utils") | |
| 
 | |
| -- Determine base path for node_modules | |
| local base_path = utils.find_node_modules_path() | |
| 
 | |
| if not base_path then | |
|     vim.notify("No global node_modules found for TypeScript server", vim.log.levels.WARN) | |
|     base_path = "" | |
| end | |
| 
 | |
| local function get_typescript_server_path(root_dir) | |
|     local global_ts = base_path .. "/typescript/lib" | |
|     local found_ts = "" | |
| 
 | |
|     local function check_dir(path) | |
|         found_ts = table.concat({ path, "typescript", "lib" }, "/") | |
|         if vim.loop.fs_stat(found_ts) then | |
|             return path | |
|         end | |
|     end | |
| 
 | |
|     -- Check frontend/node_modules first | |
|     if util.search_ancestors(root_dir .. '/frontend/node_modules', check_dir) then | |
|         return found_ts | |
|     end | |
| 
 | |
|     -- Check root node_modules | |
|     if util.search_ancestors(root_dir .. '/node_modules', check_dir) then | |
|         return found_ts | |
|     end | |
| 
 | |
|     return global_ts | |
| end | |
| 
 | |
| local function get_tsserver_cmd(root_dir) | |
|     -- Check for local typescript-language-server in node_modules | |
|     local tsserver | |
| 
 | |
|     tsserver = root_dir .. '/node_modules/.bin/typescript-language-server' | |
|     if vim.loop.fs_stat(tsserver) then | |
|         return { tsserver, '--stdio' } | |
|     end | |
| 
 | |
|     -- Check for local typescript-language-server in node_modules | |
|     tsserver = root_dir .. '/node_modules/.bin/tsserver' | |
|     if vim.loop.fs_stat(tsserver) then | |
|         return { tsserver, '--stdio' } | |
|     end | |
| 
 | |
|     -- Check frontend/node_modules | |
|     tsserver = root_dir .. '/frontend/node_modules/.bin/typescript-language-server' | |
|     if vim.loop.fs_stat(tsserver) then | |
|         return { tsserver, '--stdio' } | |
|     end | |
| 
 | |
|     -- Check frontend/node_modules | |
|     tsserver = root_dir .. '/frontend/node_modules/.bin/tsserver' | |
|     if vim.loop.fs_stat(tsserver) then | |
|         return { tsserver, '--stdio' } | |
|     end | |
| 
 | |
|     -- Fallback to Mason | |
|     return { vim.fn.expand('~/.local/share/nvim/mason/bin/typescript-language-server'), '--stdio' } | |
| end | |
| 
 | |
| local function organize_imports() | |
|     local params = { | |
|         command = "_typescript.organizeImports", | |
|         arguments = { vim.api.nvim_buf_get_name(0) }, | |
|         title = "" | |
|     } | |
|     vim.lsp.buf.execute_command(params) | |
| end | |
| 
 | |
| return { | |
|     cmd = { vim.fn.expand('~/.local/share/nvim/mason/bin/typescript-language-server'), '--stdio' }, | |
|     init_options = { | |
|         plugins = { | |
|             { | |
|                 name = "@vue/typescript-plugin", | |
|                 location = base_path .. "/@vue/typescript-plugin", | |
|                 languages = { "javascript", "typescript", "vue", "react" }, | |
|             }, | |
|         }, | |
|     }, | |
|     filetypes = { | |
|         "javascript", | |
|         "typescript", | |
|         "vue", | |
|         "typescriptreact", | |
|     }, | |
|     root_markers = { "package.json", "package-lock.json" }, | |
|     commands = { | |
|         OrganizeImports = { | |
|             organize_imports, | |
|             description = "Organize Imports" | |
|         } | |
|     }, | |
|     before_init = function(initialize_params, config) | |
|         local root_dir = initialize_params.rootPath or initialize_params.rootUri | |
|         if root_dir and root_dir:match("^file://") then | |
|             root_dir = vim.uri_to_fname(root_dir) | |
|         end | |
| 
 | |
|         -- Update cmd | |
|         config.cmd = get_tsserver_cmd(root_dir) | |
| 
 | |
|         -- Update tsdk | |
|         if get_typescript_server_path then | |
|             config.init_options.typescript = config.init_options.typescript or {} | |
|             config.init_options.typescript.tsdk = get_typescript_server_path(root_dir) | |
|         end | |
|     end, | |
| }
 |