-
Notifications
You must be signed in to change notification settings - Fork 18
/
jest-traversal.el
120 lines (104 loc) · 4.6 KB
/
jest-traversal.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
(defconst jest--brackets '((?\( ?\)) (?{ ?}) (?\[ ?\]) (?< ?>)))
(defconst jest--quotes '(?\' ?\" ?\`))
(defconst jest--blank-line-begin "^[\t\s]*")
(defun jest--get-buffer-to-point ()
(buffer-substring 1 (line-end-position)))
(defun jest--check-template (character substitutes cb)
(let ((ele substitutes) (result nil))
(while ele
(when (char-equal character (funcall cb ele))
(setq ele '())
(setq result t))
(setq ele (cdr ele)))
result))
(defun jest--check-open-bracket (character)
(jest--check-template character jest--brackets
(lambda (ch) (car (car ch)))))
(defun jest--check-close-bracket (character)
(jest--check-template character jest--brackets
(lambda (ch) (car (cdr (car ch))))))
(defun jest--check-bracket-pair (open close)
(catch 'result
(dolist (x jest--brackets)
(when (and (char-equal open (car x))
(char-equal close (car (cdr x))))
(throw 'result t)))))
(defun jest--check-char-quote (character)
(jest--check-template character jest--quotes
(lambda (ch) (car ch))))
(defun jest--is-in-quotes (stack)
(if (car stack)
(jest--check-char-quote (car (car stack)))
nil))
(defun jest--merge-collapsed-range (list-to-del new-one)
"compare the last input of list with new-one
If collapse remove last one and push
If not, just push"
(if (and (car list-to-del) (>= (car (cdr (car list-to-del))) (car new-one)))
(let ()
(jest--merge-collapsed-range (cdr list-to-del) new-one))
(if (null list-to-del)
(list new-one)
(push new-one list-to-del))))
(defun jest--remove-ranges (text list-to-del)
(if (> (length list-to-del) 0)
(let ()
(jest--remove-ranges (concat (substring text 0 (+ 1 (car (car list-to-del))))
(substring text (car (cdr (car list-to-del)))))
(cdr list-to-del)))
text))
;; todo remove text closed with pairs but need to remain complete string
;; 1 : When meet Quotes.
;; If it's same quotes, pop from stack
; if not, Inside quotes ignore. If not inside quote, add to stack.
;; 2 : When meet bracket
;; If it's inside quotes, do nothing.
;; 2-1: If not inside quotes and opening braket
;; add to stack and record index.
;; 2-2: ... and closing braket
;; remove from stack and add list do delete with opening braket index.
;; If the last inserted in list is collapsed with current one, remove last one and add.
(defun jest--remove-folded-range (text)
(let ((stack '())
(list-to-del '())
(edited text))
(dotimes (i (length text))
(let ((ch (c-int-to-char (aref text i))))
(cond
((jest--check-char-quote ch)
(if (jest--is-in-quotes stack)
(when (char-equal (car (car stack)) ch)
(setq stack (cdr stack)))
(push (list ch i) stack)))
((jest--check-open-bracket ch)
(when (not (jest--is-in-quotes stack))
(push (list ch i) stack)))
((and (jest--check-close-bracket ch)
(jest--check-bracket-pair (car (car stack)) ch))
(when (not (jest--is-in-quotes stack))
(setq list-to-del
(jest--merge-collapsed-range list-to-del
(list (car (cdr (car stack))) i)))
(setq stack (cdr stack)))))))
(jest--remove-ranges text list-to-del)))
(defun jest--current-describe-name (desc-names text)
(if (or (string-match (concat jest--blank-line-begin "describe\(\'\\(.*?\\)\'") text)
(string-match (concat jest--blank-line-begin "describe\(\"\\(.*?\\)\"") text))
(let ((describe-name (match-string 1 text)))
(jest--current-describe-name (if (> (length desc-names) 0)
(concat desc-names " " describe-name)
describe-name)
(substring text (match-beginning 1))))
desc-names))
(defun jest--current-test-fun-param (text)
(when (or (string-match (concat jest--blank-line-begin "\\(test\\|it\\)\(\'\\(.*?\\)\'") text)
(string-match (concat jest--blank-line-begin "\\(test\\|it\\)\(\"\\(.*?\\)\"") text))
(match-string 2 text)))
(defun jest--current-test-name ()
(let ((text (jest--get-buffer-to-point)))
(let ((refined (jest--remove-folded-range text)))
(let ((desc-names (jest--current-describe-name "" refined)))
(concat (when (> (length desc-names) 0)
(concat desc-names " "))
(jest--current-test-fun-param refined))))))
(provide 'jest-traversal)