This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
/
intero.el
1321 lines (1187 loc) · 50.7 KB
/
intero.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; intero.el --- Complete development mode for Haskell
;; Copyright (c) 2016 Chris Done
;; Copyright (c) 2015 Athur Fayzrakhmanov
;; Copyright (c) 2013 Herbert Valerio Riedel
;; Copyright (c) 2007 Stefan Monnier
;; Author: Chris Done <chrisdone@fpcomplete.com>
;; Maintainer: Chris Done <chrisdone@fpcomplete.com>
;; URL: https://github.com/commercialhaskell/intero
;; Created: 3rd June 2016
;; Version: 0.1.13
;; Keywords: haskell, tools
;; Package-Requires: ((flycheck "0.25") (company "0.8") (emacs "24.3") (haskell-mode "13.0"))
;; 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Mode that enables:
;;
;; * Flycheck type checking ✓
;; * Company mode completion ✓
;; * Go to definition ✓
;; * Type of selection ✓
;; * Info ✓
;; * REPL
;; * Find uses
;; * Completion of stack targets
;; * List all types in all expressions
;; * Import management
;; * Dependency management
;;; Code:
(require 'flycheck)
(require 'cl-lib)
(require 'company)
(require 'comint)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Configuration
(defconst intero-package-version "0.1.13"
"Package version to auto-install.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Modes
(defvar intero-mode-map (make-sparse-keymap)
"Intero minor mode's map.")
;;;###autoload
(define-minor-mode intero-mode
"Minor mode for Intero
\\{intero-mode-map}"
:lighter " Intero"
:keymap intero-mode-map
(when (bound-and-true-p interactive-haskell-mode)
(when (fboundp 'interactive-haskell-mode)
(message "Disabling interactive-haskell-mode ...")
(interactive-haskell-mode -1)))
(when (intero-buffer-file-name)
(if intero-mode
(progn (flycheck-select-checker 'intero)
(flycheck-mode)
(add-to-list (make-local-variable 'company-backends) 'company-intero)
(company-mode)
(set (make-local-variable 'eldoc-documentation-function) 'eldoc-intero))
(message "Intero mode disabled."))))
(define-key intero-mode-map (kbd "C-c C-t") 'intero-type-at)
(define-key intero-mode-map (kbd "C-c C-i") 'intero-info)
(define-key intero-mode-map (kbd "M-.") 'intero-goto-definition)
(define-key intero-mode-map (kbd "C-c C-l") 'intero-repl-load)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Global variables/state
(defvar intero-global-mode nil
"Global mode is enabled?")
(defun global-intero-mode ()
"Enable Intero on all Haskell mode buffers."
(interactive)
(setq intero-global-mode (not intero-global-mode))
(if intero-global-mode
(add-hook 'haskell-mode-hook 'intero-mode)
(remove-hook 'haskell-mode-hook 'intero-mode))
(when (eq this-command 'global-intero-mode)
(message "Intero mode is now %s on all future Haskell buffers."
(if intero-global-mode
"enabled" "disabled"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Buffer-local variables/state
(defvar intero-callbacks (list)
"List of callbacks waiting for output. FIFO.")
(make-variable-buffer-local 'intero-callbacks)
(defvar intero-arguments (list)
"Arguments used to call the stack process.")
(make-variable-buffer-local 'intero-arguments)
(defvar intero-targets (list)
"Targets used for the stack process.")
(make-variable-buffer-local 'intero-targets)
(defvar intero-source-buffer (list)
"Buffer from which Intero was first requested to start.")
(make-variable-buffer-local 'intero-source-buffer)
(defvar intero-project-root nil
"The project root of the current buffer.")
(make-variable-buffer-local 'intero-project-root)
(defvar intero-package-name nil
"The package name associated with the current buffer.")
(make-variable-buffer-local 'intero-package-name)
(defvar intero-deleting nil
"The process of the buffer is being deleted.")
(make-variable-buffer-local 'intero-deleting)
(defvar intero-give-up nil
"The backend could not start, or could not be
installed. Abandon trying to automate it. The user will have to
manually run M-x intero-restart or M-x intero-targets to
destroy the buffer and create a fresh one without this variable
enabled.")
(make-variable-buffer-local 'intero-give-up)
(defvar intero-try-with-build nil
"Try starting intero without --no-build. Slower, but will build
required dependencies.")
(make-variable-buffer-local 'intero-try-with-build)
(defvar intero-starting nil
"Is the intero process starting up?")
(make-variable-buffer-local 'intero-starting)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interactive commands
(defun intero-list-buffers ()
"List hidden process buffers created by intero.
You can use this to kill them or look inside."
(interactive)
(let ((buffers (cl-remove-if-not
(lambda (buffer)
(string-match " intero:" (buffer-name buffer)))
(buffer-list))))
(if buffers
(display-buffer
(list-buffers-noselect
nil
buffers))
(error "There are no Intero process buffers."))))
(defun intero-cd ()
"Change directory in the backend process."
(interactive)
(intero-async-call
'backend
(concat ":cd "
(read-directory-name "Change Intero directory: "))))
(defun intero-type-at (insert)
"Get the type of the thing or selection at point."
(interactive "P")
(let ((ty (apply #'intero-get-type-at (intero-thing-at-point))))
(if insert
(save-excursion
(goto-char (line-beginning-position))
(insert
(format "%s\n"
(with-temp-buffer
(when (fboundp 'haskell-mode)
(haskell-mode))
(insert ty)
(font-lock-fontify-buffer)
(buffer-string)))))
(message
"%s"
(with-temp-buffer
(when (fboundp 'haskell-mode)
(haskell-mode))
(insert ty)
(font-lock-fontify-buffer)
(buffer-string))))))
(defun intero-info (insert)
"Get the info of the thing at point."
(interactive "P")
(let ((info (intero-get-info-of (intero-ident-at-point))))
(message
"%s"
(with-temp-buffer
(when (fboundp 'haskell-mode)
(haskell-mode))
(insert info)
(font-lock-fontify-buffer)
(buffer-string)))))
(defun intero-goto-definition ()
"Jump to the definition of the thing at point."
(interactive)
(let ((result (apply #'intero-get-loc-at (intero-thing-at-point))))
(when (string-match "\\(.*?\\):(\\([0-9]+\\),\\([0-9]+\\))-(\\([0-9]+\\),\\([0-9]+\\))$"
result)
(if (fboundp 'xref-push-marker-stack) ;; Emacs 25
(xref-push-marker-stack)
(with-no-warnings
(ring-insert find-tag-marker-ring (point-marker))))
(let ((file (match-string 1 result))
(line (string-to-number (match-string 2 result)))
(col (string-to-number (match-string 3 result))))
(find-file file)
(pop-mark)
(goto-char (point-min))
(forward-line (1- line))
(forward-char (1- col))))))
(defun intero-restart ()
"Simply restart the process with the same configuration as before."
(interactive)
(when (intero-buffer-p 'backend)
(let ((targets (with-current-buffer (intero-buffer 'backend)
intero-targets)))
(intero-destroy 'backend)
(intero-get-worker-create 'backend targets (current-buffer)))))
(defun intero-targets ()
"Set the targets to use for stack ghci."
(interactive)
(let ((targets (split-string (read-from-minibuffer "Targets: ")
" "
t)))
(intero-destroy)
(intero-get-worker-create 'backend targets (current-buffer))))
(defun intero-destroy (&optional worker)
"Stop the current worker process and kill its associated."
(interactive)
(if worker
(intero-delete-worker worker)
(intero-delete-worker 'backend)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DevelMain integration
(defun intero-devel-reload ()
"Reload the module `DevelMain' and then run `DevelMain.update'.
This is for doing live update of the code of servers or GUI
applications. Put your development version of the program in
`DevelMain', and define `update' to auto-start the program on a
new thread, and use the `foreign-store' package to access the
running context across :load/:reloads in Intero."
(interactive)
(unwind-protect
(with-current-buffer
(or (get-buffer "DevelMain.hs")
(if (y-or-n-p
"You need to open a buffer named DevelMain.hs. Find now?")
(ido-find-file)
(error "No DevelMain.hs buffer.")))
(message "Reloading ...")
(intero-async-call
'backend
":l DevelMain"
(current-buffer)
(lambda (buffer reply)
(if (string-match "^OK, modules loaded" reply)
(intero-async-call
'backend
"DevelMain.update"
buffer
(lambda (_buffer reply)
(message "DevelMain updated. Output was:\n%s"
reply)))
(progn
(message "DevelMain FAILED. Switch to DevelMain.hs and compile that.")
(switch-to-buffer buffer)
(flycheck-buffer)
(flycheck-list-errors))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Flycheck integration
(defun intero-check (checker cont)
"Run a check and pass the status onto CONT."
(if (intero-gave-up 'backend)
(run-with-timer 0
nil
cont
'interrupted)
(let ((file-buffer (current-buffer)))
(write-region (point-min) (point-max) (intero-buffer-file-name) nil 'no-message)
(clear-visited-file-modtime)
(intero-async-call
'backend
(concat ":l " (intero-buffer-file-name))
(list :cont cont
:file-buffer file-buffer
:checker checker)
(lambda (state string)
(with-current-buffer (plist-get state :file-buffer)
(funcall (plist-get state :cont)
'finished
(intero-parse-errors-warnings
(plist-get state :checker)
(current-buffer)
string))
(when (string-match "OK, modules loaded: \\(.*\\)\\.$" string)
(let ((modules (match-string 1 string)))
(intero-async-call 'backend
(concat ":m + "
(replace-regexp-in-string modules "," ""))
nil
(lambda (_st _)))))))))))
(flycheck-define-generic-checker 'intero
"A syntax and type checker for Haskell using an Intero worker
process."
:start 'intero-check
:modes '(haskell-mode literate-haskell-mode)
:next-checkers '((warning . haskell-hlint)))
(add-to-list 'flycheck-checkers 'intero)
(defun intero-parse-errors-warnings (checker buffer string)
"Parse from the given STRING a list of flycheck errors and
warnings, adding CHECKER and BUFFER to each one."
(with-temp-buffer
(insert string)
(goto-char (point-min))
(let ((messages (list)))
(while (search-forward-regexp
(concat "[\r\n]\\([A-Z]?:?[^ \r\n:][^:\n\r]+\\):\\([0-9()-:]+\\):"
"[ \n\r]+\\([[:unibyte:][:nonascii:]]+?\\)\n[^ ]")
nil t 1)
(let* ((file (intero-canonicalize-path (match-string 1)))
(location-raw (match-string 2))
(msg (match-string 3)) ;; Replace gross bullet points.
(type (cond ((string-match "^Warning:" msg)
(setq msg (replace-regexp-in-string "^Warning: *" "" msg))
(if (string-match "^\\[-Wdeferred-type-errors\\]" msg)
'error
'warning))
((string-match "^Splicing " msg) 'splice)
(t 'error)))
(location (intero-parse-error
(concat file ":" location-raw ": x")))
(line (plist-get location :line))
(column (plist-get location :col)))
(setq messages
(cons (flycheck-error-new-at
line column type msg
:checker checker
:buffer (when (string= (intero-buffer-file-name buffer)
file)
buffer)
:filename file)
messages))))
(delete-dups messages))))
(defconst intero-error-regexp-alist
`((,(concat
"^ *\\(?1:[^\t\r\n]+?\\):"
"\\(?:"
"\\(?2:[0-9]+\\):\\(?4:[0-9]+\\)\\(?:-\\(?5:[0-9]+\\)\\)?" ;; "121:1" & "12:3-5"
"\\|"
"(\\(?2:[0-9]+\\),\\(?4:[0-9]+\\))-(\\(?3:[0-9]+\\),\\(?5:[0-9]+\\))" ;; "(289,5)-(291,36)"
"\\)"
":\\(?6: Warning:\\)?")
1 (2 . 3) (4 . 5) (6 . nil)) ;; error/warning locus
;; multiple declarations
("^ \\(?:Declared at:\\| \\) \\(?1:[^ \t\r\n]+\\):\\(?2:[0-9]+\\):\\(?4:[0-9]+\\)$"
1 2 4 0) ;; info locus
;; this is the weakest pattern as it's subject to line wrapping et al.
(" at \\(?1:[^ \t\r\n]+\\):\\(?2:[0-9]+\\):\\(?4:[0-9]+\\)\\(?:-\\(?5:[0-9]+\\)\\)?[)]?$"
1 2 (4 . 5) 0)) ;; info locus
"Regexps used for matching GHC compile messages.")
(defun intero-parse-error (string)
"Parse the line number from the error."
(let ((span nil))
(cl-loop for regex
in intero-error-regexp-alist
do (when (string-match (car regex) string)
(setq span
(list :file (match-string 1 string)
:line (string-to-number (match-string 2 string))
:col (string-to-number (match-string 4 string))
:line2 (when (match-string 3 string)
(string-to-number (match-string 3 string)))
:col2 (when (match-string 5 string)
(string-to-number (match-string 5 string)))))))
span))
(defun intero-call-in-buffer (buffer func &rest args)
"Utility function which calls FUNC in BUFFER with ARGS."
(with-current-buffer buffer
(apply func args)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Company integration (auto-completion)
(defconst intero-pragmas
'("CONLIKE" "SCC" "DEPRECATED" "INCLUDE" "INCOHERENT" "INLINABLE" "INLINE"
"LANGUAGE" "LINE" "MINIMAL" "NOINLINE" "NOUNPACK" "OPTIONS" "OPTIONS_GHC"
"OVERLAPPABLE" "OVERLAPPING" "OVERLAPS" "RULES" "SOURCE" "SPECIALIZE"
"UNPACK" "WARNING")
"Pragmas that GHC supports.")
(defun company-intero (command &optional arg &rest ignored)
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-intero))
(prefix
(unless (intero-gave-up 'backend)
(let ((prefix-info (intero-completions-grab-prefix)))
(when prefix-info
(cl-destructuring-bind
(beg end prefix _type) prefix-info
prefix)))))
(candidates
(unless (intero-gave-up 'backend)
(let ((prefix-info (intero-completions-grab-prefix)))
(when prefix-info
(cl-destructuring-bind
(beg end prefix type) prefix-info
(or (cl-case type
(haskell-completions-module-name-prefix
(intero-get-repl-completions (concat "import " prefix)))
(haskell-completions-identifier-prefix
(intero-get-completions beg end))
(haskell-completions-language-extension-prefix
(mapcar (lambda (x)
(replace-regexp-in-string "^-X" "" x))
(intero-get-repl-completions (concat ":set -X" prefix))))
(haskell-completions-pragma-name-prefix
(cl-remove-if-not
(lambda (candidate)
(string-match (concat "^" prefix) candidate))
intero-pragmas)))
(intero-get-repl-completions prefix)))))))))
(defun intero-completions-grab-prefix (&optional minlen)
"Grab prefix at point for possible completion."
(when (intero-completions-can-grab-prefix)
(let ((prefix (cond
((intero-completions-grab-pragma-prefix))
((intero-completions-grab-identifier-prefix)))))
(cond ((and minlen prefix)
(when (>= (length (nth 2 prefix)) minlen)
prefix))
(prefix prefix)))))
(defun intero-completions-can-grab-prefix ()
"Check if the case is appropriate for grabbing completion prefix."
(when (not (region-active-p))
(when (looking-at-p (rx (| space line-end punct)))
(when (not (bobp))
(save-excursion
(backward-char)
(not (looking-at-p (rx (| space line-end)))))))))
(defun intero-completions-grab-identifier-prefix ()
"Grab identifier prefix."
(let ((pos-at-point (intero-ident-pos-at-point))
(p (point)))
(when pos-at-point
(let* ((start (car pos-at-point))
(end (cdr pos-at-point))
(type 'haskell-completions-identifier-prefix)
(case-fold-search nil)
value)
(when (<= p end)
(setq end p)
(setq value (buffer-substring-no-properties start end))
(when (string-match-p (rx bos upper) value)
(save-excursion
(goto-char (line-beginning-position))
(when (re-search-forward
(rx "import"
(? (1+ space) "qualified")
(1+ space)
upper
(1+ (| alnum ".")))
p ;; bound
t) ;; no-error
(if (equal p (point))
(setq type 'haskell-completions-module-name-prefix)
(when (re-search-forward
(rx (| " as " "("))
start
t)
(setq type 'haskell-completions-identifier-prefix))))))
(when (nth 8 (syntax-ppss))
(setq type 'haskell-completions-general-prefix))
(when value (list start end value type)))))))
(defun intero-completions-grab-pragma-prefix ()
"Grab completion prefix for pragma completions.
Returns a list of form '(prefix-start-position
prefix-end-position prefix-value prefix-type) for pramga names
such as WARNING, DEPRECATED, LANGUAGE etc. Also returns
completion prefixes for options in case OPTIONS_GHC pragma, or
language extensions in case of LANGUAGE pragma. Obsolete OPTIONS
pragma is supported also."
(when (nth 4 (syntax-ppss))
;; We're inside comment
(let ((p (point))
(comment-start (nth 8 (syntax-ppss)))
(case-fold-search nil)
prefix-start
prefix-end
prefix-type
prefix-value)
(save-excursion
(goto-char comment-start)
(when (looking-at (rx "{-#" (1+ (| space "\n"))))
(let ((pragma-start (match-end 0)))
(when (> p pragma-start)
;; point stands after `{-#`
(goto-char pragma-start)
(when (looking-at (rx (1+ (| upper "_"))))
;; found suitable sequence for pragma name
(let ((pragma-end (match-end 0))
(pragma-value (match-string-no-properties 0)))
(if (eq p pragma-end)
;; point is at the end of (in)complete pragma name
;; prepare resulting values
(progn
(setq prefix-start pragma-start)
(setq prefix-end pragma-end)
(setq prefix-value pragma-value)
(setq prefix-type
'haskell-completions-pragma-name-prefix))
(when (and (> p pragma-end)
(or (equal "OPTIONS_GHC" pragma-value)
(equal "OPTIONS" pragma-value)
(equal "LANGUAGE" pragma-value)))
;; point is after pragma name, so we need to check
;; special cases of `OPTIONS_GHC` and `LANGUAGE` pragmas
;; and provide a completion prefix for possible ghc
;; option or language extension.
(goto-char pragma-end)
(when (re-search-forward
(rx (* anything)
(1+ (regexp "\\S-")))
p
t)
(let* ((str (match-string-no-properties 0))
(split (split-string str (rx (| space "\n")) t))
(val (car (last split)))
(end (point)))
(when (and (equal p end)
(not (string-match-p "#" val)))
(setq prefix-value val)
(backward-char (length val))
(setq prefix-start (point))
(setq prefix-end end)
(setq
prefix-type
(if (not (equal "LANGUAGE" pragma-value))
'haskell-completions-ghc-option-prefix
'haskell-completions-language-extension-prefix
)))))))))))))
(when prefix-value
(list prefix-start prefix-end prefix-value prefix-type)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ELDoc integration
(defun eldoc-intero ()
"ELDoc backend for intero."
(let* ((ty (apply #'intero-get-type-at (intero-thing-at-point)))
(is-error (string-match "^<.+>:.+:" ty)))
(unless is-error
(with-temp-buffer
(when (fboundp 'haskell-mode)
(haskell-mode))
(insert (replace-regexp-in-string "[ \n]+" " " ty))
(font-lock-fontify-buffer)
(buffer-string)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; REPL
(defconst intero-prompt-regexp "^\4 ")
(defun intero-repl-load ()
"Load the current file in the REPL."
(interactive)
(save-buffer)
(let ((file (intero-buffer-file-name))
(repl-buffer (intero-repl-buffer)))
(with-current-buffer repl-buffer
(comint-send-string
(get-buffer-process (current-buffer))
(concat ":l " file "\n")))
(pop-to-buffer repl-buffer)))
(defun intero-repl ()
"Start up the REPL for this stack project."
(interactive)
(switch-to-buffer (intero-repl-buffer)))
(defun intero-repl-buffer ()
(let* ((root (intero-project-root))
(package-name (intero-package-name))
(name (format "*intero:%s:%s:repl*"
(file-name-nondirectory root)
package-name))
(backend-buffer (intero-buffer 'backend)))
(if (get-buffer name)
(get-buffer name)
(with-current-buffer
(get-buffer-create name)
(cd root)
(intero-repl-mode)
(intero-repl-mode-start backend-buffer
(buffer-local-value 'intero-targets backend-buffer))
(current-buffer)))))
(define-derived-mode intero-repl-mode comint-mode "Intero-REPL"
"Interactive prompt for Intero."
(when (and (not (eq major-mode 'fundamental-mode))
(eq this-command 'intero-repl-mode))
(error "You probably meant to run: M-x intero-repl"))
(set (make-local-variable 'comint-prompt-regexp) intero-prompt-regexp))
(defun intero-repl-mode-start (buffer targets)
"Start the process for the repl buffer."
(setq intero-targets targets)
(let ((arguments (intero-make-options-list
(or targets
(let ((package-name (buffer-local-value 'intero-package-name buffer)))
(unless (equal "" package-name)
(list package-name))))
t)))
(insert (propertize
(format "Starting:\n stack ghci %s\n" (combine-and-quote-strings arguments))
'face 'font-lock-comment-face))
(let ((script (with-current-buffer (find-file-noselect (make-temp-file "intero-script"))
(insert ":set prompt \"\"
:set -fobject-code
:set prompt \"\\4 \"
")
(basic-save-buffer)
(intero-buffer-file-name))))
(let ((process (apply #'start-process "intero" (current-buffer) "stack" "ghci"
(append arguments
(list "--verbosity" "silent")
(list "--ghci-options"
(concat "-ghci-script=" script))))))
(when (process-live-p process)
(set-process-query-on-exit-flag process nil)
(message "Started Intero process for REPL."))))))
;; For live migration, remove later
(font-lock-remove-keywords
'intero-repl-mode
'(("\\(\4\\)"
(0 (prog1 ()
(compose-region (match-beginning 1)
(match-end 1)
?λ))))))
(font-lock-add-keywords
'intero-repl-mode
'(("\\(\4\\)"
(0 (prog1 ()
(compose-region (match-beginning 1)
(match-end 1)
?‽))))))
(define-key intero-repl-mode-map [remap move-beginning-of-line] 'intero-repl-beginning-of-line)
(define-key intero-repl-mode-map [remap delete-backward-char] 'intero-repl-delete-backward-char)
(defun intero-repl-delete-backward-char ()
"Delete backwards, excluding the prompt."
(interactive)
(unless (looking-back intero-prompt-regexp)
(call-interactively 'delete-backward-char)))
(defun intero-repl-beginning-of-line ()
"Go to the beginning of the line, excluding the prompt."
(interactive)
(if (search-backward-regexp intero-prompt-regexp (line-beginning-position) t 1)
(goto-char (+ 2 (line-beginning-position)))
(call-interactively 'move-beginning-of-line)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Buffer operations
(defun intero-thing-at-point ()
"Return (list START END) of something at the point."
(if (region-active-p)
(list (region-beginning)
(region-end))
(let ((pos (intero-ident-pos-at-point)))
(if pos
(list (car pos) (cdr pos))
(list (point) (point))))))
(defun intero-ident-at-point ()
"Return the identifier under point, or nil if none found.
May return a qualified name."
(let ((reg (intero-ident-pos-at-point)))
(when reg
(buffer-substring-no-properties (car reg) (cdr reg)))))
(defun intero-ident-pos-at-point ()
"Return the span of the identifier under point, or nil if none found.
May return a qualified name."
(save-excursion
;; Skip whitespace if we're on it. That way, if we're at "map ", we'll
;; see the word "map".
(if (and (not (eobp))
(eq ? (char-syntax (char-after))))
(skip-chars-backward " \t"))
(let ((case-fold-search nil))
(cl-multiple-value-bind (start end)
(list
(progn (skip-syntax-backward "w_") (point))
(progn (skip-syntax-forward "w_") (point)))
;; If we're looking at a module ID that qualifies further IDs, add
;; those IDs.
(goto-char start)
(while (and (looking-at "[[:upper:]]") (eq (char-after end) ?.)
;; It's a module ID that qualifies further IDs.
(goto-char (1+ end))
(save-excursion
(when (not (zerop (skip-syntax-forward
(if (looking-at "\\s_") "_" "w'"))))
(setq end (point))))))
;; If we're looking at an ID that's itself qualified by previous
;; module IDs, add those too.
(goto-char start)
(if (eq (char-after) ?.) (forward-char 1)) ;Special case for "."
(while (and (eq (char-before) ?.)
(progn (forward-char -1)
(not (zerop (skip-syntax-backward "w'"))))
(skip-syntax-forward "'")
(looking-at "[[:upper:]]"))
(setq start (point)))
;; This is it.
(unless (= start end)
(cons start end))))))
(defun intero-buffer-file-name (&optional buffer)
"Return buffer-file-name stripped of any text properties."
(let ((name (buffer-file-name buffer)))
(when name
(intero-canonicalize-path (substring-no-properties name)))))
(defun intero-canonicalize-path (path)
"Standardizes path names and ensures drive names are
capitalized (relevant on Windows)"
(intero-capitalize-drive-letter (convert-standard-filename path)))
(defun intero-capitalize-drive-letter (path)
"Ensures the drive letter is capitalized in paths of the form
x:\\foo\\bar (i.e., Windows)."
(save-match-data
(let ((drive-path (split-string path ":\\\\")))
(if (or (null (car drive-path)) (null (cdr drive-path)))
path
(concat (upcase (car drive-path)) ":\\" (cadr drive-path))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Query/commands
(defun intero-get-all-types ()
"Get all types in all expressions in all modules."
(intero-blocking-call 'backend ":all-types"))
(defun intero-get-type-at (beg end)
"Get the type at the given region denoted by BEG and END."
(replace-regexp-in-string
"\n$" ""
(intero-blocking-call
'backend
(format ":type-at %S %d %d %d %d %S"
(intero-buffer-file-name)
(save-excursion (goto-char beg)
(line-number-at-pos))
(save-excursion (goto-char beg)
(1+ (current-column)))
(save-excursion (goto-char end)
(line-number-at-pos))
(save-excursion (goto-char end)
(1+ (current-column)))
(buffer-substring-no-properties beg end)))))
(defun intero-get-info-of (thing)
"Get info for the thing."
(replace-regexp-in-string
"\n$" ""
(intero-blocking-call
'backend
(format ":i %s" thing))))
(defun intero-get-loc-at (beg end)
"Get the location of the identifier denoted by BEG and END."
(replace-regexp-in-string
"\n$" ""
(intero-blocking-call
'backend
(format ":loc-at %S %d %d %d %d %S"
(intero-buffer-file-name)
(save-excursion (goto-char beg)
(line-number-at-pos))
(save-excursion (goto-char beg)
(1+ (current-column)))
(save-excursion (goto-char end)
(line-number-at-pos))
(save-excursion (goto-char end)
(1+ (current-column)))
(buffer-substring-no-properties beg end)))))
(defun intero-get-uses-at (beg end)
"Get the uses of the identifier denoted by BEG and END."
(replace-regexp-in-string
"\n$" ""
(intero-blocking-call
'backend
(format ":uses %S %d %d %d %d %S"
(intero-buffer-file-name)
(save-excursion (goto-char beg)
(line-number-at-pos))
(save-excursion (goto-char beg)
(1+ (current-column)))
(save-excursion (goto-char end)
(line-number-at-pos))
(save-excursion (goto-char end)
(1+ (current-column)))
(buffer-substring-no-properties beg end)))))
(defun intero-get-completions (beg end)
"Get completions for a PREFIX."
(split-string
(intero-blocking-call
'backend
(format ":complete-at %S %d %d %d %d %S"
(intero-buffer-file-name)
(save-excursion (goto-char beg)
(line-number-at-pos))
(save-excursion (goto-char beg)
(1+ (current-column)))
(save-excursion (goto-char end)
(line-number-at-pos))
(save-excursion (goto-char end)
(1+ (current-column)))
(buffer-substring-no-properties beg end)))
"\n"
t))
(defun intero-get-repl-completions (prefix)
"Get REPL completions for PREFIX."
(mapcar
(lambda (x) (replace-regexp-in-string "\\\"" "" x))
(cdr
(split-string
(intero-blocking-call
'backend
(format ":complete repl %S" prefix))
"\n"
t))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Process communication
(defun intero-delete-worker (worker)
"Delete the given worker."
(when (intero-buffer-p worker)
(with-current-buffer (intero-get-buffer-create worker)
(when (get-buffer-process (current-buffer))
(setq intero-deleting t)
(kill-process (get-buffer-process (current-buffer)))
(delete-process (get-buffer-process (current-buffer))))
(kill-buffer (current-buffer)))))
(defun intero-blocking-call (worker cmd)
"Make a synchronous call of CMD to the process."
(let ((result (list nil)))
(intero-async-call
worker
cmd
result
(lambda (result reply)
(setf (car result) reply)))
(with-current-buffer (intero-buffer worker)
(while (not (null intero-callbacks))
(sleep-for 0.0001)))
(car result)))
(defun intero-async-call (worker cmd &optional state callback)
"Make an asynchronous call of CMD (string) to the process,
calling CALLBACK as (CALLBACK STATE REPLY)."
(let ((buffer (intero-buffer worker)))
(if (and buffer (process-live-p (get-buffer-process buffer)))
(progn (with-current-buffer buffer
(setq intero-callbacks
(append intero-callbacks
(list (list state
(or callback #'ignore)
cmd)))))
(process-send-string (intero-process worker)
(concat cmd "\n")))
(error "Intero process is not running. Run M-x intero-restart to start it."))))
(defun intero-buffer (worker)
"Get the worker buffer for the current directory."
(let ((buffer (intero-get-buffer-create worker)))
(if (get-buffer-process buffer)
buffer
(intero-get-worker-create worker nil (current-buffer)))))
(defun intero-process (worker)
"Get the worker process for the current directory."
(get-buffer-process (intero-buffer worker)))
(defun intero-get-worker-create (worker &optional targets source-buffer)
"Start an Intero worker."
(let* ((buffer (intero-get-buffer-create worker)))
(if (get-buffer-process buffer)
buffer
(let ((install-status (intero-installed-p)))
(if (eq install-status 'installed)
(intero-start-process-in-buffer buffer targets source-buffer)
(intero-auto-install buffer install-status targets source-buffer))))))
(defun intero-auto-install (buffer install-status &optional targets source-buffer)
"Automatically install Intero."
(if (buffer-local-value 'intero-give-up buffer)
buffer
(let ((source-buffer (or source-buffer (current-buffer))))
(switch-to-buffer buffer)
(erase-buffer)
(insert (cl-case install-status
(not-installed "Intero is not installed in the Stack environment.")
(wrong-version "The wrong version of Intero is installed for this Emacs package."))
(format "
Installing intero-%s automatically ...
" intero-package-version))
(redisplay)
(cl-case (call-process "stack" nil (current-buffer) t "build"
(with-current-buffer buffer
(let* ((cabal-file (intero-cabal-find-file))
(package-name (intero-package-name cabal-file)))
;; For local development. Most users'll
;; never hit this behaviour.
(if (string= package-name "intero")
"intero"
(concat "intero-" intero-package-version))))
"ghc-paths" "syb")
(0
(message "Installed successfully! Starting Intero in a moment ...")
(bury-buffer buffer)
(switch-to-buffer source-buffer)
(intero-start-process-in-buffer buffer targets source-buffer))
(1
(with-current-buffer buffer (setq-local intero-give-up t))
(insert (propertize "Could not install Intero!
We don't know why it failed. Please read the above output and try
installing manually. If that doesn't work, report this as a
problem.
WHAT TO DO NEXT
If you don't want to Intero to try installing itself again for
this project, just keep this buffer around in your Emacs.