Skip to content

Commit

Permalink
[Fix #2112] Add a new interactive command cider-find-keyword
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bbatsov committed Dec 19, 2017
1 parent 0eaeaf4 commit 677ec20
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 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 @@
* [#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.
* [#2111](https://github.com/clojure-emacs/cider/pull/2111): Add `cider-pprint-eval-last-sexp-to-comment` and `cider-pprint-eval-defun-to-comment`.
* 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).
* [#2112](https://github.com/clojure-emacs/cider/issues/2112): Add a new interactive command `cider-find-keyword` (bound to `C-c C-:`).

### Changes

Expand Down
33 changes: 33 additions & 0 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,39 @@ the results to be displayed in a different window."
(ns (completing-read "Find namespace: " namespaces)))
(cider--find-ns ns (cider--open-other-window-p arg)))))

(defun cider-find-keyword (&optional arg)
"Find the namespace of the keyword at point and its first occurrence there.
For instance - if the keyword at point is \":cider.demo/keyword\", this command
would find the namespace \"cider.demo\" and afterwards find the first mention
of \"::keyword\" there.
Prompt according to prefix ARG and `cider-prompt-for-symbol'.
A single or double prefix argument inverts the meaning of
`cider-prompt-for-symbol'. A prefix of `-` or a double prefix argument causes
the results to be displayed in a different window. The default value is
thing at point."
(interactive "P")
(cider-ensure-connected)
(let* ((kw (let ((kw-at-point (cider-symbol-at-point 'look-back)))
(if (or cider-prompt-for-symbol arg)
(read-string
(format "Keyword (default %s): " kw-at-point)
nil nil kw-at-point)
kw-at-point)))
(ns-qualifier (and
(string-match "^:+\\(.+\\)/.+$" kw)
(match-string 1 kw)))
(kw-ns (if ns-qualifier
(cider-resolve-alias (cider-current-ns) ns-qualifier)
(cider-current-ns)))
(kw-to-find (concat "::" (replace-regexp-in-string "^:+\\(.+/\\)?" "" kw))))

(when (and ns-qualifier (string= kw-ns (cider-current-ns)))
(error "Could not resolve alias `%s' in `%s'" ns-qualifier (cider-current-ns)))
(cider--find-ns kw-ns arg)
(search-forward-regexp kw-to-find nil 'noerror)))

(defvar cider-completion-last-context nil)

(defun cider-completion-symbol-start-pos ()
Expand Down
2 changes: 2 additions & 0 deletions cider-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
("Find (jump to)"
["Find definition" cider-find-var]
["Find resource" cider-find-resource]
["Find keyword" cider-find-keyword]
["Go back" cider-pop-back])
("Macroexpand"
["Macroexpand-1" cider-macroexpand-1]
Expand Down Expand Up @@ -301,6 +302,7 @@ Configure `cider-cljs-*-repl' to change the ClojureScript REPL to use for your b
(define-key map (kbd "C-c C-d") 'cider-doc-map)
(define-key map (kbd "M-.") #'cider-find-var)
(define-key map (kbd "C-c C-.") #'cider-find-ns)
(define-key map (kbd "C-c C-:") #'cider-find-keyword)
(define-key map (kbd "M-,") #'cider-pop-back)
(define-key map (kbd "C-c M-.") #'cider-find-resource)
(define-key map (kbd "M-TAB") #'complete-symbol)
Expand Down
3 changes: 3 additions & 0 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,7 @@ constructs."
(declare-function cider-find-resource "cider-interaction")
(declare-function cider-restart "cider-interaction")
(declare-function cider-find-ns "cider-interaction")
(declare-function cider-find-keyword "cider-interaction")
(declare-function cider-switch-to-last-clojure-buffer "cider-mode")

(defvar cider-repl-mode-map
Expand All @@ -1440,6 +1441,7 @@ constructs."
(define-key map (kbd "C-c C-t") 'cider-test-commands-map)
(define-key map (kbd "M-.") #'cider-find-var)
(define-key map (kbd "C-c C-.") #'cider-find-ns)
(define-key map (kbd "C-c C-:") #'cider-find-keyword)
(define-key map (kbd "M-,") #'cider-pop-back)
(define-key map (kbd "C-c M-.") #'cider-find-resource)
(define-key map (kbd "RET") #'cider-repl-return)
Expand Down Expand Up @@ -1486,6 +1488,7 @@ constructs."
("Find"
["Find definition" cider-find-var]
["Find resource" cider-find-resource]
["Find keyword" cider-find-keyword]
["Go back" cider-pop-back])
"--"
["Switch to Clojure buffer" cider-switch-to-last-clojure-buffer]
Expand Down

0 comments on commit 677ec20

Please sign in to comment.