|
1 | | -local COMMAND = "dotnet" |
2 | | -local TOOL = "csharpier" |
3 | | - |
4 | | ---- Ask the system if csharpier is installed as dotnet tool |
5 | | ---- NOTE: prefer the cached variant |
6 | | ----@return boolean |
7 | | -local function is_local_long() |
8 | | - if vim.fn.executable(COMMAND) == 0 then |
9 | | - return false -- if dotnet itself is not available, assume the csharpier executable |
10 | | - end |
11 | | - |
12 | | - local version_check = vim.system({ COMMAND, TOOL, "--version" }):wait() |
13 | | - return version_check.code == 0 -- try calling dotnet tool |
14 | | -end |
15 | | - |
16 | 1 | local is_local_cache = nil |
17 | 2 |
|
18 | 3 | --- Verify if csharpier is installed locally. |
19 | 4 | ---@return boolean |
20 | 5 | local function is_local() |
21 | 6 | if is_local_cache == nil then |
22 | | - is_local_cache = is_local_long() |
| 7 | + if vim.fn.executable("dotnet") == 0 then |
| 8 | + -- if dotnet itself is not available, assume the csharpier executable |
| 9 | + is_local_cache = false |
| 10 | + else |
| 11 | + local version_check = vim.system({ "dotnet", "csharpier", "--version" }):wait() |
| 12 | + is_local_cache = version_check.code == 0 |
| 13 | + end |
23 | 14 | end |
24 | 15 | return is_local_cache |
25 | 16 | end |
26 | 17 |
|
27 | | ---- Get command favoring locally installed csharpier. |
28 | | ----@return string |
29 | | -local function get_command() |
30 | | - if is_local() then |
31 | | - return COMMAND |
32 | | - end |
33 | | - return TOOL |
34 | | -end |
35 | | - |
36 | | ---- Get args favoring locally installed csharpier. |
37 | | ----@return string[] |
38 | | -local function get_args() |
39 | | - local args = {} |
40 | | - if is_local() then |
41 | | - table.insert(args, TOOL) |
42 | | - end |
43 | | - table.insert(args, "format") |
44 | | - return args |
45 | | -end |
46 | | - |
47 | 18 | ---@type conform.FileFormatterConfig |
48 | 19 | return { |
49 | 20 | meta = { |
50 | 21 | url = "https://github.com/belav/csharpier", |
51 | 22 | description = "The opinionated C# code formatter.", |
52 | 23 | }, |
53 | | - command = get_command, |
54 | | - args = get_args, |
| 24 | + command = function() |
| 25 | + if is_local() then |
| 26 | + return "dotnet" |
| 27 | + else |
| 28 | + return "csharpier" |
| 29 | + end |
| 30 | + end, |
| 31 | + args = function() |
| 32 | + if is_local() then |
| 33 | + return { "csharpier", "format" } |
| 34 | + else |
| 35 | + return { "format" } |
| 36 | + end |
| 37 | + end, |
55 | 38 | stdin = true, |
56 | 39 | } |
0 commit comments