Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle escaped parentheses #110

Merged
merged 3 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading