Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
167 lines (152 sloc) 6.63 KB
(ns sketchpad.editor.buffer
(:use [seesaw meta])
( File StringReader BufferedWriter OutputStreamWriter FileOutputStream)
( HtmlLanguageSupport)
( JavaLanguageSupport)
( CLanguageSupport)
( GroovyLanguageSupport)
( JavaScriptLanguageSupport)
( PerlLanguageSupport)
( PhpLanguageSupport)
( JspLanguageSupport)
( XmlLanguageSupport)
( ShellLanguageSupport))
(:require [ :as]
[sketchpad.file.file :as file]
[seesaw.core :as seesaw]
[ :as tab]
[sketchpad.project.project :as sketchpad.project]
[sketchpad.state.state :as state]
[sketchpad.tree.utils :as tree.utils]
[leiningen.core.project :as lein-project]
[ :as auto-complete]
[clojure.string :as string]
[seesaw.bind :as bind]
[seesaw.core :as seesaw]))
(defn update-buffer-info-file-title [title]
(swap! (@state/app :doc-title-atom) (fn [lbl] title)))
(defn add-auto-completion-from-type
(when @(:file buffer)
(let [suffix (last (clojure.string/split (.getName @(:file buffer)) #"\."))]
(= suffix "clj")
(auto-complete/install-auto-completion buffer)
(= suffix "java")
(let [java-lang-support (JavaLanguageSupport. )]
(reset! (:auto-complete buffer) java-lang-support)
(.install java-lang-support (get-in buffer [:component :text-area])))
(= suffix "c")
(let [c-lang-support (CLanguageSupport. )]
(reset! (:auto-complete buffer) c-lang-support)
(.install c-lang-support (get-in buffer [:component :text-area])))
(= suffix "groovy")
(let [groovy-lang-support (GroovyLanguageSupport. )]
(reset! (:auto-complete buffer) groovy-lang-support)
(.install groovy-lang-support (get-in buffer [:component :text-area])))
(= suffix "js")
(let [js-lang-support (JavaScriptLanguageSupport. )]
(reset! (:auto-complete buffer) js-lang-support)
(.install js-lang-support (get-in buffer [:component :text-area])))
(= suffix "perl")
(let [perl-lang-support (PerlLanguageSupport. )]
(reset! (:auto-complete buffer) perl-lang-support)
(.install perl-lang-support (get-in buffer [:component :text-area])))
(= suffix "php")
(let [php-lang-support (PhpLanguageSupport. )]
(reset! (:auto-complete buffer) php-lang-support)
(.install php-lang-support (get-in buffer [:component :text-area])))
(= suffix "jsp")
(let [jsp-lang-support (JspLanguageSupport. )]
(reset! (:auto-complete buffer) jsp-lang-support)
(.install jsp-lang-support (get-in buffer [:component :text-area])))
; not working correctly. There is some print out bug when looking at ac clomplets list
; (= suffix "sh")
; (let [sh-lang-support (ShellLanguageSupport. )]
; (.install sh-lang-support (get-in buffer [:component :text-area])))
(= suffix "xml")
(let [xml-lang-support (XmlLanguageSupport. )]
(reset! (:auto-complete buffer) xml-lang-support)
(.install xml-lang-support (get-in buffer [:component :text-area])))
(= suffix "html")
(let [html-lang-support (HtmlLanguageSupport. )]
(reset! (:auto-complete buffer) html-lang-support)
(.install html-lang-support (get-in buffer [:component :text-area])))))))
(defn init-buffer-tab-state [buffer]
(let [text-area (get-in buffer [:component :text-area])]
(tab/focus-buffer buffer)
(update-buffer-info-file-title (tab/title))
(tab/mark-tab-clean! buffer)
(.discardAllEdits text-area)
(.setCaretPosition text-area 0)))
(defn selected-file-path []
(when-let [tree-path (-> @state/app :docs-tree .getSelectionPaths first)]
(-> tree-path .getLastPathComponent .getUserObject .getAbsolutePath)))
(defn update-buffer-label-from-file [buffer file-path]
(let [file (File. file-path)]
(swap! (:title buffer) (fn [_] (.getName file)))
(seesaw/config! (get-in buffer [:tab :label]) :text (.getName file))
(tab/buffer-title! buffer (.getName file))))
(defn update-buffer-syntax-style [buffer file-path]
(seesaw/config! buffer :syntax (file/file-type file-path)))
(defn load-file-into-buffer [project buffer file-path]
(when-let [txt (slurp file-path)]
(let[rdr (StringReader. txt)
file (File. file-path)
text-area (:text-area buffer)]
(.read text-area rdr nil)
(update-buffer-syntax-style text-area file-path)
(update-buffer-label-from-file buffer file-path)
(swap! (:title buffer) (fn [_] (.getName file)))
(swap! (:file buffer) (fn [_] file))
(reset! (:new-file? buffer) false)
(add-auto-completion-from-type buffer))))
(defn file-project-path
(let [project-paths @(:projects @state/app)
proj (atom nil)]
(doseq [p (keys project-paths)]
(let [files (file-seq p)]
(when (contains? files file)
(println p)
(reset! proj p)))
(let [open-project-path (if (nil? @proj)
(defn- bring-buffer-to-front
(let [buffer (first (filter #(= (.getAbsolutePath ( file-path)) (.getAbsolutePath @(:file % ))) (vals (tab/current-buffers))))]
(tab/show-buffer buffer)
(defn- buffer-already-open?
(not (nil? (some #(= (.getAbsolutePath ( file-path)) (.getAbsolutePath @(:file % ))) (vals (tab/current-buffers))))))
(defn open-buffer [file-path project-path]
(if (buffer-already-open? file-path)
(bring-buffer-to-front file-path)
(let [project (sketchpad.project/project-from-path project-path)
buffer ( project-path)]
(load-file-into-buffer project buffer file-path)
(init-buffer-tab-state buffer)
(sketchpad.project/add-buffer-to-project project-path buffer)
(sketchpad.project/add-buffer-to-app buffer)
(tab/show-buffer buffer)
(defn blank-clj-buffer!
([] (blank-clj-buffer! nil))
(let [buffer ( ".sketchpad-tmp")]
(init-buffer-tab-state buffer)
(sketchpad.project/add-buffer-to-app buffer)
(tab/show-buffer buffer)))))
(defn new-project-buffer!
"Create a new buffer for a loaded project."
[project-path selection-path]
(let [buffer ( project-path selection-path)]
(sketchpad.project/add-buffer-to-project project-path buffer)
(sketchpad.project/add-buffer-to-app buffer)
(init-buffer-tab-state buffer)
(tab/show-buffer buffer)))
Jump to Line
Something went wrong with that request. Please try again.