diff --git a/lua/ivy/controller.lua b/lua/ivy/controller.lua index c0194eb..0af4afa 100644 --- a/lua/ivy/controller.lua +++ b/lua/ivy/controller.lua @@ -1,85 +1,90 @@ local window = require "ivy.window" local prompt = require "ivy.prompt" local utils = require "ivy.utils" local controller = {} controller.action = utils.actions controller.items = nil controller.callback = nil controller.run = function(name, items, callback) controller.callback = callback controller.items = items window.initialize() window.set_items { { content = "-- Loading ---" } } vim.api.nvim_buf_set_name(window.get_buffer(), name) controller.input "" end controller.input = function(char) prompt.input(char) controller.update(prompt.text()) end controller.search = function(value) prompt.set(value) controller.update(prompt.text()) end controller.update = function(text) vim.schedule(function() window.set_items(controller.items(text)) 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 controller.complete = function(action) vim.api.nvim_set_current_win(window.origin) controller.callback(window.get_current_selection(), action) controller.destroy() end controller.checkpoint = function() vim.api.nvim_set_current_win(window.origin) controller.callback(window.get_current_selection(), controller.action.CHECKPOINT) vim.api.nvim_set_current_win(window.window) end controller.next = function() local max = vim.api.nvim_buf_line_count(window.buffer) - 1 if window.index == max then return end window.index = window.index + 1 window.update() end controller.previous = function() if window.index == 0 then return end window.index = window.index - 1 window.update() end controller.origin = function() return vim.api.nvim_win_get_buf(window.origin) end controller.destroy = function() controller.items = nil controller.callback = nil window.destroy() prompt.destroy() end return controller diff --git a/lua/ivy/controller_test.lua b/lua/ivy/controller_test.lua new file mode 100644 index 0000000..9865142 --- /dev/null +++ b/lua/ivy/controller_test.lua @@ -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)