Permalink
Browse files

Added zip namespace and use zip in find-bindings-above-node.

  • Loading branch information...
1 parent f2bc3ae commit 47b13099d00f1867831170fb1bb830edc6b059a0 @tcrayford tcrayford committed Sep 13, 2010
@@ -152,7 +152,8 @@
(new-name (read-from-minibuffer "New name: ")))
(beginning-of-defun)
(mark-sexp)
- (let ((body (buffer-substring-no-properties (mark t) (point))))
+ (let ((body (escape-string-literals
+ (buffer-substring-no-properties (mark t) (point)))))
(forward-kill-sexp)
(clojure-refactoring-insert-sexp
(clojure-refactoring-call-with-string-args
@@ -0,0 +1,17 @@
+(ns clojure-refactoring.ast.zip
+ (:require [clojure.zip :as zip]))
+
+(clojure.contrib.def/defalias ast-zip zip/xml-zip)
+
+(defn find-node [zipped-ast node]
+ "Given a zipped ast, returns the loc of the first occurrence of node
+ by a depth-first walk."
+ (loop [z zipped-ast]
+ (cond (zip/end? z) nil
+ (= (zip/node z) node) z
+ :else (recur (zip/next z)))))
+
+(defn nodes-leading-to [ast expr]
+ "Returns a seq of all nodes inside node leading to expr."
+ (-> (find-node (ast-zip ast) expr)
+ zip/path))
@@ -1,5 +1,6 @@
(ns clojure-refactoring.support.find-bindings-above-node
- (:use [clojure-refactoring.support core])
+ (:use [clojure-refactoring.support core]
+ clojure-refactoring.ast.zip)
(:require [clojure-refactoring.ast :as ast]))
(defn extract-binding-syms [ast]
@@ -13,12 +14,7 @@
ast/sub-nodes
(filter ast/symbol?)))
-(defn binding-node-that-contains? [node expr]
- (and (ast/binding-node? node)
- (ast/tree-contains node expr)))
-
(defn find-bindings-above-node [node expr]
- (->> (ast/sub-nodes node)
- (filter #(binding-node-that-contains? % expr))
- (mapcat extract-symbols-from-binding-node)
- distinct))
+ (->> (nodes-leading-to node expr)
+ (filter ast/binding-node?)
+ (mapcat extract-symbols-from-binding-node)))
@@ -0,0 +1,16 @@
+(ns clojure-refactoring.ast.zip-test
+ (:use clojure-refactoring.ast.zip :reload)
+ (:require [clojure.zip :as zip])
+ (:require [clojure-refactoring.support.parser :as parser])
+ (:require [clojure-refactoring.ast :as ast])
+ (:use clojure.test))
+
+(use-fixtures :once #(time %))
+
+(deftest find-node-finds-the-first-instance-of-a-node-in-a-zipper
+ (is (=
+ (-> (ast-zip
+ (parser/parse1 "(+ 1 2)"))
+ (find-node (parser/parse1 "+"))
+ zip/node)
+ (parser/parse1 "+"))))

0 comments on commit 47b1309

Please sign in to comment.