Skip to content

Commit

Permalink
Add an alternative way to display cider-cheatsheet-select (#3686)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
katomuso authored May 29, 2024
1 parent 2b37d0f commit 5964bd3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [#3681](https://github.com/clojure-emacs/cider/pull/3681): Add an alternative way to display cheatsheet in a buffer and make it the default.
- Current `cider-cheatsheet` command is renamed to `cider-cheatsheet-select`.
- New way to display cheatsheet in a buffer is available with `cider-cheatsheet` command.
- [#3686](https://github.com/clojure-emacs/cider/pull/3686): Add an alternative way to display `cider-cheatsheet-select` when called with a prefix argument.
- [#3632](https://github.com/clojure-emacs/cider/pull/3623): Add new configuration variable `cider-clojure-cli-global-aliases`.
- [#3366](https://github.com/clojure-emacs/cider/pull/3366): Support display of error overlays with `#dbg!` and `#break!` reader macros.
- [#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).
Expand Down
40 changes: 29 additions & 11 deletions cider-cheatsheet.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
(require 'cl-lib)
(require 'map)
(require 'seq)
(require 'subr-x)

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

(defun cider-cheatsheet--flatten-hierarchy (hierarchy &optional sections)
"Transform HIERARCHY to lists each representing a path with SECTIONS before var."
(seq-mapcat (lambda (node)
(if (stringp (car node))
(cider-cheatsheet--flatten-hierarchy (cdr node) (cons (car node) sections))
(mapcar (lambda (var) (reverse (cons var sections)))
(cider-cheatsheet--expand-vars node))))
hierarchy))

;;;###autoload
(defun cider-cheatsheet-select ()
"Navigate cheatsheet sections and show documentation for selected var."
(interactive)
(let ((hierarchy cider-cheatsheet-hierarchy))
(while (stringp (caar hierarchy))
(let* ((sections (mapcar #'car hierarchy))
(section (completing-read "Select section: " sections)))
(setq hierarchy (map-elt hierarchy section))))
(let* ((vars (seq-mapcat #'cider-cheatsheet--expand-vars hierarchy))
(var (completing-read "Select var: " vars)))
(cider-doc-lookup var))))
(defun cider-cheatsheet-select (&optional flat)
"Navigate cheatsheet sections and show documentation for selected var.
With a prefix argument FLAT, represent each candidate as a full path to var."
(interactive "P")
(if flat
(let* ((hierarchy (cider-cheatsheet--flatten-hierarchy cider-cheatsheet-hierarchy))
(paths (mapcar (lambda (sections) (string-join sections " > ")) hierarchy))
(path (completing-read "Select path: " paths))
(var (car (last (split-string path " > ")))))
(cider-doc-lookup var))
(let ((hierarchy cider-cheatsheet-hierarchy))
(while (stringp (caar hierarchy))
(let* ((sections (mapcar #'car hierarchy))
(section (completing-read "Select section: " sections)))
(setq hierarchy (map-elt hierarchy section))))
(let* ((vars (seq-mapcat #'cider-cheatsheet--expand-vars hierarchy))
(var (completing-read "Select var: " vars)))
(cider-doc-lookup var)))))

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

0 comments on commit 5964bd3

Please sign in to comment.