Skip to content

Commit 7f57d76

Browse files
committed
Factor out completion processing logic
1 parent a98dc6e commit 7f57d76

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

js-comint.el

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,25 @@ CB allows chaining an action after clearing."
290290
(get-buffer-process (current-buffer))
291291
(format "%s\t" input-string)))
292292

293+
(defun js-comint--process-completion-output (completion prefix)
294+
"Format COMPLETION string as a list of candidates.
295+
PREFIX is the original completion prefix string."
296+
(let* ((completion-res (split-string completion nil 't))
297+
(completion-res (seq-remove (apply-partially #'string-search "") completion-res)))
298+
(cond
299+
;; For an exact match the response is: MATCH^M// type info^M^[[...PROMPT
300+
((equal "//" (elt completion-res 1))
301+
(list (car completion-res)))
302+
303+
;; When completing, e.g "console.", the prefix is included in every completion.
304+
;; TODO for console.[name] completion, want name to display in list, but console.name as completion
305+
((string-suffix-p "." prefix)
306+
(cdr ;; first is always the prefix in this case
307+
(seq-map (apply-partially #'string-remove-prefix prefix) completion-res)))
308+
309+
('t
310+
completion-res))))
311+
293312
(defun js-comint--completion-filter (output)
294313
"Intercepts completions in comint OUTPUT."
295314
(message "|%s|" output)
@@ -323,23 +342,12 @@ CB allows chaining an action after clearing."
323342
(js-comint--clear-repl-input)))
324343
(when (string-match-p "\\[[[:digit:]]+[AG]$" js-comint--completion-output)
325344
(message "candidates")
326-
(let* ((completion-res (string-split js-comint--completion-output nil 't))
327-
(completion-res (seq-remove (apply-partially #'string-prefix-p "") completion-res))
328-
;; special case of exact match
329-
;; node seems to print type info in comment format
330-
(completion-res (if (equal "//" (elt completion-res 1))
331-
(list (car completion-res))
332-
completion-res))
333-
;; when completing console. the prefix is included in completions
334-
;; TODO for console.[name] completion, want name to display in list, but console.name as completion
335-
(completion-res (if (string-suffix-p "." js-comint--completion-prefix)
336-
(seq-map (apply-partially #'string-remove-prefix js-comint--completion-prefix) completion-res)
337-
completion-res)))
338-
;; this clears the input used to trigger completion
345+
(let ((completion-res (js-comint--process-completion-output js-comint--completion-output js-comint--completion-prefix)))
339346
(print completion-res)
340347
(funcall js-comint--post-completion-cb completion-res)
341348
(setq js-comint--post-completion-cb nil)
342349
(js-comint--reset-completion-state)
350+
;; this clears the input used to trigger completion
343351
(js-comint--clear-repl-input))))
344352
output)
345353

0 commit comments

Comments
 (0)