forked from reasonml-editor/reason-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrescript-mode.el
More file actions
232 lines (187 loc) · 7.35 KB
/
rescript-mode.el
File metadata and controls
232 lines (187 loc) · 7.35 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
;;; rescript-mode.el --- A major mode for editing ReScript -*-lexical-binding: t-*-
;; Portions Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
;; Version: 0.4.0
;; Author: Mozilla
;; Url: https://github.com/reasonml-editor/reason-mode
;; Keywords: languages, ocaml
;; Package-Requires: ((emacs "24.3"))
;; This file is NOT part of GNU Emacs.
;; This file is distributed under the terms of both the MIT license and the
;; Apache License (version 2.0).
;;; Commentary:
;; This project provides useful functions and helpers for developing
;; code using the ReScript programming language.
;;
;; It offers:
;; - A new, familiar syntax for the battle-tested language that is OCaml.
;; - A workflow for compiling to JavaScript and native code.
;; - A set of friendly documentations, libraries and utilities.
;;
;; See the README.md for more details.
;;; Code:
(require 'rescript-indent)
(require 'rescript-interaction)
(eval-when-compile (require 'rx)
(require 'compile)
(require 'url-vars))
;; Syntax definitions and helpers
(defvar rescript-mode-syntax-table
(let ((table (make-syntax-table)))
;; Operators
(dolist (i '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?=))
(modify-syntax-entry i "." table))
;; Symbols
(modify-syntax-entry ?@ "_" table)
(modify-syntax-entry ?~ "_" table)
(modify-syntax-entry ?# "_" table)
;; Strings
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?\\ "\\" table)
(modify-syntax-entry ?\' "_" table)
(modify-syntax-entry ?\` "\"" table)
;; Comments
(modify-syntax-entry ?\/ ". 124b" table)
(modify-syntax-entry ?\* ". 23n" table)
(modify-syntax-entry ?\n "> b" table)
(modify-syntax-entry ?\^m "> b" table)
;; Quoted variable names
(modify-syntax-entry ?\\ "\\ p" table)
table))
(defgroup rescript nil
"Support for ReScript code."
:link '(url-link "https://rescript-lang.org/")
:group 'languages)
(defcustom rescript-mode-hook nil
"Hook called by `rescript-mode'."
:type 'hook
:group 'rescript)
;; Font-locking definitions and helpers
(defconst rescript-mode-keywords
(split-string
"and as assert async await constraint catch downto else exception
external for if in include lazy let module mutable of open rec
switch to try type when while with"))
(defconst rescript-mode-decorators-1
(split-string
"as bs.send.pipe dead deprecated deriving doesNotRaise genType
get get_index ignore inline int live meth module new obj optional
raises react.component return scope send set set_index string
this unboxed uncurry unwrap val variadic"))
(defconst rescript-mode-decorators-2 '("deprecated" "warning"))
(defconst rescript-mode-extensions-1 '("debugger" "identity" "raw" "re"))
(defconst rescript-mode-extensions-2 '("private" "raw"))
(defconst rescript-mode-special-values
(split-string "FILE LINE LINE_OF LOC LOC_OF MODULE POS POS_OF"))
(defconst rescript-mode-consts
'("true" "false"))
(defconst rescript-special-types
(split-string
"int float string char bool unit list array exn option ref result promise"))
(defconst rescript-camel-case
(rx symbol-start
(group upper (0+ (any word nonascii digit "_")))
symbol-end))
(eval-and-compile
(defconst rescript--char-literal-rx
(rx (seq (group "'")
(or (seq "\\" anything)
(not (any "'\\")))
(group "'")))))
(defun rescript-re-word (inner)
"Build a word regexp given INNER."
(concat "\\<" inner "\\>"))
(defun rescript-re-grab (inner)
"Build a grab regexp given INNER."
(concat "\\(" inner "\\)"))
(defun rescript-regexp-opt-symbols (words)
"Like `(regexp-opt words 'symbols)`, but will work on Emacs 23.
See rust-mode PR #42.
Argument WORDS argument to pass to `regexp-opt`."
(concat "\\_<" (regexp-opt words t) "\\_>"))
;;; Syntax highlighting for ReScript
(defvar rescript-font-lock-keywords
`((,(rescript-regexp-opt-symbols rescript-mode-keywords) . font-lock-keyword-face)
(,(rescript-regexp-opt-symbols rescript-special-types) . font-lock-builtin-face)
(,(rescript-regexp-opt-symbols rescript-mode-consts) . font-lock-constant-face)
(,rescript-camel-case 1 font-lock-type-face)
;; Field names like `foo:`, highlight excluding the :
(,(concat (rescript-re-grab rescript-re-ident) ":[^:]") 1 font-lock-variable-name-face)
;; Module names like `foo::`, highlight including the ::
(,(rescript-re-grab (concat rescript-re-ident "::")) 1 font-lock-type-face)
;; Name punned labeled args like ::foo
(,(concat "[[:space:]]+" (rescript-re-grab (concat "::" rescript-re-ident))) 1 font-lock-type-face)
;; TODO jsx attribs?
(,
(concat "<[/]?" (rescript-re-grab rescript-re-ident) "[^>]*" ">")
1 font-lock-type-face)))
(defun rescript-mode-try-find-alternate-file (mod-name extension)
"Switch to the file given by MOD-NAME and EXTENSION."
(let* ((filename (concat mod-name extension))
(buffer (get-file-buffer filename)))
(if buffer (switch-to-buffer buffer)
(find-file filename))))
(defun rescript-mode-find-alternate-file ()
"Switch to implementation/interface file."
(interactive)
(let ((name buffer-file-name))
(when (string-match "\\`\\(.*\\)\\.re\\([il]\\)?\\'" name)
(let ((mod-name (match-string 1 name))
(e (match-string 2 name)))
(cond
((string= e "i")
(rescript-mode-try-find-alternate-file mod-name ".re"))
(t
(rescript-mode-try-find-alternate-file mod-name ".rei")))))))
(defun rescript-syntax-propertize-function (start end)
"Propertize ReScript function.
Argument START marks the beginning of the function.
Argument END marks the end of the function."
(goto-char start)
(funcall
(syntax-propertize-rules
(rescript--char-literal-rx (1 "\"") (2 "\"")))
(point) end))
(defvar rescript-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-a") #'rescript-mode-find-alternate-file)
map))
;;;###autoload
(define-derived-mode rescript-mode prog-mode "ReScript"
"Major mode for ReScript code.
\\{rescript-mode-map}"
:group 'rescript
:syntax-table rescript-mode-syntax-table
:keymap rescript-mode-map
;; Syntax
(setq-local syntax-propertize-function #'rescript-syntax-propertize-function)
;; Indentation
(setq-local indent-line-function 'rescript-mode-indent-line)
;; Fonts
(setq-local font-lock-defaults '(rescript-font-lock-keywords))
;; Misc
(setq-local comment-start "/* ")
(setq-local comment-end " */")
(setq-local indent-tabs-mode nil)
;; Allow paragraph fills for comments
(setq-local comment-start-skip "/\\*+[ \t]*")
(setq-local paragraph-start
(concat "^[ \t]*$\\|\\*)$\\|" page-delimiter))
(setq-local paragraph-separate paragraph-start)
(setq-local require-final-newline t)
(setq-local normal-auto-fill-function nil)
(setq-local comment-multi-line t)
(setq-local beginning-of-defun-function 'rescript-beginning-of-defun)
(setq-local end-of-defun-function 'rescript-end-of-defun)
(setq-local parse-sexp-lookup-properties t))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.resi?$" . rescript-mode))
(defun rescript-mode-reload ()
"Reload ReScript mode."
(interactive)
(unload-feature 'rescript-mode)
(unload-feature 'rescript-indent)
(unload-feature 'rescript-interaction)
(require 'rescript-mode)
(rescript-mode))
(provide 'rescript-mode)
;;; rescript-mode.el ends here