about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/vendor/reason-interaction.el
blob: 6ceaed1e9340a7bda0a0355a10faf093a9f8935b (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
;;; reason-interaction.el --- Phrase navitagion for rtop -*-lexical-binding: t-*-

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

;;; Commentary:

;; Phrase navigation for utop and maybe other REPLs.

;; The utop compatibility layer for Reason was mainly taken from:
;; https://github.com/ocaml/tuareg/blob/master/tuareg-light.el (big thanks!)

;;; Code:

(defun reason-backward-char (&optional step)
  "Go back one char.
Similar to `backward-char` but it does not signal errors
`beginning-of-buffer` and `end-of-buffer`.  It optionally takes a
STEP parameter for jumping back more than one character."
  (when step (goto-char (- (point) step))
        (goto-char (1- (point)))))

(defun reason-forward-char (&optional step)
  "Go forward one char.
Similar to `forward-char` but it does not signal errors
`beginning-of-buffer` and `end-of-buffer`.  It optionally takes a
STEP parameter for jumping back more than one character."
  (when step (goto-char (+ (point) step))
    (goto-char (1+ (point)))))

(defun reason-in-literal-p ()
  "Return non-nil if point is inside an Reason literal."
  (nth 3 (syntax-ppss)))

(defconst reason-comment-delimiter-regexp "\\*/\\|/\\*"
  "Regex for identify either open or close comment delimiters.")

(defun reason-in-between-comment-chars-p ()
  "Return non-nil iff point is in between the comment delimiter chars.
It returns non-nil if point is between the chars only (*|/ or /|*
where | is point)."
  (and (not (bobp)) (not (eobp))
       (or (and (char-equal ?/ (char-before)) (char-equal ?* (char-after)))
           (and (char-equal ?* (char-before)) (char-equal ?/ (char-after))))))

(defun reason-looking-at-comment-delimiters-p ()
  "Return non-nil iff point in between comment delimiters."
  (looking-at-p reason-comment-delimiter-regexp))

(defun reason-in-between-comment-delimiters-p ()
  "Return non-nil if inside /* and */."
  (nth 4 (syntax-ppss)))

(defun reason-in-comment-p ()
  "Return non-nil iff point is inside or right before a comment."
  (or (reason-in-between-comment-delimiters-p)
      (reason-in-between-comment-chars-p)
      (reason-looking-at-comment-delimiters-p)))

(defun reason-beginning-of-literal-or-comment ()
  "Skip to the beginning of the current literal or comment (or buffer)."
  (interactive)
  (goto-char (or (nth 8 (syntax-ppss)) (point))))

(defun reason-inside-block-scope-p ()
  "Skip to the beginning of the current literal or comment (or buffer)."
  (and (> (nth 0 (syntax-ppss)) 0)
       (let ((delim-start (nth 1 (syntax-ppss))))
         (save-excursion
           (goto-char delim-start)
           (char-equal ?{ (following-char))))))

(defun reason-at-phrase-break-p ()
  "Is the underlying `;' a phrase break?"
  ;; Difference from OCaml, the phrase separator is a single semi-colon
  (and (not (eobp))
       (char-equal ?\; (following-char))))

(defun reason-skip-to-close-delimiter (&optional limit)
  "Skip to the end of a Reason block.
It basically calls `re-search-forward` in order to go to any
closing delimiter, not concerning itself with balancing of any
sort.  Client code needs to check that.
LIMIT is passed to `re-search-forward` directly."
  (re-search-forward "\\s)" limit 'move))

(defun reason-skip-back-to-open-delimiter (&optional limit)
  "Skip to the beginning of a Reason block backwards.
It basically calls `re-search-backward` in order to go to any
opening delimiter, not concerning itself with balancing of any
sort.  Client code needs to check that.
LIMIT is passed to `re-search-backward` directly."
  (re-search-backward "\\s(" limit 'move))

(defun reason-find-phrase-end ()
  "Skip to the end of a phrase."
  (while (and (not (eobp))
              (not (reason-at-phrase-break-p)))
    (if (re-search-forward ";" nil 'move)
        (progn (when (reason-inside-block-scope-p)
                 (reason-skip-to-close-delimiter))
               (goto-char (1- (point))))
      ;; avoid infinite loop at the end of the buffer
      (re-search-forward "[[:space:]\\|\n]+" nil 'move)))
  (min (goto-char (1+ (point))) (point-max)))

(defun reason-skip-blank-and-comments ()
  "Skip blank spaces and comments."
  (cond
   ((eobp) (point))
   ((or (reason-in-between-comment-chars-p)
        (reason-looking-at-comment-delimiters-p)) (progn
                                                    (reason-forward-char 1)
                                                    (reason-skip-blank-and-comments)))
   ((reason-in-between-comment-delimiters-p) (progn
                                               (search-forward "*/" nil t)
                                               (reason-skip-blank-and-comments)))
   ((eolp) (progn
             (reason-forward-char 1)
             (reason-skip-blank-and-comments)))
   (t (progn (skip-syntax-forward " ")
             (point)))))

(defun reason-skip-back-blank-and-comments ()
  "Skip blank spaces and comments backwards."
  (cond
   ((bobp) (point))
   ((looking-back reason-comment-delimiter-regexp) (progn
                                                     (reason-backward-char 1)
                                                     (reason-skip-back-blank-and-comments)))
   ((reason-in-between-comment-delimiters-p) (progn
                                               (search-backward "/*" nil t)
                                               (reason-backward-char 1)
                                               (reason-skip-back-blank-and-comments)))
   ((or (reason-in-between-comment-chars-p)
        (reason-looking-at-comment-delimiters-p)) (progn
                                                    (reason-backward-char 1)
                                                    (reason-skip-back-blank-and-comments)))
   ((bolp) (progn
             (reason-backward-char 1)
             (reason-skip-back-blank-and-comments)))
   (t (progn (skip-syntax-backward " ")
             (point)))))

(defun reason-ro (&rest words)
  "Build a regex matching iff at least a word in WORDS is present."
  (concat "\\<" (regexp-opt words t) "\\>"))

(defconst reason-find-phrase-beginning-regexp
  (concat (reason-ro "end" "type" "module" "sig" "struct" "class"
                     "exception" "open" "let")
          "\\|^#[ \t]*[a-z][_a-z]*\\>\\|;"))

(defun reason-at-phrase-start-p ()
  "Return t if is looking at the beginning of a phrase.
A phrase starts when a toplevel keyword is at the beginning of a line."
  (or (looking-at "#")
      (looking-at reason-find-phrase-beginning-regexp)))

(defun reason-find-phrase-beginning-backward ()
  "Find the beginning of a phrase and return point.
It scans code backwards, therefore the caller can assume that the
beginning of the phrase (if found) is always before the starting
point.  No error is signalled and (point-min) is returned when a
phrease cannot be found."
  (beginning-of-line)
  (while (and (not (bobp)) (not (reason-at-phrase-start-p)))
    (if (reason-inside-block-scope-p)
        (reason-skip-back-to-open-delimiter)
      (re-search-backward reason-find-phrase-beginning-regexp nil 'move)))
  (point))

(defun reason-discover-phrase ()
  "Discover a Reason phrase in the buffer."
  ;; TODO reason-with-internal-syntax ;; tuareg2 modifies the syntax table (removed for now)
  ;; TODO stop-at-and feature for phrase detection (do we need it?)
  ;; TODO tuareg2 has some custom logic for module and class (do we need it?)
  (save-excursion
    (let ((case-fold-search nil))
      (reason-skip-blank-and-comments)
      (list (reason-find-phrase-beginning-backward) ;; beginning
            (reason-find-phrase-end)                ;; end
            (save-excursion                         ;; end-with-comment
              (reason-skip-blank-and-comments)
              (point))))))

(defun reason-discover-phrase-debug ()
  "Discover a Reason phrase in the buffer (debug mode)."
  (let ((triple (reason-discover-phrase)))
    (message (concat "Evaluating: \"" (reason-fetch-phrase triple) "\""))
    triple))

(defun reason-fetch-phrase (triple)
  "Fetch the phrase text given a TRIPLE."
  (let* ((start (nth 0 triple))
         (end (nth 1 triple))) ;; we don't need end-with-comment
    (buffer-substring-no-properties start end)))

(defun reason-next-phrase ()
  "Skip to the beginning of the next phrase."
  (cond
   ((reason-at-phrase-start-p) (point))
   ((eolp) (progn
             (forward-char 1)
             (reason-skip-blank-and-comments)
             (reason-next-phrase)))
   ((reason-inside-block-scope-p) (progn (reason-skip-to-close-delimiter)
                                         (reason-next-phrase)))
   ((looking-at ";") (progn
                       (forward-char 1)
                       (reason-next-phrase)))
   (t (progn (end-of-line)
             (reason-next-phrase)))))

(provide 'reason-interaction)

;;; reason-interaction.el ends here