Skip to content

Commit

Permalink
Fix issue Issue 441: Namespace metadata breaks switch-to-namespace an…
Browse files Browse the repository at this point in the history
…d top-of-editor namespace label
  • Loading branch information
laurentpetit committed Oct 4, 2012
1 parent 936fb1b commit 49bc736
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/paredit/parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,40 @@
(lr+/match #{")" "]" "}" eof} s eof?))

(def gspaces #{:whitespace :comment :discard})
(def only-code (partial remove (comp gspaces :tag)))
(defn code-children [e] (only-code (:content e)))
(defn sym-name
"returns the symbol name" [e] (and (#{:symbol} (:tag e)) (apply str (:content e))))
(defn call-of [e c] (and (#{"("} (nth (code-children e) 0)) (#{c} (sym-name (nth (code-children e) 1))) e))
(defn call-args [e] (-> (code-children e) nnext butlast))
(defn form
"removes the meta(s) to get to the form"
(def only-code (partial remove (comp (conj gspaces :meta-prefix) :tag)))

(defn code-children
[e]
(only-code (:content e)))

(defn remove-meta
"removes the meta(s) to get to the form"
[e]
(if-not (#{:meta} (:tag e))
(if (not= :meta (:tag e))
e
(recur (nth (code-children e) 2))))
(recur (first (code-children e)))))

(defn ^:deprecated form
"DEPRECATED - use remove-meta instead - removes the meta(s) to get to the form"
[e] (remove-meta e))

(defn sym-name
"returns the symbol name"
[e]
(let [e (remove-meta e)]
(and (#{:symbol} (:tag e)) (apply str (:content e)))))

(defn call-of [e c]
(let [e (remove-meta e)]
(and
(#{"("} (nth (code-children e) 0))
(#{c} (sym-name (nth (code-children e) 1)))
e)))

(defn call-args
[e]
(-> (code-children e) nnext butlast))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
13 changes: 13 additions & 0 deletions src/paredit/static_analysis.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns paredit.static-analysis
(:require [paredit.parser :as p]))

(defn find-namespace [tree]
(p/sym-name
(p/remove-meta
(first
(p/call-args
(some
#(or
(p/call-of % "ns")
(p/call-of % "in-ns"))
(p/code-children tree)))))))
34 changes: 34 additions & 0 deletions test/paredit/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(:require [clojure.string :as str])
(:use [clojure.core.incubator :only [-?>]])
(:require [clojure.zip :as zip])
(:require [paredit.static-analysis :as st])
(:use paredit.loc-utils))

(def *spy?* (atom false))
Expand All @@ -24,6 +25,23 @@
([expr] (spy* "" expr))
([msg expr] (spy* msg expr)))

(defn tree
"creates parse-tree"
[text]
(-> text
paredit.parser/parse
(paredit.loc-utils/parsed-root-loc true)
(clojure.zip/node)))

(defn clean-tree
"more human readable parse tree"
[tree]
(cond
(string? tree) tree
:else (-> tree
(dissoc :build-id :count :content-cumulative-count :abstract-node :broken?)
(update-in [:content] #(map clean-tree %)))))

(defn text-spec-to-text
"Converts a text spec to text map"
[^String text-spec]
Expand Down Expand Up @@ -221,6 +239,21 @@
)
)

(deftest static-analysis-tests
(are [text]
(= "foo" (-?> text tree (st/find-namespace)))
"(ns foo)"
";some comment\n(ns foo)"
"#!nasty comment\n(ns foo)"
" \"some string\"(ns foo) "
"(ns ^:foo foo)"
"^:foo (ns foo)"
"(ns ^{:author \"Laurent Petit\"} foo)"
"^:foo (ns ^:foo foo)"
"^:foo (ns ^:foo ^{:author \"Laurent Petit\"} foo)"
"^{:a :b};sdf\n^:bar ^:foo (ns ^:foo ^{:author ;comment\n\"Laurent Petit\"}\n;foo\n#_bleh foo)"
))

(defn pts []
#_(normalized-selection-tests)
(t/line-stop-tests)
Expand All @@ -229,6 +262,7 @@
(parser-tests)
(parsetree-tests)
(unescape-string-content-tests)
(static-analysis-tests)
;;;;;;;#_(loc-for-offset-tests)
#_(leave-for-offset-tests)
#_(loc-containing-offset-tests))
Expand Down

0 comments on commit 49bc736

Please sign in to comment.