Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns script
- (:require
- [augistints.core :as ac]
- [clojure.data.json :as cdj]
- [rewrite-clj.zip :as rz])
- (:gen-class))
- (set! *warn-on-reflection* true)
- (defn find-defn-zloc-at
- [forms-str row col]
- (let [curr-zloc (-> (rz/of-string forms-str
- {:track-position? true})
- (rz/find-last-by-pos [row col]))
- initial-char (first (rz/string curr-zloc))
- curr-zloc (if (= initial-char \()
- (rz/down curr-zloc)
- curr-zloc)]
- (loop [curr-zloc curr-zloc]
- (when curr-zloc
- (let [lm-zloc (rz/leftmost curr-zloc)
- up-zloc (rz/up lm-zloc)]
- (if (#{'defn 'defn-} (rz/sexpr lm-zloc))
- up-zloc
- (recur up-zloc)))))))
- (defn my-slurp-stdin
- []
- (let [sb (StringBuilder.)]
- (loop [res (.read System/in)]
- (when (not= -1 res)
- (.append sb (char res))
- (recur (.read System/in))))
- (.toString sb)))
- (defn -main [& args]
- (let [;; XXX: expects json rpc string on stdin, e.g.
- ;; {"jsonrpc": "2.0",
- ;; "method": "inlinedef",
- ;; "params": ["(defn my-fn [a] (+ a 1))" 3 4],
- ;; "id": 1}
- slurped (my-slurp-stdin)
- {:keys [method params id]} (cdj/read-str slurped :key-fn keyword)
- [text row col] params
- defn-zloc (find-defn-zloc-at text row col)
- [new-text range]
- (if defn-zloc
- [(-> (rz/string defn-zloc)
- (ac/prepend-to-defn-body (ac/make-inline-def-with-meta-gen
- {:adorn/inlinedef true}))
- ac/cljfmt)
- (rz/position-span defn-zloc)]
- [nil nil])
- ;; XXX: check for errors?
- result (-> (assoc {"jsonrpc" "2.0", "id" id}
- "result" [new-text range])
- cdj/write-str)]
- ;; XXX: println might work...try later
- (.print System/out result)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement