Skip to content

Associative alignment doesn't work for auto-resolve keywords #2920

@romulo-oliveira-nubank

Description

@romulo-oliveira-nubank

When there is auto-resolve keywords (e.g., ::foo) and {:align-associative? true} config is present, the formatting of a given form doesn't work.

Example:

(defn foo []
(let[a   :b
aa :bb
ccc {:a :b :aa :bb :ccc :ccc :dddd ::dddd}]
))

Expected result after pressing tab:

(defn foo []
  (let [a   :b
        aa  :bb
        ccc {:a    :b
             :aa   :bb
             :ccc  :ccc
             :dddd ::dddd}]))

Actual result (no formatting at all):

(defn foo []
(let[a   :b
aa :bb
ccc {:a :b :aa :bb :ccc :ccc :dddd ::dddd}]
))

I was able to reproduce this with a test, but I couldn't figure it out the fix (thus I'm opening as issue):

diff --git a/src/cljs-lib/test/calva/fmt/formatter_test.cljs b/src/cljs-lib/test/calva/fmt/formatter_test.cljs
index d82e6ac83..7163db412 100644
--- a/src/cljs-lib/test/calva/fmt/formatter_test.cljs
+++ b/src/cljs-lib/test/calva/fmt/formatter_test.cljs
@@ -25,7 +25,7 @@ baz)")
     [x]
 
     baz)"
-         (:range-text (sut/format-text-at-idx {:eol "\n" :all-text all-text :range [10 38] :idxs [11]})))) 
+         (:range-text (sut/format-text-at-idx {:eol "\n" :all-text all-text :range [10 38] :idxs [11]}))))
   (is (= [10 38]
          (:range (sut/format-text-at-idx {:eol "\n" :all-text all-text :range [10 38] :idxs [11]}))))
   (is (= [0 5]
@@ -44,6 +44,12 @@ aa bb
 ccc {:a b :aa bb :ccc ccc}]
 ))")
 
+(def misaligned-text-with-double-collon "(def foo
+(let[a   b
+aa bb
+ccc {:a b :aa bb :ccc ccc :dddd ::dddd}]
+))")
+
 (deftest format-aligned-text-at-idx
   (testing "Aligns associative structures when `:align-associative` is `true`"
     (is (= "(def foo
@@ -58,6 +64,20 @@ ccc {:a b :aa bb :ccc ccc}]
                                                  :range    [0 56]
                                                  :idxs     [0]})))))
 
+  (testing "Aligns associative structures with double colon when `:align-associative` is `true`"
+    (is (= "(def foo
+  (let [a   b
+        aa  bb
+        ccc {:a    b
+             :aa   bb
+             :ccc  ccc
+             :dddd ::dddd}]))"
+           (:range-text (sut/format-text-at-idx {:eol      "\n"
+                                                 :all-text misaligned-text-with-double-collon
+                                                 :config   {:align-associative? true}
+                                                 :range    [0 69]
+                                                 :idxs     [0]})))))
+
   (testing "Does not align associative structures when `:align-associative` is not `true`"
     (is (= "(def foo
   (let [a   b
expected: (= "(def foo\n  (let [a   b\n        aa  bb\n        ccc {:a    b\n             :aa   bb\n             :ccc  ccc\n             :dddd ::dddd}]))" (:range-text (sut/format-text-at-idx {:eol "\n", :all-text misaligned-text-with-double-collon, :config {:align-associative? true}, :range [0 69], :idxs [0]})))
  actual: (not (= "(def foo\n  (let [a   b\n        aa  bb\n        ccc {:a    b\n             :aa   bb\n             :ccc  ccc\n             :dddd ::dddd}]))" "(def foo\n(let[a   b\naa bb\nccc {:a b :aa bb :ccc ccc :dddd ::dddd}]\n))"))

or formatted:

expected: (= "(def foo
  (let [a   b
        aa  bb
        ccc {:a    b
             :aa   bb
             :ccc  ccc
             :dddd ::dddd}]))" (:range-text (sut/format-text-at-idx {:eol "
             ", :all-text misaligned-text-with-double-collon, :config {:align-associative? true}, :range [0 69], :idxs [0]})))
  actual: (not (= "(def foo
    (let [a   b
          aa  bb
          ccc {:a    b
               :aa   bb
               :ccc  ccc
               :dddd ::dddd}]))" "(def foo
               (let[a   b
  aa bb
  ccc {:a b :aa bb :ccc ccc :dddd ::dddd}]
  ))"))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions