Permalink
Browse files

References to parsley are explicit.

  • Loading branch information...
1 parent fc71168 commit ee01cfc4b6ccf497664ed2d94fca27e8df27b9a9 @tcrayford tcrayford committed Sep 10, 2010
@@ -1,63 +1,65 @@
(ns clojure-refactoring.destructuring
(:use clojure.walk
- [clojure-refactoring.support core parsley]
+ [clojure-refactoring.support core]
[clojure.contrib.seq-utils :only [find-first]]
- [clojure.contrib.str-utils :only [str-join]]))
+ [clojure.contrib.str-utils :only [str-join]]
+ [clojure-refactoring.support.parsley :only [defparsed-fn]])
+ (:require [clojure-refactoring.support.parsley :as ast]))
(defn parsley-map-lookup? [ast]
- (let [content (relevant-content ast)]
- (and (tag= :list ast)
- (count= (filter parsley-keyword? content) 1)
+ (let [content (ast/relevant-content ast)]
+ (and (ast/tag= :list ast)
+ (count= (filter ast/parsley-keyword? content) 1)
(count= content 2))))
(defn parsley-key->sym [kw-node]
- (replace-content kw-node
+ (ast/replace-content kw-node
(list
(str-join ""
(drop 1 (first (:content kw-node)))))))
(defn parsley-find-lookups [node]
"Returns all the map lookups in a node as a set of parsley asts"
- (->> (parsley-sub-nodes node)
+ (->> (ast/parsley-sub-nodes node)
(filter parsley-map-lookup?)
set))
(defn- swap-first-with-last [ast]
- (let [[first-node last-node] (relevant-content ast)]
+ (let [[first-node last-node] (ast/relevant-content ast)]
(replace
{first-node last-node, last-node first-node}
(:content ast))))
(defn- parsley-swap-first-with-last [ast]
- (replace-content ast
+ (ast/replace-content ast
(swap-first-with-last ast)))
(defn parsley-lookup-to-canoninical-form [lookup-ast]
- (let [[maybe-keyword] (relevant-content lookup-ast)]
- (if (parsley-keyword? maybe-keyword)
+ (let [[maybe-keyword] (ast/relevant-content lookup-ast)]
+ (if (ast/parsley-keyword? maybe-keyword)
lookup-ast
(parsley-swap-first-with-last lookup-ast))))
(defn add-to-parsley-map [m key val]
"Adds key and value (which should be parsley nodes
to m, which represents a parsley map."
- (replace-content m
+ (ast/replace-content m
`("{"
~key
- ~parsley-whitespace
+ ~ast/parsley-whitespace
~val
- ~parsley-whitespace
+ ~ast/parsley-whitespace
~@(drop 1 (:content m)))))
(def relevant-content-from-canoninical-form
- (comp relevant-content parsley-lookup-to-canoninical-form))
+ (comp ast/relevant-content parsley-lookup-to-canoninical-form))
(defn- add-lookup-to-binding-map [binding-map lookup]
"Adds a lookup (a node of the form (:a a)) to a binding map."
(let [[key m] (relevant-content-from-canoninical-form lookup)]
(assoc binding-map m
(add-to-parsley-map
- (get binding-map m parsley-empty-map)
+ (get binding-map m ast/parsley-empty-map)
(parsley-key->sym key) key))))
(defn lookups-to-binding-map [lookups]
@@ -76,22 +78,22 @@
;;replacing stuff all over the place
(reduce
(fn [new-ast lookup]
- (parsley-tree-replace
+ (ast/parsley-tree-replace
lookup
- (parsley-key->sym (first (relevant-content (parsley-lookup-to-canoninical-form lookup))))
+ (parsley-key->sym (first (ast/relevant-content (parsley-lookup-to-canoninical-form lookup))))
new-ast))
ast
lookups))
(defn- add-destructured-maps-to-args [lookups root-ast]
- (let [args (parsley-fn-args root-ast)
+ (let [args (ast/parsley-fn-args root-ast)
new-args (destructured-binding-vec args lookups)]
- (parsley-tree-replace args new-args root-ast)))
+ (ast/parsley-tree-replace args new-args root-ast)))
(defparsed-fn destructure-map [root-ast]
"Destructures all calls to maps"
(let [lookups (parsley-find-lookups root-ast)]
- (parsley-to-string
+ (ast/parsley-to-string
(replace-lookups-with-destructured-symbols
lookups
(add-destructured-maps-to-args lookups root-ast)))))
@@ -1,19 +1,21 @@
(ns clojure-refactoring.extract-method
(:use [clojure-refactoring.support core formatter
- find-bindings-above-node parsley]
+ find-bindings-above-node]
+ [clojure-refactoring.support.parsley :only [defparsed-fn]]
clojure.set
- [clojure.contrib.seq-utils :only [find-first]]))
+ [clojure.contrib.seq-utils :only [find-first]])
+ (:require [clojure-refactoring.support.parsley :as ast]))
(defn- find-occurences [args node]
"Looks for any occurence of each element of args in the node"
- (seq (intersection (set args) (set (parsley-sub-nodes node)))))
+ (seq (intersection (set args) (set (ast/parsley-sub-nodes node)))))
(defn fn-name [fn-node]
- (second (relevant-content fn-node)))
+ (second (ast/relevant-content fn-node)))
(defn fn-call [fn-node]
"Uses the arguments of a function node to return a call to it."
- (parsley-list `(~(fn-name fn-node) ~@(parsley-bindings fn-node))))
+ (ast/parsley-list `(~(fn-name fn-node) ~@(ast/parsley-bindings fn-node))))
(defn- arg-occurences [f-node extracted-node]
"Finds the bindings from f-node that are also in the extracted node."
@@ -23,23 +25,23 @@
(defn- make-fn-node [name args body]
"Creates an ast representing the new function"
(format-ast
- (list-without-whitespace
- (ast-symbol 'defn)
+ (ast/list-without-whitespace
+ (ast/ast-symbol 'defn)
name
- (parsley-vector args)
- (strip-whitespace body))))
+ (ast/parsley-vector args)
+ (ast/strip-whitespace body))))
(defn call-extracted [body toplevel extracted]
- (parsley-tree-replace
+ (ast/parsley-tree-replace
body
(fn-call extracted)
toplevel))
(defn- nodes-to-string [extract-node fn-node new-fun]
"Formats the output for extract-method to print"
- (str (parsley-to-string new-fun)
+ (str (ast/parsley-to-string new-fun)
"\n\n"
- (parsley-to-string
+ (ast/parsley-to-string
(call-extracted extract-node fn-node new-fun))))
(defparsed-fn extract-method [function-node extract-node new-name]
@@ -1,54 +1,56 @@
(ns clojure-refactoring.local-binding
(:use clojure.walk
[clojure-refactoring.support core parsley formatter]
- [clojure.contrib.seq-utils :only (find-first)]))
+ [clojure.contrib.seq-utils :only (find-first)]
+ [clojure-refactoring.support.parsley :only [defparsed-fn]])
+ (:require [clojure-refactoring.support.parsley :as ast]))
(defn- get-function-definition [defn-ast]
- (find-first (tag= :list) (:content defn-ast)))
+ (find-first (ast/tag= :list) (:content defn-ast)))
(defn- add-to-binding [{content :content :as binding-node}
value var-name]
- (replace-content binding-node
+ (ast/replace-content binding-node
`(~(first content)
~@(butlast (drop 1 content))
- ~parsley-whitespace
+ ~ast/parsley-whitespace
~var-name
- ~parsley-whitespace
+ ~ast/parsley-whitespace
~value
~(last content))))
(defn is-node-the-binding-form [top-level ast]
- (= ast (parsley-fn-args top-level)))
+ (= ast (ast/parsley-fn-args top-level)))
(defn- modify-existing-let-block [form value var-name]
- (parsley-walk
+ (ast/parsley-walk
(fn [ast]
(if (is-node-the-binding-form form ast)
(add-to-binding ast value var-name)
ast))
form))
(defn let-wrap [form value var-name]
- (if (parsley-binding-node? form)
+ (if (ast/parsley-binding-node? form)
(modify-existing-let-block form value var-name)
- (list-without-whitespace
- (ast-symbol 'let)
- parsley-whitespace
- (parsley-vector
+ (ast/list-without-whitespace
+ (ast/ast-symbol 'let)
+ ast/parsley-whitespace
+ (ast/parsley-vector
[var-name value])
- parsley-whitespace
+ ast/parsley-whitespace
form)))
(defn- wrap-function-body-with-let [defn-form value var-name]
(let [fn-def (get-function-definition defn-form)]
- (parsley-tree-replace
+ (ast/parsley-tree-replace
fn-def (let-wrap fn-def value var-name)
defn-form)))
(defparsed-fn local-wrap [top value var-name]
"Extracts a value as a local variable inside top"
(-> (wrap-function-body-with-let
- (parsley-tree-replace
+ (ast/parsley-tree-replace
value var-name top)
value var-name)
- parsley-to-string))
+ ast/parsley-to-string))
@@ -1,29 +1,28 @@
(ns clojure-refactoring.rename
- (:use [clojure-refactoring.support replace parsley]
+ (:use [clojure-refactoring.support replace]
clojure.walk)
- (:require [clojure-refactoring.support.parser :as parser]))
+ (:require [clojure-refactoring.support.parser :as parser])
+ (:require [clojure-refactoring.support.parsley :as ast]))
(defn rename [node old-name new-name]
(let [ast (parser/parse node)
old (symbol old-name)
new (symbol new-name)]
- (parsley-to-string
- (replace-symbol-in-ast-node
+ (ast/parsley-to-string
+ (ast/replace-symbol-in-ast-node
old
new
ast))))
(defn renaming-fn [old-var new-sym]
"Returns a function for renaming nodes"
(fn [node]
- (replace-symbol-in-ast-node
+ (ast/replace-symbol-in-ast-node
(.sym old-var)
new-sym
node)))
(defn global-rename [ns old-name new-name]
"Sends a list of alists to emacs for processing as renames"
- #_{:pre [(do (find-ns ns)
- (ns-resolve ns old-name))]}
(let [old-var (ns-resolve ns old-name)]
(replace-callers old-var (renaming-fn old-var new-name))))
@@ -1,25 +1,24 @@
(ns clojure-refactoring.support.find-bindings-above-node
- (:use [clojure-refactoring.support core parsley]))
-
-
+ (:use [clojure-refactoring.support core])
+ (:require [clojure-refactoring.support.parsley :as ast]))
(defn extract-binding-syms [ast]
(if (#{"defmacro" "fn" "defn"}
(first (:content (second (:content ast)))))
- (relevant-content (first-vector ast))
- (evens (relevant-content (first-vector ast)))))
+ (ast/relevant-content (ast/first-vector ast))
+ (evens (ast/relevant-content (ast/first-vector ast)))))
(defn extract-symbols-from-binding-node [ast]
(->> (extract-binding-syms ast)
- parsley-sub-nodes
- (filter parsley-symbol?)))
+ ast/parsley-sub-nodes
+ (filter ast/parsley-symbol?)))
(defn binding-node-that-contains? [node expr]
- (and (parsley-binding-node? node)
- (parsley-tree-contains node expr)))
+ (and (ast/parsley-binding-node? node)
+ (ast/parsley-tree-contains node expr)))
(defn find-bindings-above-node [node expr]
- (->> (parsley-sub-nodes node)
+ (->> (ast/parsley-sub-nodes node)
(filter #(binding-node-that-contains? % expr))
(mapcat extract-symbols-from-binding-node)
distinct))
@@ -1,41 +1,42 @@
(ns clojure-refactoring.support.formatter
- (:use [clojure-refactoring.support parsley core]))
+ (:use [clojure-refactoring.support core])
+ (:require [clojure-refactoring.support.parsley :as ast]))
(defn replace-content-by [f {content :content :as ast}]
- (if (composite-tag? (:tag ast))
- (replace-content ast
+ (if (ast/composite-tag? (:tag ast))
+ (ast/replace-content ast
`(~(first content)
- ~@(f (drop-first-and-last content))
+ ~@(f (ast/drop-first-and-last content))
~(last content)))
ast))
(defn add-whitespace-to-lists [ast]
- (parsley-walk
- #(replace-content-by add-whitespace %)
+ (ast/parsley-walk
+ #(replace-content-by ast/add-whitespace %)
ast))
(defn not-replaced-in-threading? [node]
- (or (some #{(first-content node)} ["->>" "->"])
- (tag= :whitespace node)
+ (or (some #{(ast/first-content node)} ["->>" "->"])
+ (ast/tag= :whitespace node)
(string? node)))
(defn replace-this-when-threading [ast toplevel]
(not (or (not-replaced-in-threading? ast)
- (= ast (last (relevant-content toplevel))))))
+ (= ast (last (ast/relevant-content toplevel))))))
(def threading-spacing
- [parsley-newline parsley-whitespace parsley-whitespace])
+ [ast/parsley-newline ast/parsley-whitespace ast/parsley-whitespace])
(defn format-threaded [ast]
(let [with-whitespace
(add-whitespace-to-lists ast)]
- (replace-content
+ (ast/replace-content
with-whitespace
(after-each #(replace-this-when-threading % with-whitespace)
threading-spacing
(:content with-whitespace)))))
-(defmulti format-ast first-symbol)
+(defmulti format-ast ast/first-symbol)
(defmethod format-ast "->" [ast]
(format-threaded ast))
@@ -44,16 +45,16 @@
(format-threaded ast))
(defmethod format-ast "defn" [ast]
- (replace-content
+ (ast/replace-content
ast
- (after-each #(= % (parsley-fn-args ast))
+ (after-each #(= % (ast/parsley-fn-args ast))
(drop-last threading-spacing)
(:content (add-whitespace-to-lists ast)))))
(defn format-sub-nodes [ast]
- (replace-content-by #(add-whitespace (map format-ast %)) ast))
+ (replace-content-by #(ast/add-whitespace (map format-ast %)) ast))
(defmethod format-ast :default [ast]
- (if (some composite-tag? (relevant-content ast))
+ (if (some ast/composite-tag? (ast/relevant-content ast))
(format-sub-nodes ast)
(add-whitespace-to-lists ast)))
Oops, something went wrong.

0 comments on commit ee01cfc

Please sign in to comment.