~technomancy/fennel

fnlfmt: Prevent unnecessary writes with --fix v1 PROPOSED

Oliver Vartiainen: 1
 Prevent unnecessary writes with --fix

 2 files changed, 9 insertions(+), 7 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~technomancy/fennel/patches/51366/mbox | git am -3
Learn more about email & git

[PATCH fnlfmt] Prevent unnecessary writes with --fix Export this patch

in order to allow tooling around fnlfmt to work right
---
 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: