This repository has been archived by the owner on May 10, 2023. It is now read-only.
/
self_hosted.cljs
68 lines (59 loc) · 2.12 KB
/
self_hosted.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns proto-repl.self-hosted
"Provides functions for implementing a self hosted repl."
(:require [cljs.reader :as r]
[clojure.string :as str]
[cljs.nodejs :as nodejs]
[prc]
[replumb.core :as replumb]
[replumb.nodejs.io :as node-io]))
(nodejs/enable-util-print!)
; Register a default tag parser that just ignores parsing tags we don't recognize.
(r/register-default-tag-parser!
(fn [tag data]
data))
(def path-module
(delay (nodejs/require "path")))
(def atom-project-path
(delay
(let [project-path (first (js->clj (js/atom.packages.getPackageDirPaths)))
separator (.-sep (deref path-module))
path (deref path-module)]
(str/join separator [project-path "proto-repl" "lib" "proto_repl"]))))
(defn eval-str
"Evaluates the clojure code using replumb and invokes the callback."
[code callback]
(replumb/read-eval-call
;; TODO provide source paths. eval-str should take the source paths.
(replumb/nodejs-options [@atom-project-path] node-io/read-file!)
(fn [res]
(callback res))
code))
(defn ^:export eval-for-js
"Evaluates the clojure code for the javascript side. Converts the result to Javascript."
[code callback]
(eval-str code (fn [result] (callback (clj->js result)))))
(defn ^:export completions
"Provides completions for self hosted code."
[text callback-fn]
(eval-str
(str "(apropos \"" text "\" )")
(fn [result]
(if (:success? result)
(->> (r/read-string (:value result))
(mapv (fn [match]
(let [match (name match)]
{:candidate (if (str/starts-with? match "cljs.core/")
(str/replace match "cljs.core/" "")
match)
;; TODO support documentation with completion.
:docs ""
:type "var"})))
clj->js
callback-fn)
(callback-fn (clj->js result))))))
(defn -main [& args])
(set! *main-cli-fn* -main)
(set! (.-exports js/module)
#js
{:eval_str eval-for-js
:completions completions})