ivy.nvim/lua/ivy/controller.lua
Ade Attwood a712c929cb feat: make the completion candidates type more consistent
The API for `window.set_items` took to many variable types. It would
take a table in multiple different formats and a string. Now it will
only take a table in a single format and a string. It will convert the
string into the table format by splitting it on new lines.

The table format is an array of tables that must have a `content` key
that will be the text that is displayed in the completion window. The
table can have any other data that is ignored.

```lua
local items = {
  { content = "Item one" },
  { content = "Item two" }
}
```

The `set_items` function will only display the `content` key in the
completion window, it will not do any sorting or filtering, that must be
done before passing the data to the `set_items` function.
2022-09-02 21:03:56 +01:00

81 lines
1.6 KiB
Lua

local window = require "ivy.window"
local prompt = require "ivy.prompt"
local controller = {}
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 .. ")]'")
end)
end
controller.complete = function()
controller.checkpoint()
controller.destroy()
end
controller.checkpoint = function()
vim.api.nvim_set_current_win(window.origin)
controller.callback(window.get_current_selection())
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