-
Notifications
You must be signed in to change notification settings - Fork 2
/
evil-textobj-anyblock-tests.el
84 lines (80 loc) · 3.63 KB
/
evil-textobj-anyblock-tests.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(require 'evil-textobj-anyblock)
(define-key evil-inner-text-objects-map "b" 'evil-textobj-anyblock-inner-block)
(define-key evil-outer-text-objects-map "b" 'evil-textobj-anyblock-a-block)
(defmacro evil-textobj-anyblock-with (in &rest body)
"This is `lispy-with' modified for evil-textobj-anyblock.
Note that | is considered to be \"on\" a character, meaning that it is included
in a visual selection. ~ on the other hand is not considered to be on a
character, so when it represents the region end, the character after it is not
considered as part of the region."
(declare (indent 1))
`(let ((temp-buffer (generate-new-buffer " *temp*")))
(save-window-excursion
(unwind-protect
(progn
(switch-to-buffer temp-buffer)
(emacs-lisp-mode)
(transient-mark-mode 1)
(evil-mode)
(insert ,in)
(goto-char (point-min))
(when (search-forward "~" nil t)
(backward-delete-char 1)
(set-mark (point)))
(goto-char (point-max))
(search-backward "|")
(delete-char 1)
(setq current-prefix-arg nil)
,@(mapcar (lambda (x)
(if (or (stringp x)
(and (listp x)
(eq (car x) 'kbd)))
`(evil-execute-macro 1 ,x)
x))
body)
(insert "|")
(when (region-active-p)
(exchange-point-and-mark)
;; because not considering ~ as "on" like |
(when (= (point) (region-end))
(forward-char))
(insert "~"))
(buffer-substring-no-properties
(point-min)
(point-max)))
(and (buffer-name temp-buffer)
(kill-buffer temp-buffer))))))
(ert-deftest evil-textobj-anyblock-inner-block ()
(should (string= (evil-textobj-anyblock-with "(|foo) (bar)" "dib")
"(|) (bar)"))
(should (string= (evil-textobj-anyblock-with "(|foo) (bar)" "vib")
"(~fo|o) (bar)"))
;; seeking
(should (string= (evil-textobj-anyblock-with "|foo (bar)" "dib")
"foo (|)"))
(should (string= (evil-textobj-anyblock-with "|foo (bar)" "vib")
"foo (~ba|r)"))
;; expansion
(should (string= (evil-textobj-anyblock-with "\"foo ((a b) [|c d])\"" "vib")
"\"foo ((a b) [~c |d])\""))
(should (string= (evil-textobj-anyblock-with "\"foo ((a b) [~c |d])\"" "ib")
"\"foo (~(a b) [c d|])\""))
(should (string= (evil-textobj-anyblock-with "\"foo (~(a b) [c d|])\"" "ib")
"\"~foo ((a b) [c d]|)\"")))
(ert-deftest evil-textobj-anyblock-a-block ()
(should (string= (evil-textobj-anyblock-with "(|foo) (bar)" "dab")
"| (bar)"))
(should (string= (evil-textobj-anyblock-with "(|foo) (bar)" "vab")
"~(foo|) (bar)"))
;; seeking
(should (string= (evil-textobj-anyblock-with "|foo (bar)" "dab")
"foo| ")) ; depends on evil-move-cursor-back t
(should (string= (evil-textobj-anyblock-with "|foo (bar)" "vab")
"foo ~(bar|)"))
;; expansion
(should (string= (evil-textobj-anyblock-with "\"foo ((a b) [|c d])\"" "vab")
"\"foo ((a b) ~[c d|])\""))
(should (string= (evil-textobj-anyblock-with "\"foo ((a b) ~[c d|])\"" "ab")
"\"foo ~((a b) [c d]|)\""))
(should (string= (evil-textobj-anyblock-with "\"foo ~((a b) [c d]|)\"" "ab")
"~\"foo ((a b) [c d])|\"")))