Skip to content

Commit

Permalink
Pull out nailgun connector logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kotarak committed Jul 10, 2012
1 parent 3d4c9f2 commit 27317ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
29 changes: 17 additions & 12 deletions server/src/main/clojure/vimclojure/nails.clj
Expand Up @@ -68,10 +68,17 @@
(.init sys local)
old))

(defn nail-driver
"Driver for the defnail macro."
[#^NGContext ctx nail]
(let [out (ByteArrayOutputStream.)
(defn nailgun-driver
"Entry point for the nailgun connector."
[#^NGContext ctx]
(let [args (.getArgs ctx)
nail (aget args 0)
nail (let [slash (.indexOf nail "/")
[nspace nail] (if (neg? slash)
["vimclojure.nails" nail]
[(subs nail 0 slash) (subs nail slash)])]
@(resolve (symbol nspace nail)))
out (ByteArrayOutputStream.)
err (ByteArrayOutputStream.)
encoding (System/getProperty "clojure.vim.encoding" "UTF-8")
[clj-in clj-out clj-err] (make-stream-set (.in ctx) out err encoding)
Expand All @@ -82,7 +89,7 @@
*out* clj-out
*err* clj-err]
(try
(nail ctx)
(nail (next args))
(catch Throwable e
(.printStackTrace e))))]
(.flush clj-out)
Expand All @@ -108,13 +115,11 @@
'nailContext'."
[nail usage arguments & body]
`(defn ~nail
[ctx#]
(nail-driver ctx#
(fn [~(with-meta 'nailContext {:tag `NGContext})]
(util/with-command-line (next (.getArgs ~'nailContext))
~usage
~arguments
~@body)))))
[args#]
(util/with-command-line args#
~usage
~arguments
~@body)))

(defnail DocLookup
"Usage: ng vimclojure.nails.DocString [options] symbol ..."
Expand Down
19 changes: 4 additions & 15 deletions server/src/main/java/vimclojure/Nail.java
Expand Up @@ -31,10 +31,14 @@

public class Nail {
/* Load up vimclojure */
static Var nailDriver;

static {
try {
final Var require = RT.var("clojure.core", "require");
require.invoke(Symbol.create("vimclojure.nails"));

nailDriver = RT.var("vimclojure.nails", "nailgun-driver");
} catch (Exception exc) {
Throwable e = exc;
System.err.println("A crisis has arisen:");
Expand All @@ -43,21 +47,6 @@ public class Nail {
}

public static void nailMain(NGContext ctx) throws Exception {
final String nail = ctx.getArgs()[0];
int slash = nail.indexOf("/");

String namespace;
String function;

if (slash == -1) {
namespace = "vimclojure.nails";
function = nail;
} else {
namespace = nail.substring(0, slash);
function = nail.substring(slash + 1);
}

final Var nailDriver = RT.var(namespace, function);
nailDriver.invoke(ctx);
}
}

0 comments on commit 27317ec

Please sign in to comment.