|
| 1 | +--- @brief |
| 2 | +--- Utility for detecting the root directory of a TypeScript project |
| 3 | + |
| 4 | +local M = {} |
| 5 | + |
| 6 | +--- @class lspconfig.typescript.Project |
| 7 | +--- @field kind '"node"' | '"deno"' | '"bun"' |
| 8 | +--- @field root_dir string |
| 9 | + |
| 10 | +--- @class lspconfig.typescript.Package |
| 11 | +--- @field kind '"non-deno"' | '"deno"' |
| 12 | +--- @field path string |
| 13 | + |
| 14 | +--- @class lspconfig.typescript.Workspace |
| 15 | +--- @field kind '"node"' | '"deno"' | '"bun"' |
| 16 | +--- @field root_dir string |
| 17 | + |
| 18 | +--- Detect the TypeScript project in the current working directory. |
| 19 | +--- @param source (integer | string) |
| 20 | +--- @return lspconfig.typescript.Project? |
| 21 | +function M.detect_project(source) |
| 22 | + -- The search scope limit is the closer of either the current directory or the closest Git repository directly above the file. |
| 23 | + -- This prevents false positives if files like package-lock.json exist in the home directory. |
| 24 | + local cwd = vim.fn.getcwd() |
| 25 | + local git_root = vim.fs.root(source, '.git') |
| 26 | + local root = (git_root and #git_root >= #cwd) and git_root or cwd |
| 27 | + |
| 28 | + -- First, we look for configuration files that indicate the presence of a specific runtime under the root. |
| 29 | + |
| 30 | + --- @type lspconfig.typescript.Package[] |
| 31 | + local packages = {} |
| 32 | + |
| 33 | + local non_deno_package = vim.fs.root(source, { 'package.json' }) |
| 34 | + if non_deno_package and (#non_deno_package >= #root) then |
| 35 | + table.insert(packages, { kind = 'non-deno', path = non_deno_package }) |
| 36 | + end |
| 37 | + |
| 38 | + local deno_package = vim.fs.root(source, { 'deno.json', 'deno.jsonc' }) |
| 39 | + if deno_package and (#deno_package >= #root) then |
| 40 | + table.insert(packages, { kind = 'deno', path = deno_package }) |
| 41 | + end |
| 42 | + |
| 43 | + table.sort(packages, function(a, b) |
| 44 | + return #a.path > #b.path |
| 45 | + end) |
| 46 | + |
| 47 | + --- @type lspconfig.typescript.Package? |
| 48 | + local closest_package = (#packages == 1 or (#packages > 1 and #packages[1].path > #packages[2].path)) and packages[1] |
| 49 | + or nil |
| 50 | + |
| 51 | + if not closest_package then |
| 52 | + -- If no package is found, there is no useful information regarding the project. |
| 53 | + return nil |
| 54 | + end |
| 55 | + |
| 56 | + -- Second, we look for a lock file that can reliably distinguish between runtime kind. |
| 57 | + |
| 58 | + --- @type lspconfig.typescript.Workspace[] |
| 59 | + local workspaces = {} |
| 60 | + |
| 61 | + local node_workspace_root = vim.fs.root(source, { 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml' }) |
| 62 | + if node_workspace_root and (#root <= #node_workspace_root) and (#node_workspace_root <= #closest_package.path) then |
| 63 | + table.insert(workspaces, { kind = 'node', root_dir = node_workspace_root }) |
| 64 | + end |
| 65 | + |
| 66 | + local deno_workspace_root = vim.fs.root(source, { 'deno.lock' }) |
| 67 | + if deno_workspace_root and (#root <= #deno_workspace_root) and (#deno_workspace_root <= #closest_package.path) then |
| 68 | + table.insert(workspaces, { kind = 'deno', root_dir = deno_workspace_root }) |
| 69 | + end |
| 70 | + |
| 71 | + local bun_workspace_root = vim.fs.root(source, { 'bun.lock', 'bun.lockb' }) |
| 72 | + if bun_workspace_root and (#root <= #bun_workspace_root) and (#bun_workspace_root <= #closest_package.path) then |
| 73 | + table.insert(workspaces, { kind = 'bun', root_dir = bun_workspace_root }) |
| 74 | + end |
| 75 | + |
| 76 | + -- If there is only one directory that is closest, this is the workspace we should find. |
| 77 | + -- If there are multiple workspaces pointing to the same directory, or if no workspaces are found, |
| 78 | + -- `detected_workspace` will be nil as there is no useful information regarding the workspace. |
| 79 | + |
| 80 | + table.sort(workspaces, function(a, b) |
| 81 | + return #a.root_dir > #b.root_dir |
| 82 | + end) |
| 83 | + |
| 84 | + --- @type lspconfig.typescript.Workspace? |
| 85 | + local detected_workspace = ( |
| 86 | + #workspaces == 1 or (#workspaces > 1 and #workspaces[1].root_dir > #workspaces[2].root_dir) |
| 87 | + ) |
| 88 | + and workspaces[1] |
| 89 | + or nil |
| 90 | + |
| 91 | + -- If no workspace is detected, the closest package directly above the file will be detected. |
| 92 | + if not detected_workspace then |
| 93 | + if closest_package.kind == 'deno' then |
| 94 | + return { kind = 'deno', root_dir = closest_package.path } |
| 95 | + else |
| 96 | + return { kind = 'node', root_dir = closest_package.path } |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + -- If a non-Deno workspace is detected: |
| 101 | + -- - If the immediate parent package is non-Deno, it is considered a package within that workspace, and root_dir is set to the workspace root. |
| 102 | + -- - If the immediate parent package is Deno, it is considered a single Deno project located under an unrelated non-Deno workspace. |
| 103 | + if detected_workspace.kind == 'node' or detected_workspace.kind == 'bun' then |
| 104 | + if closest_package.kind == 'non-deno' then |
| 105 | + return { kind = detected_workspace.kind, root_dir = detected_workspace.root_dir } |
| 106 | + else |
| 107 | + return { kind = 'deno', root_dir = closest_package.path } |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + -- If a Deno workspace is detected: |
| 112 | + -- A package.json file within a Deno workspace cannot be used as a determining factor, |
| 113 | + -- as it might be leveraging Deno's first-class package.json support. |
| 114 | + -- Therefore, it is immediately considered a package within a Deno workspace. |
| 115 | + -- See: https://docs.deno.com/runtime/fundamentals/node/#first-class-package.json-support |
| 116 | + if detected_workspace.kind == 'deno' then |
| 117 | + return { kind = 'deno', root_dir = detected_workspace.root_dir } |
| 118 | + end |
| 119 | + |
| 120 | + return nil |
| 121 | +end |
| 122 | + |
| 123 | +return M |
0 commit comments