HS: sourceInfo API (sourceFor / lineFor / node-get)

This commit is contained in:
2026-04-25 19:10:57 +00:00
parent 9c8da50003
commit cc2a296306
2 changed files with 108 additions and 0 deletions

View File

@@ -2525,3 +2525,57 @@
(fn
(fn-name args)
(let ((fn (host-global fn-name))) (if fn (host-call-fn fn args) nil))))
;; ── SourceInfo API ────────────────────────────────────────────────
(define
hs-source-for
(fn
(node)
(substring (get node :src) (get node :start) (get node :end))))
(define
hs-line-for
(fn
(node)
(let
((lines (split (get node :src) "\n"))
(line-idx (- (get node :line) 1)))
(if (< line-idx (len lines)) (nth lines line-idx) ""))))
(define
hs-node-get
(fn
(node key)
(get (get node :fields) key)))
(define
hs-src
(fn (src-str)
(hs-source-for (hs-parse-ast src-str))))
(define
hs-src-at
(fn
(src-str path)
(define
walk
(fn
(node keys)
(if (or (nil? keys) (= (len keys) 0))
node
(walk (hs-node-get node (first keys)) (rest keys)))))
(hs-source-for (walk (hs-parse-ast src-str) path))))
(define
hs-line-at
(fn
(src-str path)
(define
walk
(fn
(node keys)
(if (or (nil? keys) (= (len keys) 0))
node
(walk (hs-node-get node (first keys)) (rest keys)))))
(hs-line-for (walk (hs-parse-ast src-str) path))))