|
12 | 12 | --- "ts=typescript" |
13 | 13 | --- } |
14 | 14 | --- ``` |
| 15 | +--- |
| 16 | +--- Some care must be taken here to correctly infer whether a file is part of a Deno program, or a TS program that |
| 17 | +--- expects to run in Node or Web Browsers. This supports having a Deno module that is a part of a mostly-not-Deno |
| 18 | +--- monorepo. We do this by finding the nearest package manager lock file, and the nearest deno.json or deno.jsonc. |
| 19 | +--- Note that this means that without a deno.json, deno.jsonc, or deno.lock file, this LSP client will not attach. |
| 20 | +--- |
| 21 | +--- Example: |
| 22 | +--- |
| 23 | +--- ``` |
| 24 | +--- project-root |
| 25 | +--- +-- node_modules/... |
| 26 | +--- +-- package-lock.json |
| 27 | +--- +-- package.json |
| 28 | +--- +-- packages |
| 29 | +--- +-- deno-module |
| 30 | +--- | +-- deno.json |
| 31 | +--- | +-- package.json <-- It's normal for Deno projects to have package.json files! |
| 32 | +--- | +-- src |
| 33 | +--- | +-- index.ts <-- this is a Deno file |
| 34 | +--- +-- node-module |
| 35 | +--- +-- package.json |
| 36 | +--- +-- src |
| 37 | +--- +-- index.ts <-- a non-Deno file (ie, should use ts_ls or tsgo) |
| 38 | +--- ``` |
| 39 | +--- |
| 40 | +--- From the file being edited, we walk up to find the nearest package manager lockfile. This is PROJECT ROOT. |
| 41 | +--- From the file being edited, find the nearest deno.json or deno.jsonc. This is DENO ROOT. |
| 42 | +--- From the file being edited, find the nearest deno.lock. This is DENO LOCK ROOT |
| 43 | +--- If DENO LOCK ROOT is found, and PROJECT ROOT is missing or shorter, then this is a deno file, and we attach. |
| 44 | +--- If DENO ROOT is found, and it's longer than or equal to PROJECT ROOT, then this is a Deno file, and we attach. |
| 45 | +--- Otherwise, we abort, because this is a non-Deno TS file. |
15 | 46 |
|
16 | 47 | local lsp = vim.lsp |
17 | 48 |
|
@@ -77,21 +108,23 @@ return { |
77 | 108 | }, |
78 | 109 | root_dir = function(bufnr, on_dir) |
79 | 110 | -- The project root is where the LSP can be started from |
80 | | - local root_markers = { 'deno.lock' } |
| 111 | + local root_markers = { 'deno.lock', 'deno.json', 'deno.jsonc' } |
81 | 112 | -- Give the root markers equal priority by wrapping them in a table |
82 | 113 | root_markers = vim.fn.has('nvim-0.11.3') == 1 and { root_markers, { '.git' } } |
83 | 114 | or vim.list_extend(root_markers, { '.git' }) |
84 | | - -- exclude non-deno projects (npm, yarn, pnpm, bun) |
85 | | - local non_deno_path = vim.fs.root( |
86 | | - bufnr, |
87 | | - { 'package.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb', 'bun.lock' } |
88 | | - ) |
| 115 | + -- only include deno projects |
| 116 | + local deno_root = vim.fs.root(bufnr, { 'deno.json', 'deno.jsonc' }) |
| 117 | + local deno_lock_root = vim.fs.root(bufnr, { 'deno.lock' }) |
89 | 118 | local project_root = vim.fs.root(bufnr, root_markers) |
90 | | - if non_deno_path and (not project_root or #non_deno_path >= #project_root) then |
91 | | - return |
| 119 | + if |
| 120 | + (deno_lock_root and (not project_root or #deno_lock_root > #project_root)) |
| 121 | + or (deno_root and (not project_root or #deno_root >= #project_root)) |
| 122 | + then |
| 123 | + -- deno config is closer than or equal to package manager lock, |
| 124 | + -- or deno lock is closer than package manager lock. Attach at the project root, |
| 125 | + -- or deno lock or deno config path. At least one of these is always set at this point. |
| 126 | + on_dir(project_root or deno_lock_root or deno_root) |
92 | 127 | end |
93 | | - -- We fallback to the current working directory if no project root is found |
94 | | - on_dir(project_root or vim.fn.getcwd()) |
95 | 128 | end, |
96 | 129 | settings = { |
97 | 130 | deno = { |
|
0 commit comments