-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathshm-case-split.el
318 lines (286 loc) · 11.4 KB
/
shm-case-split.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
;;; shm-case-split.el --- Case splitting functionality
;; Copyright (c) 2014 Chris Done. All rights reserved.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Produces a list of case alternatives from a sum type data
;; declaration.
;;; Code:
(require 'shm)
(require 'shm-ast)
(require 'haskell-process)
(defun shm-case-split-insert-pattern (alts)
"Takes the first alt in ALTS and inserts a pattern match for
it."
(when (car alts)
(let ((alt (car alts)))
(when (> (cdr alt) 0)
(insert "("))
(insert (car alt))
(loop for i from 1 to (cdr alt)
do (progn (insert " _")
(shm-evaporate (1- (point)) (point))))
(when (> (cdr alt) 0)
(insert ")")))))
(defun shm-case-split-insert-alts (alts)
"Inserts case alts for the given ALTS. It will create
evaporating slots for each part. E.g.
case x of
|
for data Maybe a = Just a | Nothing will insert
case x of
Just _ -> undefined
Nothing -> undefined
Where the _ and undefineds are evaporating slots."
(let ((column (current-column)))
(loop for alt in alts
do (progn (when (/= column (current-column))
(insert "\n")
(indent-to column))
(insert (car alt))
(loop for i from 1 to (cdr alt)
do (progn (insert " _")
(shm-evaporate (1- (point)) (point))))
(insert " -> undefined")
(shm-evaporate (- (point) (length "undefined"))
(point))))))
(defun shm-case-split-alts-from-data-decl (string)
"Given a data declaration STRING, generate a list of alternatives."
(with-temp-buffer
(insert (replace-regexp-in-string
"[A-Z][a-zA-Z0-9_'.]+?\\."
""
(replace-regexp-in-string
"[a-zA-Z0-9]+-[0-9.]+:"
""
string)))
(text-mode)
(structured-haskell-mode)
(setq shm-last-parse-start (point-max))
(setq shm-last-parse-end (point-min))
(shm/reparse)
(mapcar #'shm-case-split-name-and-arity
(shm-case-split-get-constructors))))
(defun shm-case-split-generate-alt (cons)
"Generate an alt from the given NODE-PAIR."
(let ((name (car cons))
(arity (cdr cons)))
(format "%s%s"
name
(apply 'concat
(loop for i from 1 to arity
collect " _")))))
(defun shm-case-split-name-and-arity (node-pair)
"Get the constructor name and arity of the given constructor NODE-PAIR."
(let* ((parent (shm-node-child-pair node-pair))
(name-node (shm-node-child parent)))
(goto-char (shm-node-end name-node))
(cons (shm-node-string name-node)
(or (when (/= (shm-node-end name-node)
(shm-node-end (cdr parent)))
(shm/forward-node)
(shm/reparse)
(let ((n 0)
(last-node 0)
(current-pair (shm-current-node-pair)))
(while (and (/= (point) (point-max))
current-pair
(= (car parent)
(car (shm-node-parent current-pair))))
(when (/= (car current-pair)
last-node)
(setq n (1+ n))
(setq last-node (car current-pair)))
(unless (= (point)
(point-max))
(shm/forward-node)
(shm/reparse)
(setq current-pair (shm-current-node-pair))))
n))
0))))
(defun shm-case-split-get-constructors ()
"Get a list of constructors."
(goto-char (point-min))
(or (search-forward "= " nil t 1)
(error "Couldn't find any constructors (searched for '=')."))
(let ((conses (list)))
(while (/= (point) (point-max))
(let ((cons (shm-case-split-get-constructor)))
(when cons
(setq conses (cons cons conses)))))
(reverse conses)))
(defun shm-case-split-get-constructor ()
"Get the constructor at point."
(shm/reparse)
(let ((cons-pair (shm-node-ancestor-at-point (shm-current-node-pair)
(point))))
(goto-char (shm-node-end (cdr cons-pair)))
(or (search-forward "| " nil t 1)
(goto-char (point-max)))
cons-pair))
;; Backend based on haskell-process.el
(defun shm-trim-string (string)
"Remove white spaces in beginning and ending of STRING.
White space here is any of: space, tab, emacs newline (line feed, ASCII 10)."
(replace-regexp-in-string "\\`[ \t\n]*" "" (replace-regexp-in-string "[ \t\n]*\\'" "" string)))
(defun haskell-process-get-type (expr)
"Get the type of the given expression or name."
(let ((reply
(haskell-process-queue-sync-request (haskell-process)
(format ":t %s\n" expr))))
(shm-trim-string (car (last (split-string reply " :: "))))))
(defun shm-cleanup-type-string-for-case (s)
"Remove constraints and replace polymorphic type variables with
() to allow shm/case-split to work in more cases."
(let* ((clean-s (car
(last
(mapcar 'shm-trim-string
(split-string s "=>"))))))
(if s
(let ((case-fold-search nil))
(replace-regexp-in-string "\\b[a-z_][A-Za-z_]*\\b" "()" clean-s))
s)))
(defun haskell-process-get-data-type (name)
"Get the data type definition of the given name."
(let ((reply
(haskell-process-queue-sync-request (haskell-process)
(format ":i %s\n" name))))
(car (split-string reply "[\n\t ]+-- Defined "))))
(defun shm/case-split (name &optional expr-string)
"Prompt for a type then do a case split based on it."
(interactive (list (read-from-minibuffer "Type: ")))
(save-excursion
(let ((column (current-column))
(case-expr (if expr-string
expr-string
"undefined")))
(insert (concat "case " case-expr " "))
(if (not expr-string)
(shm-evaporate (- (point) (+ 1 (length "undefined")))
(- (point) 1)))
(insert "of\n")
(indent-to (+ column 2))
(shm-case-split-insert-alts
(shm-case-split-alts-from-data-decl
(haskell-process-get-data-type name))))))
(defun shm/case-split-shm-node ()
"Do a case split based on the current node expression type."
(interactive)
(let* ((expr (shm-current-node-string))
(expr-type (haskell-process-get-type expr))
(clean-expr (shm-cleanup-type-string-for-case expr-type)))
(if expr-type
(progn
(shm/kill-node)
(shm/case-split clean-expr expr)))))
(defun shm/do-case-split (arg)
"Without prefix, calculate type of current node expression and replace it
with a case expression based on its type. With prefix, insert a case expression based
on the type given at the prompt."
(interactive "P")
(if arg
(call-interactively 'shm/case-split)
(call-interactively 'shm/case-split-shm-node)))
(defun shm/expand-pattern (name)
"Expand a pattern match on a data type."
(interactive (list (read-from-minibuffer "Type: ")))
(save-excursion
(shm-case-split-insert-pattern
(shm-case-split-alts-from-data-decl
(haskell-process-get-data-type name)))))
(defun shm/case-split-completing-read (&optional expr-string)
(interactive)
"Using whichever `completing-read' function is available, this
will gather all the data types currently within the current
buffer (and also those given in the imports) that is loaded into
an interactive haskell session and present them in a list (the
manner in which is specified by `completing-read'). Upon
selection of a data type, the corresponding case statement for
that type will be inserted into the buffer. EXPR-STRING will be
used as the variable to match on in the case statement when it is
non-nil."
(let* ((err "Can't work with this type.")
(execute
(condition-case nil
(shm/case-split
(completing-read
"Choose a type: "
(shm-haskell-interactive-get-types))
expr-string)
(error err))))
(when execute
(delete-region (point-at-bol) (point-at-eol))
(delete-char 1)
execute)))
(defun shm-haskell-interactive-get-types ()
"When an interactive-haskell session is currently loaded,
gather all the data types necessarily loaded in the current
session."
(if (haskell-process)
(progn
(require 'rx)
(require 'dash)
(let* ((imports
(save-excursion
(goto-char (point-min))
(let (collect)
(while (re-search-forward
"^import \\(qualified\\)*\\s-+" nil t)
(setq collect
(cons
(buffer-substring-no-properties
(point)
(skip-chars-forward
(rx (or alphanumeric (any ".")))
(point-at-eol)))
collect)))
collect))))
(-filter
(lambda (str) (not (string= "" str)))
(split-string
(mapconcat
'identity
(-filter
(lambda (str) (not (string= "" str)))
(mapcar
(lambda (import)
(let ((reply
(haskell-process-queue-sync-request
(haskell-process)
(concat ":browse " import))))
(with-temp-buffer
(insert reply)
(keep-lines "^data" (point-min) (point-max))
(goto-char (point-min))
(haskell-mode)
(structured-haskell-mode -1)
(while (/= (point) (point-max))
(delete-char 5)
(forward-sexp 1)
(delete-region (point) (point-at-eol))
(forward-line 1))
(fundamental-mode)
(eod-region-remove-properties (point-min) (point-max))
(buffer-string))))
(cons "" ;the empty string is necessary
;so that the current module is
;searched
(if (member "Prelude" imports)
imports
(cons "Prelude" imports)))))
"")
"\n"))))
(error
"You do not have an interactive haskell session
loaded. Load an interactive haskell process by executing
M-x `haskell-session' or by pressing C-c
C-z (or M-x `haskell-interactive-switch').")))
(provide 'shm-case-split)