Skip to content

Instantly share code, notes, and snippets.

@sogaiu
Last active November 16, 2025 02:18
Show Gist options
  • Select an option

  • Save sogaiu/fcbd43c48b38f130e7d0383a74aec44b to your computer and use it in GitHub Desktop.

Select an option

Save sogaiu/fcbd43c48b38f130e7d0383a74aec44b to your computer and use it in GitHub Desktop.
find-calls-to.janet demo 3
$ VISUAL=emacs FC_PRED="|(> (length $) 3)" janet find-calls-to.janet file/write ~/src/git-some-janets/repos 2>/dev/null
# emacs +20 codeberg.org/veqq/deforester/deforester.janet
(file/write f (time-string) " " data "\n")
# emacs +46 codeberg.org/veqq/deforester/deforester.janet
(file/write f data "," "\n")
# emacs +192 github.com/janet-lang/spork/spork/declare-cc.janet
(file/write out "};\n\n"
"const unsigned char * const " name "_embed = bytes;\n"
"const size_t " name "_embed_size = sizeof(bytes);\n")
# emacs +150 github.com/janet-lang/jpm/jpm/cc.janet
(file/write out
"#include <janet.h>\n"
"static const unsigned char bytes[] = {"
(string/join (interpose ", " chunks))
"};\n\n"
"const unsigned char *" name "_embed = bytes;\n"
"size_t " name "_embed_size = sizeof(bytes);\n")
# emacs +54 github.com/Holmqvist1990/choosing-a-scripting-language/janet/pcm_db.janet
(file/write (json/encode db)
(file/flush)
(file/close))
# emacs +192 github.com/pyrmont/markable/bundle/spork/declare-cc.janet
(file/write out "};\n\n"
"const unsigned char * const " name "_embed = bytes;\n"
"const size_t " name "_embed_size = sizeof(bytes);\n")
# emacs +192 github.com/pyrmont/predoc/bundle/spork/declare-cc.janet
(file/write out "};\n\n"
"const unsigned char * const " name "_embed = bytes;\n"
"const size_t " name "_embed_size = sizeof(bytes);\n")
# emacs +192 github.com/pyrmont/jeep/deps/spork/spork/declare-cc.janet
(file/write out "};\n\n"
"const unsigned char * const " name "_embed = bytes;\n"
"const size_t " name "_embed_size = sizeof(bytes);\n")
# files pre-searched: 5202
# files searched: 107
# processing time: 1.23 seconds
(import ./location :as l)
(import ./zipper :as j)
(import ./loc-jipper :as j)
(import ./common :as c)
########################################################################
(defn find-calls-to
[name src &opt opts]
(default opts {:pred identity})
(def {:pred pred} opts)
(def tree (l/par src))
(var cur-zloc (j/zip-down tree))
(def results @[])
#
(while (def next-zloc
(j/search-from cur-zloc
|(match (j/node $)
[:symbol _ (@ name)]
$)))
(def parent-zloc (j/up next-zloc))
(when (= :tuple (get (j/node parent-zloc) 0))
(def node (j/node parent-zloc))
(def raw-code-str (l/gen node))
(def parsed
(try
(parse raw-code-str)
([e]
(eprintf "failed to parse: %s" raw-code-str))))
(when (and parsed (pred parsed))
# ensure the first non-trivial element of the tuple is `name`
(when (= name (string (first parsed)))
(def {:bc bc :bl bl} (get node 1))
(def leading-ws (string/repeat " " (dec bc)))
(def code-str (string leading-ws raw-code-str))
(array/push results [bl code-str]))))
#
(set cur-zloc (j/df-next next-zloc)))
#
results)
(comment
(find-calls-to
"pp"
``
(defn hello
[x]
(pp x)
(print "hi")
(if true
(pp [:x x])
(print "oh no")))
``)
# =>
@[[3 " (pp x)"]
[6 " (pp [:x x])"]]
)
########################################################################
(defn main
[_ & args]
(def {:name name
:includes includes
:pred pred
:editor editor} (c/determine-inputs args {:name true}))
# find .janet files
(def src-filepaths
(c/collect-paths includes |(string/has-suffix? ".janet" $)))
# search the paths
(c/search-and-report find-calls-to name src-filepaths
{:pred pred :editor editor}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment