Skip to content

Commit 0b463f3

Browse files
committed
Define few helper functions
+ hs-utils/capture-expr-bounds + hs-utils/compose-type-at-command + hs-utils/reduce-string + hs-utils/insert-type-signature + hs-utils/echo-or-present
1 parent e67cf64 commit 0b463f3

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

haskell-commands.el

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,4 +856,69 @@ the :uses command from GHCi."
856856
(error (propertize "No reply. Is :uses supported?"
857857
'face 'compilation-error)))))))
858858

859+
(defun hs-utils/capture-expr-bounds ()
860+
"Capture position bounds of expression at point.
861+
If there is an active region then it returns region
862+
bounds. Otherwise it uses `haskell-spanable-pos-at-point` to
863+
capture identifier bounds. If latter function returns NIL this function
864+
will return cons cell where min and max positions both are equal
865+
to point."
866+
(or (when (region-active-p)
867+
(cons (region-beginning)
868+
(region-end)))
869+
(haskell-spanable-pos-at-point)
870+
(cons (point) (point))))
871+
872+
(defun hs-utils/compose-type-at-command (pos)
873+
"Prepare :type-at command to be send to haskell process.
874+
POS is a cons cell containing min and max positions, i.e. target
875+
expression bounds."
876+
(replace-regexp-in-string
877+
"\n$"
878+
""
879+
(format ":type-at %s %d %d %d %d %s"
880+
(buffer-file-name)
881+
(progn (goto-char (car pos))
882+
(line-number-at-pos))
883+
(1+ (current-column))
884+
(progn (goto-char (cdr pos))
885+
(line-number-at-pos))
886+
(1+ (current-column))
887+
(buffer-substring-no-properties (car pos)
888+
(cdr pos)))))
889+
890+
(defun hs-utils/reduce-string (s)
891+
"Remove newlines ans extra whitespace from S.
892+
Removes all extra whitespace at the beginning of each line leaving
893+
only single one. Then removes all newlines."
894+
(let ((s_ (replace-regexp-in-string "^\s+" " " s)))
895+
(replace-regexp-in-string "\n" "" s_)))
896+
897+
(defun hs-utils/insert-type-signature (signature)
898+
"Insert type signature.
899+
In case of active region is present, wrap it by parentheses and
900+
append SIGNATURE to original expression. Otherwise tries to
901+
carefully insert SIGNATURE above identifier at point. Removes
902+
newlines and extra whitespace in signature before insertion."
903+
(let* ((ident-pos (or (haskell-ident-pos-at-point)
904+
(cons (point) (point))))
905+
(min-pos (car ident-pos))
906+
(sig (hs-utils/reduce-string signature)))
907+
(save-excursion
908+
(goto-char min-pos)
909+
(let ((col (current-column)))
910+
(insert sig "\n")
911+
(indent-to col)))))
912+
913+
(defun hs-utils/echo-or-present (msg &optional name)
914+
"Present message in some manner depending on configuration.
915+
If variable `haskell-process-use-presentation-mode' is NIL it will output
916+
modified message MSG to echo area.
917+
Optinal NAME will be used as presentation mode buffer name."
918+
(if haskell-process-use-presentation-mode
919+
(let ((bufname (or name "*Haskell Presentation*"))
920+
(session (haskell-process-session (haskell-interactive-process))))
921+
(haskell-present bufname session msg))
922+
(let (m (hs-utils/reduce-string msg))
923+
(message m))))
859924
(provide 'haskell-commands)

0 commit comments

Comments
 (0)