This commit is contained in:
Marc 2023-04-14 14:35:14 +03:00 committed by GitHub
commit 8dd67590de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -10,6 +10,19 @@ local utils = require('runner.handlers.utils')
local M = {}
--- Runs a command in a shell by opening it in a new split window, with a terminal buffer.
---
--- Usage:
--- ```lua
--- local helpers = require('runner.handlers.helpers')
--- require('runner').set_handler(
--- 'rust',
--- helpers.shell_handler('cargo run', true),
--- )
--- ```
---
--- @param command string The shell command to run when the handler called
--- @param editable boolean 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
M.shell_handler = function(command, editable)
if editable == nil then
editable = false
@ -35,13 +48,42 @@ M.shell_handler = function(command, editable)
end
end
--- Runs a vim command
---
--- Usage:
--- ```lua
--- local helpers = require('runner.handlers.helpers')
--- require('runner').set_handler(
--- 'lua',
--- helpers.command_handler('luafile %'),
--- )
--- ```
---
--- @param command string The vim command to run when the handler called
M.command_handler = function(command)
return function()
vim.cmd(command)
end
end
-- handlers = { 'Run Tests' = test_handler, 'Run Code' = code_handler }
--- Opens a `Telescope` finder to allow the user to choose which handler to run
---
--- Usage:
--- ```lua
--- local helpers = require('runner.handlers.helpers')
--- require('runner').set_handler(
--- 'lua',
--- helpers.choice({
--- ['Run current file'] = helpers.command_handler('luafile %'),
--- ['Foo'] = helpers.shell_handler('foo'),
--- ['Bar'] = function(buffer)
--- -- Custom handler here...
--- end,
--- }),
--- )
--- ```
---
--- @param handlers table<string, function> The vim command to run when the handler called
M.choice = function(handlers)
local handlers_count = vim.tbl_count(handlers)
if handlers_count == 0 then

View File

@ -11,6 +11,19 @@ M.setup = function(options)
config.setup(options)
end
--- **Overrides** the handler for the specified filetype
---
--- Usage:
--- ```lua
--- local helpers = require('runner.handlers.helpers')
--- require('runner').set_handler(
--- 'lua',
--- helpers.command_handler('luafile %'),
--- )
--- ```
---
--- @param filetype string The filetype on which to run the given handler
--- @param handler function The handler to run when the current file matches the filetype
M.set_handler = function(filetype, handler)
M._handlers[filetype] = handler
end