-
Notifications
You must be signed in to change notification settings - Fork 10
/
purescript-show.el
259 lines (237 loc) · 10.9 KB
/
purescript-show.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
;;; purescript-show.el --- A pretty printer for PureScript Show values -*- lexical-binding: t -*-
;; Copyright (C) 2011 Chris Done
;; Author: Chris Done <[email protected]>
;; This file is not part of GNU Emacs.
;; This program 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 of the
;; License, or (at your option) any later version.
;; This program 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:
;; It doesn't support some number literals (probably). I'm not
;; precisely sure what values Show will always produce. There is
;; however a test suite available, so patches for extra Show support
;; is welcome and should be easy to test.
;;; Code:
(defvar sexp-show "sexp-show")
(require 'purescript-string)
(require 'cl-lib)
(defun purescript-show-replace-region ()
"Replace the given region with a pretty printed version."
(interactive)
(purescript-show-replace (region-beginning) (region-end)))
;;;###autoload
(defun purescript-show-replace (start end)
"Replace the given region containing a Show value with a pretty
printed collapsible version."
(let ((text (buffer-substring-no-properties start end)))
(goto-char start)
(delete-region start end)
(purescript-show-parse-and-insert text)))
;;;###autoload
(defun purescript-show-parse-and-insert (given)
"Parse a `string' containing a Show instance value and insert
it pretty printed into the current buffer."
(when (not (string= "" (purescript-trim given)))
(let ((current-column (- (point)
(line-beginning-position)))
(result (purescript-show-parse given)))
(if (string-match "^[\\(]" result)
(let ((v (read result)))
(if (equal (car v) 'arbitrary)
(insert given)
(purescript-show-insert-pretty current-column v)))
(insert given)))))
;;;###autoload
(defun purescript-show-parse (given)
"Parse the given input into a tree."
(with-temp-buffer
(insert given)
(shell-command-on-region
(point-min)
(point-max)
sexp-show
t)
(buffer-substring-no-properties (point-min) (point-max))))
(defun purescript-show-insert-pretty (column tree &optional parens)
"Insert a Show `tree' into the current buffer with collapsible nodes."
(cl-case (car tree)
('list (let ((start (point)))
(insert "[")
(purescript-show-mapcar/i (lambda (x i len)
(purescript-show-insert-pretty (+ column 1) x)
(unless (> i (- len 2))
(if (< (+ column (length (purescript-show-pretty tree parens)))
80)
(insert ",")
(insert (concat ",\n" (purescript-show-indent (+ 1 column) ""))))))
(cdr tree))
(insert "]")))
('tuple (let ((start (point)))
(insert "(")
(purescript-show-mapcar/i (lambda (x i len)
(purescript-show-insert-pretty (+ column 1) x)
(unless (> i (- len 2))
(if (< (+ column (length (purescript-show-pretty tree parens)))
80)
(insert ",")
(insert (concat ",\n" (purescript-show-indent (+ 1 column) ""))))))
(cdr tree))
(insert ")")))
('record
(let ((record (cdr tree)) (overlay (list 'nil)))
(insert (if parens "(" ""))
(let ((link-start (point)))
(insert (car record))
(let ((button (make-text-button link-start (point) :type 'purescript-show-toggle-button)))
(put-text-property link-start (point) 'face 'font-lock-type-face)
(button-put button 'overlay overlay)))
(insert " {\n")
(let ((curly-start (1- (point)))
(show-len (+ column (length (purescript-show-pretty tree parens)))))
(purescript-show-mapcar/i (lambda (field i len)
(insert
(purescript-show-indent
(if (and (> i 0) (< show-len 80)) 0 column)
(car field)))
(insert " = ")
(put-text-property (- (point) 3) (point) 'face
'font-lock-constant-face)
(purescript-show-insert-pretty
(if (< show-len 80)
0
(+ (length (car field)) column 3))
(cdr field))
(unless (> i (- len 2))
(if (< show-len 80)
(insert ", ")
(insert ",\n"))))
(cdr record))
(insert (concat "\n" (purescript-show-indent column "}")))
(progn
(setf (car overlay) (make-overlay curly-start (- (point) 1) nil t))
(overlay-put (car overlay) 'invisible t))
(insert (if parens ")" "")))))
('num (let ((num-start (point)))
(insert (format "%d" (cdr tree)))
(put-text-property num-start (point) 'face 'font-lock-constant-face)))
('string (let ((str-start (point)))
(insert "\"")
(if (< (+ column (length (cdr tree))) 60)
(progn
(insert (format "%s" (cdr tree)))
(put-text-property (+ 1 str-start) (point) 'face 'font-lock-string-face))
(progn
(insert "…")
(insert (format "%s" (cdr tree)))
(let ((overlay (make-overlay (+ 2 str-start) (point) nil t)))
(overlay-put overlay 'invisible t)
(put-text-property (+ 2 str-start) (point) 'face 'font-lock-string-face)
(let ((button (make-text-button (+ 1 str-start) (+ 2 str-start)
:type 'purescript-show-toggle-button)))
(put-text-property (+ 1 str-start) (+ 2 str-start)
'face 'font-lock-keyword-face)
(button-put button 'overlay (list overlay))
(button-put button 'hide-on-click t)))))
(insert "\"")))
('data (let ((data (cdr tree)))
(insert (if parens "(" ""))
(let ((cons-start (point)))
(insert (car data))
(put-text-property cons-start (point) 'face 'font-lock-type-face))
(unless (null (cdr data))
(progn (insert " ")
(purescript-show-mapcar/i
(lambda (x i len)
(purescript-show-insert-pretty column x t)
(unless (> i (- len 2))
(insert " ")))
(cdr data))))
(insert (if parens ")" ""))))
('char (progn (insert "'")
(insert (char-to-string (cdr tree)))
(put-text-property (- (point) 1) (point) 'face 'font-lock-string-face)
(insert "'")))
('arbitrary (let ((start (point)))
(insert (cdr tree))
(put-text-property start (point) 'face 'font-lock-comment-face)))
(otherwise (error "Unsupported node type: %S" tree))))
(define-button-type 'purescript-show-toggle-button
'action 'purescript-show-toggle-button-callback
'follow-link t
'help-echo "Click to expand…")
(defun purescript-show-toggle-button-callback (btn)
"The callback to toggle the overlay visibility."
(let ((overlay (button-get btn 'overlay)))
(when overlay
(overlay-put (car overlay)
'invisible (not (overlay-get (car overlay)
'invisible)))))
(let ((hide (button-get btn 'remove-on-click)))
(when hide
(button-put btn 'invisible t))))
(defun purescript-show-pretty (tree &optional parens)
"Show a Show `tree'."
(cl-case (car tree)
('list (format "[%s]"
(mapconcat
(lambda (x)
(purescript-show-pretty x))
(cdr tree)
",")))
('record (let ((record (cdr tree)))
(format "%s%s {%s}%s"
(if parens "(" "")
(car record)
(mapconcat (lambda (field)
(format "%s = %s"
(car field)
(purescript-show-pretty (cdr field))))
(cdr record)
", ")
(if parens ")" ""))))
('num (format "%s" (cdr tree)))
('string (format "%S" (cdr tree)))
('data (let ((data (cdr tree)))
(format "%s%s%s%s"
(if parens "(" "")
(car data)
(if (null (cdr data))
""
(concat " "
(mapconcat
(lambda (x) (purescript-show-pretty x t))
(cdr data)
" ")))
(if parens ")" ""))))
('tuple (format "(%s)"
(mapconcat
(lambda (x)
(purescript-show-pretty x))
(cdr tree)
",")))
('char (format "'%s'" (if (= (cdr tree) ?')
"\\'"
(char-to-string (cdr tree)))))
('arbitrary (cdr tree))
(otherwise (error "Unsupported node type: %S" tree))))
(defun purescript-show-mapcar/i (f xs)
"Map `f' across `xs' giving the index and length to `f' as extra parameters."
(let ((len (length xs))
(i 0))
(mapcar (lambda (x)
(funcall f x i len)
(setq i (1+ i)))
xs)))
(defun purescript-show-indent (n s)
"Indent a string `s' at colum `n'."
(concat (make-string n ? )
s))
(provide 'purescript-show)
;;; purescript-show.el ends here