fix: exscpe test when passing it to vim syntax match

This was causing an issue an invalid regex. Now we are escapeing the
text to make it a valid regex.

There is also a small improvement where we no longer try and highlight
matched words if the "text" is empty.

Ref: #26
This commit is contained in:
Ade Attwood 2022-10-06 07:45:58 +01:00
parent 03ab57da3b
commit e7263a1b50
2 changed files with 57 additions and 1 deletions

View file

@ -34,7 +34,12 @@ controller.update = function(text)
vim.schedule(function() vim.schedule(function()
window.set_items(controller.items(text)) window.set_items(controller.items(text))
vim.cmd "syntax clear IvyMatch" vim.cmd "syntax clear IvyMatch"
vim.cmd("syntax match IvyMatch '[(" .. text .. ")]'") if #text > 0 then
-- Escape characters so they do not throw an error when vim tries to use
-- the "text" as a regex
local escaped_text = string.gsub(text, "([-/])", "\\%1")
vim.cmd("syntax match IvyMatch '[" .. escaped_text .. "]'")
end
end) end)
end end

View file

@ -0,0 +1,51 @@
local vim_mock = require "ivy.vim_mock"
local window = require "ivy.window"
local controller = require "ivy.controller"
-- The number of the mock buffer where all the test completions gets put
local buffer_number = 10
before_each(function()
vim_mock.reset()
window.initialize()
end)
after_each(function()
controller.destroy()
end)
it("will run", function(t)
controller.run("Testing", function()
return { { content = "Some content" } }
end, function()
return {}
end)
local lines = vim_mock.get_lines()
local completion_lines = lines[buffer_number]
t.assert_equal(#completion_lines, 1)
t.assert_equal(completion_lines[1], "Some content")
end)
it("will not try and highlight the buffer if there is nothing to highlight", function(t)
controller.items = function()
return { { content = "Hello" } }
end
controller.update ""
local commands = vim_mock.get_commands()
t.assert_equal(#commands, 1)
end)
it("will escape a - when passing it to be highlighted", function(t)
controller.items = function()
return { { content = "Hello" } }
end
controller.update "some-file"
local commands = vim_mock.get_commands()
local syntax_command = commands[2]
t.assert_equal("syntax match IvyMatch '[some\\-file]'", syntax_command)
end)