Skip to content

Commit

Permalink
fix: handle escaped parentheses (#110)
Browse files Browse the repository at this point in the history
* fix: handle escaped parentheses

Currently, if an escaped paren is encountered in Clojure code; it's
interpreted as a delimiter and breaks deletion

* Bypass sp-get-enclosing-sexp for deletion

Use evil-cp--matching-paren-pos's value instead.

* Fix deletion for escaped parens

---------

Co-authored-by: Ellis Kenyo <me@elken.dev>
  • Loading branch information
tomdl89 and elken authored Oct 1, 2023
1 parent 51b2735 commit 134fe33
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
24 changes: 22 additions & 2 deletions evil-cleverparens-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,21 @@ golf foxtrot deltahotel india"))
(evil-cp-test-buffer
"(alpha (bravo[ ]charlie))"
("d^")
"(( charlie))")))

"(( charlie))"))
(ert-info ("Can delete line containing escaped parens")
(evil-test-buffer ;; As we need to enable evil-cp later anyway
"(alpha\n(b[r]avo ?\\))\ncharlie)"
(emacs-lisp-mode)
(evil-cleverparens-mode t)
("dd")
"(alpha\ncharlie)"))
(ert-info ("Can delete unbalanced line containing escaped parens")
(evil-test-buffer ;; As we need to enable evil-cp later anyway
"(alpha\n(b[r]avo ?\\)\ncharlie)\ndelta)"
(emacs-lisp-mode)
(evil-cleverparens-mode t)
("dd")
"(alpha\n (charlie)\n delta)")))

;; (alpha[ ]bravo) charlie
;; charlie (bravo[ ]alpha)
Expand Down Expand Up @@ -668,6 +681,13 @@ golf foxtrot deltahotel india"))
"alpha bravo (charlie [d]elta echo) foxtrot golf"
(">")
"alpha bravo (charlie [d]elta echo foxtrot) golf"))
(ert-info ("Can slurp escaped parens")
(evil-test-buffer ;; As we need to enable evil-cp later anyway
"(alpha[)] ?\\) bravo"
(emacs-lisp-mode)
(evil-cleverparens-mode t)
(">")
"(alpha ?\\)) bravo"))
(ert-info ("Can barf backwards when on opening delimiter")
(evil-cp-test-buffer
"alpha bravo [(]charlie delta echo) foxtrot golf"
Expand Down
12 changes: 12 additions & 0 deletions evil-cleverparens-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@
(when pos (goto-char pos))
(looking-at-p evil-cp--ws-regexp)))

(defun evil-cp--looking-at-escape-p (&optional pos)
(save-excursion
(when pos (goto-char pos))
(looking-at-p (regexp-quote (or sp-escape-char "\\")))))

(defun evil-cp--looking-at-escaped-p (&optional pos)
(or (evil-cp--looking-at-escape-p pos)
(evil-cp--looking-at-escape-p (1- (or pos (point))))))

(defun evil-cp--pair-for (pair pairs)
(cond
((not pairs)
Expand All @@ -72,6 +81,7 @@ question. Ignores parentheses inside strings."
(save-excursion
(when pos (goto-char pos))
(and (sp--looking-at-p (evil-cp--get-opening-regexp))
(not (evil-cp--looking-at-escaped-p))
(not (evil-cp--inside-string-p)))))

(defun evil-cp--looking-at-opening-anywhere-p (&optional pos)
Expand All @@ -81,6 +91,7 @@ question. Includes parentheses inside strings."
(save-excursion
(when pos (goto-char pos))
(and (sp--looking-at-p (evil-cp--get-opening-regexp))
(not (evil-cp--looking-at-escaped-p))
(not (evil-cp--looking-at-string-closing-p)))))

(defun evil-cp--looking-at-closing-p (&optional pos)
Expand All @@ -90,6 +101,7 @@ question. Ignores parentheses inside strings."
(save-excursion
(when pos (goto-char pos))
(and (sp--looking-at-p (evil-cp--get-closing-regexp))
(not (evil-cp--looking-at-escaped-p))
(not (evil-cp--inside-string-p)))))

(defun evil-cp--looking-at-paren-p (&optional pos)
Expand Down
14 changes: 8 additions & 6 deletions evil-cleverparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -597,17 +597,19 @@ delimiters in the region defined by BEG and END."
(delete-char diff)
(setq chars-left (- chars-left diff))))
((evil-cp--looking-at-any-opening-p)
(let ((other-end (evil-cp--matching-paren-pos)))
(let ((p (point))
(other-end (evil-cp--matching-paren-pos)))
;; matching paren is in the range of the command
(if (<= (point) other-end end)
(let ((char-count
(evil-cp--guard-point
(sp-get (sp-get-enclosing-sexp)
(- :end :beg)))))
(if (<= p other-end end)
;; 1+ makes the char-count inclusive
(let ((char-count (1+ (- other-end p))))
(delete-char char-count)
(setq chars-left (- chars-left char-count)))
(forward-char)
(cl-decf chars-left))))
((and (evil-cp--looking-at-escape-p) (< 1 chars-left))
(delete-char 2)
(cl-decf chars-left 2))
((and (evil-cp--looking-at-any-closing-p) (= chars-left 1))
(cl-decf chars-left))
((evil-cp--looking-at-any-closing-p)
Expand Down

0 comments on commit 134fe33

Please sign in to comment.