aboutsummaryrefslogtreecommitdiff
path: root/lua/nvim-jack-in.lua
blob: 7610aa458704be8433b92146ab6d54763bf81e3c (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
local vim = vim
local M = {}

M.config = {
	-- clj dependencies
	clj_dependencies = {
		{ name = "nrepl/nrepl", version = "RELEASE" },
		{ name = "cider/cider-nrepl", version = "RELEASE" },
	},
	-- clj middleware
	clj_middleware = {
		"cider.nrepl/cider-middleware",
	},
	-- leiningen plugins
	lein_plugins = {
		{ name = "cider/cider-nrepl", version = "RELEASE" },
	},
	-- buffer, background, vsplit, split, tab
	location = "buffer",
	force_powershell = false,
}

local function is_powershell()
	return vim.o.shell == "powershell.exe"
		or vim.o.shell == "pwsh"
		or vim.o.shell == "powershell"
		or vim.o.shell == "pwsh.exe"
		or M.config.force_powershell == true
end

-- windows needs to add quotes for powershell because well powershell...
local function version_escape(version)
	if is_powershell() == true then
		return '""""""' .. version .. '""""""'
	end
	return '"' .. version .. '"'
end

local function map_clj_deps_to_string()
	local string = ""
	for _, v in pairs(M.config.clj_dependencies) do
		string = string .. v.name .. " {:mvn/version " .. version_escape(v.version) .. "} "
	end
	return string
end

local function map_clj_middleware_to_string()
	local string = ""
	for _, v in pairs(M.config.clj_middleware) do
		string = string .. '"' .. v .. '" '
	end
	return string
end

local function clj_string(args)
	if args == nil then
		args = ""
	end

	local cmd = "clj"
	local deps = "'{:deps {" .. map_clj_deps_to_string() .. "}}' "
	local cider = "-M -m nrepl.cmdline --interactive --middleware '[" .. map_clj_middleware_to_string() .. "]'"

	return cmd .. " -Sdeps " .. deps .. args .. " " .. cider
end

local function map_lein_plugins_to_string()
	local string = ""
	for _, v in pairs(M.config.lein_plugins) do
		string = string .. v.name .. " " .. version_escape(v.version) .. " "
	end
	return string
end

local function lein_string(args)
	if args == nil then
		args = ""
	end

	return "lein update-in :plugins conj '[" .. map_lein_plugins_to_string() .. "]' -- repl" .. args
end

local function jack_in(execution_string)
	if M.config.location == "vsplit" then
		vim.cmd("vsplit")
	elseif M.config.location == "split" then
		vim.cmd("split")
	elseif M.config.location == "tab" then
		vim.cmd("tabnew")
	end

	if M.config.force_powershell == true then
		vim.cmd(":term powershell " .. execution_string)
	else
		vim.cmd(":term " .. execution_string)
	end
	if M.config.location == "background" then
		-- swap to the previous buffer if available
		vim.cmd("bp")
	end
end

function M.setup(user_opts)
	M.config = vim.tbl_extend("force", M.config, user_opts or {})

	vim.api.nvim_create_user_command("Clj", function(opts)
		jack_in(clj_string(opts.args))
	end, { nargs = "*" })

	vim.api.nvim_create_user_command("Lein", function(opts)
		jack_in(lein_string(opts.args))
	end, { nargs = "*" })
end

return M