Permalink
Browse files

add token ns

  • Loading branch information...
1 parent 4303401 commit 98e57825de7952d269cc82f415faf027a584ed9c @ghostandthemachine committed Aug 30, 2012
Showing with 52 additions and 0 deletions.
  1. +52 −0 src/sketchpad/buffer/token.clj
@@ -0,0 +1,52 @@
+(ns sketchpad.buffer.token
+ (:require [sketchpad.util.tab :as tab]
+ [seesaw.core :as seesaw])
+ (:import (org.fife.ui.rsyntaxtextarea.RSyntaxUtilities)))
+
+(defn token-list-for-line
+"Returns the token list for a given line."
+ ([] (token-list-for-line (tab/current-text-area)))
+ ([text-area]
+ (let [doc (.getDocument text-area)
+ line (.getCaretLine text-area)
+ token (.getTokenListForLine doc line)]
+ token)))
+
+(defn current-token
+"Returns the Token object for the current token."
+ ([] (current-token (tab/current-text-area)))
+ ([text-area]
+ (let [doc (.getDocument text-area)
+ line (.getCaretLine text-area)
+ token (.getTokenListForLine doc line)
+ dot (.getCaretPosition text-area)
+ cur-token (org.fife.ui.rsyntaxtextarea.RSyntaxUtilities/getTokenAtOffset token dot)]
+ cur-token)))
+
+(defn line-token
+ ([] (line-token (tab/current-text-area)))
+ ([text-area]
+ (let [list-from-line (token-list-for-line text-area)
+ token-list (atom [(.getLexeme list-from-line)])]
+ (loop [t list-from-line]
+ (when-not (nil? (.getNextToken t))
+ (swap! token-list (fn [tl] (conj tl (.getLexeme t))))
+ (recur (.getNextToken t))))
+ @token-list)))
+
+(defn- file-exists?
+ [possible-path]
+ (let [f (clojure.java.io/file possible-path)]
+ (.exists f)))
+
+(defn- is-number?
+ [possible-number]
+ (=
+ (type (load-string possible-number))
+ java.lang.Long))
+
+(defn- can-be-opened?
+ [line-tokens]
+ (and
+ (file-exists? (first line-tokens))
+ (is-number? (second line-tokens))))

0 comments on commit 98e5782

Please sign in to comment.