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

Allow lookup of function symbol when cursor is on opening paren #2738

Merged
merged 2 commits into from
Nov 2, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### New features

* [#2738](https://github.com/clojure-emacs/cider/pull/2738): Add ability to lookup a function symbol when cursor is at the opening paren.
* [#2735](https://github.com/clojure-emacs/cider/pull/2735): New debugger command `P` to inspect an arbitrary expression, it was previously bound to `p` which now inspects the current value.
* [#2729](https://github.com/clojure-emacs/cider/pull/2729): New cider inspector command `cider-inspector-def-current-val` lets you define a var with the current inspector value.

Expand Down
2 changes: 2 additions & 0 deletions cider-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ find a symbol if there isn't one at point."
(when look-back
(save-excursion
(ignore-errors
(when (looking-at "(")
(forward-char 1))
(while (not (looking-at "\\sw\\|\\s_\\|\\`"))
(forward-sexp -1)))
(cider-symbol-at-point)))))
Expand Down
62 changes: 30 additions & 32 deletions test/cider-util-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
(require 'buttercup)
(require 'cider-util)

(defmacro with-clojure-buffer (contents &rest body)
"Execute BODY in a clojure-mode buffer with CONTENTS

CONTENTS is a string containing an optional character `|' indicating the
cursor position. If not present, the cursor is placed at the end of the
buffer."
(declare (indent 1))
`(with-temp-buffer
(delay-mode-hooks (clojure-mode))
(insert ,contents)
(goto-char (point-min))
(when (search-forward "|" nil 'noerror)
(delete-backward-char 1))
,@body))

;;; cider-util tests

(describe "cider--version"
Expand Down Expand Up @@ -71,33 +86,33 @@

(describe "cider-symbol-at-point"
(it "doesn't move the cursor"
(with-temp-buffer
(clojure-mode)
(insert "something else\n")
(with-clojure-buffer "something else\n"
(expect (cider-symbol-at-point) :not :to-be-truthy)
(expect (cider-symbol-at-point 'lookback) :to-equal "else")
(expect (point) :to-equal (point-max))))

(describe "when there is a symbol at point"
(it "returns the symbol"
(with-temp-buffer
(clojure-mode)
(insert "some-symbol ")
(with-clojure-buffer "some-symbol "
(expect (cider-symbol-at-point) :not :to-be-truthy)
(expect (cider-symbol-at-point 'look-back) :to-equal "some-symbol"))))

(describe "when the symbol at point has a trailing ."
(it "returns the symbol without the ."
(with-temp-buffer
(clojure-mode)
(insert "SomeRecord.")
(with-clojure-buffer "SomeRecord."
(expect (cider-symbol-at-point) :to-equal "SomeRecord"))))

(describe "when there's nothing at point"
(it "returns nil"
(spy-on 'thing-at-point :and-return-value nil)
(expect (cider-symbol-at-point) :not :to-be-truthy)))

(describe "when on an opening paren"
(it "returns the following symbol"
(with-clojure-buffer "(some function call)"
(goto-char (point-min))
(expect (cider-symbol-at-point 'look-back) :to-equal "some"))))

(it "can identify symbols in a repl, ignoring the repl prompt"
;; ignores repl prompts
(spy-on 'thing-at-point :and-return-value (propertize "user>" 'field 'cider-repl-prompt))
Expand All @@ -112,10 +127,7 @@
(describe "cider-sexp-at-point"
(describe "when the param 'bounds is not given"
(it "returns the sexp at point"
(with-temp-buffer
(clojure-mode)
(insert "a\n\n,")
(save-excursion (insert "(defn ...)\n\nb"))
(with-clojure-buffer "a\n\n,|(defn ...)\n\nb"
(expect (cider-sexp-at-point) :to-equal "(defn ...)")
(insert "@")
(expect (cider-sexp-at-point) :to-equal "(defn ...)")
Expand All @@ -125,31 +137,22 @@

(describe "when the param 'bounds is given"
(it "returns the bounds of starting and ending positions of the sexp"
(with-temp-buffer
(clojure-mode)
(insert "a\n\n,")
(save-excursion (insert "(defn ...)\n\nb"))
(with-clojure-buffer "a\n\n,|(defn ...)\n\nb"
(delete-char -1)
(insert "'")
(expect (cider-sexp-at-point 'bounds) :to-equal '(5 15))))))

(describe "cider-defun-at-point"
(describe "when the param 'bounds is not given"
(it "returns the defun at point"
(with-temp-buffer
(clojure-mode)
(insert "a\n\n(defn ...)")
(save-excursion (insert "\n\nb"))
(with-clojure-buffer "a\n\n(defn ...)|\n\nb"
(expect (cider-defun-at-point) :to-equal "(defn ...)\n")
(forward-sexp -1)
(expect (cider-defun-at-point) :to-equal "(defn ...)\n"))))

(describe "when the param 'bounds is given"
(it "returns the bounds of starting and ending positions of the defun"
(with-temp-buffer
(clojure-mode)
(insert "a\n\n(defn ...)")
(save-excursion (insert "\n\nb"))
(with-clojure-buffer "a\n\n(defn ...)|\n\nb"
(expect (cider-defun-at-point 'bounds) :to-equal '(4 15))))))

(describe "cider-repl-prompt-function"
Expand Down Expand Up @@ -209,10 +212,7 @@

(describe "cider-second-sexp-in-list"
(it "returns the second sexp in the list"
(with-temp-buffer
(clojure-mode)
(insert "(test-function arg1 arg2 arg3)")
(backward-char 2)
(with-clojure-buffer "(test-function arg1 arg2 arg|3)"
(expect (cider-second-sexp-in-list) :to-equal "arg1"))))

(describe "cider-ansi-color-string-detect"
Expand Down Expand Up @@ -253,9 +253,7 @@

(describe "works in buffers"
(it "fontifies with correct face"
(with-temp-buffer
(insert "aaa bbb\n cccc\n ddddd")
(goto-char 1)
(with-clojure-buffer "|aaa bbb\n cccc\n ddddd"
(cider-add-face "c+" 'font-lock-comment-face)
(expect (get-pos-property 11 'face)
:to-be 'font-lock-comment-face)))))