Commits

Meikel Brandmeyer committed de47eee

Fix issues with -/_ mangling and path separators

* cljtags.clj (mangle-name, namespace->directory): new functions

* cljtags.clj (get-definition-position): implement heuristic
to choose ambiguous files.

Comments (0)

Files changed (1)

src/de/kotka/cljtags.clj

      [clojure.contrib.duck-streams :only (writer)]
      [clojure.contrib.command-line :only (with-command-line)]))
 
+(defn- mangle-name
+  "Replace - with _ in directories and filenames."
+  [n]
+  (.replace n \- \_))
+
+(defn- namespace->directory
+  [n]
+  (-> n mangle-name (.replace \. File/separatorChar)))
+
 (defn- get-definition-position
   [p m]
-  (let [vns    (name (ns-name (m :ns)))
-        nsbase (.substring vns (inc (.lastIndexOf vns (int \.))))
-        nspath (.replace vns \. \/)
+  (let [nspath (-> m :ns ns-name name namespace->directory)
+        sl-pos (.lastIndexOf nspath (int File/separatorChar))
+        nsbase (.substring nspath (inc sl-pos))
         file   (m :file)
         fbase  (.substring file 0 (.lastIndexOf file (int \.)))
         line   (m :line)]
     (list (name (m :name))
-          (if (= fbase nsbase)
-            (str p File/separator nspath ".clj")
-            (str p File/separator nspath File/separator file))
+          ; XXX: Beh, ugly heuristic, but necessary...
+          ; 1. Is the file basename equal to the last component of the
+          ;    namespace (after mangling)? => Use it.
+          ; 2. Does the file exists on the same level as the namespace's
+          ;    master file? => Use it.
+          ; Otherwise expect the file to be in the namespace directory.
+          (let [c (apply str (interpose File/separator
+                                        [p (.substring nspath 0 sl-pos) file]))]
+            (cond
+              (= fbase nsbase)      (str p File/separator nspath ".clj")
+              (-> c File. .exists)  c
+              :else                 (apply str (interpose File/separator
+                                                          [p nspath file]))))
           line)))
 
 (defn -main