diff --git a/lua/ivy/utils.lua b/lua/ivy/utils.lua index 41fca29..86ee506 100644 --- a/lua/ivy/utils.lua +++ b/lua/ivy/utils.lua @@ -1,92 +1,106 @@ local utils = {} -- A list of all of the actions defined by ivy. The callback function can -- implement as many of them as necessary. As a minimum it should implement the -- "EDIT" action that is called on the default complete. utils.actions = { EDIT = "EDIT", CHECKPOINT = "CHECKPOINT", VSPLIT = "VSPLIT", SPLIT = "SPLIT", } utils.command_map = { [utils.actions.EDIT] = "edit", [utils.actions.CHECKPOINT] = "edit", [utils.actions.VSPLIT] = "vsplit", [utils.actions.SPLIT] = "split", } +utils.existing_command_map = { + [utils.actions.EDIT] = "buffer", + [utils.actions.CHECKPOINT] = "buffer", + [utils.actions.VSPLIT] = "vsplit | buffer", + [utils.actions.SPLIT] = "split | buffer", +} + utils.command_finder = function(command, min) if min == nil then min = 3 end return function(input) -- Dont run the commands unless we have somting to search that wont -- return a ton of results or on some commands the command files with -- no search term if #input < min then return "-- Please type more than " .. min .. " chars --" end -- TODO(ade): Think if we want to start escaping the command here. I -- dont know if its causing issues while trying to use regex especially -- with word boundaries `input:gsub("'", "\\'"):gsub('"', '\\"')` local handle = io.popen(command .. " " .. input .. " 2>&1") if handle == nil then return {} end local result = handle:read "*a" handle:close() return result end end utils.vimgrep_action = function() return function(item, action) -- Match file and line form vimgrep style commands local file = item:match "([^:]+):" local line = item:match ":(%d+):" -- Cant do anything if we cant find a file to go to if file == nil then return end utils.file_action()(file, action) if line ~= nil then vim.cmd(line) end end end utils.file_action = function() return function(file, action) if file == nil then return end - local command = utils.command_map[action] + local buffer_number = vim.fn.bufnr(file) + local command + if buffer_number > -1 then + command = utils.existing_command_map[action] + else + command = utils.command_map[action] + end + if command == nil then vim.api.nvim_err_writeln("[IVY] The file action is unable the handel the action " .. action) return end vim.cmd(command .. " " .. utils.escape_file_name(file)) end end utils.line_action = function() return function(item) local line = item:match "^%s+(%d+):" vim.cmd(line) end end utils.escape_file_name = function(input) return string.gsub(input, "([$])", "\\%1") end return utils diff --git a/lua/ivy/vim_mock.lua b/lua/ivy/vim_mock.lua index f8febd5..e333b57 100644 --- a/lua/ivy/vim_mock.lua +++ b/lua/ivy/vim_mock.lua @@ -1,84 +1,89 @@ local mock = { commands = {}, lines = {}, cursors = {}, } mock.get_lines = function() return mock.lines end mock.get_commands = function() return mock.commands end mock.reset = function() mock.commands = {} mock.lines = {} mock.cursors = {} _G.vim = { notify = function() end, cmd = function(cmd) table.insert(mock.commands, cmd) end, api = { nvim_echo = function() end, nvim_get_current_win = function() return 10 end, nvim_command = function() end, nvim_win_get_buf = function() return 10 end, nvim_win_set_option = function() end, nvim_buf_set_option = function() end, nvim_buf_set_name = function() end, nvim_buf_set_var = function() end, nvim_buf_set_keymap = function() end, nvim_buf_delete = function() end, nvim_buf_set_lines = function(buffer_number, state_index, end_index, _, items) local new_lines = {} for index = 1, state_index do if mock.lines[buffer_number][index] == nil then table.insert(new_lines, "") else table.insert(new_lines, mock.lines[buffer_number][index]) end end for index = 1, #items do table.insert(new_lines, items[index]) end if end_index ~= -1 then error("Mock of nvim_buf_set_lines dose not support a end_index grater than -1 found " .. end_index) end mock.lines[buffer_number] = new_lines end, nvim_win_set_height = function() end, nvim_win_set_cursor = function(window_number, position) mock.cursors[window_number] = position end, nvim_buf_get_lines = function(buffer_number, start_index, end_index) local lines = {} for index = start_index, end_index do table.insert(lines, mock.lines[buffer_number][index + 1]) end if #lines == 0 then return nil end return lines end, }, + fn = { + bufnr = function() + return -1 + end, + }, schedule = function(callback) callback() end, } end return mock