Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
most used keywords in codebase
(ns cnd.analyse-code
"Use rewrite-clj zipper to walk code.
Niggles
* Doesn't use the (ns...) aliases to resolve aliases to full paths.
"
(:require [clojure.java.io :as io]
[clojure.zip :as zip]
[rewrite-clj.zip :as z]))
(defn find-all
[zloc pred]
(let [*ks (atom nil)]
(-> (zip/up zloc)
(z/postwalk
(fn select [zloc]
(let [expr (z/sexpr zloc)]
(when (pred expr)
(swap! *ks conj expr)))
nil)))
@*ks))
(defn file? [f] (.isFile f))
(defn get-path [f] (.getPath f))
(defn all-source-files
[dir-path]
(->> (io/file dir-path)
(file-seq)
(filter file?)
(map get-path)))
(comment (all-source-files "src"))
(defn all-keywords
[file-paths]
(mapcat (fn [file-path]
(try
(find-all (z/of-file file-path) qualified-keyword?)
(catch Exception e
;(println file-path)
;(println e)
nil)))
file-paths))
(defn most-used-keywords
[dir-path]
(->> (all-source-files dir-path)
all-keywords
(frequencies)
(sort-by val)
(reverse)
(take 20)))
(comment (most-used-keywords "src/cljs"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment