Skip to content

Commit 7cff0e0

Browse files
committed
feat: Improve pop-to-buffer selection UI.
Before this change, the user was supposed to select the short documentation of the functions, which might not be very practical, depending on how the user's completing-read works. With this change, the user is supposed to select a short symbol, with the shortdoc displayed as annotation. This change also adds tests for the pop-to-buffer selection UI.
1 parent 8f8bf5c commit 7cff0e0

File tree

3 files changed

+112
-15
lines changed

3 files changed

+112
-15
lines changed

test/turtles-test.el

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,92 @@
10711071
(should (eq (alist-get 'window-system (frame-parameters))
10721072
(turtles-pop-to-buffer-other-frame :check nil nil))))
10731073

1074+
(ert-deftest turtles-pop-to-buffer-interactive ()
1075+
(turtles-ert-test)
1076+
1077+
(let ((inst (turtles-start-instance 'default))
1078+
(buf (turtles-instance-eval
1079+
'default
1080+
'(with-current-buffer
1081+
(generate-new-buffer "*pop-to-buffer-test*")
1082+
(insert "foo bar")
1083+
(current-buffer))))
1084+
(turtles-pop-to-buffer-actions
1085+
(list #'turtles-pop-to-buffer-embedded
1086+
#'turtles-pop-to-buffer-copy))
1087+
(inhibit-message t))
1088+
1089+
(turtles-with-minibuffer
1090+
(let ((completion-cycle-threshold 0))
1091+
(turtles-pop-to-buffer buf))
1092+
1093+
(turtles-with-grab-buffer (:name "initial")
1094+
(should (equal "Display buffer:" (buffer-string))))
1095+
1096+
(minibuffer-complete)
1097+
(minibuffer-complete)
1098+
(turtles-with-grab-buffer (:buf "*Completions*")
1099+
(goto-char (point-min))
1100+
(search-forward "possible completions:\n")
1101+
(should (equal (concat "copy Display a copy of the instance buffer.\n"
1102+
"embedded Display buffer in the terminal buffer.")
1103+
(buffer-substring (point) (point-max))))
1104+
1105+
(execute-kbd-macro (kbd "copy"))))
1106+
1107+
;; pop-to-buffer should have made a copy. Let's check it.
1108+
(with-current-buffer (window-buffer (selected-window))
1109+
(should (string-prefix-p "[default] *pop-to-buffer-test*" (buffer-name)))
1110+
(should (equal "foo bar" (buffer-string)))
1111+
(kill-buffer (current-buffer)))))
1112+
1113+
(ert-deftest turtles-pop-to-buffer-interactive-lambdas ()
1114+
(turtles-ert-test)
1115+
1116+
(let* ((inst (turtles-start-instance 'default))
1117+
(buf (turtles-instance-eval
1118+
'default
1119+
'(with-current-buffer
1120+
(generate-new-buffer "*pop-to-buffer-test*")
1121+
(insert "foo bar")
1122+
(current-buffer))))
1123+
(calls nil)
1124+
(turtles-pop-to-buffer-actions
1125+
(list
1126+
(lambda (action &rest _args)
1127+
"My Lambda 1."
1128+
(when (eq :display action)
1129+
(push 'lambda-1 calls))
1130+
t)
1131+
(lambda (action &rest _args)
1132+
"My Lambda 2."
1133+
(when (eq :display action)
1134+
(push 'lambda-2 calls))
1135+
t)))
1136+
(inhibit-message t))
1137+
1138+
(turtles-with-minibuffer
1139+
(let ((completion-cycle-threshold 0))
1140+
(turtles-pop-to-buffer buf))
1141+
1142+
(turtles-with-grab-buffer (:name "initial")
1143+
(should (equal "Display buffer:" (buffer-string))))
1144+
1145+
(minibuffer-complete)
1146+
(minibuffer-complete)
1147+
(turtles-with-grab-buffer (:buf "*Completions*")
1148+
(goto-char (point-min))
1149+
(search-forward "possible completions:\n")
1150+
(should (equal (concat "lambda-1 My Lambda 1.\n"
1151+
"lambda-2 My Lambda 2.")
1152+
(buffer-substring (point) (point-max)))))
1153+
1154+
(execute-kbd-macro (kbd "1"))
1155+
(turtles-with-grab-buffer (:name "choice")
1156+
(should (equal "Display buffer: lambda-1" (buffer-string)))))
1157+
1158+
(should (equal '(lambda-1) calls))))
1159+
10741160
(ert-deftest turtles--split-minibuffer-body ()
10751161
(should
10761162
(equal
@@ -1264,3 +1350,4 @@
12641350
(goto-char (match-beginning 0))
12651351
(should (string-equal-ignore-case "#276ce2" (foreground-color-at-point)))
12661352
(should (string-equal-ignore-case "#0c1526" (background-color-at-point))))))
1353+

turtles-instance.el

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ or nil.
466466
If specified, PREDICATE must be a function that takes a single
467467
argument of type `turtles-instance'."
468468
(let ((completion-extra-properties
469-
'(:annotation-function
470-
(lambda (id-as-str)
471-
(when-let* ((inst (turtles-get-instance (intern id-as-str)))
472-
(doc (turtles-instance-shortdoc inst)))
473-
(concat " " doc)))))
469+
`(:annotation-function
470+
,(lambda (id-as-str)
471+
(when-let* ((inst (turtles-get-instance (intern id-as-str)))
472+
(doc (turtles-instance-shortdoc inst)))
473+
(concat " " doc)))))
474474
(predicate (if predicate
475475
(lambda (cell)
476476
(funcall predicate (cdr cell)))

turtles.el

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,19 +1061,29 @@ itself."
10611061
((length= actions 1)
10621062
(apply (car actions) :display inst buffer-name pop-to-buffer-args))
10631063
(t
1064-
(let* ((action-alist (mapcar (lambda (func)
1065-
(cons (or (car (split-string (documentation func) "\n"))
1066-
(when (symbolp func) (symbol-name func))
1067-
"Anonymous action")
1068-
func))
1064+
(let* ((action-alist (mapcar (let ((counter 0))
1065+
(lambda (func)
1066+
(cons
1067+
(if (symbolp func)
1068+
(string-remove-prefix
1069+
"turtles-pop-to-buffer-" (symbol-name func))
1070+
(format "lambda-%d" (cl-incf counter)))
1071+
func)))
10691072
actions))
1073+
(completion-extra-properties
1074+
`(:annotation-function
1075+
,(lambda (key)
1076+
(let ((func (alist-get key action-alist nil nil #'string=)))
1077+
(when-let ((shortdoc (car (split-string (documentation func) "\n"))))
1078+
(concat " " shortdoc))))))
10701079
(action
1071-
(completing-read
1072-
"Display buffer: "
1073-
action-alist nil 'require-match nil 'pop-to-buffer-action-history)))
1080+
(alist-get
1081+
(completing-read
1082+
"Display buffer: "
1083+
action-alist nil 'require-match nil 'pop-to-buffer-action-history)
1084+
action-alist nil nil #'string=)))
10741085
(when action
1075-
(apply (alist-get action action-alist nil nil #'string=)
1076-
:display inst buffer-name pop-to-buffer-args)))))))
1086+
(apply action :display inst buffer-name pop-to-buffer-args)))))))
10771087

10781088
(defun turtles-pop-to-buffer-embedded (action inst buffer-name &rest pop-to-buffer-args)
10791089
"Display buffer in the terminal buffer.

0 commit comments

Comments
 (0)