about summary refs log tree commit diff
path: root/emacs.d/vendor/reason-mode.el
blob: 789735955db24b9744a1dca4ed323990e37c1072 (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
;;; reason-mode.el --- A major mode for editing ReasonML -*-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 Reason programming language (https://facebook.github.io/reason).
;;
;; Reason is an umbrella project that provides a curated layer for OCaml.
;;
;; 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 'reason-indent)
(require 'refmt)
(require 'reason-interaction)

(eval-when-compile (require 'rx)
                   (require 'compile)
                   (require 'url-vars))

;; Syntax definitions and helpers
(defvar reason-mode-syntax-table
  (let ((table (make-syntax-table)))

    ;; Operators
    (dolist (i '(?+ ?- ?* ?/ ?& ?| ?^ ?! ?< ?> ?~ ?@))
      (modify-syntax-entry i "." table))

    ;; Strings
    (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)

    table))

(defgroup reason nil
  "Support for Reason code."
  :link '(url-link "http://facebook.github.io/reason/")
  :group 'languages)

(defcustom reason-mode-hook nil
  "Hook called by `reason-mode'."
  :type 'hook
  :group 'reason)

;; Font-locking definitions and helpers
(defconst reason-mode-keywords
  '("and" "as"
    "else" "external"
    "fun" "for"
    "if" "impl" "in" "include"
    "let"
    "module" "match" "mod" "move" "mutable"
    "open"
    "priv" "pub"
    "rec" "ref" "return"
    "self" "static" "switch" "struct" "super"
    "trait" "type"
    "use"
    "virtual"
    "where" "when" "while"))

(defconst reason-mode-consts
  '("true" "false"))

(defconst reason-special-types
  '("int" "float" "string" "char"
    "bool" "unit" "list" "array" "exn"
    "option" "ref"))

(defconst reason-camel-case
  (rx symbol-start
      (group upper (0+ (any word nonascii digit "_")))
      symbol-end))

(eval-and-compile
  (defconst reason--char-literal-rx
    (rx (seq (group "'")
             (or (seq "\\" anything)
                 (not (any "'\\")))
             (group "'")))))

(defun reason-re-word (inner)
  "Build a word regexp given INNER."
  (concat "\\<" inner "\\>"))

(defun reason-re-grab (inner)
  "Build a grab regexp given INNER."
  (concat "\\(" inner "\\)"))

(defun reason-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 Reason
(defvar reason-font-lock-keywords
  `((,(reason-regexp-opt-symbols reason-mode-keywords) . font-lock-keyword-face)
    (,(reason-regexp-opt-symbols reason-special-types) . font-lock-builtin-face)
    (,(reason-regexp-opt-symbols reason-mode-consts) . font-lock-constant-face)

    (,reason-camel-case 1 font-lock-type-face)

    ;; Field names like `foo:`, highlight excluding the :
    (,(concat (reason-re-grab reason-re-ident) ":[^:]") 1 font-lock-variable-name-face)
    ;; Module names like `foo::`, highlight including the ::
    (,(reason-re-grab (concat reason-re-ident "::")) 1 font-lock-type-face)
    ;; Name punned labeled args like ::foo
    (,(concat "[[:space:]]+" (reason-re-grab (concat "::" reason-re-ident))) 1 font-lock-type-face)

    ;; TODO jsx attribs?
    (,
     (concat "<[/]?" (reason-re-grab reason-re-ident) "[^>]*" ">")
     1 font-lock-type-face)))

(defun reason-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 reason-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")
          (reason-mode-try-find-alternate-file mod-name ".re"))
         (t
          (reason-mode-try-find-alternate-file mod-name ".rei")))))))

(defun reason--syntax-propertize-multiline-string (end)
  "Propertize Reason multiline string.
Argument END marks the end of the string."
  (let ((ppss (syntax-ppss)))
    (when (eq t (nth 3 ppss))
      (let ((key (save-excursion
                   (goto-char (nth 8 ppss))
                   (and (looking-at "{\\([a-z]*\\)|")
                        (match-string 1)))))
        (when (search-forward (format "|%s}" key) end 'move)
          (put-text-property (1- (match-end 0)) (match-end 0)
                             'syntax-table (string-to-syntax "|")))))))

(defun reason-syntax-propertize-function (start end)
  "Propertize Reason function.
Argument START marks the beginning of the function.
Argument END marks the end of the function."
  (goto-char start)
  (reason--syntax-propertize-multiline-string end)
  (funcall
   (syntax-propertize-rules
    (reason--char-literal-rx (1 "\"") (2 "\""))
    ;; multi line strings
    ("\\({\\)[a-z]*|"
     (1 (prog1 "|"
          (goto-char (match-end 0))
          (reason--syntax-propertize-multiline-string end)))))
   (point) end))

(defvar reason-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-c\C-a" #'reason-mode-find-alternate-file)
    (define-key map "\C-c\C-r" #'refmt-region-ocaml-to-reason)
    (define-key map "\C-c\C-o" #'refmt-region-reason-to-ocaml)
    map))

;;;###autoload
(define-derived-mode reason-mode prog-mode "Reason"
  "Major mode for Reason code.

\\{reason-mode-map}"
  :group 'reason
  :syntax-table reason-mode-syntax-table
  :keymap reason-mode-map

  ;; Syntax
  (setq-local syntax-propertize-function #'reason-syntax-propertize-function)
  ;; Indentation
  (setq-local indent-line-function 'reason-mode-indent-line)
  ;; Fonts
  (setq-local font-lock-defaults '(reason-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 'reason-beginning-of-defun)
  (setq-local end-of-defun-function 'reason-end-of-defun)
  (setq-local parse-sexp-lookup-properties t))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rei?\\'" . reason-mode))

(defun reason-mode-reload ()
  "Reload Reason mode."
  (interactive)
  (unload-feature 'reason-mode)
  (unload-feature 'reason-indent)
  (unload-feature 'reason-interaction)
  (require 'reason-mode)
  (reason-mode))

(provide 'reason-mode)

;;; reason-mode.el ends here