Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display cheatsheet in a separate buffer #3681

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### New features

- [#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.
- [#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
35 changes: 34 additions & 1 deletion cider-cheatsheet.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
;;; Code:

(require 'cider-doc)
(require 'cl-lib)
(require 'seq)

(defconst cider-cheatsheet-hierarchy
Expand Down Expand Up @@ -555,7 +556,7 @@ The list can hold one or more lists inside - one per each namespace."
(cider-doc-lookup (completing-read "Select var: " namespaced-vars))))

;;;###autoload
(defun cider-cheatsheet ()
(defun cider-cheatsheet-select ()
"Navigate `cider-cheatsheet-hierarchy' with `completing-read'.

When you make it to a Clojure var its doc buffer gets displayed."
Expand All @@ -568,6 +569,38 @@ When you make it to a Clojure var its doc buffer gets displayed."
(setq cheatsheet-data (cdr section-data))))
(cider-cheatsheet--select-var cheatsheet-data)))

(cl-defun cider-cheatsheet--insert-hierarchy (hierarchy &optional (level 0))
"Insert HIERARCHY with visual indentation for LEVEL."
(dolist (node hierarchy)
(if (stringp (car node))
(progn
(insert (make-string (* level 2) ?\s) (car node) "\n")
(cider-cheatsheet--insert-hierarchy (cdr node) (1+ level)))
(dolist (var (cider-cheatsheet--expand-vars node))
(insert (make-string (* level 2) ?\s))
(insert-text-button var
'var var
'action (lambda (btn)
(cider-doc-lookup (button-get btn 'var)))
'help-echo (format "Show documentation for %s" var))
(insert "\n")))))

(defun cider-cheatsheet--buffer-contents ()
"Generate cheatsheet buffer contents based on the cheatsheet hierarchy."
(with-temp-buffer
(cider-cheatsheet--insert-hierarchy cider-cheatsheet-hierarchy)
(buffer-string)))

;;;###autoload
(defun cider-cheatsheet ()
"Display cheatsheet in a popup buffer."
(interactive)
(with-current-buffer (cider-popup-buffer "*cider-cheatsheet*")
(read-only-mode -1)
(insert (cider-cheatsheet--buffer-contents))
(read-only-mode 1)
(goto-char (point-min))))

(provide 'cider-cheatsheet)

;;; cider-cheatsheet.el ends here
Loading