A Neovim plugin for cider jack-in for REPL driven development with Clojure
neovim
clojure
lua
1local vim = vim
2local M = {}
3
4M.config = {
5 -- clj dependencies
6 clj_dependencies = {
7 { name = "nrepl/nrepl", version = "RELEASE" },
8 { name = "cider/cider-nrepl", version = "RELEASE" },
9 },
10 -- clj middleware
11 clj_middleware = {
12 "cider.nrepl/cider-middleware",
13 },
14 -- leiningen plugins
15 lein_plugins = {
16 { name = "cider/cider-nrepl", version = "RELEASE" },
17 },
18 -- buffer, background, vsplit, split, tab
19 location = "buffer",
20 force_powershell = false,
21}
22
23local function is_powershell()
24 return vim.o.shell == "powershell.exe"
25 or vim.o.shell == "pwsh"
26 or vim.o.shell == "powershell"
27 or vim.o.shell == "pwsh.exe"
28 or M.config.force_powershell == true
29end
30
31-- windows needs to add quotes for powershell because well powershell...
32local function version_escape(version)
33 if is_powershell() == true then
34 return '""""""' .. version .. '""""""'
35 end
36 return '"' .. version .. '"'
37end
38
39local function map_clj_deps_to_string()
40 local string = ""
41 for _, v in pairs(M.config.clj_dependencies) do
42 string = string .. v.name .. " {:mvn/version " .. version_escape(v.version) .. "} "
43 end
44 return string
45end
46
47local function map_clj_middleware_to_string()
48 local string = ""
49 for _, v in pairs(M.config.clj_middleware) do
50 string = string .. '"' .. v .. '" '
51 end
52 return string
53end
54
55local function clj_string(args)
56 if args == nil then
57 args = ""
58 end
59
60 local cmd = "clj"
61 local deps = "'{:deps {" .. map_clj_deps_to_string() .. "}}' "
62 local cider = "-M -m nrepl.cmdline --interactive --middleware '[" .. map_clj_middleware_to_string() .. "]'"
63
64 return cmd .. " -Sdeps " .. deps .. args .. " " .. cider
65end
66
67local function map_lein_plugins_to_string()
68 local string = ""
69 for _, v in pairs(M.config.lein_plugins) do
70 string = string .. v.name .. " " .. version_escape(v.version) .. " "
71 end
72 return string
73end
74
75local function lein_string(args)
76 if args == nil then
77 args = ""
78 end
79
80 return "lein update-in :plugins conj '[" .. map_lein_plugins_to_string() .. "]' -- repl" .. args
81end
82
83local function jack_in(execution_string)
84 if M.config.location == "vsplit" then
85 vim.cmd("vsplit")
86 elseif M.config.location == "split" then
87 vim.cmd("split")
88 elseif M.config.location == "tab" then
89 vim.cmd("tabnew")
90 end
91
92 if M.config.force_powershell == true then
93 vim.cmd(":term powershell " .. execution_string)
94 else
95 vim.cmd(":term " .. execution_string)
96 end
97 if M.config.location == "background" then
98 -- swap to the previous buffer if available
99 vim.cmd("bp")
100 end
101end
102
103function M.setup(user_opts)
104 M.config = vim.tbl_extend("force", M.config, user_opts or {})
105
106 vim.api.nvim_create_user_command("Clj", function(opts)
107 jack_in(clj_string(opts.args))
108 end, { nargs = "*" })
109
110 vim.api.nvim_create_user_command("Lein", function(opts)
111 jack_in(lein_string(opts.args))
112 end, { nargs = "*" })
113end
114
115return M