aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LICENSE21
-rw-r--r--README.md110
-rw-r--r--lua/nvim-jack-in.lua115
3 files changed, 246 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..72c8105
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 Trey Bastian
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..ee40b33
--- /dev/null
+++ b/README.md
@@ -0,0 +1,110 @@
+# nvim-jack-in
+
+---
+Jack in to CLJ & Leiningen REPLs from Neovim. Inspired by [vim-jack-in](https://github.com/clojure-vim/vim-jack-in)
+
+## Installation
+
+### Lazy
+
+```lua
+{'TreyBastian/nvim-jack-in', config = true}
+```
+
+#### Example with options
+
+```lua
+{
+ 'TreyBastian/nvim-jack-in',
+ opts = {
+ location = 'vsplit',
+ force_powershell = true,
+ },
+ config = true,
+}
+```
+
+### Options
+
+#### `clj_dependencies`
+
+Dependencies that should be injected into clj
+
+##### Default
+
+```lua
+clj_dependencies = {
+ { name = "nrepl/nrepl", version = "RELEASE" },
+ { name = "cider/cider-nrepl", version = "RELEASE" },
+}
+```
+
+#### `clj_middleware`
+
+Middleware that should be injected into clj
+
+##### Default
+
+```lua
+clj_middleware = {
+ "cider.nrepl/cider-middleware"
+}
+```
+
+#### `lein_plugins`
+
+Plugins that should be injected into leiningen
+
+##### Default
+
+```lua
+lein_plugins = {
+ { name = "cider/cider-nrepl", version = "RELEASE" },
+}
+```
+
+#### `location`
+
+Location where the REPL should open
+
+##### Default
+
+```lua
+location = 'buffer'
+```
+
+##### Choices
+
+`buffer` - opens REPL in a new buffer with focus
+`background` - opens REPL in a new buffer but you stay focused on current buffer
+`split` - opens REPL in new split
+`vsplit` - opens REPL in new vertical split
+`tab` - opens REPL in new tab
+
+#### `force_powershell`
+
+Force the usage of powershell. In windows setting `vim.o.shell = powershell` might sometimes not be optimal. Clj by default on windows is installed as a powershell module. This forces the plugin to use powershell.
+
+##### Default
+
+```lua
+force_powershell = false
+```
+
+## Usage
+
+This plugin automatically registers 2 commands `:Clj`and `:Lein` to start their respective REPLs
+
+You can easily map these commands to a key.
+
+```lua
+vim.keymap.set("n", "<leader>rc", "<CMD>:Clj<CR>")
+vim.keymap.net("n", "<leader>rl", "<CMD>:Lein<CR>")
+```
+
+You can supply additional arguments to the command. For example to use [Kit-Clj](https://kit-clj.github.io) development mode you can do.
+`:Clj -A:dev`
+
+## Contributing
+
+This project is open source, not just public source. If you wish to contribute start with an issue.
diff --git a/lua/nvim-jack-in.lua b/lua/nvim-jack-in.lua
new file mode 100644
index 0000000..7610aa4
--- /dev/null
+++ b/lua/nvim-jack-in.lua
@@ -0,0 +1,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