2022-07-10 20:07:33 +00:00
|
|
|
local window = require "ivy.window"
|
|
|
|
|
local prompt = require "ivy.prompt"
|
2022-09-07 20:11:49 +00:00
|
|
|
local utils = require "ivy.utils"
|
2022-07-10 20:07:33 +00:00
|
|
|
|
|
|
|
|
local controller = {}
|
|
|
|
|
|
|
|
|
|
controller.items = nil
|
|
|
|
|
controller.callback = nil
|
|
|
|
|
|
2022-07-23 19:14:52 +00:00
|
|
|
controller.run = function(name, items, callback)
|
2022-07-10 20:07:33 +00:00
|
|
|
controller.callback = callback
|
|
|
|
|
controller.items = items
|
|
|
|
|
|
|
|
|
|
window.initialize()
|
2022-09-02 19:48:30 +00:00
|
|
|
|
2022-09-04 15:13:02 +00:00
|
|
|
window.set_items { { content = "-- Loading ---" } }
|
2022-07-23 19:14:52 +00:00
|
|
|
vim.api.nvim_buf_set_name(window.get_buffer(), name)
|
2022-07-23 13:59:51 +00:00
|
|
|
|
2022-07-10 20:07:33 +00:00
|
|
|
controller.input ""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
controller.input = function(char)
|
|
|
|
|
prompt.input(char)
|
2022-07-23 19:53:07 +00:00
|
|
|
controller.update(prompt.text())
|
2022-07-10 20:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
controller.search = function(value)
|
|
|
|
|
prompt.set(value)
|
2022-07-23 19:53:07 +00:00
|
|
|
controller.update(prompt.text())
|
|
|
|
|
end
|
|
|
|
|
|
2023-06-02 16:05:57 +00:00
|
|
|
controller.paste = function()
|
|
|
|
|
controller.search(prompt.text() .. vim.fn.getreg "+p")
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-23 19:53:07 +00:00
|
|
|
controller.update = function(text)
|
2022-07-23 13:59:51 +00:00
|
|
|
vim.schedule(function()
|
2022-07-23 19:53:07 +00:00
|
|
|
window.set_items(controller.items(text))
|
2022-09-04 15:13:02 +00:00
|
|
|
vim.cmd "syntax clear IvyMatch"
|
2022-10-06 06:45:58 +00:00
|
|
|
if #text > 0 then
|
|
|
|
|
-- Escape characters so they do not throw an error when vim tries to use
|
|
|
|
|
-- the "text" as a regex
|
2023-04-25 19:14:52 +00:00
|
|
|
local escaped_text = string.gsub(text, "([-/\\])", "\\%1")
|
2022-10-06 06:45:58 +00:00
|
|
|
vim.cmd("syntax match IvyMatch '[" .. escaped_text .. "]'")
|
|
|
|
|
end
|
2022-07-23 13:59:51 +00:00
|
|
|
end)
|
2022-07-10 20:07:33 +00:00
|
|
|
end
|
|
|
|
|
|
2022-09-07 20:11:49 +00:00
|
|
|
controller.complete = function(action)
|
2022-09-04 14:22:11 +00:00
|
|
|
vim.api.nvim_set_current_win(window.origin)
|
2022-09-07 20:11:49 +00:00
|
|
|
controller.callback(window.get_current_selection(), action)
|
2022-09-04 14:22:11 +00:00
|
|
|
|
2022-07-10 20:07:33 +00:00
|
|
|
controller.destroy()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
controller.checkpoint = function()
|
2022-07-23 19:17:31 +00:00
|
|
|
vim.api.nvim_set_current_win(window.origin)
|
2024-09-03 18:34:17 +00:00
|
|
|
controller.callback(window.get_current_selection(), utils.actions.CHECKPOINT)
|
2022-07-10 20:07:33 +00:00
|
|
|
vim.api.nvim_set_current_win(window.window)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
controller.next = function()
|
2022-07-29 20:02:40 +00:00
|
|
|
local max = vim.api.nvim_buf_line_count(window.buffer) - 1
|
|
|
|
|
if window.index == max then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-10 20:07:33 +00:00
|
|
|
window.index = window.index + 1
|
|
|
|
|
window.update()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
controller.previous = function()
|
2022-07-29 20:02:40 +00:00
|
|
|
if window.index == 0 then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-10 20:07:33 +00:00
|
|
|
window.index = window.index - 1
|
|
|
|
|
window.update()
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-23 19:17:31 +00:00
|
|
|
controller.origin = function()
|
|
|
|
|
return vim.api.nvim_win_get_buf(window.origin)
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-10 20:07:33 +00:00
|
|
|
controller.destroy = function()
|
|
|
|
|
controller.items = nil
|
|
|
|
|
controller.callback = nil
|
|
|
|
|
|
|
|
|
|
window.destroy()
|
|
|
|
|
prompt.destroy()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return controller
|