Skip to content

Commit 5964bd3

Browse files
authored
Add an alternative way to display cider-cheatsheet-select (#3686)
Instead of having the multi-step selection process, which is the default cider-cheatsheet-select behavior, we represent each candidate as a full path to a var when cider-cheatsheet-select is called with a prefix argument. This can be handy with fuzzy completion style and vertical candidates display.
1 parent 2b37d0f commit 5964bd3

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [#3681](https://github.com/clojure-emacs/cider/pull/3681): Add an alternative way to display cheatsheet in a buffer and make it the default.
88
- Current `cider-cheatsheet` command is renamed to `cider-cheatsheet-select`.
99
- New way to display cheatsheet in a buffer is available with `cider-cheatsheet` command.
10+
- [#3686](https://github.com/clojure-emacs/cider/pull/3686): Add an alternative way to display `cider-cheatsheet-select` when called with a prefix argument.
1011
- [#3632](https://github.com/clojure-emacs/cider/pull/3623): Add new configuration variable `cider-clojure-cli-global-aliases`.
1112
- [#3366](https://github.com/clojure-emacs/cider/pull/3366): Support display of error overlays with `#dbg!` and `#break!` reader macros.
1213
- [#3622](https://github.com/clojure-emacs/cider/pull/3461): Basic support for using CIDER from [clojure-ts-mode](https://github.com/clojure-emacs/clojure-ts-mode).

Diff for: cider-cheatsheet.el

+29-11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
(require 'cl-lib)
3333
(require 'map)
3434
(require 'seq)
35+
(require 'subr-x)
3536

3637
(defconst cider-cheatsheet-hierarchy
3738
'(("Documentation"
@@ -549,18 +550,35 @@ This list is supposed to have the following format:
549550
(mapcar #'symbol-name vars)
550551
(mapcar (lambda (var) (format "%s/%s" ns var)) vars))))
551552

553+
(defun cider-cheatsheet--flatten-hierarchy (hierarchy &optional sections)
554+
"Transform HIERARCHY to lists each representing a path with SECTIONS before var."
555+
(seq-mapcat (lambda (node)
556+
(if (stringp (car node))
557+
(cider-cheatsheet--flatten-hierarchy (cdr node) (cons (car node) sections))
558+
(mapcar (lambda (var) (reverse (cons var sections)))
559+
(cider-cheatsheet--expand-vars node))))
560+
hierarchy))
561+
552562
;;;###autoload
553-
(defun cider-cheatsheet-select ()
554-
"Navigate cheatsheet sections and show documentation for selected var."
555-
(interactive)
556-
(let ((hierarchy cider-cheatsheet-hierarchy))
557-
(while (stringp (caar hierarchy))
558-
(let* ((sections (mapcar #'car hierarchy))
559-
(section (completing-read "Select section: " sections)))
560-
(setq hierarchy (map-elt hierarchy section))))
561-
(let* ((vars (seq-mapcat #'cider-cheatsheet--expand-vars hierarchy))
562-
(var (completing-read "Select var: " vars)))
563-
(cider-doc-lookup var))))
563+
(defun cider-cheatsheet-select (&optional flat)
564+
"Navigate cheatsheet sections and show documentation for selected var.
565+
566+
With a prefix argument FLAT, represent each candidate as a full path to var."
567+
(interactive "P")
568+
(if flat
569+
(let* ((hierarchy (cider-cheatsheet--flatten-hierarchy cider-cheatsheet-hierarchy))
570+
(paths (mapcar (lambda (sections) (string-join sections " > ")) hierarchy))
571+
(path (completing-read "Select path: " paths))
572+
(var (car (last (split-string path " > ")))))
573+
(cider-doc-lookup var))
574+
(let ((hierarchy cider-cheatsheet-hierarchy))
575+
(while (stringp (caar hierarchy))
576+
(let* ((sections (mapcar #'car hierarchy))
577+
(section (completing-read "Select section: " sections)))
578+
(setq hierarchy (map-elt hierarchy section))))
579+
(let* ((vars (seq-mapcat #'cider-cheatsheet--expand-vars hierarchy))
580+
(var (completing-read "Select var: " vars)))
581+
(cider-doc-lookup var)))))
564582

565583
(cl-defun cider-cheatsheet--insert-hierarchy (hierarchy &optional (level 0))
566584
"Insert HIERARCHY with visual indentation for LEVEL."

0 commit comments

Comments
 (0)