|
107 | 107 | (second (re-find #"^(.*)\n?" tail))))))
|
108 | 108 |
|
109 | 109 |
|
110 |
| -(defn- normalize-indents |
| 110 | +(defn normalize-indents |
111 | 111 | "Normalizes indents based on where the text starts on the first line"
|
112 |
| - [{:keys [range-text eol] :as m}] |
113 |
| - (let [indent-before (apply str (repeat (indent-before-range m) " ")) |
114 |
| - lines (clojure.string/split range-text #"\r?\n(?!\s*;)" -1)] |
115 |
| - (assoc m :range-text (clojure.string/join (str eol indent-before) lines)))) |
| 112 | + [{:keys [range-text indent eol] :as m}] |
| 113 | + (let [lines (clojure.string/split range-text #"\r?\n(?!\s*;)" -1) |
| 114 | + padded-lines (map (fn [line] |
| 115 | + (if (re-matches #"^ *$" line) |
| 116 | + eol |
| 117 | + (str eol indent (clojure.string/replace line #" *$" "")))) |
| 118 | + (rest lines))] |
| 119 | + (assoc m :range-text (clojure.string/join (conj padded-lines (first lines)))))) |
116 | 120 |
|
117 | 121 |
|
118 | 122 | (defn index-for-tail-in-range
|
|
135 | 139 | (defn format-text-at-range
|
136 | 140 | "Formats text from all-text at the range"
|
137 | 141 | [{:keys [range idx] :as m}]
|
138 |
| - (let [indent-before (indent-before-range m) |
139 |
| - padding (apply str (repeat indent-before " ")) |
| 142 | + (let [indents (indent-before-range m) |
| 143 | + padding (apply str (repeat indents " ")) |
140 | 144 | range-text (extract-range-text m)
|
141 |
| - padded-text (str padding range-text) |
142 | 145 | range-index (- idx (first range))
|
143 | 146 | tail (subs range-text range-index)
|
144 |
| - formatted-m (format-text (assoc m :range-text padded-text)) |
145 |
| - formatted-text (subs (:range-text formatted-m) indent-before)] |
| 147 | + formatted-m (format-text (assoc m :range-text range-text)) |
| 148 | + formatted-text (:range-text formatted-m)] |
146 | 149 | (-> (assoc formatted-m
|
147 | 150 | :range-text formatted-text
|
148 |
| - :range-tail tail)))) |
149 |
| - |
150 |
| -(defn format-text-at-range-bridge |
151 |
| - [m] |
152 |
| - (format-text-at-range m)) |
| 151 | + :range-tail tail |
| 152 | + :indent padding) |
| 153 | + (normalize-indents)))) |
153 | 154 |
|
154 | 155 | (comment
|
| 156 | + (-> {:eol "\n" |
| 157 | + :all-text " (foo)\n(defn bar\n[x]\nbaz)" |
| 158 | + :range [2 26]} |
| 159 | + format-text-at-range) |
| 160 | + (-> {:eol "\n" |
| 161 | + :all-text " (foo) |
| 162 | + (defn bar |
| 163 | + [x] |
| 164 | +
|
| 165 | +baz)" |
| 166 | + :range [10 38]} |
| 167 | + format-text-at-range) |
155 | 168 | (format-text-at-range {:all-text " '([]\n[])"
|
156 | 169 | :idx 7
|
157 | 170 | :on-type true
|
|
162 | 175 | (format-text-at-range {:eol "\n"
|
163 | 176 | :all-text "[:foo\n\n(foo)(bar)]"
|
164 | 177 | :idx 6
|
165 |
| - :range [0 18]})) |
| 178 | + :range [0 18]}) |
| 179 | + (-> {:eol "\n" |
| 180 | + :all-text " (defn foo [a b] |
| 181 | + (let [x + a b] |
| 182 | + (println \"sum is\" x))) |
| 183 | +
|
| 184 | +(defn bar [a b] |
| 185 | + (let [x + a b] |
| 186 | + (println \"sum is\" x)))" |
| 187 | + :idx 6 |
| 188 | + :range [6 129]} |
| 189 | + (format-text-at-range))) |
| 190 | + |
| 191 | + |
| 192 | +(defn format-text-at-range-bridge |
| 193 | + [m] |
| 194 | + (format-text-at-range m)) |
| 195 | + |
166 | 196 |
|
167 | 197 |
|
168 | 198 | (defn add-indent-token-if-empty-current-line
|
|
0 commit comments