1
1
; ;; haskell-completions.el --- Haskell Completion package -*- lexical-binding : t -*-
2
2
3
- ; ; Copyright © 2015 Athur Fayzrakhmanov. All rights reserved.
3
+ ; ; Copyright © 2015-2016 Athur Fayzrakhmanov. All rights reserved.
4
4
5
5
; ; This file is part of haskell-mode package.
6
6
; ; You can contact with authors using GitHub issue tracker:
40
40
(require 'haskell-process )
41
41
(require 'haskell-interactive-mode )
42
42
43
- (defvar haskell-completions-pragma-names
43
+ (defvar haskell-completions-- pragma-names
44
44
(list " DEPRECATED"
45
45
" INCLUDE"
46
46
" INCOHERENT"
63
63
" WARNING" )
64
64
" A list of supported pragmas.
65
65
This list comes from GHC documentation (URL
66
- `https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/pragmas.html' .
67
- " )
66
+ `https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/pragmas.html' ." )
67
+
68
+ (defvar haskell-completions--keywords
69
+ (list
70
+ " as"
71
+ " case"
72
+ " class"
73
+ " data family"
74
+ " data instance"
75
+ " data"
76
+ " default"
77
+ " deriving instance"
78
+ " deriving"
79
+ " do"
80
+ " else"
81
+ " family"
82
+ " forall"
83
+ " foreign import"
84
+ " foreign"
85
+ " hiding"
86
+ " if"
87
+ " import qualified"
88
+ " import"
89
+ " in"
90
+ " infix"
91
+ " infixl"
92
+ " infixr"
93
+ " instance"
94
+ " let"
95
+ " mdo"
96
+ " module"
97
+ " newtype"
98
+ " of"
99
+ " proc"
100
+ " qualified"
101
+ " rec"
102
+ " then"
103
+ " type family"
104
+ " type instance"
105
+ " type"
106
+ " where" )
107
+ " A list of Haskell's keywords (URL `https://wiki.haskell.org/Keywords' ).
108
+ Single char keywords and operator like keywords are not included
109
+ in this list." )
110
+
68
111
69
112
(defun haskell-completions-can-grab-prefix ()
70
113
" Check if the case is appropriate for grabbing completion prefix.
@@ -206,9 +249,20 @@ identifier at point depending on result of function
206
249
Returns a list of form '(prefix-start-position
207
250
prefix-end-position prefix-value prefix-type) depending on
208
251
situation, e.g. is it needed to complete pragma, module name,
209
- arbitrary identifier, etc. Returns nil in case it is
252
+ arbitrary identifier, etc. Returns nil in case it is
210
253
impossible to grab prefix.
211
254
255
+ Possible prefix types are:
256
+
257
+ * haskell-completions-pragma-name-prefix
258
+ * haskell-completions-ghc-option-prefix
259
+ * haskell-completions-language-extension-prefix
260
+ * haskell-completions-module-name-prefix
261
+ * haskell-completions-identifier-prefix
262
+ * haskell-completions-general-prefix
263
+
264
+ the last type is used in cases when completing things inside comments.
265
+
212
266
If provided optional MINLEN parameter this function will return
213
267
result only if prefix length is not less than MINLEN."
214
268
(when (haskell-completions-can-grab-prefix)
@@ -220,36 +274,77 @@ result only if prefix length is not less than MINLEN."
220
274
prefix))
221
275
(prefix prefix)))))
222
276
277
+ (defun haskell-completions--simple-completions (prefix )
278
+ " Provide a list of completion candidates for given PREFIX.
279
+ This function is used internally in
280
+ `haskell-completions-completion-at-point' and
281
+ `haskell-completions-sync-repl-completion-at-point' .
282
+
283
+ It provides completions for haskell keywords, language pragmas,
284
+ GHC's options, and language extensions.
285
+
286
+ PREFIX should be a list such one returned by
287
+ `haskell-completions-grab-identifier-prefix' ."
288
+ (cl-destructuring-bind (beg end _pfx typ) prefix
289
+ (let ((candidates
290
+ (cl-case typ
291
+ ('haskell-completions-pragma-name-prefix
292
+ haskell-completions--pragma-names)
293
+ ('haskell-completions-ghc-option-prefix
294
+ haskell-ghc-supported-options)
295
+ ('haskell-completions-language-extension-prefix
296
+ haskell-ghc-supported-extensions)
297
+ (otherwise
298
+ haskell-completions--keywords))))
299
+ (list beg end candidates))))
300
+
301
+
302
+ (defun haskell-completions-completion-at-point ()
303
+ " Provide completion list for thing at point.
304
+ This function is used in non-interactive `haskell-mode' . It
305
+ provides completions for haskell keywords, language pragmas,
306
+ GHC's options, and language extensions, but not identifiers."
307
+ (let ((prefix (haskell-completions-grab-prefix)))
308
+ (haskell-completions--simple-completions prefix)))
309
+
310
+ (defun haskell-completions-sync-repl-completion-at-point ()
311
+ " A completion function used in `interactive-haskell-mode' .
312
+ Completion candidates are provided quering current haskell
313
+ process, that is sending `:complete repl' command.
314
+
315
+ Completes all possible things: everything that can be completed
316
+ with non-interactive function
317
+ `haskell-completions-completion-at-point' plus identifier
318
+ completions.
223
319
224
- (defun haskell-completions-sync-completions-at-point ()
225
- " A `completion-at-point' function using the current haskell process.
226
320
Returns nil if no completions available."
227
321
(let ((prefix-data (haskell-completions-grab-prefix)))
228
322
(when prefix-data
229
323
(cl-destructuring-bind (beg end pfx typ) prefix-data
230
- (let ((imp (eql typ 'haskell-completions-module-name-prefix ))
231
- lst)
232
- (setq lst
233
- (cl-case typ
234
- ; ; non-interactive completions first
235
- ('haskell-completions-pragma-name-prefix
236
- haskell-completions-pragma-names)
237
- ('haskell-completions-ghc-option-prefix
238
- haskell-ghc-supported-options)
239
- ('haskell-completions-language-extension-prefix
240
- haskell-ghc-supported-extensions)
241
- (otherwise
242
- (when (and
243
- (not (eql typ 'haskell-completions-general-prefix ))
244
- (haskell-session-maybe )
245
- (not
246
- (haskell-process-cmd (haskell-interactive-process))))
247
- ; ; if REPL is available and not busy try to query it
248
- ; ; for completions list in case of module name or
249
- ; ; identifier prefixes
250
- (haskell-completions-sync-complete-repl pfx imp)))))
251
- (when lst
252
- (list beg end lst)))))))
324
+ (when (not (eql typ 'haskell-completions-general-prefix ))
325
+ ; ; do not complete things in comments
326
+ (if (cl-member
327
+ typ
328
+ '(haskell-completions-pragma-name-prefix
329
+ haskell-completions-ghc-option-prefix
330
+ haskell-completions-language-extension-prefix))
331
+ ; ; provide simple completions
332
+ (haskell-completions--simple-completions prefix-data)
333
+ ; ; only two cases left: haskell-completions-module-name-prefix
334
+ ; ; and haskell-completions-identifier-prefix
335
+ (let* ((is-import (eql typ 'haskell-completions-module-name-prefix ))
336
+ (candidates
337
+ (when (and (haskell-session-maybe )
338
+ (not (haskell-process-cmd
339
+ (haskell-interactive-process))))
340
+ ; ; if REPL is available and not busy try to query it for
341
+ ; ; completions list in case of module name or identifier
342
+ ; ; prefixes
343
+ (haskell-completions-sync-complete-repl pfx is-import))))
344
+ ; ; append candidates with keywords
345
+ (list beg end (append
346
+ candidates
347
+ haskell-completions--keywords)))))))))
253
348
254
349
(defun haskell-completions-sync-complete-repl (prefix &optional import )
255
350
" Return completion list for given PREFIX querying REPL synchronously.
0 commit comments