Skip to content

fix: Leading slashes of spaces in paths on Windows when opening files #1077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2023

Conversation

wizcas
Copy link
Contributor

@wizcas wizcas commented Jul 30, 2023

Closes #1076

@miversen33 Could you please take a look at this fix? It's a subsequent fix of the Windows path adaption since PR #1023.

I've tested on my side for these test cases and all of them have passed:

  • d:\test path\test.txt
  • d:\test\nested dir\test.txt
  • d:\test\(inner)\test.txt <-- regression test for 889 windows path escape #1023
  • d:\test\ leading\test.txt
  • d:\test\trailing \test.txt

image

@cseickel cseickel requested a review from miversen33 July 30, 2023 13:07
@miversen33
Copy link
Collaborator

I wouldn't have expected this to work on linux but it sure does! Nice catch and good fix :)

Tested with the below config (for future reference)

-- Minimal configuration
-- mini.lua
-- Use with the --clean -u flags. EG nvim --clean -u mini.lua
-- This config will create a temp directory and will blow away that temp directory
-- everytime this configuration is loaded. Great for simulating a new installation
-- of a plugin

-- Setting some basic vim options
-- Some junk because I am sick of formatting tables in print
local _print = _G.print
local clean_string = function(...)
    local args = { n = select("#", ...), ... }
    local formatted_args = {}
    for i=1, args.n do
        local item = select(i, ...)
        if not item then item = 'nil' end
        local t_type = type(item)
        if t_type == 'table' or t_type == 'function' or t_type == 'userdata' then
            item = vim.inspect(item)
        end
        table.insert(formatted_args, item)
    end
    return table.concat(formatted_args, ' ')
end
_G.print = function(...)
    _print(clean_string(...))
end

vim.opt.mouse = 'a'
vim.opt.termguicolors = true
-- If you want to play around with this, you can set the do_clean
-- variable to false. This will allow changes made to
-- underlying plugins to persist between sessions, while
-- still keeping everything in its own directory so
-- as to not affect your existing neovim installation.
--
-- Setting this to true will result in a fresh clone of
-- all modules
local do_clean = true

local sep = vim.loop.os_uname().sysname:lower():match('windows') and '\\' or '/' -- \ for windows, mac and linux both use \

local mod_path = string.format("%s%sclean-test%s", vim.fn.stdpath('cache'), sep, sep)
if vim.loop.fs_stat(mod_path) and do_clean then
    print("Found previous clean test setup. Cleaning it out")
    -- Clearing out the mods directory and recreating it so 
    -- you have a fresh run everytime
    vim.fn.delete(mod_path, 'rf')
end

vim.fn.mkdir(mod_path, 'p')

local modules = {
    {'nvim-lua/plenary.nvim'},
    {'nvim-tree/nvim-web-devicons'},
    {'MunifTanjim/nui.nvim'},
    {'wizcas/neo-tree.nvim', branch='1076-win-space-in-path', mod = 'neo-tree'},
}

for _, module in ipairs(modules) do
    local repo = module[1]
    local branch = module.branch
    local module_name = repo:match('/(.*)')
    local module_path = string.format('%s%s%s', mod_path, sep, module_name)
    if not vim.loop.fs_stat(module_name) then
        -- The module doesn't exist, download it
        local cmd = {
            'git',
            'clone'
        }
        if branch then
            table.insert(cmd, '--branch')
            table.insert(cmd, branch)
        end
        table.insert(cmd, string.format('https://github.com/%s', repo))
        table.insert(cmd, module_path)
        vim.fn.system(cmd)
        local message = string.format("Downloaded %s", module_name)
        if branch then
            message = string.format("%s on branch %s", message, branch)
        end
        print(message)
    end
    vim.opt.runtimepath:append(module_path)
end

print("Finished installing plugins. Beginning Setup of plugins")

for _, module in ipairs(modules) do
    if module.mod then
        print(string.format("Loading %s", module.mod))
        local success, err = pcall(require, module.mod)
        if not success then
            print(string.format("Failed to load module %s", module.mod))
            error(err)
        end
    end
end

-- --> Do you module setups below this line <-- --

local neo_tree = require("neo-tree")


neo_tree.setup(
{
    sources = {
        "filesystem",
    },
    filesystem = {
        follow_current_file = true,
    },
}
)

-- --> Do your module setups above this line <-- --

print("Creating Environment to emulate #1076")
vim.fn.delete('./test', 'rf')
vim.fn.mkdir('./test/outer/(inner)', 'p')
vim.fn.mkdir('./test/test path/', 'p')
vim.fn.mkdir('./test/nested dir/', 'p')
vim.fn.mkdir('./test/(inner)', 'p')
vim.fn.mkdir('./test/ leading/', 'p')
vim.fn.mkdir('./test/trailing /', 'p')
local h = nil
h = io.open('./test/outer/(inner)/file.txt', 'w')
h:write("file.txt content\n")
h:close()
h = io.open('./test/outer/test.txt', 'w')
h:write("test.txt content\n")
h:close()
h = io.open('./test/test path/test.txt', 'w')
h:write("test path.txt content\n")
h:close()
h = io.open('./test/nested dir/test.txt', "w")
h:write("test nested dir test.txt content\n")
h:close()
h = io.open('./test/(inner)/test.txt', "w")
h:write("test inner test.txt content\n")
h:close()
h = io.open("./test/ leading/test.txt", "w")
h:write("test  leading test.txt content\n")
h:close()
h = io.open("./test/trailing /test.txt", 'w')
h:write("test trailing  test.txt content\n")
h:close()
vim.api.nvim_command('cd ./test/')
print("Created environment")
print("Completed minimal setup! Execute :Neotree to get started")

@wizcas
Copy link
Contributor Author

wizcas commented Jul 30, 2023

@cseickel could you please review and approve?

@cseickel
Copy link
Contributor

Thanks for testing @miversen33!

@cseickel cseickel merged commit 7951701 into nvim-neo-tree:main Jul 31, 2023
@wizcas wizcas deleted the 1076-win-space-in-path branch August 1, 2023 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Cannot open files with spaces in the path on Windows
3 participants