aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorsrdusr <trevorgray@srdusr.com>2023-10-31 21:44:09 +0200
committersrdusr <trevorgray@srdusr.com>2023-10-31 21:44:09 +0200
commit6225a2060ba8c547d89e32af18623a2b3433bbb2 (patch)
tree27acb72ed3a64eba915985f812a4e6856938a8b7 /lua
parent19b43a9d96ad49c0364b047102f110f7166672e9 (diff)
downloaddotfiles-6225a2060ba8c547d89e32af18623a2b3433bbb2.tar.gz
dotfiles-6225a2060ba8c547d89e32af18623a2b3433bbb2.zip
RunCode() self code runner for various filetypes
Diffstat (limited to 'lua')
-rw-r--r--lua/user/mods.lua91
1 files changed, 91 insertions, 0 deletions
diff --git a/lua/user/mods.lua b/lua/user/mods.lua
index 6d357f5..a74d7fb 100644
--- a/lua/user/mods.lua
+++ b/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