Skip to content

Commit

Permalink
Apply padding indents on all lines
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Nov 21, 2021
1 parent f9727f1 commit 21a76be
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
62 changes: 46 additions & 16 deletions src/cljs-lib/src/calva/fmt/formatter.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,16 @@
(second (re-find #"^(.*)\n?" tail))))))


(defn- normalize-indents
(defn normalize-indents
"Normalizes indents based on where the text starts on the first line"
[{:keys [range-text eol] :as m}]
(let [indent-before (apply str (repeat (indent-before-range m) " "))
lines (clojure.string/split range-text #"\r?\n(?!\s*;)" -1)]
(assoc m :range-text (clojure.string/join (str eol indent-before) lines))))
[{:keys [range-text indent eol] :as m}]
(let [lines (clojure.string/split range-text #"\r?\n(?!\s*;)" -1)
padded-lines (map (fn [line]
(if (re-matches #"^ *$" line)
eol
(str eol indent (clojure.string/replace line #" *$" ""))))
(rest lines))]
(assoc m :range-text (clojure.string/join (conj padded-lines (first lines))))))


(defn index-for-tail-in-range
Expand All @@ -135,23 +139,32 @@
(defn format-text-at-range
"Formats text from all-text at the range"
[{:keys [range idx] :as m}]
(let [indent-before (indent-before-range m)
padding (apply str (repeat indent-before " "))
(let [indents (indent-before-range m)
padding (apply str (repeat indents " "))
range-text (extract-range-text m)
padded-text (str padding range-text)
range-index (- idx (first range))
tail (subs range-text range-index)
formatted-m (format-text (assoc m :range-text padded-text))
formatted-text (subs (:range-text formatted-m) indent-before)]
formatted-m (format-text (assoc m :range-text range-text))
formatted-text (:range-text formatted-m)]
(-> (assoc formatted-m
:range-text formatted-text
:range-tail tail))))

(defn format-text-at-range-bridge
[m]
(format-text-at-range m))
:range-tail tail
:indent padding)
(normalize-indents))))

(comment
(-> {:eol "\n"
:all-text " (foo)\n(defn bar\n[x]\nbaz)"
:range [2 26]}
format-text-at-range)
(-> {:eol "\n"
:all-text " (foo)
(defn bar
[x]
baz)"
:range [10 38]}
format-text-at-range)
(format-text-at-range {:all-text " '([]\n[])"
:idx 7
:on-type true
Expand All @@ -162,7 +175,24 @@
(format-text-at-range {:eol "\n"
:all-text "[:foo\n\n(foo)(bar)]"
:idx 6
:range [0 18]}))
:range [0 18]})
(-> {:eol "\n"
:all-text " (defn foo [a b]
(let [x + a b]
(println \"sum is\" x)))
(defn bar [a b]
(let [x + a b]
(println \"sum is\" x)))"
:idx 6
:range [6 129]}
(format-text-at-range)))


(defn format-text-at-range-bridge
[m]
(format-text-at-range m))



(defn add-indent-token-if-empty-current-line
Expand Down
7 changes: 5 additions & 2 deletions src/cljs-lib/test/calva/fmt/formatter_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[calva.fmt.formatter :as sut]))

(deftest format-text-at-range
(is (= "(foo)\n(defn bar\n [x]\n baz)"
(is (= "(foo)\n (defn bar\n [x]\n baz)"
(:range-text (sut/format-text-at-range {:eol "\n" :all-text " (foo)\n(defn bar\n[x]\nbaz)" :range [2 26]}))))
(is (not (contains? (sut/format-text-at-range {:eol "\n" :all-text " (foo)\n(defn bar\n[x]\nbaz)" :range [2 26]}) :new-index))))

Expand Down Expand Up @@ -115,6 +115,7 @@ bar))")
(is (= "(foo)\n (defn bar\n [x]\n baz)"
(:range-text (sut/normalize-indents {:eol "\n"
:all-text " (foo)\n(defn bar\n[x]\nbaz)"
:indent " "
:range [2 26]
:range-text "(foo)\n(defn bar\n [x]\n baz)"})))))

Expand All @@ -140,8 +141,10 @@ bar))")
(deftest format-text-at-idx-on-type
(is (= "(bar \n\n )"
(:range-text (sut/format-text-at-idx-on-type {:eol "\n" :all-text "(bar \n\n)" :range [0 8] :idx 7}))))
(is (= "(bar \n \n )"
(is (= "(bar \n\n )"
(:range-text (sut/format-text-at-idx-on-type {:eol "\n" :all-text "(bar \n \n)" :range [0 9] :idx 8}))))
(is (= "(bar \n \n )"
(:range-text (sut/format-text-at-idx-on-type {:eol "\n" :all-text "(bar \n \n)" :range [0 9] :idx 6}))))
(is (= "(bar \n \n )"
(:range-text (sut/format-text-at-idx-on-type {:eol "\n" :all-text "(bar \n\n)" :range [0 8] :idx 6}))))
(is (= "\"bar \n \n \""
Expand Down

0 comments on commit 21a76be

Please sign in to comment.