-
Notifications
You must be signed in to change notification settings - Fork 31
/
evil-matchit-javascript.el
127 lines (109 loc) · 4.09 KB
/
evil-matchit-javascript.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
121
122
123
124
125
126
127
;;; evil-matchit-javascript.el --- simple match plugin of evil-matchit -*- lexical-binding: t; -*-
;; Copyright (C) 2014-2020 Chen Bin
;; Author: Chen Bin
;; This file is not part of GNU Emacs.
;;; License:
;; This file is part of evil-matchit
;;
;; evil-matchit is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; evil-matchit is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;;; Code:
(require 'evil-matchit-sdk)
;; should try next howto, the purpose is avoid missing any howto
(defvar evilmi-javascript-extract-keyword-howtos
'(("const .* *= *\\(styled\\)[^`]*` *$" 1) ; styled component
("^[ \t]*\\(`\\);? *$" 1)))
(defvar evilmi-javascript-match-tags
'((("styled") () "`")))
(defvar evilmi-javascript-matching-chars
(string-to-list "{[(}}])"))
;; javascript code: "(function(...) { ..."
;; C code: "} else {"
;; React JS code: " return ("
;; line could ends with C++ or C comment
(defvar evilmi-javascript-open-brace-pattern
"^[ \t]*[(}]?[$_a-zA-Z0-9]+.*\\([{(\[]\\)[ \t]*\\(\/\/.*\\|\/\*[^/]*\*\/\\)?$"
"Pattern to match line which ends with brace or bracket character.")
(defun evilmi--javascript-find-open-brace (cur-line)
"Find open brace from CUR-LINE."
(let* (rlt)
(cond
((string-match evilmi-javascript-open-brace-pattern
cur-line)
(setq rlt (list 1 (match-string 1 cur-line))))
(t
(save-excursion
(forward-line)
(if (string-match "^[ \t]*{[ \t]*$" (evilmi-sdk-curline))
(setq rlt (list 2 "{"))))))
rlt))
;;;###autoload
(defun evilmi-javascript-get-tag ()
"Get tag at point."
;; only handle open tag
(when evilmi-debug
(message "evilmi-javascript-get-tag called"))
(let* (rlt)
(cond
;; bracket
((memq (following-char)
evilmi-javascript-matching-chars)
(when evilmi-debug
(message "evilmi-javascript-get-tag. following char=%s is in `evilmi-javascript-matching-chars'"
(following-char)))
(setq rlt (list (point))))
;; use defined tag
((setq rlt (evilmi-sdk-get-tag evilmi-javascript-match-tags
evilmi-javascript-extract-keyword-howtos))
(when evilmi-debug
(message "evilmi-javascript-get-tag. current line has tag=%s in `evilmi-javascript-extract-keyword-howtos'"
rlt)))
;; other javascript statements containing brackets
(t
(let* ((r (evilmi--javascript-find-open-brace (evilmi-sdk-curline)))
(p (line-beginning-position)))
(when r
(when evilmi-debug
(message "evilmi-javascript-get-tag. open brace=%s" r))
(forward-line (1- (car r)))
(search-forward (cadr r) nil nil)
(backward-char)
(setq rlt (list p))))))
rlt))
;;;###autoload
(defun evilmi-javascript-jump (info num)
"Jump to the matching tag using INFO and NUM."
(cond
((not info)
;; do nothing
)
((evilmi-sdk-get-tag evilmi-javascript-match-tags
evilmi-javascript-extract-keyword-howtos)
(evilmi-sdk-jump info
num
evilmi-javascript-match-tags
evilmi-javascript-extract-keyword-howtos))
(t
(evilmi-sdk-simple-jump)
(let* ((cur-line (evilmi-sdk-curline)))
;; hack for javascript
(if (or (string-match "^[ \t]*}\)\(.*\)\; *$" cur-line)
(string-match "^[ \t]*}\(.*\))\; *$" cur-line)
(string-match "^[ \t]*}\])\; *$" cur-line))
(line-end-position)
(1+ (point)))))))
(provide 'evil-matchit-javascript)
;;; evil-matchit-javascript.el ends here