Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Last active April 14, 2022 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivergeorge/600ea4630fc253cc02a0676f5d561829 to your computer and use it in GitHub Desktop.
Save olivergeorge/600ea4630fc253cc02a0676f5d561829 to your computer and use it in GitHub Desktop.
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