Skip to content

Commit 90a986f

Browse files
authored
Merge pull request #148 from aditya-an1l/main
fix #147 white-space bug in by shellescape-ing all variable substitutions
2 parents 8099dca + 84b61e7 commit 90a986f

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

lua/code_runner/commands.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ function M.run_code(filetype, user_argument)
4343
local cmd_to_execute = utils:getCommand(filetype)
4444
if cmd_to_execute then
4545
utils.opt.before_run_filetype()
46-
utils:runMode(cmd_to_execute, vim.fn.expand("%:t:r"))
46+
local filename = vim.fn.shellescape(vim.fn.expand("%:t:r"))
47+
utils:runMode(cmd_to_execute, filename)
4748
return
4849
end
4950
return -- Exit if there is no valid command.

lua/code_runner/utils.lua

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function Utils:ctor(opt)
4545
"Vimux"
4646
)
4747
end
48-
end
48+
end,
4949
}
5050
end
5151

@@ -69,15 +69,17 @@ function Utils:replaceVars(command, path)
6969
-- Check if we already have the result cached
7070
local cache_key = command .. ":" .. path
7171
local cached = var_cache[cache_key]
72-
if cached then return cached end
72+
if cached then
73+
return cached
74+
end
7375

7476
local no_sub_command = command
7577

7678
-- Pre-calculate replacement values to avoid multiple vim.fn calls
7779
local file_info = {
78-
nameWithoutExt = vim.fn.fnamemodify(path, ":t:r"),
79-
name = vim.fn.fnamemodify(path, ":t"),
80-
dir = vim.fn.fnamemodify(path, ":p:h")
80+
nameWithoutExt = vim.fn.shellescape(vim.fn.fnamemodify(path, ":t:r")),
81+
name = vim.fn.shellescape(vim.fn.fnamemodify(path, ":t")),
82+
dir = vim.fn.shellescape(vim.fn.fnamemodify(path, ":p:h")),
8183
}
8284

8385
-- Use gsub once with a replacement function
@@ -87,7 +89,7 @@ function Utils:replaceVars(command, path)
8789
elseif var == "fileName" then
8890
return file_info.name
8991
elseif var == "file" then
90-
return path
92+
return vim.fn.shellescape(path)
9193
elseif var == "dir" then
9294
return file_info.dir
9395
elseif var == "end" then
@@ -98,7 +100,7 @@ function Utils:replaceVars(command, path)
98100
end)
99101

100102
if command == no_sub_command then
101-
command = command .. " " .. path
103+
command = command .. " " .. vim.fn.shellescape(path)
102104
end
103105

104106
-- Store result in cache

0 commit comments

Comments
 (0)