Skip to content

Commit 677ec20

Browse files
committed
[Fix #2112] Add a new interactive command cider-find-keyword
It basically finds the first usage of the namespace-qualified keywords. For `::other.namespace/foo` this command would go to `other.namespace` and then find the first mention of `:foo` in it.
1 parent 0eaeaf4 commit 677ec20

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#2082](https://github.com/clojure-emacs/cider/pull/2082), [cider-nrepl#440](https://github.com/clojure-emacs/cider-nrepl/pull/440): Add specialized stacktraces for clojure.spec assertions.
88
* [#2111](https://github.com/clojure-emacs/cider/pull/2111): Add `cider-pprint-eval-last-sexp-to-comment` and `cider-pprint-eval-defun-to-comment`.
99
* Add a REPL shortcut for `cider-repl-require-repl-utils` (this makes it easy to require common functions like `doc`, `source`, etc. in REPL buffers).
10+
* [#2112](https://github.com/clojure-emacs/cider/issues/2112): Add a new interactive command `cider-find-keyword` (bound to `C-c C-:`).
1011

1112
### Changes
1213

cider-interaction.el

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,39 @@ the results to be displayed in a different window."
504504
(ns (completing-read "Find namespace: " namespaces)))
505505
(cider--find-ns ns (cider--open-other-window-p arg)))))
506506

507+
(defun cider-find-keyword (&optional arg)
508+
"Find the namespace of the keyword at point and its first occurrence there.
509+
510+
For instance - if the keyword at point is \":cider.demo/keyword\", this command
511+
would find the namespace \"cider.demo\" and afterwards find the first mention
512+
of \"::keyword\" there.
513+
514+
Prompt according to prefix ARG and `cider-prompt-for-symbol'.
515+
A single or double prefix argument inverts the meaning of
516+
`cider-prompt-for-symbol'. A prefix of `-` or a double prefix argument causes
517+
the results to be displayed in a different window. The default value is
518+
thing at point."
519+
(interactive "P")
520+
(cider-ensure-connected)
521+
(let* ((kw (let ((kw-at-point (cider-symbol-at-point 'look-back)))
522+
(if (or cider-prompt-for-symbol arg)
523+
(read-string
524+
(format "Keyword (default %s): " kw-at-point)
525+
nil nil kw-at-point)
526+
kw-at-point)))
527+
(ns-qualifier (and
528+
(string-match "^:+\\(.+\\)/.+$" kw)
529+
(match-string 1 kw)))
530+
(kw-ns (if ns-qualifier
531+
(cider-resolve-alias (cider-current-ns) ns-qualifier)
532+
(cider-current-ns)))
533+
(kw-to-find (concat "::" (replace-regexp-in-string "^:+\\(.+/\\)?" "" kw))))
534+
535+
(when (and ns-qualifier (string= kw-ns (cider-current-ns)))
536+
(error "Could not resolve alias `%s' in `%s'" ns-qualifier (cider-current-ns)))
537+
(cider--find-ns kw-ns arg)
538+
(search-forward-regexp kw-to-find nil 'noerror)))
539+
507540
(defvar cider-completion-last-context nil)
508541

509542
(defun cider-completion-symbol-start-pos ()

cider-mode.el

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
269269
("Find (jump to)"
270270
["Find definition" cider-find-var]
271271
["Find resource" cider-find-resource]
272+
["Find keyword" cider-find-keyword]
272273
["Go back" cider-pop-back])
273274
("Macroexpand"
274275
["Macroexpand-1" cider-macroexpand-1]
@@ -301,6 +302,7 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
301302
(define-key map (kbd "C-c C-d") 'cider-doc-map)
302303
(define-key map (kbd "M-.") #'cider-find-var)
303304
(define-key map (kbd "C-c C-.") #'cider-find-ns)
305+
(define-key map (kbd "C-c C-:") #'cider-find-keyword)
304306
(define-key map (kbd "M-,") #'cider-pop-back)
305307
(define-key map (kbd "C-c M-.") #'cider-find-resource)
306308
(define-key map (kbd "M-TAB") #'complete-symbol)

cider-repl.el

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,7 @@ constructs."
14311431
(declare-function cider-find-resource "cider-interaction")
14321432
(declare-function cider-restart "cider-interaction")
14331433
(declare-function cider-find-ns "cider-interaction")
1434+
(declare-function cider-find-keyword "cider-interaction")
14341435
(declare-function cider-switch-to-last-clojure-buffer "cider-mode")
14351436

14361437
(defvar cider-repl-mode-map
@@ -1440,6 +1441,7 @@ constructs."
14401441
(define-key map (kbd "C-c C-t") 'cider-test-commands-map)
14411442
(define-key map (kbd "M-.") #'cider-find-var)
14421443
(define-key map (kbd "C-c C-.") #'cider-find-ns)
1444+
(define-key map (kbd "C-c C-:") #'cider-find-keyword)
14431445
(define-key map (kbd "M-,") #'cider-pop-back)
14441446
(define-key map (kbd "C-c M-.") #'cider-find-resource)
14451447
(define-key map (kbd "RET") #'cider-repl-return)
@@ -1486,6 +1488,7 @@ constructs."
14861488
("Find"
14871489
["Find definition" cider-find-var]
14881490
["Find resource" cider-find-resource]
1491+
["Find keyword" cider-find-keyword]
14891492
["Go back" cider-pop-back])
14901493
"--"
14911494
["Switch to Clojure buffer" cider-switch-to-last-clojure-buffer]

0 commit comments

Comments
 (0)