From 2694dced737261698b0fe99a2483c095a53e188d Mon Sep 17 00:00:00 2001 From: Aria Haghighi Date: Wed, 27 Oct 2010 02:11:54 -0400 Subject: [PATCH] Autocomplete actually works now. Sensitive to namepsace during autocomplete --- Support/bin/autocomplete.clj | 55 ++++++++++++++++++++++++++++++------ Support/utils.clj | 15 ++++++---- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Support/bin/autocomplete.clj b/Support/bin/autocomplete.clj index 63988b0..0f56982 100755 --- a/Support/bin/autocomplete.clj +++ b/Support/bin/autocomplete.clj @@ -5,12 +5,51 @@ (clojure.core/require '[clojure.repl :as repl]) (clojure.core/load-file (clojure.core/str (io/file (cake/*env* "TM_BUNDLE_SUPPORT") "utils.clj"))) -; (println "symbol:" (get-symbol-to-autocomplete)) -; (println (ns-refers (file-ns)) "
") -(defn- get-completions [] - (let [cur-symbol (get-symbol-to-autocomplete)] - (concat - (for [s (map (comp str first) (concat (ns-publics (file-ns)) (ns-refers (file-ns)))) - :when (.startsWith s cur-symbol)] s)))) +(defn- raw-symbol-completes [ns symb-prefix target-ns] + (let [possible (if target-ns + (ns-publics target-ns) + (concat (ns-publics ns) (ns-refers ns)))] + (for [symb possible + :let [name (-> symb first str)] + :when (.startsWith name symb-prefix)] + name))) + +(defn- raw-ns-completes [ns ns-prefix] + (for [[alias-ns _] (ns-aliases ns) + :let [name (.toString alias-ns)] + :when (and (.startsWith name ns-prefix))] + name)) + +(defn- get-completions + ([so-far] + (let [[_ ns-name var-name] (re-matches #"(?:([\w?_-]+)/)?([\w?_-]*)" so-far) + target-ns (when ns-name ((ns-aliases (file-ns)) (symbol ns-name)))] + #_(println "Complete:" (.toString target-ns)) + (concat + (raw-symbol-completes (file-ns) var-name target-ns) + (when-not target-ns (raw-ns-completes (file-ns) var-name))))) + ([] (get-completions (get-symbol-to-autocomplete)))) -(println (string/join " " (get-completions))) \ No newline at end of file +(println + (try + (string/join " " (get-completions)) + (catch Exception _ ""))) + +(comment + (get-completions "repl/s") + (ns-resolve) + (ns-resolve (file-ns) (symbol "get-completions")) + (ns-aliases (file-ns)) + (ns-refers) + (doc symbol) + (.toString ((ns-aliases (file-ns)) (symbol "repl"))) + + (raw-ns-completes (file-ns) "re") + (-> get-completions .getClass .getMethods) + (raw-symbol-completes (file-ns) "re" "") + (va (second (first (ns-publics (file-ns))))) + (ns-interns (file-ns)) + (ns-refers (file-ns)) + (ns-map (file-ns)) + (.toString (ffirst (ns-aliases (file-ns)))) +) \ No newline at end of file diff --git a/Support/utils.clj b/Support/utils.clj index 41afcdd..11a94d8 100644 --- a/Support/utils.clj +++ b/Support/utils.clj @@ -167,11 +167,16 @@ (defn get-symbol-to-autocomplete [] (let [#^String line (-> "TM_CURRENT_LINE" cake/*env* escape-str) - stop (dec (last (carret-info)))] - (loop [index stop] - (cond (zero? index) (.substring line 0 stop) - (not (symbol-char? (.charAt line index))) (.substring line (inc index) (inc stop)) - :else (recur (dec index)))))) + stop (dec (last (carret-info)))] + #_(println (carret-info)) + (loop [index stop] + (let [ch (.charAt line index)] + #_(println "index:" index "char:" ch"
") + (cond + (zero? index) (.trim (.substring line 0 (inc stop))) + (or (nil? ch) (not (symbol-char? (.charAt line index)))) + (.trim (.substring line (inc index) (inc stop))) + :else (recur (dec index))))))) (defn get-current-symbol-str