diff --git a/lua/runner/handlers/helpers.lua b/lua/runner/handlers/helpers.lua index a2b13c6..1d26cf2 100644 --- a/lua/runner/handlers/helpers.lua +++ b/lua/runner/handlers/helpers.lua @@ -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 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 diff --git a/lua/runner/init.lua b/lua/runner/init.lua index f81d540..fa8d7e5 100644 --- a/lua/runner/init.lua +++ b/lua/runner/init.lua @@ -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