Skip to content

Commit 89f5a99

Browse files
authored
paredit: split-at-pos no longer throws @ str quote (#357)
`split-at-pos` no longer throws when attempting to split at string's opening quote. Closes #350
1 parent e8ff5fd commit 89f5a99

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

CHANGELOG.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ A release with known breaking changes is marked with:
3333
{issue}321[#321] ({lread}, thanks for the issue {person}openvest[@openvest]!)
3434
** `join` no longer removes comments that were between joined strings
3535
{issue}351[#351] ({lread})
36+
** `split-at-pos` no longer throws on split at string opening quote
37+
{issue}350[#350] ({lread})
3638

3739
=== v1.1.49 - 2024-11-18 [[v1.1.49]]
3840

src/rewrite_clj/paredit.cljc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,19 +446,22 @@
446446
(update-in [0] #(subs % split-col))))))))
447447

448448
(defn split-at-pos
449-
"In string aware split
450-
451-
Perform split at given position `pos` Like split, but if inside string splits string into two strings.
449+
"In-string aware split. Returns `zloc` with node found at `pos` split.
450+
If `pos` is inside a string, splits string into two strings else calls [[split]].
452451
453452
- `zloc` location is (inclusive) starting point for `pos` depth-first search
454453
- `pos` can be a `{:row :col}` map or a `[row col]` vector. The `row` and `col` values are
455454
1-based and relative to the start of the source code the zipper represents.
456455
457-
Throws if `zloc` was not created with [position tracking](/doc/01-user-guide.adoc#position-tracking)."
456+
Throws if `zloc` was not created with [position tracking](/doc/01-user-guide.adoc#position-tracking).
457+
458+
- `[1 2 |3 4 5] => [1 2 |3] [4 5]`
459+
- `(\"Hello |World\") => (|\"Hello\" \"World\")`"
458460
[zloc pos]
459461
(if-let [candidate (z/find-last-by-pos zloc pos)]
460-
(let [pos (fz/pos-as-map pos)]
461-
(if (string-node? candidate)
462+
(let [pos (fz/pos-as-map pos)
463+
candidate-pos (fz/pos-as-map (-> candidate z/position fz/pos-as-map))]
464+
(if (and (string-node? candidate) (not= pos candidate-pos))
462465
(split-string candidate pos)
463466
(split candidate)))
464467
zloc))

test/rewrite_clj/paredit_test.cljc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,11 @@
213213
(deftest split-at-pos-test
214214
;; for this pos fn test, ⊚ in `s` represents character row/col the the `pos`
215215
;; ⊚ in `expected` is at zipper node granularity
216-
(doseq [[s expected]
217-
[["(\"Hello ⊚World\")" "(⊚\"Hello \" \"World\")"]]]
216+
(doseq [[s expected]
217+
[["(\"Hello ⊚World\" 42)" "(⊚\"Hello \" \"World\" 42)"]
218+
["(\"⊚Hello World\" 101)" "(⊚\"\" \"Hello World\" 101)"]
219+
["(\"H⊚ello World\" 101)" "(⊚\"H\" \"ello World\" 101)"]
220+
["(⊚\"Hello World\" 101)" "(⊚\"Hello World\") (101)"]]]
218221
(let [{:keys [pos s]} (th/pos-and-s s)
219222
zloc (z/of-string* s {:track-position? true})]
220223
(doseq [pos [pos [(:row pos) (:col pos)]]]

0 commit comments

Comments
 (0)