diff --git a/src/paredit/core.clj b/src/paredit/core.clj index 847a55c..c6cec1e 100644 --- a/src/paredit/core.clj +++ b/src/paredit/core.clj @@ -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))] @@ -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))) @@ -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))] @@ -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))) @@ -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)) diff --git a/src/paredit/core_commands.clj b/src/paredit/core_commands.clj index 5b0e727..87ddde7 100644 --- a/src/paredit/core_commands.clj +++ b/src/paredit/core_commands.clj @@ -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)"}]]