-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cider and AST for smarter refactorings
- very very first draft (more rough edges than achievements) - starts up a clj-refactor cider repl session when clj-refactor minor mode is switched on -- presumably only once - provides a minimal clj project and user namespace with helper functions - demonstrates the usage of the cider session when searching for referred functions when removing not used requires
- Loading branch information
1 parent
01a338a
commit cdf1c16
Showing
3 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
(ns user | ||
(:refer-clojure :exclude [macroexpand-1 read read-string]) | ||
(:require [clojure.java.io :as io] | ||
[clojure.tools.analyzer :as ana] | ||
[clojure.tools.analyzer.utils :refer [resolve-var]] | ||
[clojure.tools.analyzer.ast :refer :all] | ||
[clojure.tools.reader :as r]) | ||
(:import java.io.PushbackReader)) | ||
|
||
;; will I need this? | ||
;; perhaps just get the ns as string from the emacs buffer | ||
(defn read-all | ||
[input] | ||
(let [eof (Object.)] | ||
(take-while #(not= % eof) (repeatedly #(read input false eof))))) | ||
|
||
(defn read-all-file [file] (-> file io/reader (PushbackReader.) read-all)) | ||
|
||
(defn desugar-host-expr [[op & expr :as form]] | ||
(if (symbol? op) | ||
(let [opname (name op)] | ||
(cond | ||
(= (first opname) \.) ; (.foo bar ..) | ||
(let [[target & args] expr | ||
args (list* (symbol (subs opname 1)) args)] | ||
(with-meta (list '. target (if (= 1 (count args)) ;; we don't know if (.foo bar) ia | ||
(first args) args)) ;; a method call or a field access | ||
(meta form))) | ||
(= (last opname) \.) ;; (class. ..) | ||
(with-meta (list* 'new (symbol (subs opname 0 (dec (count opname)))) expr) | ||
(meta form)) | ||
:else form)) | ||
form)) | ||
|
||
(defn macroexpand-1 [form env] | ||
(if (seq? form) | ||
(let [op (first form)] | ||
(if (ana/specials op) | ||
form | ||
(let [v (resolve-var op env)] | ||
(if (and (not (-> env :locals (get op))) ;; locals cannot be macros | ||
(:macro (meta v))) | ||
(apply v form env (rest form)) ; (m &form &env & args) | ||
(desugar-host-expr form))))) | ||
form)) | ||
|
||
(def e (ana/empty-env)) | ||
|
||
(defmacro ast [form] | ||
`(binding [ana/macroexpand-1 macroexpand-1 | ||
ana/create-var ~(fn [sym env] | ||
(doto (intern (:ns env) sym) | ||
(reset-meta! (meta sym)))) | ||
ana/parse ana/-parse | ||
ana/var? ~var?] | ||
(ana/analyze '~form e))) | ||
|
||
(defmacro string-ast [string] | ||
`(binding [ana/macroexpand-1 macroexpand-1 | ||
ana/create-var ~(fn [sym env] | ||
(doto (intern (:ns env) sym) | ||
(reset-meta! (meta sym)))) | ||
ana/parse ana/-parse | ||
ana/var? ~var?] | ||
(ana/analyze (r/read-string ~string) e))) | ||
|
||
(defmacro file-ast [file] | ||
`(binding [ana/macroexpand-1 macroexpand-1 | ||
ana/create-var ~(fn [sym env] | ||
(doto (intern (:ns env) sym) | ||
(reset-meta! (meta sym)))) | ||
ana/parse ana/-parse | ||
ana/var? ~var?] | ||
(ana/analyze (read-all-file ~file) e))) | ||
|
||
(defn find-referred [ns-body referred] | ||
(some #(= (symbol referred) (:class %)) (nodes (string-ast ns-body)))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
(defproject clj-refactor "0.12.0" | ||
:description "To initate a helper cider session with analyzer support for ASTs" | ||
:profiles {:dev {:source-paths ["dev"] | ||
:dependencies [[org.clojure/tools.analyzer "0.1.0-beta10"] | ||
[org.clojure/tools.analyzer.jvm "0.1.0-beta10"] | ||
[org.clojure/tools.reader "0.8.4"]]}}) |