Permalink
Browse files

Prefer candidates from project namespaces

Addresses the greater half of the TODO item:

;; TODO: prefer things in src/classes to jars
  • Loading branch information...
guns committed Nov 18, 2013
1 parent c0abd61 commit 0f449a54cfe7f49ae613ebc5723331e0099c3450
Showing with 17 additions and 5 deletions.
  1. +12 −5 src/slam/hound/regrow.clj
  2. +5 −0 test/slam/hound/regrow_test.clj
View
@@ -154,23 +154,30 @@
(defn- last-segment-matches-fn [type missing]
(let [alias (name missing)]
(fn [candidate]
- (if (= type :alias)
- (if (= alias (peek (string/split (name candidate) #"\.")))
- 0
- 1)
+ (if (and (= type :alias)
+ (= alias (peek (string/split (name candidate) #"\."))))
+ 0
1))))
+(defn- is-project-namespace-fn [type missing]
+ (fn [candidate]
+ (if (and (contains? #{:alias :refer} type)
+ (contains? (search/namespaces-from-files) candidate))
+ 0
+ 1)))
+
(defn disambiguate
"Select the most likely class or ns symbol in the given set of candidates,
returning [type candidate-sym]"
[candidates type missing old-ns-map]
- ;; TODO: prefer things in src/classes to jars
+ ;; TODO: prefer things in classes to jars
(debug :disambiguating missing :in candidates)
(let [cs (filter-excludes candidates type missing old-ns-map)
cs (remove #(re-find disambiguator-blacklist (str %)) cs)
cs (sort-by (juxt
(in-originals-fn type missing old-ns-map)
(last-segment-matches-fn type missing)
+ (is-project-namespace-fn type missing)
(comp count str))
cs)]
(when-let [c (first cs)]
@@ -24,6 +24,7 @@
(def / :special-case-token)
(def CapitalVar true)
(def Pattern "Not java.util.Pattern")
+(def trim (constantly "Conflicts with clojure.string/trim"))
(deftest ^:unit test-candidates
(testing "finds static and dynamically created Java packages"
@@ -75,6 +76,10 @@
(testing "prefers aliases where the last segment matches"
(is (= (disambiguate '#{clojure.set clojure.string} :alias 'set {})
'[:alias clojure.set])))
+ (testing "prefers candidates in project namespaces"
+ (is (= (disambiguate
+ '#{clojure.string slam.hound.regrow-test} :refer 'trim {})
+ '[:refer slam.hound.regrow-test])))
(testing "prefers shortest candidates when no other predicates match"
(is (= (disambiguate '#{clojure.java.io clojure.set clojure.string}
:alias 'a {})

0 comments on commit 0f449a5

Please sign in to comment.