about summary refs log tree commit diff
path: root/emacs/.emacs.d/vendor/reason-indent.el
blob: 8fd3c94258669d88534d77e01d81f774058e544c (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
;;; reason-indent.el --- Indentation functions for ReasonML -*-lexical-binding: t-*-

;; Portions Copyright (c) 2015-present, Facebook, Inc. All rights reserved.

;;; Commentary:

;; Indentation functions for Reason.

;;; Code:

(defconst reason-re-ident "[[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*")

(defcustom reason-indent-offset 2
  "Indent Reason code by this number of spaces."
  :type 'integer
  :group 'reason-mode
  :safe #'integerp)

(defun reason-looking-back-str (str)
  "Like `looking-back' but for fixed strings rather than regexps.
Works around some regexp slowness.
Argument STR string to search for."
  (let ((len (length str)))
    (and (> (point) len)
         (equal str (buffer-substring-no-properties (- (point) len) (point))))))

(defun reason-paren-level ()
  "Get the level of nesting inside parentheses."
  (nth 0 (syntax-ppss)))

(defun reason-in-str-or-cmnt ()
  "Return whether point is currently inside a string or a comment."
  (nth 8 (syntax-ppss)))

(defun reason-rewind-past-str-cmnt ()
  "Rewind past string or comment."
  (goto-char (nth 8 (syntax-ppss))))

(defun reason-rewind-irrelevant ()
  "Rewind past irrelevant characters (whitespace of inside comments)."
  (interactive)
  (let ((starting (point)))
    (skip-chars-backward "[:space:]\n")
    (if (reason-looking-back-str "*/") (backward-char))
    (if (reason-in-str-or-cmnt)
        (reason-rewind-past-str-cmnt))
    (if (/= starting (point))
        (reason-rewind-irrelevant))))

(defun reason-align-to-expr-after-brace ()
  "Align the expression at point to the expression after the previous brace."
  (save-excursion
    (forward-char)
    ;; We don't want to indent out to the open bracket if the
    ;; open bracket ends the line
    (when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
      (when (looking-at "[[:space:]]")
        (forward-word 1)
        (backward-word 1))
      (current-column))))

(defun reason-align-to-prev-expr ()
  "Align the expression at point to the previous expression."
  (let ((alignment (save-excursion
                     (forward-char)
                     ;; We don't want to indent out to the open bracket if the
                     ;; open bracket ends the line
                     (when (not (looking-at "[[:blank:]]*\\(?://.*\\)?$"))
                       (if (looking-at "[[:space:]]")
                           (progn
                             (forward-word 1)
                             (backward-word 1))
                         (backward-char))
                       (current-column)))))
    (if (not alignment)
        (save-excursion
          (forward-char)
          (forward-line)
          (back-to-indentation)
          (current-column))
      alignment)))

;;; Start of a reason binding
(defvar reason-binding
  (regexp-opt '("let" "type" "module" "fun")))

(defun reason-beginning-of-defun (&optional arg)
  "Move backward to the beginning of the current defun.

With ARG, move backward multiple defuns.  Negative ARG means
move forward.

This is written mainly to be used as `beginning-of-defun-function'.
Don't move to the beginning of the line.  `beginning-of-defun',
which calls this, does that afterwards."
  (interactive "p")
  (re-search-backward (concat "^\\(" reason-binding "\\)\\_>")
                      nil 'move (or arg 1)))

(defun reason-end-of-defun ()
  "Move forward to the next end of defun.

With argument, do it that many times.
Negative argument -N means move back to Nth preceding end of defun.

Assume that this is called after ‘beginning-of-defun’.  So point is
at the beginning of the defun body.

This is written mainly to be used as `end-of-defun-function' for Reason."
  (interactive)
  ;; Find the opening brace
  (if (re-search-forward "[{]" nil t)
      (progn
        (goto-char (match-beginning 0))
        ;; Go to the closing brace
        (condition-case nil
            (forward-sexp)
          (scan-error
           ;; The parentheses are unbalanced; instead of being unable to fontify, just jump to the end of the buffer
           (goto-char (point-max)))))
    ;; There is no opening brace, so consider the whole buffer to be one "defun"
    (goto-char (point-max))))

(defun reason-rewind-to-beginning-of-current-level-expr ()
  "Rewind to the beginning of the expression on the current level of nesting."
  (interactive)
  (let ((current-level (reason-paren-level)))
    (back-to-indentation)
    (when (looking-at "=>")
      (reason-rewind-irrelevant)
      (back-to-indentation))
    (while (> (reason-paren-level) current-level)
      (backward-up-list)
      (back-to-indentation))))

(defun reason-mode-indent-line ()
  "Indent current line."
  (interactive)
  (let ((indent
         (save-excursion
           (back-to-indentation)
           ;; Point is now at beginning of current line
           (let* ((level (reason-paren-level))
                  (baseline
                   ;; Our "baseline" is one level out from the indentation of the expression
                   ;; containing the innermost enclosing opening bracket. That
                   ;; way if we are within a block that has a different
                   ;; indentation than this mode would give it, we still indent
                   ;; the inside of it correctly relative to the outside.
                   (if (= 0 level)
                       0
                     (save-excursion
                       (reason-rewind-irrelevant)
                       (if (save-excursion
                             (reason-rewind-to-beginning-of-current-level-expr)
                             (looking-at "<"))
                           (progn
                             (reason-rewind-to-beginning-of-current-level-expr)
                             (current-column))
                           (progn
                             (backward-up-list)
                             (reason-rewind-to-beginning-of-current-level-expr)

                             (cond
                              ((looking-at "switch")
                               (current-column))

                              ((looking-at "|")
                               (+ (current-column) (* reason-indent-offset 2)))

                              (t
                               (let ((current-level (reason-paren-level)))
                                 (save-excursion
                                   (while (and (= current-level (reason-paren-level))
                                               (not (looking-at reason-binding)))
                                     (reason-rewind-irrelevant)
                                     (reason-rewind-to-beginning-of-current-level-expr))
                                   (+ (current-column) reason-indent-offset)))))))))))
             (cond
              ;; A function return type is indented to the corresponding function arguments
              ((looking-at "=>")
               (+ baseline reason-indent-offset))

              ((reason-in-str-or-cmnt)
               (cond
                ;; In the end of the block -- align with star
                ((looking-at "*/") (+ baseline 1))
                ;; Indent to the following shape:
                ;; /* abcd
                ;;  * asdf
                ;;  */
                ;;
                ((looking-at "*") (+ baseline 1))
                ;; Indent to the following shape:
                ;; /* abcd
                ;;    asdf
                ;;  */
                ;;
                (t (+ baseline (+ reason-indent-offset 1)))))

              ((looking-at "</") (- baseline reason-indent-offset))

              ;; A closing brace is 1 level unindented
              ((looking-at "}\\|)\\|\\]")
               (save-excursion
                 (reason-rewind-irrelevant)
                 (let ((jsx? (reason-looking-back-str ">")))
                   (backward-up-list)
                   (reason-rewind-to-beginning-of-current-level-expr)
                   (cond
                    ((looking-at "switch") baseline)

                    (jsx? (current-column))

                    (t (- baseline reason-indent-offset))))))

              ;; Doc comments in /** style with leading * indent to line up the *s
              ((and (nth 4 (syntax-ppss)) (looking-at "*"))
               (+ 1 baseline))

              ;; If we're in any other token-tree / sexp, then:
              (t
               (or
                ;; If we are inside a pair of braces, with something after the
                ;; open brace on the same line and ending with a comma, treat
                ;; it as fields and align them.
                (when (> level 0)
                  (save-excursion
                    (reason-rewind-irrelevant)
                    (backward-up-list)
                    ;; Point is now at the beginning of the containing set of braces
                    (reason-align-to-expr-after-brace)))

                (progn
                  (back-to-indentation)
                  (cond ((looking-at (regexp-opt '("and" "type")))
                         baseline)
                        ((save-excursion
                           (reason-rewind-irrelevant)
                           (= (point) 1))
                         baseline)
                        ((save-excursion
                           (while (looking-at "|")
                             (reason-rewind-irrelevant)
                             (back-to-indentation))
                           (looking-at (regexp-opt '("type"))))
                         (+ baseline reason-indent-offset))
                        ((looking-at "|\\|/[/*]")
                         baseline)
                        ((and (> level 0)
                              (save-excursion
                                (reason-rewind-irrelevant)
                                (backward-up-list)
                                (reason-rewind-to-beginning-of-current-level-expr)
                                (looking-at "switch")))
                         (+ baseline reason-indent-offset))
                        ((save-excursion
                           (reason-rewind-irrelevant)
                           (looking-back "[{;,\\[(]" (- (point) 2)))
                         baseline)
                        ((and
                          (save-excursion
                            (reason-rewind-irrelevant)
                            (reason-rewind-to-beginning-of-current-level-expr)
                            (and (looking-at reason-binding)
                                 (not (progn
                                        (forward-sexp)
                                        (forward-sexp)
                                        (skip-chars-forward "[:space:]\n")
                                        (looking-at "=")))))
                          (not (save-excursion
                                 (skip-chars-backward "[:space:]\n")
                                 (reason-looking-back-str "=>"))))
                         (save-excursion
                           (reason-rewind-irrelevant)
                           (backward-sexp)
                           (reason-align-to-prev-expr)))
                        ((save-excursion
                           (reason-rewind-irrelevant)
                           (looking-back "<\/.*?>" (- (point) 30)))
                         baseline)
                        (t
                         (save-excursion
                           (reason-rewind-irrelevant)
                           (reason-rewind-to-beginning-of-current-level-expr)

                           (if (looking-at "|")
                               baseline
                             (+ baseline reason-indent-offset)))))
                  ;; Point is now at the beginning of the current line
                  ))))))))

    (when indent
      ;; If we're at the beginning of the line (before or at the current
      ;; indentation), jump with the indentation change.  Otherwise, save the
      ;; excursion so that adding the indentations will leave us at the
      ;; equivalent position within the line to where we were before.
      (if (<= (current-column) (current-indentation))
          (indent-line-to indent)
        (save-excursion (indent-line-to indent))))))

(provide 'reason-indent)

;;; reason-indent.el ends here