~technomancy/fennel

210178f0223276f3ab1faf7b5dc0d3dbac729942 — Phil Hagelberg 2 months ago 1625c31 fn-source
Save off source from functions into metadata.
2 files changed, 13 insertions(+), 3 deletions(-)

M src/fennel/specials.fnl
M src/fennel/view.fnl
M src/fennel/specials.fnl => src/fennel/specials.fnl +6 -2
@@ 181,7 181,7 @@ Main purpose to print function argument list in docstring."
            (: :gsub "\"" "\\\""))
        (tostring x))))

(fn set-fn-metadata [arg-list docstring parent fn-name]
(fn set-fn-metadata [arg-list docstring parent fn-name ast]
  (when utils.root.options.useMetadata
    (let [args (utils.map arg-list #(: "\"%s\"" :format (deep-tostring $)))
          meta-fields ["\"fnl/arglist\"" (.. "{" (table.concat args ", ") "}")]]


@@ 194,6 194,10 @@ Main purpose to print function argument list in docstring."
                                          (: :gsub "\n" "\\n")
                                          (: :gsub "\"" "\\\""))
                                      "\"")))
      (when true ;utils.root.options.useSourceMetadata
        (table.insert meta-fields "\"fnl/source\"")
        (let [source-str (string.gsub (view (view ast)) "\n" "\\n")]
          (table.insert meta-fields source-str)))
      (let [meta-str (: "require(\"%s\").metadata" :format
                        (or utils.root.options.moduleName :fennel))]
        (compiler.emit parent


@@ 221,7 225,7 @@ Main purpose to print function argument list in docstring."
                 ast)
  (compiler.emit parent f-chunk ast)
  (compiler.emit parent :end ast)
  (set-fn-metadata arg-list docstring parent fn-name)
  (set-fn-metadata arg-list docstring parent fn-name ast)
  (utils.hook :fn ast f-scope)
  (utils.expr fn-name :sym))


M src/fennel/view.fnl => src/fennel/view.fnl +7 -1
@@ 222,6 222,10 @@ as numeric escapes rather than letter-based escapes, which is ugly."
      (tset defaults k v))
    defaults))

(fn pp-function [f options]
  (match (pcall require (or options.fennel-module-name :fennel))
    (true fennel) (fennel.metadata:get f "fnl/source")))

(set pp (fn [x options indent colon?]
          ;; main serialization loop, entry point is defined below
          (let [indent (or indent 0)


@@ 242,7 246,9 @@ as numeric escapes rather than letter-based escapes, which is ugly."
                (pp-string x options indent)
                (or (= tv :boolean) (= tv :nil))
                (tostring x)
                (.. "#<" (tostring x) ">")))))
                (or (and (= tv :function)
                         (pp-function x options))
                    (.. "#<" (tostring x) ">"))))))

(fn view [x options]
  "Return a string representation of x.