Skip to content

Commit 83d3c6a

Browse files
committed
Simplify the search buffer
1 parent 116a432 commit 83d3c6a

File tree

1 file changed

+55
-100
lines changed

1 file changed

+55
-100
lines changed

emacs/merlin.el

+55-100
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,6 @@ a call to `merlin-occurrences'."
137137
See `merlin-debug'."
138138
:group 'merlin :type 'string)
139139

140-
(defcustom merlin-polarity-search-buffer-name "*merlin-polarity-search-result*"
141-
"The name of the buffer displaying result of polarity search."
142-
:group 'merlin :type 'string)
143-
144-
(defcustom merlin-search-by-type-buffer-name "*merlin-search-by-type-result*"
145-
"The name of the buffer displaying result of a search by type query."
146-
:group 'merlin :type 'string)
147-
148140
(defcustom merlin-favourite-caml-mode nil
149141
"The OCaml mode to use for the *merlin-types* buffer."
150142
:group 'merlin :type 'symbol)
@@ -1098,107 +1090,70 @@ An ocaml atom is any string containing [a-z_0-9A-Z`.]."
10981090
(cons (if bounds (car bounds) (point))
10991091
(point))))
11001092

1101-
;;;;;;;;;;;;;;;;;;;;;
1102-
;; COMMON SEARCH ;;
1103-
;;;;;;;;;;;;;;;;;;;;;
11041093

1105-
(defun merlin--render-search-result (name type)
1106-
(let ((plain-name (string-remove-prefix "Stdlib__" name)))
1107-
(concat
1108-
(propertize "val " 'face (intern "font-lock-keyword-face"))
1109-
(propertize plain-name 'face (intern "font-lock-function-name-face"))
1110-
" : "
1111-
(propertize type 'face (intern "font-lock-doc-face")))))
1112-
1113-
;;;;;;;;;;;;;;;;;;;;;
1114-
;; SEARCH BY TYPE ;;
1115-
;;;;;;;;;;;;;;;;;;;;;
1094+
;;;;;;;;;;;;
1095+
;; SEARCH ;;
1096+
;;;;;;;;;;;;
11161097

1117-
(defun merlin--search-by-type (query)
1098+
(defun merlin--search (query)
11181099
(merlin-call "search-by-type"
11191100
"-query" query
11201101
"-position" (merlin-unmake-point (point))))
11211102

1122-
(defun merlin--get-search-by-type-result-buff ()
1123-
(get-buffer-create merlin-search-by-type-buffer-name))
1124-
1125-
(defun merlin--search-result-to-entry (entry)
1126-
(let ((function-name (cdr (assoc 'name entry)))
1127-
(function-type (cdr (assoc 'type entry))))
1128-
(list function-name (vector (merlin--render-search-result
1129-
function-name
1130-
function-type)))))
1131-
1132-
(defun merlin-search-by-type (query)
1133-
"Search a value definition by type expression"
1134-
(interactive "sSearch query: ")
1135-
(let ((entries (merlin--search-by-type query))
1136-
(previous-buff (current-buffer)))
1137-
(let ((search-by-type-buffer (merlin--get-search-by-type-result-buff))
1138-
(inhibit-read-only t))
1139-
(with-current-buffer search-by-type-buffer
1140-
(switch-to-buffer-other-window search-by-type-buffer)
1141-
(goto-char 1)
1142-
(tabulated-list-mode)
1143-
(setq tabulated-list-format [("Search By Type Result" 100 t)])
1144-
(setq tabulated-list-entries
1145-
(mapcar 'merlin--search-result-to-entry entries))
1146-
(setq tabulated-list-padding 2)
1147-
(face-spec-set 'header-line '((t :weight bold :height 1.2)))
1148-
(tabulated-list-init-header)
1149-
(tabulated-list-print t)
1150-
(setq buffer-read-only t)
1151-
(switch-to-buffer-other-window previous-buff)))))
1152-
1153-
;;;;;;;;;;;;;;;;;;;;;
1154-
;; POLARITY SEARCH ;;
1155-
;;;;;;;;;;;;;;;;;;;;;
1156-
1157-
(defun merlin--search (query)
1158-
(merlin-call "search-by-polarity"
1159-
"-query" query
1160-
"-position" (merlin-unmake-point (point))))
1161-
1162-
(defun merlin--get-polarity-buff ()
1163-
(get-buffer-create merlin-polarity-search-buffer-name))
1164-
1165-
(defun merlin--polarity-result-to-list (entry)
1166-
(let ((function-name (merlin-completion-entry-text "" entry))
1167-
(function-type (merlin-completion-entry-short-description entry)))
1168-
(list function-name
1169-
(vector (merlin--render-search-result function-name function-type)))))
1170-
1171-
(defun merlin-search-by-polarity (query)
1172-
"Search a value definition by polarity"
1173-
(interactive "sSearch query: ")
1174-
(let* ((result (merlin--search query))
1175-
(entries (cdr (assoc 'entries result)))
1176-
(previous-buff (current-buffer)))
1177-
(let ((pol-buff (merlin--get-polarity-buff))
1178-
(inhibit-read-only t))
1179-
(with-current-buffer pol-buff
1180-
(switch-to-buffer-other-window pol-buff)
1181-
(goto-char 1)
1182-
(tabulated-list-mode)
1183-
(setq tabulated-list-format [("Polarity Search Result" 100 t)])
1184-
(setq tabulated-list-entries
1185-
(mapcar 'merlin--polarity-result-to-list entries))
1186-
(setq tabulated-list-padding 2)
1187-
(face-spec-set 'header-line '((t :weight bold :height 1.2)))
1188-
(tabulated-list-init-header)
1189-
(tabulated-list-print t)
1190-
(setq buffer-read-only t)
1191-
(switch-to-buffer-other-window previous-buff)))))
1192-
1193-
(defun merlin--is-polarity-query (query)
1194-
(or (string-prefix-p "-" query) (string-prefix-p "+" query)))
1103+
(defun merlin--search-format-key (name type doc)
1104+
(let ((plain-name (string-remove-prefix "Stdlib__" name)))
1105+
(concat
1106+
(propertize plain-name 'face (intern "font-lock-function-name-face"))
1107+
" : "
1108+
(propertize type 'face (intern "font-lock-doc-face"))
1109+
" "
1110+
(propertize doc 'face (intern "font-lock-comment-face")))))
1111+
1112+
(defun merlin--get-documentation-line-from-entry (entry)
1113+
(let* ((doc-entry (cdr (assoc 'doc entry)))
1114+
(doc (if (eq doc-entry 'null) "" doc-entry))
1115+
(doc-lines (split-string doc "[\r\n]+")))
1116+
(car doc-lines)))
1117+
1118+
(defun merlin--search-entry-to-completion-entry (entry)
1119+
(let ((value-name (cdr (assoc 'name entry)))
1120+
(value-hole (cdr (assoc 'constructible entry)))
1121+
(value-type (cdr (assoc 'type entry)))
1122+
(value-docs (merlin--get-documentation-line-from-entry entry)))
1123+
(let ((key (merlin--search-format-key value-name value-type value-docs))
1124+
(value value-hole))
1125+
(cons key value))))
1126+
1127+
(defun merlin--search-select-completion-result (choices selected)
1128+
(alist-get selected choices nil nil #'equal))
1129+
1130+
(defun merlin--search-substitute-constructible (elt)
1131+
(progn
1132+
(when (region-active-p)
1133+
(delete-region (region-beginning) (region-end)))
1134+
(insert (concat "(" elt ")"))))
1135+
1136+
(defun merlin--search-completion-presort (choices)
1137+
(lambda (string pred action)
1138+
(if (eq action 'metadata)
1139+
'(metadata (display-sort-function . identity)
1140+
(cycle-sort-function . identity))
1141+
(complete-with-action action choices string pred))))
11951142

11961143
(defun merlin-search (query)
1197-
"Search a value defintion by polarity or by type expression"
1144+
"Search values by types or polarity"
11981145
(interactive "sSearch query: ")
1199-
(if (merlin--is-polarity-query query)
1200-
(merlin-search-by-polarity query)
1201-
(merlin-search-by-type query)))
1146+
(let* ((entries (merlin--search query))
1147+
(choices
1148+
(mapcar #'merlin--search-entry-to-completion-entry entries)))
1149+
(let ((constructible
1150+
(merlin--search-select-completion-result
1151+
choices
1152+
(completing-read (concat "Candidates: ")
1153+
(merlin--search-completion-presort choices)
1154+
nil nil nil t))))
1155+
(merlin--search-substitute-constructible constructible))))
1156+
12021157

12031158
;;;;;;;;;;;;;;;;;
12041159
;; TYPE BUFFER ;;

0 commit comments

Comments
 (0)