|
| 1 | +(ns neovim-client.parser |
| 2 | + "Turn Neovim's api-info schema into Clojure functions." |
| 3 | + (:require [clojure.string :as string])) |
| 4 | + |
| 5 | +(defn- name-parts |
| 6 | + [api-op-spec] |
| 7 | + (-> api-op-spec |
| 8 | + (get "name") |
| 9 | + (string/replace-first "nvim_" "") |
| 10 | + (string/replace-first "vim_" "") |
| 11 | + (string/split #"_"))) |
| 12 | + |
| 13 | +(defn- fn-name |
| 14 | + [api-op-spec] |
| 15 | + (->> api-op-spec |
| 16 | + name-parts |
| 17 | + rest |
| 18 | + (string/join "-") |
| 19 | + symbol)) |
| 20 | + |
| 21 | +;; TODO - factor? |
| 22 | +(defn- fn-ns |
| 23 | + [api-op-spec] |
| 24 | + (->> api-op-spec |
| 25 | + name-parts |
| 26 | + first |
| 27 | + (str "neovim-client.nvim.") |
| 28 | + symbol)) |
| 29 | + |
| 30 | +(defn- params |
| 31 | + [api-op-spec] |
| 32 | + (mapv (comp symbol second) (get api-op-spec "parameters"))) |
| 33 | + |
| 34 | +(defn api-op-spec->fn-spec |
| 35 | + [api-op-spec] |
| 36 | + {:ns (fn-ns api-op-spec) |
| 37 | + :op (get api-op-spec "name") |
| 38 | + :params (params api-op-spec) |
| 39 | + :name (fn-name api-op-spec)}) |
| 40 | + |
| 41 | +(defn fn-spec->fn |
| 42 | + [{:keys [params op]}] |
| 43 | + `(fn [~@params] |
| 44 | + (neovim-client.nvim/exec ~op ~@params))) |
| 45 | + |
| 46 | +;; TODO - will f# cloud docstring? |
| 47 | +(defn fn-spec->fn-async |
| 48 | + [{:keys [params op]}] |
| 49 | + `(fn [~@params f#] |
| 50 | + (neovim-client.nvim/exec-async ~op ~@params f#))) |
| 51 | + |
| 52 | +;; TODO - is quoting pattern here a smell? |
| 53 | +(defn fn-spec->intern |
| 54 | + [{:keys [name ns params] :as fn-spec}] |
| 55 | + `(do (intern '~ns '~name ~(fn-spec->fn fn-spec)) |
| 56 | + (intern '~ns '~(symbol (str name "-async")) |
| 57 | + ~(fn-spec->fn-async fn-spec)))) |
| 58 | + |
| 59 | +(def test-op-spec1 '{"since" 1, |
| 60 | + "method" true, |
| 61 | + "name" "nvim_win_get_number", |
| 62 | + "return_type" "Integer", |
| 63 | + "parameters" (("Window" "window"))}) |
| 64 | + |
| 65 | +#_(eval (ns neovim-client.nvim.win)) |
| 66 | +#_(eval (fn-spec->intern (api-op-spec->fn-spec test-op-spec1))) |
0 commit comments