From 0d937e01a1f15e2ec74c5521acc34d5fb0f35624 Mon Sep 17 00:00:00 2001 From: andreadev-it <68549121+andreadev-it@users.noreply.github.com> Date: Fri, 27 May 2022 22:32:09 +0200 Subject: [PATCH 1/5] Fixed problem with shellslash This should fix at least some of the problems caused by having shellslash active on vim. --- lua/plenary/path.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua index 4de52615..c90fa66c 100644 --- a/lua/plenary/path.lua +++ b/lua/plenary/path.lua @@ -21,7 +21,7 @@ path.home = vim.loop.os_homedir() path.sep = (function() if jit then local os = string.lower(jit.os) - if os == "linux" or os == "osx" or os == "bsd" then + if os == "linux" or os == "osx" or os == "bsd" or vim.o.shellslash then return "/" else return "\\" From 907387a43ca86cb769e886e446804b542b96b127 Mon Sep 17 00:00:00 2001 From: Andrea Dragotta Date: Sat, 28 May 2022 16:54:34 +0200 Subject: [PATCH 2/5] Fixed error on linux (shellslash option doesn't exists) --- lua/plenary/path.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua index c90fa66c..ed0143df 100644 --- a/lua/plenary/path.lua +++ b/lua/plenary/path.lua @@ -20,8 +20,13 @@ path.home = vim.loop.os_homedir() path.sep = (function() if jit then + local shellslash_exists, _ = pcall(function() local _ = vim.o.shellslash end) + local use_shellslash = false + if shellslash_exists then + use_shellslash = vim.o.shellslash + end local os = string.lower(jit.os) - if os == "linux" or os == "osx" or os == "bsd" or vim.o.shellslash then + if os == "linux" or os == "osx" or os == "bsd" or use_shellslash then return "/" else return "\\" From 504353b00760b9156abf0f94fb6417d1aa193839 Mon Sep 17 00:00:00 2001 From: andreadev-it <68549121+andreadev-it@users.noreply.github.com> Date: Sun, 29 May 2022 08:03:16 +0200 Subject: [PATCH 3/5] Improved check for shellslash function Co-authored-by: Ben Weedon --- lua/plenary/path.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua index ed0143df..faf7a766 100644 --- a/lua/plenary/path.lua +++ b/lua/plenary/path.lua @@ -20,7 +20,7 @@ path.home = vim.loop.os_homedir() path.sep = (function() if jit then - local shellslash_exists, _ = pcall(function() local _ = vim.o.shellslash end) + local shellslash_exists = vim.fn.exists("+shellslash") ~= 0 local use_shellslash = false if shellslash_exists then use_shellslash = vim.o.shellslash From 328110222dd6dbc6ee087022d20742f63097fbec Mon Sep 17 00:00:00 2001 From: Andrea Dragotta Date: Mon, 31 Oct 2022 16:26:51 +0100 Subject: [PATCH 4/5] Added system module and uniformed checks for windows --- lua/plenary/curl.lua | 3 ++- lua/plenary/path.lua | 19 +++++++------------ lua/plenary/scandir.lua | 9 +++++---- lua/plenary/strings.lua | 5 +++-- lua/plenary/system.lua | 22 ++++++++++++++++++++++ tests/plenary/path_spec.lua | 11 ++++++----- 6 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 lua/plenary/system.lua diff --git a/lua/plenary/curl.lua b/lua/plenary/curl.lua index 7f4eef8c..2a052b77 100644 --- a/lua/plenary/curl.lua +++ b/lua/plenary/curl.lua @@ -32,6 +32,7 @@ local util, parse, request = {}, {}, nil local F = require "plenary.functional" local J = require "plenary.job" local P = require "plenary.path" +local S = require "plenary.system" -- Utils ---------------------------------------------------- ------------------------------------------------------------- @@ -70,7 +71,7 @@ util.gen_dump_path = function() local v = (l == "x") and math.random(0, 0xf) or math.random(0, 0xb) return string.format("%x", v) end) - if P.path.sep == "\\" then + if S.is_windows() then path = string.format("%s\\AppData\\Local\\Temp\\plenary_curl_%s.headers", os.getenv "USERPROFILE", id) else path = "/tmp/plenary_curl_" .. id .. ".headers" diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua index 5d8fb928..a54692db 100644 --- a/lua/plenary/path.lua +++ b/lua/plenary/path.lua @@ -7,6 +7,7 @@ local bit = require "plenary.bit" local uv = vim.loop local F = require "plenary.functional" +local S = require "plenary.system" local S_IF = { -- S_IFDIR = 0o040000 # directory @@ -20,13 +21,7 @@ path.home = vim.loop.os_homedir() path.sep = (function() if jit then - local shellslash_exists = vim.fn.exists("+shellslash") ~= 0 - local use_shellslash = false - if shellslash_exists then - use_shellslash = vim.o.shellslash - end - local os = string.lower(jit.os) - if os ~= "windows" or use_shellslash then + if not S.is_windows() or S.uses_shellslash() then return "/" else return "\\" @@ -37,7 +32,7 @@ path.sep = (function() end)() path.root = (function() - if path.sep == "/" then + if not S.is_windows() or S.uses_shellslash then return function() return "/" end @@ -60,7 +55,7 @@ local concat_paths = function(...) end local function is_root(pathname) - if path.sep == "\\" then + if S.is_windows() then return string.match(pathname, "^[A-Z]:\\?$") end return pathname == "/" @@ -82,7 +77,7 @@ local is_uri = function(filename) end local is_absolute = function(filename, sep) - if sep == "\\" then + if S.is_windows() then return string.match(filename, "^[%a]:\\.*$") ~= nil end return string.sub(filename, 1, 1) == sep @@ -108,7 +103,7 @@ local function _normalize_path(filename, cwd) local split_without_disk_name = function(filename_local) local parts = _split_by_separator(filename_local) -- Remove disk name part on Windows - if path.sep == "\\" and is_abs then + if S.is_windows() and is_abs then table.remove(parts, 1) end return parts @@ -436,7 +431,7 @@ local function shorten_len(filename, len, exclude) end local shorten = (function() - if jit and path.sep ~= "\\" then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned char char_u; diff --git a/lua/plenary/scandir.lua b/lua/plenary/scandir.lua index 16e4ed54..80850408 100644 --- a/lua/plenary/scandir.lua +++ b/lua/plenary/scandir.lua @@ -1,6 +1,7 @@ local Path = require "plenary.path" local os_sep = Path.path.sep local F = require "plenary.functional" +local S = require "plenary.system" local uv = vim.loop @@ -319,7 +320,7 @@ local gen_date = (function() end)() local get_username = (function() - if jit and os_sep ~= "\\" then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned int __uid_t; @@ -369,7 +370,7 @@ local get_username = (function() end)() local get_groupname = (function() - if jit and os_sep ~= "\\" then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned int __gid_t; @@ -446,8 +447,8 @@ local gen_ls = function(data, path, opts) local results, sections = {}, {} - local users_tbl = os_sep ~= "\\" and {} or nil - local groups_tbl = os_sep ~= "\\" and {} or nil + local users_tbl = not S.is_windows() and {} or nil + local groups_tbl = not S.is_windows() and {} or nil local stats, permissions_cache = {}, {} for _, v in ipairs(data) do diff --git a/lua/plenary/strings.lua b/lua/plenary/strings.lua index bc374739..3b1828c2 100644 --- a/lua/plenary/strings.lua +++ b/lua/plenary/strings.lua @@ -1,9 +1,10 @@ local path = require("plenary.path").path +local S = require "plenary.system" local M = {} M.strdisplaywidth = (function() - if jit and path.sep ~= [[\]] then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned char char_u; @@ -29,7 +30,7 @@ M.strdisplaywidth = (function() end)() M.strcharpart = (function() - if jit and path.sep ~= [[\]] then + if jit and not S.is_windows() then local ffi = require "ffi" ffi.cdef [[ typedef unsigned char char_u; diff --git a/lua/plenary/system.lua b/lua/plenary/system.lua new file mode 100644 index 00000000..6685bb74 --- /dev/null +++ b/lua/plenary/system.lua @@ -0,0 +1,22 @@ +local system = {} + +function system.is_windows() + -- The shellslash option is not able to change this variable + if package.config.sub(1,1) == "\\" then + return true + end + + return false +end + +function system.uses_shellslash() + local shellslash_exists = vim.fn.exists("+shellslash") ~= 0 + + if shellslash_exists then + return vim.o.shellslash + end + + return false +end + +return system diff --git a/tests/plenary/path_spec.lua b/tests/plenary/path_spec.lua index f5a90972..f64cab61 100644 --- a/tests/plenary/path_spec.lua +++ b/tests/plenary/path_spec.lua @@ -1,5 +1,6 @@ local Path = require "plenary.path" local path = Path.path +local System = require "plenary.system" describe("Path", function() it("should find valid files", function() @@ -104,7 +105,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to a given path", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local r = Path:new { root, "home", "prime" } local p = Path:new { "aoeu", "agen.lua" } local absolute = r.filename .. path.sep .. p.filename @@ -120,7 +121,7 @@ describe("Path", function() end) it("can take double separator absolute paths and make them relative to a given path", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local r = Path:new { root, "home", "prime" } local p = Path:new { "aoeu", "agen.lua" } local absolute = r.filename .. path.sep .. path.sep .. p.filename @@ -129,7 +130,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to a given path with trailing separator", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local r = Path:new { root, "home", "prime" } local p = Path:new { "aoeu", "agen.lua" } local absolute = r.filename .. path.sep .. p.filename @@ -138,7 +139,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to the root directory", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local p = Path:new { "home", "prime", "aoeu", "agen.lua" } local absolute = root .. p.filename local relative = Path:new(absolute):make_relative(root) @@ -146,7 +147,7 @@ describe("Path", function() end) it("can take absolute paths and make them relative to themselves", function() - local root = path.sep == "\\" and "c:\\" or "/" + local root = System.is_windows() and "c:\\" or "/" local p = Path:new { root, "home", "prime", "aoeu", "agen.lua" } local relative = Path:new(p.filename):make_relative(p.filename) assert.are.same(relative, ".") From 5f70cf1c8928c3390b7162f04e38c822b82afe04 Mon Sep 17 00:00:00 2001 From: Andrea Dragotta Date: Mon, 31 Oct 2022 17:06:06 +0100 Subject: [PATCH 5/5] Fixed typo that prevented correct Windows check --- lua/plenary/system.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/plenary/system.lua b/lua/plenary/system.lua index 6685bb74..e04b0cf2 100644 --- a/lua/plenary/system.lua +++ b/lua/plenary/system.lua @@ -2,7 +2,7 @@ local system = {} function system.is_windows() -- The shellslash option is not able to change this variable - if package.config.sub(1,1) == "\\" then + if package.config:sub(1,1) == "\\" then return true end