Skip to content
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

fix(#2961): windows: escape brackets and parentheses when opening file #2962

Merged
merged 8 commits into from
Oct 25, 2024

Conversation

ljie-PI
Copy link
Collaborator

@ljie-PI ljie-PI commented Oct 19, 2024

Description
With this change (45a93d9#diff-eeb21711fceb0abba1450bd1adcbecc4fd90cf8ce4d5d7a8233f886c1bebd41fR375), I found it doesn't work well on Windows for 2 cases:

  1. When I open a file from nvim-tree, the root of nvim-tree window changed to the directory of that file
  2. The buffer of this file can not be listed when I use telescope command <cmd>lua require('telescope.builtin').buffers(require('telescope.themes').get_dropdown{previewer = false})<cr> Neovim version

This PR is to revert the change and fix the issue #2961 and #2862

@@ -59,6 +59,17 @@ function M.path_basename(path)
return path:sub(i + 1, #path)
end

--- Check if there are parentheses before brackets, it causes problems for windows.
--- Refer to issue #2862 and #2961 for more details.
local function has_parentheses_and_brackets(path)
Copy link
Member

@alex-courtis alex-courtis Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work fixing this one.

--- Escapes special characters in string if windows else returns unmodified string.
---@param path string
---@return string|nil
function M.escape_special_chars(path)
if path == nil then
return path
end
return M.is_windows and path:gsub("\\", "/") or path
return M.is_windows and escape_special_char_for_windows(path) or path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this one definitely should be behind the windows feature flag.

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for this fix! This all looks reasonable and I can see how you have tested comprehensively.

The is_windows feature flag only applies to powershell, hence you do not need to test WSL.

The nvim-tree developers don't have access to nor expertise with windows, if there is an issue we expect you to resolve it.

  • All windows fixes must go behind the feature flags utils.is_windows etc. The existing codepaths for non-windows must remain unchanged.

@@ -370,36 +370,35 @@ end
---@param mode string
---@param filename string
function M.fn(mode, filename)
local fname = utils.escape_special_chars(filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a change in behaviour that will affect linux and macos users.

Please put this behind the windows feature flag, to ensure that it only applies to windows users.

Something like

local fname
if utils.is_windows then
  fname = filename
else
  fname = utils.escape_special_chars(filename)
end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alex-courtis , change of this line is caused by the revert of previous fix. I think there wasn't such line at the begining.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please accept my apologies, I did not Seek First To Understand.

git diff bd4881660bf0ddfa6acb21259f856ba3dcb26a93 lua/nvim-tree/actions/node/open-file.lua

shows that the change #2903 has been completely reverted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I reverted all changes of #2903 .

In #2903 util.escape_special_chats is called here and impact all file names below, which are not correct. (my tests failed)

Now and before #2903 utils.escape_special_chars is called in the open_in_new_window function (line 334 and 336), so I think I'm just following the logic before #2903 . And the is_windows flag is already in the utils.escape_special_chars function, so not added outside utils.escape_special_chars

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just the one windows feature flag check needed.

Alternative idea:
I was able to cleanly git revert 45a93d99794fff3064141d5b3a50db98ce352697 on master
You could instead do that on a new branch and apply your fixes on top.
That would result in cleaner history and a simpler PR.
Your choice.

@@ -370,36 +370,35 @@ end
---@param mode string
---@param filename string
function M.fn(mode, filename)
local fname = utils.escape_special_chars(filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please accept my apologies, I did not Seek First To Understand.

git diff bd4881660bf0ddfa6acb21259f856ba3dcb26a93 lua/nvim-tree/actions/node/open-file.lua

shows that the change #2903 has been completely reverted.

end

local _, r = norm_path:find(M.path_add_trailing(relative_to), 1, true)
local p = norm_path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path is executed for all OS, which may cause issues for such paths. Please execute only when is_windows

This injected error is definitely thrown on linux, showing that the code path is executed:

--- Check if there are parentheses before brackets, it causes problems for windows.
--- Refer to issue #2862 and #2961 for more details.
local function has_parentheses_and_brackets(path)
  error("has_parentheses_and_brackets")
E5108: Error executing lua: ...d/site/pack/packer/start/ljie-PI/lua/nvim-tree/utils.lua:65: has_parentheses_and_brackets called
stack traceback:
        [C]: in function 'error'
        ...d/site/pack/packer/start/ljie-PI/lua/nvim-tree/utils.lua:65: in function 'has_parentheses_and_brackets'
        ...d/site/pack/packer/start/ljie-PI/lua/nvim-tree/utils.lua:84: in function 'path_relative'
        .../packer/start/ljie-PI/lua/nvim-tree/explorer/filters.lua:161: in function 'custom'
        .../packer/start/ljie-PI/lua/nvim-tree/explorer/filters.lua:247: in function 'should_filter_as_reason'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added flag on line 84, so that norm_path will keep unchanged for non-windows OS.

@alex-courtis alex-courtis changed the title fix(#2961): [On windows] Root of nvim-tree changed unexpectedly and buffer not listed by telescope fix(#2961): windows: escape brackets and parentheses when opening file Oct 20, 2024
@alex-courtis
Copy link
Member

alex-courtis commented Oct 21, 2024 via email

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for your contribution!

@alex-courtis alex-courtis merged commit 63c7ad9 into nvim-tree:master Oct 25, 2024
5 checks passed
@ljie-PI ljie-PI deleted the win_root_change branch October 25, 2024 16:16
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.

2 participants