Skip to content

Commit

Permalink
[Fix #841] Allow sp-rewrap to remove pairs when empty pair is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuco1 committed Jan 18, 2018
1 parent 65fbcfc commit 163a593
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
66 changes: 36 additions & 30 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -7573,37 +7573,43 @@ Examples:

(foo |bar baz) -> [(foo |bar baz)] ;; \\[universal-argument] ["
(interactive (list
(let ((available-pairs (sp--get-pair-list-context 'wrap))
ev ac (pair-prefix ""))
(while (not ac)
(setq ev (read-event (format "Rewrap with: %s" pair-prefix) t))
(setq pair-prefix (concat pair-prefix (format-kbd-macro (vector ev))))
(unless (--any? (string-prefix-p pair-prefix (car it)) available-pairs)
(user-error "Impossible pair prefix selected: %s" pair-prefix))
(setq ac (--first (equal pair-prefix (car it)) available-pairs)))
ac)
(catch 'done
(let ((available-pairs (sp--get-pair-list-context 'wrap))
ev ac (pair-prefix ""))
(while (not ac)
(setq ev (read-event (format "Rewrap with: %s" pair-prefix) t))
(if (and (equal pair-prefix "")
(eq ev 'return))
(throw 'done nil))
(setq pair-prefix (concat pair-prefix (format-kbd-macro (vector ev))))
(unless (--any? (string-prefix-p pair-prefix (car it)) available-pairs)
(user-error "Impossible pair prefix selected: %s" pair-prefix))
(setq ac (--first (equal pair-prefix (car it)) available-pairs)))
ac))
current-prefix-arg))
(-when-let (enc (sp-get-enclosing-sexp))
(save-excursion
(sp-get enc
(goto-char :end)
(unless keep-old
(delete-char (- :cl-l)))
(insert (cdr pair))
(goto-char :beg)
(insert (car pair))
(unless keep-old
(delete-char :op-l))
(setq sp-last-wrapped-region
(sp--get-last-wraped-region
:beg (+ :end
(length (car pair))
(length (cdr pair))
(- :op-l)
(- :cl-l))
(car pair) (cdr pair)))))
(sp--run-hook-with-args (car pair) :post-handlers 'rewrap-sexp
(list :parent (sp-get enc :op)))))
(if (not pair)
(sp-unwrap-sexp)
(-when-let (enc (sp-get-enclosing-sexp))
(save-excursion
(sp-get enc
(goto-char :end)
(unless keep-old
(delete-char (- :cl-l)))
(insert (cdr pair))
(goto-char :beg)
(insert (car pair))
(unless keep-old
(delete-char :op-l))
(setq sp-last-wrapped-region
(sp--get-last-wraped-region
:beg (+ :end
(length (car pair))
(length (cdr pair))
(- :op-l)
(- :cl-l))
(car pair) (cdr pair)))))
(sp--run-hook-with-args (car pair) :post-handlers 'rewrap-sexp
(list :parent (sp-get enc :op))))))

(defun sp-swap-enclosing-sexp (&optional arg)
"Swap the enclosing delimiters of this and the parent expression.
Expand Down
11 changes: 11 additions & 0 deletions test/smartparens-commands-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,17 @@ be."
(sp--test-sp-rewrap-sexp-python "\"foo 'bar' b|az\"" '("'" . "'") "'foo \\'bar\\' b|az'")
(sp--test-sp-rewrap-sexp-python "'foo \\'b|ar\\' baz'" '("\"" . "\"") "'foo \"b|ar\" baz'"))

;; #841
(ert-deftest sp-test-command-sp-rewrap-empty-pair-should-unwrap ()
(sp--test-sp-rewrap-sexp "[fo|o]" nil "fo|o"))

;; #841
(ert-deftest sp-test-command-sp-rewrap-empty-pair-should-unwrap-interactive-spec ()
(sp-test-with-temp-elisp-buffer "[fo|o]"
(let ((unread-command-events (list 'return)))
(call-interactively 'sp-rewrap-sexp)
(sp-buffer-equals "fo|o"))))

(ert-deftest sp-test-command-sp-rewrap-sexp-invalid-pair ()
(should-error
(sp-test-with-temp-elisp-buffer "(fo|o)"
Expand Down

0 comments on commit 163a593

Please sign in to comment.