Skip to content

Commit

Permalink
use inline popup for multiple jump choices
Browse files Browse the repository at this point in the history
  • Loading branch information
jacktasia committed Feb 22, 2016
1 parent 27c0826 commit 6f4bb2e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 53 deletions.
43 changes: 16 additions & 27 deletions dumb-jump.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Copyright (C) 2015 jack angers
;; Author: jack angers
;; Version: 1.0
;; Package-Requires: ((f "0.17.3") (s "1.11.0") (dash "2.9.0"))
;; Package-Requires: ((f "0.17.3") (s "1.11.0") (dash "2.9.0") (popup "0.5.3"))
;; Keywords: programming
;;; Commentary:

Expand All @@ -15,6 +15,7 @@
(require 'f)
(require 's)
(require 'dash)
(require 'popup)

(defgroup dumb-jump nil
"Easily jump to project function and variable definitions"
Expand Down Expand Up @@ -273,31 +274,19 @@ Optionally pass t to see a list of all failed rules"
(substring line right-loc-start right-loc-end))))
`(:left ,left :right ,right)))

(defun dumb-jump-generate-prompt-text (look-for proj results)
(let* ((title (format "Multiple results for '%s':\n\n" look-for))
(choices (-map-indexed (lambda (index result)
(format "%d. %s:%s" (1+ index)
(s-replace proj "" (plist-get result :path))
(plist-get result :line)))
results)))
(concat title (s-join "\n" choices) "\n\nChoice: ")))

(defun dumb-jump-parse-input (total input)
"Return INPUT if it is less than TOTAL and gte 1"
(let* ((choice (string-to-number input)))
(when (and
(<= choice total)
(>= choice 1))
choice)))

(defun dumb-jump-prompt-user-for-choice (look-for proj results)
"Prompt a user to pick between multiple RESULTS and go to LOOK-FOR if valid"
(let* ((prompt-text (dumb-jump-generate-prompt-text look-for proj results))
(input (read-from-minibuffer prompt-text))
(choice (dumb-jump-parse-input (length results) input)))
(if choice
(dumb-jump-result-follow (nth (1- choice) results))
(dumb-jump-message "Sorry, that's an invalid choice."))))
(defun dumb-jump-prompt-user-for-choice (proj results)
(let* ((choices (-map (lambda (result)
(format "%s:%s %s"
(s-replace proj "" (plist-get result :path))
(plist-get result :line)
(s-trim (plist-get result :context))))
results))
(input (popup-menu* choices))
(result-index (--find-index (string= input it) choices))
(result (when result-index
(nth result-index results))))
(when result
(dumb-jump-result-follow result))))

(defun dumb-jump-get-project-root (filepath)
"Keep looking at the parent dir of FILEPATH until a
Expand Down Expand Up @@ -423,7 +412,7 @@ denoter file/dir is found or uses dumb-jump-default-profile"
;(dumb-jump-message-prin1 "type: %s | jump? %s | matches: %s | sorted: %s | results: %s" ctx-type var-to-jump matches match-sorted results)
(if do-var-jump
(dumb-jump-result-follow var-to-jump)
(dumb-jump-prompt-user-for-choice look-for proj-root match-cur-file-front))))
(dumb-jump-prompt-user-for-choice proj-root match-cur-file-front))))

(defun dumb-jump-read-config (root config-file)
"Get options (exclusions, inclusions) from config file .dumbjump CONFIG-FILE in the project ROOT"
Expand Down
6 changes: 6 additions & 0 deletions test/data/proj1/src/js/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ $scope.resized = function(path, w, h) {
}
return $scope.resizeHost + encodeURIComponent(path) + '?w=' + w + '&h=' + h;
};

someFunc();



//
7 changes: 7 additions & 0 deletions test/data/proj1/src/js/fake3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var someFunc = function() {
console.log('some func a');
};

function someFunc() {
console.log('some func b');
}
34 changes: 8 additions & 26 deletions test/dumb-jump-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(require 's)
(require 'dash)
(require 'noflet)
(require 'popup)

(setq test-data-dir (f-expand "./test/data"))
(setq test-data-dir-elisp (f-join test-data-dir "proj2-elisp"))
Expand Down Expand Up @@ -173,33 +174,14 @@
(ctx-type (dumb-jump-get-ctx-type-by-language "javascript" pt-ctx)))
(should (string= ctx-type "function"))))

(ert-deftest dumb-jump-multiple-choice-input-test ()
(progn
(should (= (dumb-jump-parse-input 5 "4") 4))
(should (= (dumb-jump-parse-input 50 "1") 1))
(should (null (dumb-jump-parse-input 50 "242")))
(should (null (dumb-jump-parse-input 5 "0")))
(should (null (dumb-jump-parse-input 500 "asdf")))
(should (null (dumb-jump-parse-input 5 "6")))))

(ert-deftest dumb-jump-multiple-choice-text-test ()
(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-prompt-user-for-choice-invalid-test ()
(noflet ((read-from-minibuffer (input) "2")
(dumb-jump-message (input)
(should (string= input "Sorry, that's an invalid choice."))))

(dumb-jump-prompt-user-for-choice "asdf" "/usr/blah" '((:path "/usr/blah/test.txt" :line "54")))))

(ert-deftest dumb-jump-prompt-user-for-choice-correct-test ()
(noflet ((read-from-minibuffer (input) "2")
(dumb-jump-result-follow (result)
(should (string= (plist-get result :path) "/usr/blah/test2.txt"))))

(dumb-jump-prompt-user-for-choice "asdf" "/usr/blah" '((:path "/usr/blah/test.txt" :line "54") (:path "/usr/blah/test2.txt" :line "52")))))
(let* ((results '((:path "/usr/blah/test.txt" :line 54 :context "function thing()") (:path "/usr/blah/test2.txt" :line 52 :context "var thing = function()" :target "a"))))
(noflet ((dumb-jump-result-follow (r)
(should (string= (plist-get r :path) "/usr/blah/test2.txt"))
(should (= (plist-get r :line) 52)))
(popup-menu* (choices)
"/test2.txt:52 var thing = function()"))
(dumb-jump-prompt-user-for-choice "/usr/blah" results))))

(ert-deftest dumb-jump-fetch-results-test ()
(let ((js-file (f-join test-data-dir-proj1 "src" "js" "fake.js")))
Expand Down

0 comments on commit 6f4bb2e

Please sign in to comment.