1+ local Config = require (" kulala.config" )
2+ local Graphql = require (" kulala.parser.graphql" )
13local Logger = require (" kulala.logger" )
4+ local Shell = require (" kulala.cmd.shell_utils" )
25
36local M = {}
47
@@ -7,25 +10,66 @@ M.format = function(formatter, contents, opts)
710 verbose = true ,
811 })
912
10- if type (formatter ) == " function" then
11- return formatter (contents )
12- elseif type (formatter ) == " table" then
13- local cmd = formatter
13+ if type (formatter ) == " function" then return formatter (contents ) end
14+ if not type (formatter ) == " table" then return contents end
1415
15- local status , result = pcall (function ()
16- return vim .system (cmd , { stdin = contents , text = true }):wait ()
17- end )
16+ local executable = formatter [1 ]
1817
19- if not status or result .code ~= 0 then
20- _ = opts .verbose
21- and Logger .warn ((" Error running external formatter: %s" ):format (not status and result or result .stderr ))
22- return contents
23- end
18+ if vim .fn .executable (executable ) == 0 then
19+ _ = opts .versbose and Logger .warn (" Formatting failed: " .. executable .. " is not available." )
20+ return contents
21+ end
22+
23+ local result = Shell .run (formatter , {
24+ sync = true ,
25+ stdin = contents ,
26+ err_msg = " Failed to format with " .. executable ,
27+ abort_on_stderr = true ,
28+ })
29+
30+ if not result or result .code ~= 0 or result .stderr ~= " " or result .stdout == " " then return contents end
31+
32+ return result .stdout
33+ end
34+
35+ M .json = function (contents , opts )
36+ local formatter = Config .get ().contenttypes [" application/json" ]
37+ if not formatter then return contents end
38+
39+ opts = vim .tbl_deep_extend (" keep" , opts or {}, { sort = true })
40+ _ = opts .sort and formatter and table.insert (formatter .formatter , 2 , " --sort-keys" )
41+
42+ contents = type (contents ) == " table" and vim .json .encode (contents , opts ) or contents
2443
25- return result .stdout
44+ return M .format (formatter .formatter , contents , opts )
45+ end
46+
47+ M .graphql = function (contents , opts )
48+ local formatter = Config .get ().contenttypes [" application/graphql" ]
49+ if not formatter then return contents end
50+
51+ local _ , json = Graphql .get_json (contents )
52+ if not json then return contents end
53+
54+ local formatted = M .format (formatter .formatter , json .query , opts )
55+
56+ if json .variables and next (json .variables ) then
57+ formatted = formatted .. " \n " .. M .json (json .variables , { sort = opts .sort })
2658 end
2759
28- return contents
60+ return formatted
61+ end
62+
63+ M .html = function (contents , opts )
64+ local formatter = Config .get ().contenttypes [" application/html" ]
65+ if not formatter then return contents end
66+ return M .format (formatter .formatter , contents , opts )
67+ end
68+
69+ M .xml = function (contents , opts )
70+ local formatter = Config .get ().contenttypes [" application/xml" ]
71+ if not formatter then return contents end
72+ return M .format (formatter .formatter , contents , opts )
2973end
3074
3175return M
0 commit comments