1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +00:00

Add doc-of function to core for reverse documentation lookup.

This commit is contained in:
Calvin Rose 2021-01-11 20:30:24 -06:00
parent 9078d3bd37
commit dfbdd17dce
2 changed files with 42 additions and 21 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file.
## Unreleased - ???
- Add `doc-of` for reverse documentation lookup.
- Add `ev/gather` and `chan` argument to `ev/go`. This new argument allows "supervisor channels"
for fibers to enable structured concurrency.
- Make `-k` flag work on stdin if no files are given.

View File

@ -2018,20 +2018,8 @@
(print (doc-format (string "Dynamics:\n\n" (string/join dynamics " "))))
(print "\n Use (doc sym) for more information on a binding.\n"))
(defn doc*
"Get the documentation for a symbol in a given environment. Function form of doc."
[&opt sym]
(cond
(string? sym)
(print-index (fn [x] (string/find sym x)))
sym
(do
(def x (dyn sym))
(if (not x)
(print "symbol " sym " not found.")
(do
(defn- print-module-entry
[x]
(def bind-type
(string " "
(cond
@ -2047,7 +2035,26 @@
(string " " path " on line " line ", column " col "\n") "")
(if (or d sm) "\n" "")
(if d (doc-format d) " no documentation found.")
"\n\n"))))
"\n\n"))
(def module/cache
"Table mapping loaded module identifiers to their environments."
@{})
(defn doc*
"Get the documentation for a symbol in a given environment. Function form of doc."
[&opt sym]
(cond
(string? sym)
(print-index (fn [x] (string/find sym x)))
sym
(do
(def x (dyn sym))
(if (not x)
(print "symbol " sym " not found.")
(print-module-entry x)))
# else
(print-index identity)))
@ -2060,8 +2067,25 @@
[&opt sym]
~(,doc* ',sym))
(defn doc-of
`Searches all loaded modules in module/cache for a given binding and prints out its documentation.
This does a search by value instead of by name. Returns nil.`
[x]
(var found false)
(loop [module-set :in [[root-env] module/cache]
module :in module-set
value :in module]
(let [check (or (get value :ref) (get value :value))]
(when (= check x)
(print-module-entry value)
(set found true)
(break))))
(if-not found
(print "documentation for value " x " not found.")))
(undef env-walk)
(undef print-index)
(undef print-module-entry)
###
###
@ -2644,10 +2668,6 @@
(setdyn :syspath (boot/opts "JANET_PATH"))
(setdyn :headerpath (boot/opts "JANET_HEADERPATH"))
(def module/cache
"Table mapping loaded module identifiers to their environments."
@{})
(defn module/add-paths
```
Add paths to module/paths for a given loader such that