Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lua/code_runner/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ function M.run_code(filetype, user_argument)
local cmd_to_execute = utils:getCommand(filetype)
if cmd_to_execute then
utils.opt.before_run_filetype()
utils:runMode(cmd_to_execute, vim.fn.expand("%:t:r"))
local filename = vim.fn.shellescape(vim.fn.expand("%:t:r"))
utils:runMode(cmd_to_execute, filename)
return
end
return -- Exit if there is no valid command.
Expand Down
16 changes: 9 additions & 7 deletions lua/code_runner/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Utils:ctor(opt)
"Vimux"
)
end
end
end,
}
end

Expand All @@ -69,15 +69,17 @@ function Utils:replaceVars(command, path)
-- Check if we already have the result cached
local cache_key = command .. ":" .. path
local cached = var_cache[cache_key]
if cached then return cached end
if cached then
return cached
end

local no_sub_command = command

-- Pre-calculate replacement values to avoid multiple vim.fn calls
local file_info = {
nameWithoutExt = vim.fn.fnamemodify(path, ":t:r"),
name = vim.fn.fnamemodify(path, ":t"),
dir = vim.fn.fnamemodify(path, ":p:h")
nameWithoutExt = vim.fn.shellescape(vim.fn.fnamemodify(path, ":t:r")),
name = vim.fn.shellescape(vim.fn.fnamemodify(path, ":t")),
dir = vim.fn.shellescape(vim.fn.fnamemodify(path, ":p:h")),
}

-- Use gsub once with a replacement function
Expand All @@ -87,7 +89,7 @@ function Utils:replaceVars(command, path)
elseif var == "fileName" then
return file_info.name
elseif var == "file" then
return path
return vim.fn.shellescape(path)
elseif var == "dir" then
return file_info.dir
elseif var == "end" then
Expand All @@ -98,7 +100,7 @@ function Utils:replaceVars(command, path)
end)

if command == no_sub_command then
command = command .. " " .. path
command = command .. " " .. vim.fn.shellescape(path)
end

-- Store result in cache
Expand Down