Created
July 15, 2012 16:16
-
-
Save nkkarthik/3117608 to your computer and use it in GitHub Desktop.
Clojure-complete: Partial class name completion patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From ec8d67db882c28c74097020cc09b09f4cddc3097 Mon Sep 17 00:00:00 2001 | |
From: Krishna Karthik <nkkarthik@gmail.com> | |
Date: Sun, 15 Jul 2012 18:12:11 +0530 | |
Subject: [PATCH] Partial class name completion | |
--- | |
src/complete/core.clj | 24 ++++++++++++++++++++---- | |
test/complete/core_test.clj | 10 +++++++++- | |
2 files changed, 29 insertions(+), 5 deletions(-) | |
diff --git a/src/complete/core.clj b/src/complete/core.clj | |
index f6c218a..87ace7f 100644 | |
--- a/src/complete/core.clj | |
+++ b/src/complete/core.clj | |
@@ -87,11 +87,16 @@ | |
(class (clojure.main/repl-exception e))) | |
(throw e))))) | |
+(defn- maybe-java-class? [prefix] | |
+ "Returns true if the prefix could mean a java class. Just checks if | |
+there is a capital letter in the prefix." | |
+ (re-find #"[A-Z]" prefix)) | |
+ | |
(defmulti potential-completions | |
(fn [prefix ns] | |
(cond (.contains prefix "/") :scoped | |
- (.contains prefix ".") :class | |
- :else :var))) | |
+ (or (.contains prefix ".") (maybe-java-class? prefix)) :class | |
+ :else :var))) | |
(defmethod potential-completions :scoped | |
[prefix ns] | |
@@ -117,9 +122,20 @@ | |
(ns-classes ns) | |
(ns-java-methods ns))) | |
+(defn- re-match [prefix] | |
+ "Returns a predicate which given a complete class name returns true | |
+ if the prefix matches the class name." | |
+ (let [regex (apply str (replace {\$ "\\$", \. "\\.", \* ".*"} | |
+ prefix)) | |
+ pkg-pat "[^.]+\\." | |
+ pattern (re-pattern (format "^(%s)*%s" pkg-pat regex))] | |
+ #(re-find pattern #^String %))) | |
+ | |
(defn completions | |
"Return a sequence of matching completions given a prefix string and an optional current namespace." | |
([prefix] (completions prefix *ns*)) | |
([prefix ns] | |
- (sort (for [completion (potential-completions prefix ns) :when (.startsWith completion prefix)] | |
- completion)))) | |
+ (let [matches? (re-match prefix)] | |
+ (sort (for [completion (potential-completions prefix ns) | |
+ :when (matches? completion)] | |
+ completion))))) | |
\ No newline at end of file | |
diff --git a/test/complete/core_test.clj b/test/complete/core_test.clj | |
index 4a720d9..faef94a 100644 | |
--- a/test/complete/core_test.clj | |
+++ b/test/complete/core_test.clj | |
@@ -22,4 +22,12 @@ | |
(some #{"valueOf"} (completions "String/")) | |
- (not (some #{"String/indexOf" ".indexOf"} (completions "String/")))) | |
+ (not (some #{"String/indexOf" ".indexOf"} (completions "String/"))) | |
+ | |
+ ;; partial class name without package | |
+ (is (some #{"javax.swing.JFrame"} | |
+ (completions "JF"))) | |
+ | |
+ ;; partial class name with wildcard | |
+ (is (some #{"javax.swing.tree.DefaultTreeModel"} | |
+ (completions "Def*Tre")))) | |
-- | |
1.7.9.5 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment