From 30a212a6e0e68cf4f5f11339c3ab004574fbd724 Mon Sep 17 00:00:00 2001 From: Kato Muso Date: Wed, 29 May 2024 07:30:17 +0000 Subject: [PATCH] Add an alternative way to display cider-cheatsheet-select --- cider-cheatsheet.el | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/cider-cheatsheet.el b/cider-cheatsheet.el index 86fc745b9..896cf0d7d 100644 --- a/cider-cheatsheet.el +++ b/cider-cheatsheet.el @@ -32,6 +32,7 @@ (require 'cl-lib) (require 'map) (require 'seq) +(require 'subr-x) (defconst cider-cheatsheet-hierarchy '(("Documentation" @@ -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 (string-split 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."