shell: respect shellslash option on Windows

Always use backlashes as path separators
in commands when they are used in cmd.exe.

Fixes #2844 #1965
This commit is contained in:
Attila Tajti 2022-11-30 11:39:18 +01:00 committed by Stephan Seitz
parent d31c71c959
commit 8b5080b1bf

View file

@ -1,6 +1,17 @@
local fn = vim.fn local fn = vim.fn
local utils = require "nvim-treesitter.utils" local utils = require "nvim-treesitter.utils"
-- Convert path for cmd.exe on Windows.
-- This is needed when vim.opt.shellslash is in use.
local function cmdpath(p)
if vim.opt.shellslash:get() then
local r = p:gsub("/", "\\")
return r
else
return p
end
end
local M = {} local M = {}
function M.select_mkdir_cmd(directory, cwd, info_msg) function M.select_mkdir_cmd(directory, cwd, info_msg)
@ -8,7 +19,7 @@ function M.select_mkdir_cmd(directory, cwd, info_msg)
return { return {
cmd = "cmd", cmd = "cmd",
opts = { opts = {
args = { "/C", "mkdir", directory }, args = { "/C", "mkdir", cmdpath(directory) },
cwd = cwd, cwd = cwd,
}, },
info = info_msg, info = info_msg,
@ -32,7 +43,7 @@ function M.select_rm_file_cmd(file, info_msg)
return { return {
cmd = "cmd", cmd = "cmd",
opts = { opts = {
args = { "/C", "if", "exist", file, "del", file }, args = { "/C", "if", "exist", cmdpath(file), "del", cmdpath(file) },
}, },
info = info_msg, info = info_msg,
err = "Could not delete " .. file, err = "Could not delete " .. file,
@ -135,7 +146,7 @@ function M.select_install_rm_cmd(cache_folder, project_name)
return { return {
cmd = "cmd", cmd = "cmd",
opts = { opts = {
args = { "/C", "if", "exist", dir, "rmdir", "/s", "/q", dir }, args = { "/C", "if", "exist", cmdpath(dir), "rmdir", "/s", "/q", cmdpath(dir) },
}, },
} }
else else
@ -153,7 +164,7 @@ function M.select_mv_cmd(from, to, cwd)
return { return {
cmd = "cmd", cmd = "cmd",
opts = { opts = {
args = { "/C", "move", "/Y", from, to }, args = { "/C", "move", "/Y", cmdpath(from), cmdpath(to) },
cwd = cwd, cwd = cwd,
}, },
} }
@ -261,7 +272,7 @@ end
function M.make_directory_change_for_command(dir, command) function M.make_directory_change_for_command(dir, command)
if fn.has "win32" == 1 then if fn.has "win32" == 1 then
return string.format("pushd %s & %s & popd", dir, command) return string.format("pushd %s & %s & popd", cmdpath(dir), command)
else else
return string.format("cd %s;\n %s", dir, command) return string.format("cd %s;\n %s", dir, command)
end end