[PATCH fnlfmt] Prevent unnecessary writes with --fix
Export this patch
in order to allow tooling around fnlfmt to work right
Thanks!
I realized this same logic would be useful for the --check
implementation, not just --fix, so I've added it to that as well.
I've applied and pushed.
-Phil
---
cli.fnl | 9 +++++ ----
fnlfmt.fnl | 7 ++++ ---
2 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/cli.fnl b/cli.fnl
index 504fb6c..9e01c50 100644
--- a/cli.fnl
+++ b/cli.fnl
@@ -33,10 +33,11 @@
(os.exit)))
(fn fix [filename]
- (let [new (format-file filename options)
- f (assert (io.open filename :w))]
- (f:write new)
- (f:close)))
+ (let [(new changed?) (format-file filename options)]
+ (when changed? ; prevent unnecessary writes
+ (let [f (assert (io.open filename :w))]
+ (f:write new)
+ (f:close)))))
(match arg
[:--version] (print (.. "fnlfmt version " version))
diff --git a/fnlfmt.fnl b/fnlfmt.fnl
index 2700f86..d9b3042 100644
--- a/fnlfmt.fnl
+++ b/fnlfmt.fnl
@@ -396,8 +396,8 @@ When f returns a truthy value, recursively walks the children."
(let [f (match filename
"-" io.stdin
_ (or (io.open filename :r) (abort filename)))
- contents (f:read :*all)
- parser (-> (fennel.stringStream contents)
+ original (f:read :*all)
+ parser (-> (fennel.stringStream original)
(fennel.parser filename {:comments (not no-comments)}))
out []]
(f:close)
@@ -420,6 +420,7 @@ When f returns a truthy value, recursively walks the children."
(set skip-next? false)))
(set prev-ast ast))
(table.insert out "")
- (table.concat out "\n")))
+ (let [formatted (table.concat out "\n")]
+ (values formatted (not= formatted original)))))
{: fnlfmt : format-file :version :0.3.2-dev}
--
2.39.3 (Apple Git-145)
Oliver Vartiainen <oliver@rakkine.fi> writes: