Skip to content

Commit

Permalink
split out part of dumb-jump-go and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktasia committed Jan 12, 2016
1 parent d7bb60b commit cce4e4a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
27 changes: 16 additions & 11 deletions dumb-jump.el
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
:regex "\\\(defvar\\b\\s*JJJ\\b\\s*" :tests ("(defvar test "))
(:type "variable" :language "elisp"
:regex "\\\(setq\\b\\s*JJJ\\b\\s*" :tests ("(setq test 123)"))
(:type "variable" :language "elisp"
:regex "\\\(JJJ\\b\\s*" :tests ("(let ((test 123)))"))

;; python
(:type "variable" :language "python"
Expand Down Expand Up @@ -219,9 +221,7 @@ If not found, then return dumb-jump-default-profile"
(plist-get (car result) :language)
nil)))

(defun dumb-jump-go ()
"Go to the function/variable declaration for thing at point"
(interactive)
(defun dumb-jump-fetch-results ()
(let* ((cur-file (buffer-file-name))
(cur-line (thing-at-point 'line))
(cur-line-num (line-number-at-pos))
Expand All @@ -239,22 +239,27 @@ If not found, then return dumb-jump-default-profile"
(dumb-jump-read-exclusions proj-config)
""))
(raw-results (dumb-jump-run-command look-for proj-root regexes include-args exclude-args cur-line-num))
(results (-map (lambda (r) (plist-put r :target look-for)) raw-results))
(result-count (length results))
(top-result (car results)))
(results (-map (lambda (r) (plist-put r :target look-for)) raw-results)))
`(:results ,results :lang ,(if (null lang) "" lang) :symbol ,look-for :ctx-type ,(if (null ctx-type) "" ctx-type) :file ,cur-file :root ,proj-root)))

(defun dumb-jump-go ()
"Go to the function/variable declaration for thing at point"
(interactive)
(let* ((info (dumb-jump-fetch-results))
(results (plist-get info :results))
(result-count (length results)))
; (message-prin1 "lang:%s type:%s results: %s" lang ctx-type results)
(cond
((and (not (listp results)) (s-blank? results))
(message "Could not find rules for language '%s'." lang))
(message "Could not find rules for language '%s'." (plist-get info :lang)))
((= result-count 1)
(dumb-jump-result-follow top-result))
(dumb-jump-result-follow (car results)))
((> result-count 1)
;; multiple results so let the user pick from a list
;; unless the match is in the current file
(dumb-jump-handle-results results cur-file proj-root ctx-type look-for)
)
(dumb-jump-handle-results results (plist-get info :file) (plist-get info :root) (plist-get info :ctx-type) (plist-get info :symbol)))
((= result-count 0)
(message "'%s' %s %s declaration not found." look-for (if (null lang) "" lang) (if (null ctx-type) "" ctx-type)))
(message "'%s' %s %s declaration not found." (plist-get info :symbol) (plist-get info :lang) (plist-get info :ctx-type)))
(t
(message "Un-handled results: %s " (prin1-to-string results))))))

Expand Down
11 changes: 10 additions & 1 deletion test/dumb-jump-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

(ert-deftest dumb-jump-generate-command-no-ctx-test ()
(let ((regexes (dumb-jump-get-contextual-regexes "elisp" nil))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\s*' -e '\\(defvar\\b\\s*tester\\b\\s*' -e '\\(setq\\b\\s*tester\\b\\s*' ."))
(expected "LANG=C grep -REn -e '\\(defun\\s+tester\\s*' -e '\\(defvar\\b\\s*tester\\b\\s*' -e '\\(setq\\b\\s*tester\\b\\s*' -e '\\(tester\\b\\s*' ."))
(should (string= expected (dumb-jump-generate-command "tester" "." regexes "" "")))))

(ert-deftest dumb-jump-generate-command-with-ctx-test ()
Expand Down Expand Up @@ -123,3 +123,12 @@
(let* ((choice-txt (dumb-jump-generate-prompt-text "asdf" "/usr/blah" '((:path "/usr/blah/test.txt" :line "54"))))
(expected "Multiple results for 'asdf':\n\n1. /test.txt:54\n\nChoice: "))
(should (string= choice-txt expected))))

(ert-deftest dumb-jump-fetch-results-test ()
(let ((js-file (f-join test-data-dir-proj1 "src" "js" "fake.js")))
(with-current-buffer (find-file-noselect js-file t)
(forward-line 2)
(forward-char 10)
(let ((results(dumb-jump-fetch-results)))
(should (string= "doSomeStuff" (plist-get results :symbol)))
(should (string= "javascript" (plist-get results :lang)))))))

0 comments on commit cce4e4a

Please sign in to comment.