Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 26efa02

Browse files
committed
Merge pull request #123 from haskell/emacs-process-log
(elisp) Add log of hie process input / output
2 parents 8a7802d + cf05790 commit 26efa02

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

elisp/hie.el

+30-24
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
(defvar hie-process nil
2626
"Variable holding current Haskell IDE Engine process")
2727

28-
(defvar hie-buffer nil
29-
"Variable holding current Haskell IDE Engine buffer")
28+
(defvar hie-log-buffer nil
29+
"Variable holding current Haskell IDE Engine log buffer")
30+
31+
(defvar hie-process-buffer nil
32+
"Variable holding current Haskell IDE Engine process buffer")
3033

3134
(defvar hie-process-handle-message nil
3235
"A function to handle json object.")
@@ -41,36 +44,31 @@
4144

4245
(defun hie-process-filter (process input)
4346
(let ((prev-buffer (current-buffer)))
44-
(with-current-buffer hie-buffer
45-
47+
(with-current-buffer hie-process-buffer
4648
(let ((point (point)))
4749
(insert input)
4850
(save-excursion
4951
(goto-char point)
5052
(when (re-search-forward "\^b" nil t)
51-
(let* ((end-of-current-json-object (match-beginning 0))
52-
(after-stx-marker (match-end 0))
53+
(let* ((after-stx-marker (match-end 0))
54+
(input-text (buffer-substring-no-properties (point-min) (match-beginning 0)))
55+
(handle-error (lambda ()
56+
(when hie-process-handle-invalid-input
57+
(hie-log "<-parse-error %s" input-text)
58+
(funcall hie-process-handle-invalid-input input-text))))
5359
(json-array-type 'list))
5460
(goto-char (point-min))
5561
(condition-case nil
5662
(let ((json (json-read)))
5763
(when hie-process-handle-message
5864
(with-current-buffer prev-buffer
65+
(hie-log "<- %s" input-text)
5966
(funcall hie-process-handle-message json))))
6067
;; json-readtable-error is when there is an unexpected character in input
61-
(json-readtable-error
62-
(when hie-process-handle-invalid-input
63-
(funcall hie-process-handle-invalid-input
64-
(buffer-substring-no-properties (point-min) end-of-current-json-object))))
68+
(json-readtable-error (funcall handle-error))
6569
;; json-unknown-keyword when unrecognized keyword is parsed
66-
(json-unknown-keyword
67-
(when hie-process-handle-invalid-input
68-
(funcall hie-process-handle-invalid-input
69-
(buffer-substring-no-properties (point-min) end-of-current-json-object))))
70-
(end-of-file
71-
(when hie-process-handle-invalid-input
72-
(funcall hie-process-handle-invalid-input
73-
(buffer-substring-no-properties (point-min) end-of-current-json-object)))))
70+
(json-unknown-keyword (funcall handle-error))
71+
(end-of-file (funcall handle-error)))
7472
(delete-region (point-min) after-stx-marker))))))))
7573

7674
(defun hie-start-process ()
@@ -81,12 +79,14 @@ running this function does nothing."
8179
(interactive)
8280

8381
(unless (hie-process-live-p)
84-
(setq hie-buffer
85-
(get-buffer-create "*hie*"))
82+
(setq hie-log-buffer
83+
(get-buffer-create "*hie-log*"))
84+
(setq hie-process-buffer
85+
(get-buffer-create "*hie-process*"))
8686
(setq hie-process
8787
(apply #'start-process
8888
"Haskell IDE Engine"
89-
hie-buffer
89+
hie-process-buffer
9090
hie-command
9191
hie-command-args))
9292
(set-process-query-on-exit-flag hie-process nil)
@@ -104,8 +104,14 @@ running this function does nothing."
104104
(when (hie-process-live-p)
105105
(kill-process hie-process)
106106
(setq hie-process nil)
107-
(kill-buffer hie-buffer)
108-
(setq hie-buffer nil)))
107+
(kill-buffer hie-process-buffer)
108+
(setq hie-process-buffer nil)))
109+
110+
(defun hie-log (&rest args)
111+
(with-current-buffer hie-log-buffer
112+
(goto-char (point-max))
113+
(insert (apply #'format args)
114+
"\n")))
109115

110116
(defun hie-post-message (json)
111117
"Post a message to Haskell IDE Engine.
@@ -118,7 +124,7 @@ by `hie-handle-message'."
118124
;; accepts missing fields and default to empty when possible.
119125
(let ((prepared-json (hie-prepare-json json)))
120126
(run-hook-with-args 'hie-post-message-hook prepared-json)
121-
127+
(hie-log "-> %s" prepared-json)
122128
(process-send-string hie-process prepared-json)
123129
;; send \STX marker and flush buffers
124130
(process-send-string hie-process "\^b\n")))

elisp/tests/hie-tests.el

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15990."
166166
(hie-process-handle-invalid-input
167167
(lambda (input)
168168
(setq response input)))
169-
(hie-buffer
170-
(get-buffer-create "*hie*")))
169+
(hie-process-buffer
170+
(get-buffer-create "*hie-process*")))
171171

172172
(unwind-protect
173173
(progn
174174
(hie-process-filter nil "not a json text\^b")
175175

176176
(should (equal "not a json text" response)))
177-
(kill-buffer hie-buffer))))
177+
(kill-buffer hie-process-buffer))))
178178

179179
(hie-define-test
180180
hie-can-handle-input-in-chunks

0 commit comments

Comments
 (0)