-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-js2-mode.el
138 lines (116 loc) · 5.2 KB
/
setup-js2-mode.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
128
129
130
131
132
133
134
135
136
137
138
(setq-default js2-allow-rhino-new-expr-initializer nil)
(setq-default js2-auto-indent-p nil)
(setq-default js2-enter-indents-newline nil)
(setq-default js2-global-externs '("module" "require" "jQuery" "$" "_" "buster" "sinon" "assert" "refute" "setTimeout" "clearTimeout" "setInterval" "clearInterval" "location" "__dirname" "console" "JSON"))
(setq-default js2-idle-timer-delay 0.1)
(setq-default js2-indent-on-enter-key nil)
(setq-default js2-mirror-mode nil)
(setq-default js2-strict-inconsistent-return-warning nil)
(setq-default js2-auto-indent-p t)
(setq-default js2-rebind-eol-bol-keys nil)
(setq-default js2-include-rhino-externs nil)
(setq-default js2-include-gears-externs nil)
(setq-default js2-concat-multiline-strings 'eol)
(require 'js2-refactor)
(require 'js2-imenu-extras)
(js2-imenu-extras-setup)
(define-key js2-mode-map (kbd "C-c RET jt") 'jump-to-test-file)
(define-key js2-mode-map (kbd "C-c RET ot") 'jump-to-test-file-other-window)
(define-key js2-mode-map (kbd "C-c RET js") 'jump-to-source-file)
(define-key js2-mode-map (kbd "C-c RET os") 'jump-to-source-file-other-window)
(define-key js2-mode-map (kbd "C-c RET jo") 'jump-between-source-and-test-files)
(define-key js2-mode-map (kbd "C-c RET oo") 'jump-between-source-and-test-files-other-window)
(define-key js2-mode-map (kbd "C-c RET ta") 'toggle-assert-refute)
(defadvice js2r-inline-var (after reindent-buffer activate)
(cleanup-buffer))
(defun js2-hide-test-functions ()
(interactive)
(save-excursion
(goto-char (point-min))
(ignore-errors
(while (re-search-forward "\"[^\"]+\": function (")
(js2-mode-hide-element)))))
(define-key js2-mode-map (kbd "C-c t") 'js2-hide-test-functions)
;; js2-mode steals TAB, let's steal it back for yasnippet
(defun js2-tab-properly ()
(interactive)
(let ((yas/fallback-behavior 'return-nil))
(unless (yas/expand)
(indent-for-tab-command)
(if (looking-back "^\s*")
(back-to-indentation)))))
(define-key js2-mode-map (kbd "TAB") 'js2-tab-properly)
;; Don't redefine C-a for me please, js2-mode
(define-key js2-mode-map (kbd "C-a") nil)
;; When renaming/deleting js-files, check for corresponding testfile
(define-key js2-mode-map (kbd "C-x C-r") 'js2r-rename-current-buffer-file)
(define-key js2-mode-map (kbd "C-x C-k") 'js2r-delete-current-buffer-file)
;; Use lambda for anonymous functions
(font-lock-add-keywords
'js2-mode `(("\\(function\\) *("
(0 (progn (compose-region (match-beginning 1)
(match-end 1) "\u0192")
nil)))))
;; Use right arrow for return in one-line functions
(font-lock-add-keywords
'js2-mode `(("function *([^)]*) *{ *\\(return\\) "
(0 (progn (compose-region (match-beginning 1)
(match-end 1) "\u2190")
nil)))))
;; After js2 has parsed a js file, we look for jslint globals decl comment ("/* global Fred, _, Harry */") and
;; add any symbols to a buffer-local var of acceptable global vars
;; Note that we also support the "symbol: true" way of specifying names via a hack (remove any ":true"
;; to make it look like a plain decl, and any ':false' are left behind so they'll effectively be ignored as
;; you can;t have a symbol called "someName:false"
(add-hook 'js2-post-parse-callbacks
(lambda ()
(when (> (buffer-size) 0)
(let ((btext (replace-regexp-in-string
": *true" " "
(replace-regexp-in-string "[\n\t ]+" " " (buffer-substring-no-properties 1 (buffer-size)) t t))))
(mapc (apply-partially 'add-to-list 'js2-additional-externs)
(split-string
(if (string-match "/\\* *global *\\(.*?\\) *\\*/" btext) (match-string-no-properties 1 btext) "")
" *, *" t))
))))
(defun cjsp--eldoc-innards (beg)
(save-excursion
(goto-char beg)
(search-forward "=")
(let ((start (point)))
(search-forward "*/")
(forward-char -2)
(buffer-substring-no-properties start (point)))))
(defun cjsp--indentation-of-html-line (html line-number)
(with-temp-buffer
(insert html)
(html-mode)
(indent-region (point-min) (point-max))
(goto-line line-number)
(back-to-indentation)
(current-column)))
(defun cjsp--line-number-in-eldoc (p beg)
(save-excursion
(goto-char p)
(let ((l (line-number-at-pos)))
(goto-char beg)
(- l (line-number-at-pos) -1))))
(defun js2-lineup-comment (parse-status)
"Indent a multi-line block comment continuation line."
(let* ((beg (nth 8 parse-status))
(first-line (js2-same-line beg))
(p (point))
(offset (save-excursion
(goto-char beg)
(cond
((looking-at "/\\*:DOC ")
(+ 2 (current-column)
(cjsp--indentation-of-html-line
(cjsp--eldoc-innards beg)
(cjsp--line-number-in-eldoc p beg))))
((looking-at "/\\*")
(+ 1 (current-column)))
(:else 0)))))
(unless first-line
(indent-line-to offset))))
(provide 'setup-js2-mode)