@@ -12,7 +12,7 @@ function M.setup(opts)
1212 group = group ,
1313 pattern = { " *.smk" , " Snakefile" },
1414 callback = function ()
15- M .format ()
15+ M .format (true )
1616 end ,
1717 desc = " Auto-format with snakefmt on save" ,
1818 })
@@ -32,14 +32,15 @@ function M.setup(opts)
3232 end , { desc = " Show snakefmt execution info" })
3333end
3434
35- function M .format ()
35+ function M .format (sync )
3636 local cmd = paths .get_snakefmt_bin (config .options )
3737 if not cmd then
3838 vim .notify (" snakefmt: binary not found and uvx fallback failed" , vim .log .levels .ERROR )
3939 return
4040 end
4141
4242 local bufnr = vim .api .nvim_get_current_buf ()
43+ local changedtick = vim .api .nvim_buf_get_changedtick (bufnr )
4344 local content = vim .api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
4445 local stdin = table.concat (content , " \n " ) .. " \n "
4546
@@ -51,12 +52,30 @@ function M.format()
5152 end
5253 table.insert (full_cmd , " -" )
5354
55+ if sync then
56+ local obj = vim .system (full_cmd , { stdin = stdin }):wait ()
57+ if obj .code == 0 then
58+ local formatted = vim .split (obj .stdout , " \n " )
59+ if formatted [# formatted ] == " " then
60+ table.remove (formatted )
61+ end
62+ vim .api .nvim_buf_set_lines (bufnr , 0 , - 1 , false , formatted )
63+ else
64+ vim .notify (" snakefmt error: " .. obj .stderr , vim .log .levels .ERROR )
65+ end
66+ return
67+ end
68+
5469 vim .system (full_cmd , { stdin = stdin }, function (obj )
5570 vim .schedule (function ()
5671 if not vim .api .nvim_buf_is_valid (bufnr ) then
5772 return
5873 end
5974
75+ if vim .api .nvim_buf_get_changedtick (bufnr ) ~= changedtick then
76+ return
77+ end
78+
6079 if obj .code == 0 then
6180 local formatted = vim .split (obj .stdout , " \n " )
6281 if formatted [# formatted ] == " " then
0 commit comments