Skip to content

Commit

Permalink
Verified behavior is correct when cursor is in a string in barf/slurp…
Browse files Browse the repository at this point in the history
… commands
  • Loading branch information
thickey committed Dec 12, 2012
1 parent 8f618e4 commit fc18b62
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 87 deletions.
142 changes: 59 additions & 83 deletions src/paredit/core.clj
Expand Up @@ -774,7 +774,6 @@
(with-important-memoized
(if-let [rloc (-?> parse-tree (parsed-root-loc true))]
(let [[l r] (normalized-selection rloc offset length)
;; TODO need to check if we are in a string (still slurp, but move end-quote)
starting-loc (if (*tag-closing-brackets* (loc-tag l)) ;; if on closing punct, must select parent differently
(loc-for-offset rloc offset)
(if-let [nl (z/up (parse-node l))]
Expand All @@ -795,7 +794,9 @@
;; TODO potentially need to re-indent the slurped-in text (if multi-line)
ret (-> t
(assoc-in [:text] (t/str-replace text replace-offset replace-length replace-text))
(update-in [:modifs] conj {:offset replace-offset :length replace-length :text replace-text}))]
(update-in [:modifs] conj {:offset replace-offset
:length replace-length
:text replace-text}))]
ret)
t))
t)))
Expand All @@ -806,7 +807,6 @@
(with-important-memoized
(if-let [rloc (-?> parse-tree (parsed-root-loc true))]
(let [[l r] (normalized-selection rloc offset length)
;; TODO need to check if we are in a string (still slurp, but move end-quote)
starting-loc (if (*tag-closing-brackets* (loc-tag l)) ;; if on closing punct, must select parent differently
(loc-for-offset rloc offset)
(if-let [nl (z/up (parse-node l))]
Expand All @@ -828,15 +828,9 @@
;; TODO potentially need to re-indent the slurped-in text (if multi-line)
ret (-> t
(assoc-in [:text] (t/str-replace text replace-offset replace-length replace-text))
(update-in [:modifs] conj {:offset replace-offset :length replace-length :text replace-text}))]
(printf "text-to-replace: '%s'
replace-text: '%s'
slurp-text: '%s'
open-punct: '%s'"
text-to-replace
replace-text
slurp-text
open-punct)
(update-in [:modifs] conj {:offset replace-offset
:length replace-length
:text replace-text}))]
ret)
t))
t)))
Expand All @@ -845,82 +839,64 @@ open-punct: '%s'"
:paredit-forward-barf-sexp
[cmd {:keys #{parse-tree buffer}} {:keys [^String text offset length] :as t}]
(with-important-memoized
(if-let [rloc (-?> parse-tree (parsed-root-loc true))]
(let [[l r] (normalized-selection rloc offset length)
;; TODO need to check if we are in a string (still barf, but needs to be balanced... see orig paredit)
starting-loc (if (*tag-closing-brackets* (loc-tag l)) ;; if on closing punct, must select parent differently
(loc-for-offset rloc offset)
(if-let [nl (z/up (parse-node l))]
nl l))
;; TODO what does this do with single elem lists?
rightmost (-> starting-loc z/down z/rightmost z/left non-puncts-to-left)
more-non-puncts (-> (first rightmost) z/left non-puncts-to-left rest)
barfees (concat more-non-puncts rightmost)
(let [rloc (-?> parse-tree (parsed-root-loc true))
[l r] (when rloc
(normalized-selection rloc offset length))]
(if (and rloc l (in-code? l))
(let [starting-loc (if (*tag-closing-brackets* (loc-tag l)) ;; if on closing punct, must select parent differently
(loc-for-offset rloc offset)
(if-let [nl (z/up (parse-node l))]
nl l))
;; TODO what does this do with single elem lists?
rightmost (-> starting-loc z/down z/rightmost z/left non-puncts-to-left)
more-non-puncts (-> (first rightmost) z/left non-puncts-to-left rest)
barfees (concat more-non-puncts rightmost)
barf-text (apply str (map loc-text barfees))
barf-start-loc (first barfees)
barf-so (start-offset barf-start-loc)
barf-eo (end-offset starting-loc)
text-to-replace (.substring text barf-so barf-eo)
close-punct (*tag-closing-brackets* (loc-tag starting-loc))
replace-text (str close-punct barf-text)
replace-length (.length text-to-replace)
replace-offset barf-so
ret (-> t
(assoc-in [:text] (t/str-replace text replace-offset replace-length replace-text))
(update-in [:modifs] conj {:offset replace-offset
:length replace-length
:text replace-text}))]
ret)
t))))

(defmethod paredit
:paredit-backward-barf-sexp
[cmd {:keys #{parse-tree buffer}} {:keys [^String text offset length] :as t}]
;; (with-important-memoized)
(let [rloc (-?> parse-tree (parsed-root-loc true))
[l r] (when rloc
(normalized-selection rloc offset length))]
(if (and rloc l (in-code? l))
(let [starting-loc (if (*tag-closing-brackets* (loc-tag l)) ;; if on closing punct, must select parent differently
(loc-for-offset rloc offset)
(if-let [nl (z/up (parse-node l))]
nl l))
lefts (-> starting-loc z/down z/leftmost z/right non-puncts-to-right)
leftmost (reverse lefts)
more-non-puncts (-> (first lefts) z/right non-puncts-to-right rest reverse)
barfees (concat leftmost more-non-puncts)
barf-text (apply str (map loc-text barfees))
barf-start-loc (first barfees)
barf-so (start-offset barf-start-loc)
barf-eo (end-offset starting-loc)
barf-end-loc (first (reverse barfees))
barf-so (start-offset starting-loc)
barf-eo (end-offset barf-end-loc)
text-to-replace (.substring text barf-so barf-eo)
close-punct (*tag-closing-brackets* (loc-tag starting-loc))
replace-text (str close-punct barf-text)
open-punct (*tag-opening-brackets* (loc-tag starting-loc))
replace-text (str barf-text open-punct)
replace-length (.length text-to-replace)
replace-offset barf-so
ret (-> t
(assoc-in [:text] (t/str-replace text replace-offset replace-length replace-text))
(update-in [:modifs] conj {:offset replace-offset :length replace-length :text replace-text}))]
(printf "rightmost: '%s'
more-non-puncts: '%s'
barfees: '%s'
text-to-replace: '%s'
replace-text: '%s'
text: '%s'"
(apply str (map loc-text rightmost))
(apply str (map loc-text more-non-puncts))
(apply str (map loc-text barfees))
text-to-replace
replace-text
(:text ret))
(update-in [:modifs] conj {:offset replace-offset
:length replace-length
:text replace-text}))]
ret)
t)))

(defmethod paredit
:paredit-backward-barf-sexp
[cmd {:keys #{parse-tree buffer}} {:keys [^String text offset length] :as t}]
;; (with-important-memoized)
(if-let [rloc (-?> parse-tree (parsed-root-loc true))]
(let [[l r] (normalized-selection rloc offset length)
;; TODO need to check if we are in a string (still barf, but needs to be balanced... see orig paredit)
starting-loc (if (*tag-closing-brackets* (loc-tag l)) ;; if on closing punct, must select parent differently
(loc-for-offset rloc offset)
(if-let [nl (z/up (parse-node l))]
nl l))
lefts (-> starting-loc z/down z/leftmost z/right non-puncts-to-right)
leftmost (reverse lefts)
more-non-puncts (-> (first lefts) z/right non-puncts-to-right rest reverse)
barfees (concat leftmost more-non-puncts)
barf-text (apply str (map loc-text barfees))
barf-end-loc (first (reverse barfees))
barf-so (start-offset starting-loc)
barf-eo (end-offset barf-end-loc)
text-to-replace (.substring text barf-so barf-eo)
open-punct (*tag-opening-brackets* (loc-tag starting-loc))
replace-text (str barf-text open-punct)
replace-length (.length text-to-replace)
replace-offset barf-so
ret (-> t
(assoc-in [:text] (t/str-replace text replace-offset replace-length replace-text))
(update-in [:modifs] conj {:offset replace-offset :length replace-length :text replace-text}))]
(printf "leftmost: '%s'
more-non-puncts: '%s'
barfees: '%s'
text-to-replace: '%s'
replace-text: '%s'
text: '%s'"
(apply str (map loc-text leftmost))
(apply str (map loc-text more-non-puncts))
(apply str (map loc-text barfees))
text-to-replace
replace-text
(:text ret))
ret)
t))
15 changes: 11 additions & 4 deletions src/paredit/core_commands.clj
Expand Up @@ -492,18 +492,25 @@
"(a b ((c| d) );;a\n e f)" "(a b ((c| d) ;;a\n e) f)"
"#{:a| :b} :c" "#{:a| :b :c}"
"(let [a 1 b 2]|)\n(do blah)" "(let [a 1 b 2]|\n(do blah))"
"[[:a| :b] :c]" "[[:a| :b :c]]"}]
"[[:a| :b] :c]" "[[:a| :b :c]]"
"(foo (\"ba|r\" baz) quux zot)" "(foo (\"ba|r baz\") quux zot)"}]
["C-(" :paredit-backward-slurp-sexp
{"(foo bar (baz| quux) zot)"
"(foo (bar baz| quux) zot)"
"(a b ((c| d)) e f)"
"(a (b (c| d)) e f)"}]
"(a (b (c| d)) e f)"
"(foo (baz \"ba|r\") quux zot)"
"(foo (\"baz ba|r\") quux zot)"}]
["C-}" :paredit-forward-barf-sexp
{"(foo (bar |baz quux) zot)"
"(foo (bar |baz) quux zot)"}]
"(foo (bar |baz) quux zot)"
"(foo (baz \"ba|r\") quux zot)"
"(foo (baz \"ba|r\") quux zot)"}]
["C-{" :paredit-backward-barf-sexp
{"(foo (bar baz |quux) zot)"
"(foo bar (baz |quux) zot)"}]]
"(foo bar (baz |quux) zot)"
"(foo (baz \"ba|r\") quux zot)"
"(foo (baz \"ba|r\") quux zot)"}]]



Expand Down

0 comments on commit fc18b62

Please sign in to comment.