diff options
| author | srdusr <trevorgray@srdusr.com> | 2023-10-31 21:44:09 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2023-11-04 17:37:20 +0200 |
| commit | 98f4aae2b177410c96b6d926eba2042f8a16d51e (patch) | |
| tree | 5aeb66d864eca621f41075bece234be0cac977e8 /.config/nvim/lua | |
| parent | 5624711b92c08d4108ea5c01e81c1d2cac756ba8 (diff) | |
| download | dotfiles-98f4aae2b177410c96b6d926eba2042f8a16d51e.tar.gz dotfiles-98f4aae2b177410c96b6d926eba2042f8a16d51e.zip | |
RunCode() self code runner for various filetypes
Diffstat (limited to '.config/nvim/lua')
| -rw-r--r-- | .config/nvim/lua/user/mods.lua | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/.config/nvim/lua/user/mods.lua b/.config/nvim/lua/user/mods.lua index 6d357f5..a74d7fb 100644 --- a/.config/nvim/lua/user/mods.lua +++ b/.config/nvim/lua/user/mods.lua @@ -850,5 +850,96 @@ vim.api.nvim_create_autocmd('BufHidden', { -------------------------------------------------- +local function substitute(cmd) + cmd = cmd:gsub('%%', vim.fn.expand('%')) + cmd = cmd:gsub('$fileBase', vim.fn.expand('%:r')) + cmd = cmd:gsub('$filePath', vim.fn.expand('%:p')) + cmd = cmd:gsub('$file', vim.fn.expand('%')) + cmd = cmd:gsub('$dir', vim.fn.expand('%:p:h')) + cmd = cmd:gsub('#', vim.fn.expand('#')) + cmd = cmd:gsub('$altFile', vim.fn.expand('#')) + + return cmd +end + +function M.RunCode() + local file_extension = vim.fn.expand('%:e') + local selected_cmd = '' + local term_cmd = 'bot 10 new | term ' + local supported_filetypes = { + html = { + default = '%', + }, + c = { + default = 'gcc % -o $fileBase && ./$fileBase', + debug = 'gcc -g % -o $fileBase && ./$fileBase', + }, + cs = { + default = 'dotnet run', + }, + cpp = { + default = 'g++ % -o $fileBase && ./$fileBase', + debug = 'g++ -g % -o ./$fileBase', + competitive = 'g++ -std=c++17 -Wall -DAL -O2 % -o $fileBase && $fileBase<input.txt', + }, + py = { + default = 'python %', + }, + go = { + default = 'go run %', + }, + java = { + default = 'java %', + }, + js = { + default = 'node %', + debug = 'node --inspect %', + }, + ts = { + default = 'tsc % && node $fileBase', + }, + rs = { + default = 'rustc % && $fileBase', + }, + php = { + default = 'php %', + }, + r = { + default = 'Rscript %', + }, + jl = { + default = 'julia %', + }, + rb = { + default = 'ruby %', + }, + pl = { + default = 'perl %', + }, + } + + if supported_filetypes[file_extension] then + local choices = vim.tbl_keys(supported_filetypes[file_extension]) + + if #choices == 0 then + vim.notify("It doesn't contain any command", vim.log.levels.WARN, { title = 'Code Runner' }) + elseif #choices == 1 then + selected_cmd = supported_filetypes[file_extension][choices[1]] + vim.cmd(term_cmd .. substitute(selected_cmd)) + else + vim.ui.select(choices, { prompt = 'Choose a command: ' }, function(choice) + selected_cmd = supported_filetypes[file_extension][choice] + if selected_cmd then + vim.cmd(term_cmd .. substitute(selected_cmd)) + end + end) + end + else + vim.notify("The filetype isn't included in the list", vim.log.levels.WARN, { title = 'Code Runner' }) + end +end + +-------------------------------------------------- + -- ... return M |
