|
1 |
| -if not vim.fs.joinpath then |
2 |
| - function vim.fs.joinpath(...) |
3 |
| - return (table.concat({ ... }, "/"):gsub("//+", "/")) |
4 |
| - end |
5 |
| -end |
6 |
| - |
7 |
| -local uv = vim.uv or vim.loop |
8 |
| - |
9 | 1 | if not vim.g.workspace then
|
10 |
| - local file = vim.fs.find(function(name) |
11 |
| - return vim.fn.fnamemodify(name, ":e") == "code-workspace" |
12 |
| - end, { upward = true }) |
| 2 | + local file = vim.fs.find(function(name) |
| 3 | + return vim.fn.fnamemodify(name, ":e") == "code-workspace" |
| 4 | + end, { upward = true }) |
13 | 5 |
|
14 |
| - if file[1] then |
15 |
| - local workspace |
16 |
| - local path = file[1] |
17 |
| - local dir = vim.fs.dirname(path) |
18 |
| - local handle = io.open(path) |
19 |
| - if handle then |
20 |
| - workspace = vim.json.decode(handle:read("*a")) |
21 |
| - handle:close() |
22 |
| - end |
23 |
| - local name = vim.fn.fnamemodify(path, ":t:r") |
| 6 | + if file[1] then |
| 7 | + local workspace |
| 8 | + local path = file[1] |
| 9 | + local dir = vim.fs.dirname(path) |
| 10 | + workspace = vim.json.decode(vim.fn.join(vim.fn.readfile(path), "\n")) |
| 11 | + local name = vim.fn.fnamemodify(path, ":t:r") |
24 | 12 |
|
25 |
| - if workspace then |
26 |
| - workspace["name"] = name |
| 13 | + if workspace then |
| 14 | + workspace["name"] = name |
27 | 15 |
|
28 |
| - local folders = vim.tbl_map(function(folder) |
29 |
| - local uri = vim.uri_from_fname(uv.fs_realpath(vim.fs.joinpath(dir, folder.path))) |
30 |
| - local folder_name = vim.fs.basename(uv.fs_realpath(folder.path)) |
| 16 | + local folders = vim |
| 17 | + .iter(workspace.folders) |
| 18 | + :map(function(folder) |
| 19 | + local full_path = vim.fs.joinpath(dir, folder.path) |
| 20 | + local real_path = vim.uv.fs_realpath(full_path) |
| 21 | + if real_path then |
| 22 | + return { |
| 23 | + name = vim.fs.basename(vim.uv.fs_realpath(folder.path)), |
| 24 | + uri = vim.uri_from_fname(real_path), |
| 25 | + } |
| 26 | + else |
| 27 | + return nil |
| 28 | + end |
| 29 | + end) |
| 30 | + :filter(function(folder) |
| 31 | + return folder ~= nil |
| 32 | + end) |
| 33 | + :totable() |
31 | 34 |
|
32 |
| - return { name = folder_name, uri = uri } |
33 |
| - end, workspace.folders) |
| 35 | + vim.print(folders) |
34 | 36 |
|
35 |
| - workspace["folders"] = folders |
36 |
| - vim.g.workspace = workspace |
37 |
| - end |
38 |
| - end |
| 37 | + workspace["folders"] = folders |
| 38 | + vim.g.workspace = workspace |
| 39 | + end |
| 40 | + end |
39 | 41 | end
|
0 commit comments