Skip to content

Commit 954f183

Browse files
committed
Synchronous type information with interaction mode
haskell-doc-show-type should now show types of things not available in its precomputed docstring list, same as the eldoc function.
1 parent 1746567 commit 954f183

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

haskell-doc.el

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,8 @@ current buffer."
15781578
(unless sym (setq sym (haskell-ident-at-point)))
15791579
;; if printed before do not print it again
15801580
(unless (string= sym (car haskell-doc-last-data))
1581-
(let ((doc (haskell-doc-sym-doc sym)))
1581+
(let ((doc (or (haskell-doc-current-info--interaction t)
1582+
(haskell-doc-sym-doc sym))))
15821583
(when (and doc (haskell-doc-in-code-p))
15831584
;; In Emacs 19.29 and later, and XEmacs 19.13 and later, all
15841585
;; messages are recorded in a log. Do not put haskell-doc messages
@@ -1595,9 +1596,13 @@ current buffer."
15951596
"If non-nil, a previous eldoc message from an async call, that
15961597
hasn't been displayed yet.")
15971598

1598-
(defun haskell-doc-current-info--interaction ()
1599+
(defun haskell-doc-current-info--interaction (&optional sync)
15991600
"Asynchronous call to `haskell-process-get-type', suitable for
1600-
use in the eldoc function `haskell-doc-current-info'."
1601+
use in the eldoc function `haskell-doc-current-info'.
1602+
1603+
If SYNC is non-nil, the call will be synchronous instead, and
1604+
instead of calling `eldoc-print-current-symbol-info', the result
1605+
will be returned directly."
16011606
;; Return nil if nothing is available, or 'async if something might
16021607
;; be available, but asynchronously later. This will call
16031608
;; `eldoc-print-current-symbol-info' later.
@@ -1611,49 +1616,57 @@ use in the eldoc function `haskell-doc-current-info'."
16111616
(buffer-substring-no-properties
16121617
(region-beginning) (region-end))
16131618
(thing-at-point 'symbol 'no-properties)))
1614-
(haskell-process-get-type
1615-
sym (lambda (response)
1616-
(setq haskell-doc-current-info--interaction-last
1617-
(cons 'async response))
1618-
(eldoc-print-current-symbol-info)))
1619-
'async))))
1620-
1621-
(defun haskell-process-get-type (expr-string &optional callback)
1619+
(if sync
1620+
(haskell-process-get-type sym #'identity t)
1621+
(haskell-process-get-type
1622+
sym (lambda (response)
1623+
(setq haskell-doc-current-info--interaction-last
1624+
(cons 'async response))
1625+
(eldoc-print-current-symbol-info)))
1626+
'async)))))
1627+
1628+
(defun haskell-process-get-type (expr-string &optional callback sync)
16221629
"Asynchronously get the type of a given string.
16231630
16241631
EXPR-STRING should be an expression passed to :type in ghci.
16251632
1626-
CALLBACK will be called with a formatted type string."
1633+
CALLBACK will be called with a formatted type string.
1634+
1635+
If SYNC is non-nil, make the call synchronously instead."
16271636
(let ((process (haskell-process))
16281637
;; Avoid passing bad strings to ghci
16291638
(expr-okay (not (string-match-p "\n" expr-string)))
1630-
(ghci-command (concat ":type " expr-string)))
1639+
(ghci-command (concat ":type " expr-string))
1640+
(complete-func
1641+
(lambda (_ response)
1642+
;; Responses with empty first line are likely errors
1643+
(if (string-match-p (rx string-start line-end) response)
1644+
(setq response nil)
1645+
;; Remove a newline at the end
1646+
(setq response (replace-regexp-in-string "\n\\'" "" response))
1647+
;; Propertize for eldoc
1648+
(save-match-data
1649+
(when (string-match " :: " response)
1650+
;; Highlight type
1651+
(let ((name (substring response 0 (match-end 0)))
1652+
(type (propertize
1653+
(substring response (match-end 0))
1654+
'face 'eldoc-highlight-function-argument)))
1655+
(setq response (concat name type)))))
1656+
(when haskell-doc-prettify-types
1657+
(dolist (re '(("::" . "") ("=>" . "") ("->" . "")))
1658+
(setq response
1659+
(replace-regexp-in-string (car re) (cdr re) response)))))
1660+
(when callback (funcall callback response)))))
16311661
(when (and process expr-okay)
1632-
(haskell-process-queue-command
1633-
(haskell-process)
1634-
(make-haskell-command
1635-
:go (lambda (_) (haskell-process-send-string process ghci-command))
1636-
:complete
1637-
(lambda (_ response)
1638-
;; Responses with empty first line are likely errors
1639-
(if (string-match-p (rx string-start line-end) response)
1640-
(setq response nil)
1641-
;; Remove a newline at the end
1642-
(setq response (replace-regexp-in-string "\n\\'" "" response))
1643-
;; Propertize for eldoc
1644-
(save-match-data
1645-
(when (string-match " :: " response)
1646-
;; Highlight type
1647-
(let ((name (substring response 0 (match-end 0)))
1648-
(type (propertize
1649-
(substring response (match-end 0))
1650-
'face 'eldoc-highlight-function-argument)))
1651-
(setq response (concat name type)))))
1652-
(when haskell-doc-prettify-types
1653-
(dolist (re '(("::" . "") ("=>" . "") ("->" . "")))
1654-
(setq response
1655-
(replace-regexp-in-string (car re) (cdr re) response)))))
1656-
(when callback (funcall callback response))))))))
1662+
(if sync
1663+
(let ((response (haskell-process-queue-sync-request process ghci-command)))
1664+
(funcall complete-func nil response))
1665+
(haskell-process-queue-command
1666+
process
1667+
(make-haskell-command
1668+
:go (lambda (_) (haskell-process-send-string process ghci-command))
1669+
:complete complete-func))))))
16571670

16581671
(defun haskell-doc-sym-doc (sym)
16591672
"Show the type of the function near point.

0 commit comments

Comments
 (0)