From cf3e7e3044acdce65e1c2a873c4f350c709c9bce Mon Sep 17 00:00:00 2001 From: Ade Attwood Date: Tue, 13 Feb 2024 09:19:05 +0000 Subject: [PATCH] fix: opening file with square brackets in them Summary: Fixes an issue where you could not open files that were already open with ivy. If the file path contains a square brackets and that file is already loaded into a buffer, ivy will throw an error when trying to open it via "files" or "buffers". This is an issue with the file escaping before we try and cal `buffer` passing in the file path to go to the buffer rather than open a new buffer. This is common with JS frameworks like next js for parameters in file based routing. Test Plan: Test have been added for this change. I have also added tests for the dollar that was previously handled. --- lua/ivy/utils.lua | 3 ++- lua/ivy/utils_escape_test.lua | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 lua/ivy/utils_escape_test.lua diff --git a/lua/ivy/utils.lua b/lua/ivy/utils.lua index 92f74e0..910e57f 100644 --- a/lua/ivy/utils.lua +++ b/lua/ivy/utils.lua @@ -104,7 +104,8 @@ utils.line_action = function() end utils.escape_file_name = function(input) - return string.gsub(input, "([$])", "\\%1") + local file, _ = string.gsub(input, "([$%]\\[])", "\\%1") + return file end return utils diff --git a/lua/ivy/utils_escape_test.lua b/lua/ivy/utils_escape_test.lua new file mode 100644 index 0000000..ebe41d2 --- /dev/null +++ b/lua/ivy/utils_escape_test.lua @@ -0,0 +1,11 @@ +local utils = require "ivy.utils" + +it("will escape a dollar in the file name", function(t) + local result = utils.escape_file_name "/path/to/$file/$name.lua" + t.assert_equal(result, "/path/to/\\$file/\\$name.lua") +end) + +it("will escape a brackets in the file name", function(t) + local result = utils.escape_file_name "/path/to/[file]/[name].lua" + t.assert_equal(result, "/path/to/\\[file\\]/\\[name\\].lua") +end)