Skip to content

Commit 9d859cb

Browse files
committed
refactor(csharpier): simplify formatter logic
1 parent 18eea16 commit 9d859cb

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed
Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,39 @@
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-
161
local is_local_cache = nil
172

183
--- Verify if csharpier is installed locally.
194
---@return boolean
205
local function is_local()
216
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
2314
end
2415
return is_local_cache
2516
end
2617

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-
4718
---@type conform.FileFormatterConfig
4819
return {
4920
meta = {
5021
url = "https://github.com/belav/csharpier",
5122
description = "The opinionated C# code formatter.",
5223
},
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,
5538
stdin = true,
5639
}

0 commit comments

Comments
 (0)