This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hie.el
362 lines (290 loc) · 10.5 KB
/
hie.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
;;; hie.el --- minor mode Haskell-In-Emacs (ide like stuff)
;; Copyright (C) 2012 Christopher Monsanto.
;; Author: Christopher Monsanto <[email protected]>
;; Maintainer: Christopher Monsanto <[email protected]>
;; Created: 15 Mar 2012
;; Version: 0.1
;; Keywords: haskell intellisense autocomplete
;; License: GPLv3
;;
(require 'cl)
(require 'haskell-mode)
;
(defvar hie-modules-dir "~/.hie/"
"Where should we look for global definitions?")
(defvar hie-modules-cache-dir "~/.hie-cache/"
"Where should we cache stuff?")
(defvar hie-update-interval 0.5
"How long should you be idle before we update?")
;
(defvar hie-mode-map
(let ((map (make-keymap)))
(define-key map (kbd "C-c h") 'hie-show-signature)
(define-key map (kbd "C-c j") 'hie-jump-to-definition)
map)
"hie-mode keymap")
(define-minor-mode hie-mode "Toggle hie mode." nil " hie" hie-mode-map
(hie-setup-buffer))
;
(defun hie-new-hash ()
"Create a hash table."
(make-hash-table :test 'equal))
(defvar hie-import-defs-hash (hie-new-hash)
"A hash table mapping hieimports to module hashes. We are not editing any of these files in Emacs. (Think system libraries).
If you DO edit this, you can clear all.")
(defvar hie-buffer-idents-hash nil
"A hash table mapping local identifiers to hiedefs.")
(make-variable-buffer-local 'hie-buffer-idents-hash)
(defvar hie-buffer-module-sxhash nil
"This is here to avoid setting up and destroying over and over...")
(make-variable-buffer-local 'hie-buffer-module-sxhash)
(defvar hie-buffer-imports nil
"This is here to avoid setting up and destroying over and over...")
(make-variable-buffer-local 'hie-buffer-imports)
(defvar hie-buffer-check nil)
(make-variable-buffer-local 'hie-buffer-check)
;
; Structures
;
(defstruct hieimport
name list alias is-qualified is-hidden)
(defstruct hiedef
name is-instance parent file line column signature help)
(defstruct hiemod
name defs imports exports)
;
; Main interactives
;
(defun hie-global-defshash ()
(if hie-mode
hie-buffer-idents-hash
hie-buffer-idents-hash))
(defun hie-initial-text ()
(when (gethash (haskell-ident-at-point)
(hie-global-defshash))
(haskell-ident-at-point)))
(defvar hie-read-ident-history nil)
(defun hie-read-ident (prompt &optional filter)
(let ((name (completing-read prompt
(hie-global-defshash)
filter
t
(hie-initial-text)
'hie-read-ident-history)))
(gethash name (hie-global-defshash))))
(defun hie-jump (def)
(find-file (hiedef-file def))
(goto-char (point-min))
(forward-line (1- (hiedef-line def)))
(forward-char (1- (hiedef-column def))))
(defun hie-jump-to-definition ()
"Jump to the given definition."
(interactive)
(let ((def (hie-read-ident "Jump to def: " (lambda (key def) (not (hiedef-is-instance def))))))
(when def
(hie-jump def))))
(defun hie-show-signature ()
"Show the signature in the mode line."
(interactive)
(let ((def (hie-read-ident "Signature: ")))
(when def
(display-message-or-buffer (hie-make-help def)))))
(defun hie-make-help (def)
(if (and (hiedef-signature def) (hiedef-help def))
(concat (hiedef-signature def) "\n\n" (hiedef-help def))
(or (hiedef-signature def) (hiedef-help def) "")))
;
(defun hie-load-file (file)
"Create completion table, etc for given module."
(setq *hie-load* nil)
(load file t t)
*hie-load*)
(defun hie-write-file (filename mod)
(with-temp-file filename
(insert (format "(setq *hie-load* %s)" (prin1-to-string mod)))))
(defun hie-is-exportable-module (mod)
(not (member (hiemod-name mod) (list nil "Main"))))
(defun hie-write-module (mod)
(hie-write-file (concat hie-modules-dir (hiemod-name mod)) mod)
; Delete the cache stuff
(condition-case nil
(delete-file (concat hie-modules-cache-dir (hiemod-name mod)))
('error nil))
(condition-case nil
(delete-file (concat hie-modules-cache-dir (hiemod-name mod) ".elc"))
('error nil)))
(defun hie-dump-defs (name defs)
(with-temp-file (concat hie-modules-cache-dir name)
(insert (format "(setq *hie-load* (list %s))"
(mapconcat 'identity
(loop for def in defs
collect (prin1-to-string def)) " "))))
(byte-compile-file (concat hie-modules-cache-dir name)))
(defun hie-import-defs (import)
; Try globals
(let ((defs (gethash import hie-import-defs-hash 'missing))
(name (hieimport-name import)))
(cond
((eq defs 'recursive-hack) nil)
((and (eq defs 'missing) (not (hieimport-alias import))) ; if we dont have the raw version, make it
(let ((defs (hie-load-file (concat hie-modules-cache-dir name))))
(if defs
(puthash import defs hie-import-defs-hash)
(let ((mod (hie-load-file
(concat hie-modules-dir name))))
(if mod
(progn
(puthash (make-hieimport :name name) 'recursive-hack hie-import-defs-hash)
(let ((defs (hie-resolve-exports-defs
(hiemod-exports mod)
(hie-resolve-imports-defs (hiemod-imports mod) (hiemod-defs mod)))))
(hie-dump-defs name defs)
(puthash (make-hieimport :name name) defs hie-import-defs-hash)))
(puthash (make-hieimport :name name) nil hie-import-defs-hash))))))
((eq defs 'missing) ; if this isn't the raw version, import the raw version and fix it
(let ((defs (hie-import-defs (make-hieimport :name name))))
(puthash import (if defs (hie-fix-raw-import-defs defs import) nil) hie-import-defs-hash)))
(t defs))))
(defun hie-fix-raw-import-defs (base-defs import)
(let* ((defs (if (hieimport-is-hidden import)
(reduce (lambda (rest exp)
(hie-filter-defs nil exp rest))
(hieimport-list import) :initial-value base-defs)
(loop for export in (hieimport-list import)
append (hie-filter-defs t export base-defs))))
(alias-defs (hie-alias-defs (hieimport-alias import) defs)))
(if (hieimport-is-qualified import)
alias-defs
(append defs alias-defs))))
(defun hie-resolve-exports-defs (exports defs)
(hie-filter-prefixes (loop for export in exports append
(hie-filter-defs t export defs))))
(defun hie-resolve-imports-defs (imports defs)
(append defs
(loop for import in imports
append (hie-import-defs import))))
(defun hie-has-prefix (prefix string)
(eq 0 (string-match
(concat "^" (regexp-quote prefix) "\\.") string)))
(defun hie-filter-prefixes (defs)
(loop for def in defs
collect
(progn
(setq def (copy-hiedef def))
(setf (hiedef-name def)
(replace-regexp-in-string "[A-Za-z0-9]+\\." "" (hiedef-name def)))
def)))
(defun hie-filter-defs (they-select export defs)
(destructuring-bind (tag name) export
(cond
((eq tag 'id)
(loop for def in defs
if (eq they-select (equal (hiedef-name def) name))
collect def))
((eq tag 'all)
(loop for def in defs
if (eq they-select (or (equal (hiedef-name def) name)
(equal (hiedef-parent def) name)))
collect def))
((eq tag 'mod)
(loop for def in defs
if (eq they-select (hie-has-prefix
name
(hiedef-name def)))
collect def)))))
(defun hie-alias-defs (new-scope defs)
"Strips out anything not in the namespace old-mod, and renames the rest to new-mod."
(loop for def in defs
collect (progn
(setq def (copy-hiedef def))
(setf (hiedef-name def)
(concat new-scope "." (hiedef-name def)))
def)))
; I'll make this "multithreaded" once I get lexically scoped variables... not everyone uses Emacs 24 I guess. :(
(defun hie-run ()
"Runs hie and loads the module it generates."
(let ((file (make-temp-file "hie")))
(call-process-region (point-min) (point-max) "hie" nil (list :file file) nil (buffer-file-name))
(hie-load-file file)))
;
(defun hie-populate-defshash (defs defshash)
(loop for def in defs do (puthash (hiedef-name def) def defshash)))
(defun hie-setup-buffer ()
"Prepare the buffer for hie."
(interactive)
(let* ((mod (hie-run))
(sx (sxhash mod)))
(setq hie-buffer-check nil)
(when (and mod (not (eq hie-buffer-module-sxhash sx)))
(setq hie-buffer-module-sxhash sx)
;; local business
(setq hie-buffer-idents-hash (hie-new-hash))
(loop for import in (hiemod-imports mod)
do (hie-populate-defshash (hie-import-defs import) hie-buffer-idents-hash))
(hie-populate-defshash (hiemod-defs mod) hie-buffer-idents-hash)
(setq hie-buffer-imports (loop for import in (hiemod-imports mod) collect (hieimport-name import)))
;; global business
mod)))
(defun hie-save-buffer (mod)
(hie-write-module mod)
(loop for buffer in (buffer-list)
unless (equal buffer (current-buffer)) do
(with-current-buffer buffer
(when (and hie-mode (member (hiemod-name mod) hie-buffer-imports))
(setq hie-buffer-check t)
(setq hie-buffer-module-sxhash 0))))
(loop for import being the hash-keys in hie-import-defs-hash
do (when (equal (hieimport-name import) (hiemod-name mod))
(remhash import hie-import-defs-hash))))
;; Autocomplete stuff
(defun hie-ac-candidates ()
(let (candidates)
(maphash
(lambda (name def)
(unless (hiedef-is-instance def)
(setq candidates (cons name candidates))))
hie-buffer-idents-hash)
candidates))
(defun hie-source-prefix ()
(let ((p (point)))
(save-excursion
(beginning-of-line)
(unless (looking-at "^[ \t]*import")
(goto-char p)
(ac-prefix-symbol)))))
(defun hie-ac-document (name)
(let ((def (gethash name hie-buffer-idents-hash)))
(hie-make-help def)))
(defvar ac-source-hie
'((candidates . hie-ac-candidates)
(prefix . hie-source-prefix)
(document . hie-ac-document)))
(defvar ac-source-hie-no-quick-help
'((candidates . hie-ac-candidates)
(prefix . hie-source-prefix)))
(defun hie-ac-module-candidates ()
(directory-files hie-modules-dir nil "^[^.]" t))
(defvar ac-source-hie-modules
'((candidates . hie-ac-module-candidates)
(prefix . "^[ \t]*import[ \t]*\\(.*\\)")
(cache)))
;
(defun hie-try-idle-update ()
"Try to update the current buffer when we're idle."
(when (and hie-mode hie-buffer-check)
(hie-setup-buffer)))
(defun hie-try-save-update ()
"Try to update the current buffer when we're idle."
(when hie-mode
(let ((mod (hie-setup-buffer)))
(when (and mod (hie-is-exportable-module mod))
(hie-save-buffer mod)))))
(defun hie-allow-update (b e l)
(setq hie-buffer-check t))
(defvar hie-idle-timer nil)
(when hie-idle-timer
(cancel-timer hie-idle-timer))
(setq hie-idle-timer (run-with-idle-timer hie-update-interval t 'hie-try-idle-update))
(add-hook 'after-save-hook 'hie-try-save-update)
(add-hook 'after-change-functions 'hie-allow-update)
(provide 'hie)