diff --git a/fnl/conjure-expand/main.fnl b/fnl/conjure-expand/main.fnl index 6ebdf77..5c9f6ce 100644 --- a/fnl/conjure-expand/main.fnl +++ b/fnl/conjure-expand/main.fnl @@ -1,31 +1,32 @@ -(module conjure-expand.main - {require {a aniseed.core - nvim aniseed.nvim - str aniseed.string - bridge conjure.bridge - client conjure.client - eval conjure.eval - extract conjure.extract - log conjure.log - mapping conjure.mapping - buffer conjure.buffer - editor conjure.editor}}) +(module conjure-expand.main) + +(local a (require "aniseed.core")) +(local nvim (require "aniseed.nvim")) +(local str (require "aniseed.string")) +(local bridge (require "conjure.bridge")) +(local client (require "conjure.client")) +(local eval (require "conjure.eval")) +(local extract (require "conjure.extract")) +(local log (require "conjure.log")) +(local mapping (require "conjure.mapping")) +(local buffer (require "conjure.buffer")) +(local editor (require "conjure.editor")) ;; Adapted from conjure.eval/current-form -(defn- current-form [] +(fn current-form [] (let [form (extract.form {})] (when form (let [{: content} form] content)))) -(defn- clj-client [f args] +(fn clj-client [f args] (client.with-filetype "clojure" f args)) -(defn- output-expanded [orig] +(fn output-expanded [orig] (fn [r] (log.append (a.concat [(.. "; " orig)] (str.split r "\n")) {:break? true}))) -(defn clj-macroexpand [expand-cmd] +(fn clj-macroexpand [expand-cmd] (let [form (current-form) me-form (.. "(" (or expand-cmd "clojure.walk/macroexpand-all") " '" form ")")] (clj-client eval.eval-str @@ -34,7 +35,7 @@ :passive? true :on-result (output-expanded me-form)}))) -(defn- dir-word [] +(fn dir-word [] (let [{: content : range : node} (extract.word)] (when (not (a.empty? content)) (let [dir-code (.. "(clojure.repl/dir " content ")")] @@ -44,13 +45,13 @@ :node node :origin :word}))))) -(defn- replace-with-expanded-form [] +(fn replace-with-expanded-form [] (let [form (extract.form {})] (when form (let [{: content : range} form buf (vim.api.nvim_win_get_buf 0) win (vim.api.nvim_tabpage_get_win 0) - me-form (.. "(" (or expand-cmd "clojure.walk/macroexpand-all") " '" content ")")] + me-form (.. "(clojure.walk/macroexpand-all '" content ")")] (clj-client eval.eval-str {:origin :conjure-macroreplace :code me-form @@ -65,8 +66,18 @@ (a.get-in range [:start 1]) (a.inc (a.get-in range [:start 2]))))}))))) +(fn tap-root [] + (let [{: content : range : node} (extract.form {"root?" true})] + (when (not (a.empty? content)) + (let [tap-code (.. "(tap> " content ")")] + (clj-client eval.eval-str + {:code tap-code + :range range + :node node + :origin :tap}))))) -(defn add-buf-mappings [] + +(fn add-buf-mappings [] (mapping.buf :CljMacroexpand "cm" #(clj-macroexpand) {:desc "Call macroexpand-all on the symbol under the cursor"}) (mapping.buf :CljMacroexpand0 "c0" #(clj-macroexpand "clojure.core/macroexpand") @@ -76,9 +87,11 @@ (mapping.buf :CljMacroexpandReplace "mr" #(replace-with-expanded-form) {:desc "Call macroexpand-1 on the symbol under the cursor then replace that src with the expansion"}) (mapping.buf :CljDirWord "dw" #(dir-word) - {:desc "Calls (clojure.repl/dir ,,,) for the namespace under the cursor"})) + {:desc "Calls (clojure.repl/dir ,,,) for the namespace under the cursor"}) + (mapping.buf :CljEvalTap "et" #(tap-root) + {:desc "Calls (tap> ,,,) for the root form under the cursor"})) -(defn init [] +(fn init [] (when (or (not nvim.g.conjure_macroexpand_disable_mappings) (= 0 nvim.g.conjure_macroexpand_disable_mappings)) (nvim.ex.autocmd diff --git a/lua/conjure-expand/main.lua b/lua/conjure-expand/main.lua index 9086272..cc9e6af 100644 --- a/lua/conjure-expand/main.lua +++ b/lua/conjure-expand/main.lua @@ -10,18 +10,17 @@ do _2amodule_2a["aniseed/locals"] = {} _2amodule_locals_2a = (_2amodule_2a)["aniseed/locals"] end -local a, bridge, buffer, client, editor, eval, extract, log, mapping, nvim, str = require("conjure-expand.aniseed.core"), require("conjure.bridge"), require("conjure.buffer"), require("conjure.client"), require("conjure.editor"), require("conjure.eval"), require("conjure.extract"), require("conjure.log"), require("conjure.mapping"), require("conjure-expand.aniseed.nvim"), require("conjure-expand.aniseed.string") -do end (_2amodule_locals_2a)["a"] = a -_2amodule_locals_2a["bridge"] = bridge -_2amodule_locals_2a["buffer"] = buffer -_2amodule_locals_2a["client"] = client -_2amodule_locals_2a["editor"] = editor -_2amodule_locals_2a["eval"] = eval -_2amodule_locals_2a["extract"] = extract -_2amodule_locals_2a["log"] = log -_2amodule_locals_2a["mapping"] = mapping -_2amodule_locals_2a["nvim"] = nvim -_2amodule_locals_2a["str"] = str +local a = require("conjure-expand.aniseed.core") +local nvim = require("conjure-expand.aniseed.nvim") +local str = require("conjure-expand.aniseed.string") +local bridge = require("conjure.bridge") +local client = require("conjure.client") +local eval = require("conjure.eval") +local extract = require("conjure.extract") +local log = require("conjure.log") +local mapping = require("conjure.mapping") +local buffer = require("conjure.buffer") +local editor = require("conjure.editor") local function current_form() local form = extract.form({}) if form then @@ -32,24 +31,20 @@ local function current_form() return nil end end -_2amodule_locals_2a["current-form"] = current_form local function clj_client(f, args) return client["with-filetype"]("clojure", f, args) end -_2amodule_locals_2a["clj-client"] = clj_client local function output_expanded(orig) local function _3_(r) return log.append(a.concat({("; " .. orig)}, str.split(r, "\n")), {["break?"] = true}) end return _3_ end -_2amodule_locals_2a["output-expanded"] = output_expanded local function clj_macroexpand(expand_cmd) local form = current_form() local me_form = ("(" .. (expand_cmd or "clojure.walk/macroexpand-all") .. " '" .. form .. ")") return clj_client(eval["eval-str"], {origin = "conjure-macroexpand", code = me_form, ["passive?"] = true, ["on-result"] = output_expanded(me_form)}) end -_2amodule_2a["clj-macroexpand"] = clj_macroexpand local function dir_word() local _let_4_ = extract.word() local content = _let_4_["content"] @@ -62,7 +57,6 @@ local function dir_word() return nil end end -_2amodule_locals_2a["dir-word"] = dir_word local function replace_with_expanded_form() local form = extract.form({}) if form then @@ -71,7 +65,7 @@ local function replace_with_expanded_form() local range = _let_6_["range"] local buf = vim.api.nvim_win_get_buf(0) local win = vim.api.nvim_tabpage_get_win(0) - local me_form = ("(" .. (__fnl_global__expand_2dcmd or "clojure.walk/macroexpand-all") .. " '" .. content .. ")") + local me_form = ("(clojure.walk/macroexpand-all '" .. content .. ")") local function _7_(result) buffer["replace-range"](buf, range, result) return editor["go-to"](win, a["get-in"](range, {"start", 1}), a.inc(a["get-in"](range, {"start", 2}))) @@ -81,30 +75,44 @@ local function replace_with_expanded_form() return nil end end -_2amodule_locals_2a["replace-with-expanded-form"] = replace_with_expanded_form +local function tap_root() + local _let_9_ = extract.form({["root?"] = true}) + local content = _let_9_["content"] + local range = _let_9_["range"] + local node = _let_9_["node"] + if not a["empty?"](content) then + local tap_code = ("(tap> " .. content .. ")") + return clj_client(eval["eval-str"], {code = tap_code, range = range, node = node, origin = "tap"}) + else + return nil + end +end local function add_buf_mappings() - local function _9_() + local function _11_() return clj_macroexpand() end - mapping.buf("CljMacroexpand", "cm", _9_, {desc = "Call macroexpand-all on the symbol under the cursor"}) - local function _10_() + mapping.buf("CljMacroexpand", "cm", _11_, {desc = "Call macroexpand-all on the symbol under the cursor"}) + local function _12_() return clj_macroexpand("clojure.core/macroexpand") end - mapping.buf("CljMacroexpand0", "c0", _10_, {desc = "Call macroexpand on the symbol under the cursor"}) - local function _11_() + mapping.buf("CljMacroexpand0", "c0", _12_, {desc = "Call macroexpand on the symbol under the cursor"}) + local function _13_() return clj_macroexpand("clojure.core/macroexpand-1") end - mapping.buf("CljMacroexpand1", "c1", _11_, {desc = "Call macroexpand-1 on the symbol under the cursor"}) - local function _12_() + mapping.buf("CljMacroexpand1", "c1", _13_, {desc = "Call macroexpand-1 on the symbol under the cursor"}) + local function _14_() return replace_with_expanded_form() end - mapping.buf("CljMacroexpandReplace", "mr", _12_, {desc = "Call macroexpand-1 on the symbol under the cursor then replace that src with the expansion"}) - local function _13_() + mapping.buf("CljMacroexpandReplace", "mr", _14_, {desc = "Call macroexpand-1 on the symbol under the cursor then replace that src with the expansion"}) + local function _15_() return dir_word() end - return mapping.buf("CljDirWord", "dw", _13_, {desc = "Calls (clojure.repl/dir ,,,) for the namespace under the cursor"}) + mapping.buf("CljDirWord", "dw", _15_, {desc = "Calls (clojure.repl/dir ,,,) for the namespace under the cursor"}) + local function _16_() + return tap_root() + end + return mapping.buf("CljEvalTap", "et", _16_, {desc = "Calls (tap> ,,,) for the root form under the cursor"}) end -_2amodule_2a["add-buf-mappings"] = add_buf_mappings local function init() if (not nvim.g.conjure_macroexpand_disable_mappings or (0 == nvim.g.conjure_macroexpand_disable_mappings)) then return nvim.ex.autocmd("FileType", "clojure", bridge["viml->lua"]("conjure-expand.main", "add-buf-mappings")) @@ -112,5 +120,4 @@ local function init() return nil end end -_2amodule_2a["init"] = init return _2amodule_2a \ No newline at end of file