diff --git a/README.md b/README.md index 872d2fe..963b528 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # runner.nvim -A customizable Neovim plugin to run code inside the editor +A customizable Neovim plugin to run code inside the editor ## Demo -![](./demo/demo.gif) + +![](./demo/demo.gif) ## Table of Contents @@ -53,11 +54,24 @@ A customizable Neovim plugin to run code inside the editor ## Usage +### Commands + +| Command | lua | Description | +|-------------------|------------------------------------|------------------------------------------| +| `:Runner` | `require('runner').run()` | Runs code in buffer | +| `:AutoRunner` | `require('runner').autorun()` | Runs code in buffer on every file save | +| `:AutoRunnerStop` | `require('runner').autorun_stop()` | Stops `AutoRunner` and closes the window | + +### Lua + ```lua require('runner').run() + require('runner').autorun() + require('runner').autorun_stop() -- Can also be called with the buffer number where the code is: -- require('runner').run( ) + -- require('runner').autorun( ) -- To set a mapping vim.keymap.set('n', '', require('runner').run) @@ -65,9 +79,9 @@ A customizable Neovim plugin to run code inside the editor ## Configuration - #### `setup(options)` +#### `setup(options)` - Using this setup function is **optional**. Runner comes with the following defaults: + Runner comes with the following defaults: ```lua require('runner').setup({ @@ -93,7 +107,7 @@ A customizable Neovim plugin to run code inside the editor For using multiple handlers on the same filetype, see the [choice helper](#choicehandlers). - #### `set_handler(filetype, handler)` +#### `set_handler(filetype, handler)` This function **overwrites** the handler set for the specified filetype. Default handlers can be found [here](./lua/runner/handlers/init.lua). @@ -122,13 +136,13 @@ A customizable Neovim plugin to run code inside the editor Here is a description of each one: - - #### `shell_handler(command, editable)` +- #### `shell_handler(command, editable)` Runs a command in a shell by opening it in a new split window, with a terminal buffer. The split window's position will be determined by the `position` value from the config. It will be overwritten when using the telescope mapping to open horizontally or vertically. - - `select_horizontal` (defaults to ``): Opens the window at the bottom of the screen. - - `select_vertical` (defaults to ``): Opens the window at the right of the screen. + - `select_horizontal` (defaults to ``): Opens the window at the bottom of the screen. + - `select_vertical` (defaults to ``): Opens the window at the right of the screen.

@@ -136,7 +150,7 @@ A customizable Neovim plugin to run code inside the editor |---------------- | --------------- | --------------- | | `command` | The shell command to run when the handler is called | `string` | | `editable`| Whether the user should be prompted to edit the command using `vim.input()` before running it. Useful when giving command line arguments to a script | `boolean` *(optional, defaults to false)* | - + Example: ```lua @@ -144,14 +158,14 @@ A customizable Neovim plugin to run code inside the editor require('runner').set_handler('rust', shell_handler('cargo run', true)) ``` - - #### `command_handler(command)` - +- #### `command_handler(command)` + Runs a command in the Vim command mode. | Argument name | Description | Type | |---------------- | --------------- | --------------- | | `command` | The Vim command to run when the handler is called | `string` | - + Example: ```lua @@ -159,14 +173,14 @@ A customizable Neovim plugin to run code inside the editor require('runner').set_handler('lua', command_handler('luafile %')) ``` - - #### `choice(handlers)` - +- #### `choice(handlers)` + Opens a `Telescope` finder to allow the user to choose which handler to run. - + | Argument name | Description | Type | |---------------- | --------------- | --------------- | | `handlers` | The list of handlers to choose from | `table` where the keys are the name of the handlers in the `telescope` finder, and where the values are the actual handlers | - + Example: ```lua @@ -177,6 +191,7 @@ A customizable Neovim plugin to run code inside the editor ['Custom'] = helpers.shell_handler('cargo ', true), })) ``` + ## Advanced handlers configurations For creating dynamic handlers like one for each `npm` or `cargo` script, you can write your own handler function that generates the other handlers, gives them to the choice handler, and runs it itself. @@ -187,22 +202,26 @@ A customizable Neovim plugin to run code inside the editor This project uses [StyLua](https://github.com/JohnnyMorganz/StyLua) for enforcing code style, and has a [pre-commit](https://pre-commit.com/) hook setup for running it automatically. `runner.nvim` also has a Github Action that runs the linter on every Pull request. If a check doesn't pass on a specific Pull request, please lint the code and commit it again. For running them locally, you have to have them installed on your system: - - [StyLua Installation](https://github.com/JohnnyMorganz/StyLua#installation) - - [pre-commit Installation](https://pre-commit.com/#install) - #### Some useful commands: +- [StyLua Installation](https://github.com/JohnnyMorganz/StyLua#installation) +- [pre-commit Installation](https://pre-commit.com/#install) + +#### Some useful commands + +- Install the pre-commit hook - - Install the pre-commit hook ```bash pre-commit install ``` - - Check for StyLua errors +- Check for StyLua errors + ```bash stylua --check lua/ ``` - - - Fix StyLua errors + +- Fix StyLua errors + ```bash stylua lua/ ``` diff --git a/lua/runner/handlers/utils.lua b/lua/runner/handlers/utils.lua index f436e31..b763631 100644 --- a/lua/runner/handlers/utils.lua +++ b/lua/runner/handlers/utils.lua @@ -5,6 +5,7 @@ local config = require('runner.config') M._terminal_buffer = nil M._terminal_window = nil M._last_command = nil +M._last_handler = nil M.create_buffer = function() if M._terminal_buffer then diff --git a/lua/runner/init.lua b/lua/runner/init.lua index fa8d7e5..86cccd7 100644 --- a/lua/runner/init.lua +++ b/lua/runner/init.lua @@ -9,6 +9,18 @@ M._handlers = handlers M.setup = function(options) config.setup(options) + + vim.api.nvim_create_user_command('Runner', function() + require('runner').run() + end, { desc = 'Run code inside the editor' }) + + vim.api.nvim_create_user_command('AutoRunner', function() + require('runner').autorun() + end, { desc = 'Execute `Runner` on a file save' }) + + vim.api.nvim_create_user_command('AutoRunnerStop', function() + require('runner').autorun_stop() + end, { desc = 'Stop `AutoRunner`' }) end --- **Overrides** the handler for the specified filetype @@ -28,29 +40,49 @@ M.set_handler = function(filetype, handler) M._handlers[filetype] = handler end +--- @param bufnr integer? M.run = function(bufnr) - local buffer if bufnr == nil or bufnr == 0 then - buffer = vim.api.nvim_get_current_buf() - else - buffer = bufnr + bufnr = vim.api.nvim_get_current_buf() end - if buffer == utils._terminal_buffer then + if bufnr == utils._terminal_buffer then helpers.shell_handler(utils._last_command, false)() return end - local filetype = vim.filetype.match { buf = buffer } + local filetype = vim.filetype.match { buf = bufnr } local handler = M._handlers[filetype] if not handler then - print(string.format("No handler defined for filetype '%s'", filetype)) + print('No handler defined for filetype ' .. filetype) return end - handler(buffer) + utils._last_handler = handler + handler(bufnr) +end + +--- @param bufnr integer? +M.autorun = function(bufnr) + M.run(bufnr) + vim.api.nvim_create_autocmd('BufWritePost', { + group = vim.api.nvim_create_augroup('AutoRunner', { clear = true }), + pattern = '*', + callback = function() + if utils._terminal_window then + helpers.shell_handler(utils._last_command, false)() + else + utils._last_handler(bufnr) + end + end, + }) +end + +M.autorun_stop = function() + vim.api.nvim_del_augroup_by_name('AutoRunner') + vim.api.nvim_win_close(utils._terminal_window, true) end return M