Skip to content

Commit 4f76cb4

Browse files
committed
refactor(nix_flake_fmt): pass data as JSON rather than ad-hoc lines
This makes the communication between our lua code and the `nix eval` subprocess a bit clearer.
1 parent b75f346 commit 4f76cb4

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

lua/null-ls/builtins/formatting/nix_flake_fmt.lua

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ local find_nix_fmt = function(opts, done)
7373

7474
local eval_nix_formatter = [[
7575
let
76-
currentSystem = "]] .. nix_current_system .. [[";
76+
system = "]] .. nix_current_system .. [[";
7777
# Various functions vendored from nixpkgs lib (to avoid adding a
7878
# dependency on nixpkgs).
7979
lib = rec {
@@ -89,15 +89,17 @@ local find_nix_fmt = function(opts, done)
8989
};
9090
in
9191
formatterBySystem:
92-
if formatterBySystem ? ${currentSystem} then
93-
let
94-
formatter = formatterBySystem.${currentSystem};
95-
drv = formatter.drvPath;
96-
bin = lib.getExe formatter;
97-
in
98-
drv + "\n" + bin + "\n"
99-
else
100-
""
92+
builtins.toJSON (
93+
if formatterBySystem ? ${system} then
94+
let
95+
formatter = formatterBySystem.${system};
96+
drv = formatter.drvPath;
97+
bin = lib.getExe formatter;
98+
in
99+
{ inherit drv bin; }
100+
else
101+
{ error = "this flake does not define a formatter for system: ${system}"; }
102+
)
101103
]]
102104

103105
client.send_progress_notification(NOTIFICATION_TOKEN, {
@@ -128,19 +130,18 @@ local find_nix_fmt = function(opts, done)
128130
return
129131
end
130132

131-
if #stdout_lines == 0 then
133+
local stdout = table.concat(stdout_lines, "\n")
134+
local result = vim.json.decode(stdout)
135+
136+
if result.error ~= nil then
132137
vim.defer_fn(function()
133-
log:warn(
134-
string.format("this flake does not define a formatter for your system: %s", nix_current_system)
135-
)
138+
log:warn(result.error)
136139
end, 0)
137140
return
138141
end
139142

140-
-- stdout has 2 lines of output:
141-
-- 1. drv path
142-
-- 2. exe path
143-
local drv_path, nix_fmt_path = unpack(stdout_lines)
143+
local drv_path = result.drv
144+
local nix_fmt_path = result.bin
144145
return drv_path, nix_fmt_path
145146
end
146147

0 commit comments

Comments
 (0)