branch: elpa/cider
commit ef5bbe080fdd7b4e33f31e8354eaea04413aefca
Author: vemv <v...@users.noreply.github.com>
Commit: vemv <v...@users.noreply.github.com>

    Improve the reliability of `cider--find-keyword-loc`
    
    * Allow to jump to a qualified keyword within the same ns
    * Explicitly forbid querying non-qualified keywords
---
 cider-find.el            | 32 ++++++++++++++++++++++----------
 test/cider-find-tests.el | 17 +++++++++++------
 2 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/cider-find.el b/cider-find.el
index f835c2a249..feb54cbbfe 100644
--- a/cider-find.el
+++ b/cider-find.el
@@ -219,21 +219,33 @@ are disregarded."
 
 Returns the dict in all cases.  `dest-point' indicates success:
 integer on successful finds, nil otherwise."
-  (let* ((ns-qualifier (and
-                        (string-match "^:+\\(.+\\)/.+$" kw)
-                        (match-string 1 kw)))
-         (kw-ns (or (cider-resolve--get-in (cider-current-ns) "aliases" 
ns-qualifier)
-                    ns-qualifier))
+  (let* ((simple-ns-qualifier (and
+                               (string-match "^:\\(.+\\)/.+$" kw)
+                               (match-string 1 kw)))
+         (auto-resolved-ns-qualifier (and
+                                      (string-match "^::\\(.+\\)/.+$" kw)
+                                      (match-string 1 kw)))
+         (kw-ns (or (and auto-resolved-ns-qualifier
+                         (or (cider-resolve-alias (cider-current-ns) 
auto-resolved-ns-qualifier)
+                             (user-error "Could not resolve alias: %S" 
auto-resolved-ns-qualifier)))
+                    (and (string-match "^::" kw)
+                         (cider-current-ns :no-default))
+                    simple-ns-qualifier
+                    (user-error "Not a ns-qualified keyword: %S" kw)))
          (kw-name (replace-regexp-in-string "^:+\\(.+/\\)?" "" kw))
-         (end-of-word "\\>") ;; important: if searching for foo, we don't want 
to match foobar (a larger word)
-         (kw-to-find (concat "\\("
+         (beginning-of-symbol "\\_<")
+         (end-of-symbol "\\_>") ;; important: if searching for foo, we don't 
want to match foobar (a larger symbol)
+         (kw-to-find (concat beginning-of-symbol
+                             "\\("
                              (concat "::" kw-name)
                              "\\|"
                              (concat ":" kw-ns "/" kw-name)
                              "\\)"
-                             end-of-word)))
-    (let* ((url (cider-sync-request:ns-path kw-ns t))
-           (dest (cider-find-file url))
+                             end-of-symbol)))
+    (let* ((url (when kw-ns
+                  (cider-sync-request:ns-path kw-ns t)))
+           (dest (when url
+                   (cider-find-file url)))
            (dest-point (when dest
                          (cider--find-keyword-in-buffer dest kw-to-find))))
       (nrepl-dict "url" url "dest" dest "dest-point" dest-point))))
diff --git a/test/cider-find-tests.el b/test/cider-find-tests.el
index cdf87786da..70efa73b56 100644
--- a/test/cider-find-tests.el
+++ b/test/cider-find-tests.el
@@ -53,10 +53,10 @@ more
 stuff"
       (let* ((sample-buffer (current-buffer)))
         (spy-on 'cider-ensure-connected :and-return-value t)
-        (spy-on 'cider-resolve--get-in :and-return-value nil)
-        (spy-on 'cider-current-ns :and-return-value nil)
         (spy-on 'cider-sync-request:ns-path :and-call-fake (lambda (kw-ns _)
                                                              kw-ns))
+        (spy-on 'cider-resolve-alias :and-call-fake (lambda (_ns ns-qualifier)
+                                                      ns-qualifier))
         (spy-on 'cider-find-file :and-call-fake (lambda (kw-ns)
                                                   (when (equal kw-ns "some.ns")
                                                     sample-buffer)))
@@ -68,6 +68,13 @@ stuff"
             ;; important - ensure that we're looking at ::foo and not ::foobar:
             (expect (cider-symbol-at-point 'look-back) :to-equal "::foo")))
 
+        (nrepl-dbind-response (cider--find-keyword-loc "::foo") (dest 
dest-point)
+          (expect dest-point :to-equal 63)
+          (with-current-buffer dest
+            (goto-char dest-point)
+            ;; important - ensure that we're looking at ::foo and not ::foobar:
+            (expect (cider-symbol-at-point 'look-back) :to-equal "::foo")))
+
         (nrepl-dbind-response (cider--find-keyword-loc ":some.ns/foo") (dest 
dest-point)
           (expect dest-point :to-equal 63)
           (with-current-buffer dest
@@ -81,12 +88,10 @@ stuff"
         (nrepl-dbind-response (cider--find-keyword-loc ":some.ns/bar") (dest 
dest-point)
           (expect dest-point :to-equal nil))
 
-        (nrepl-dbind-response (cider--find-keyword-loc ":foo") (dest 
dest-point)
-          (expect dest-point :to-equal nil))
+        (expect (cider--find-keyword-loc ":foo") :to-throw 'user-error)
 
         (nrepl-dbind-response (cider--find-keyword-loc ":unrelated/foo") (dest 
dest-point)
           (expect dest-point :to-equal nil))
 
         (nrepl-dbind-response (cider--find-keyword-loc "::unrelated/foo") 
(dest dest-point)
-          (expect dest-point :to-equal nil))
-        ))))
+          (expect dest-point :to-equal nil))))))

Reply via email to