Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F30987
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lua/ivy/controller.lua b/lua/ivy/controller.lua
index b1dd66f..f6dba23 100644
--- a/lua/ivy/controller.lua
+++ b/lua/ivy/controller.lua
@@ -1,63 +1,67 @@
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 { "-- Loading ---" }
vim.api.nvim_buf_set_name(window.get_buffer(), name)
controller.input ""
end
controller.input = function(char)
prompt.input(char)
vim.schedule(function()
window.set_items(controller.items(prompt.text()))
end)
end
controller.search = function(value)
prompt.set(value)
vim.schedule(function()
window.set_items(controller.items(prompt.text()))
end)
end
controller.complete = function()
controller.checkpoint()
controller.destroy()
end
controller.checkpoint = function()
- vim.api.nvim_set_current_win(window.previous)
+ 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()
window.index = window.index + 1
window.update()
end
controller.previous = function()
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/window.lua b/lua/ivy/window.lua
index 0a33f65..7b14308 100644
--- a/lua/ivy/window.lua
+++ b/lua/ivy/window.lua
@@ -1,142 +1,142 @@
-- Constent options that will be used for the keymaps
local opts = { noremap = true, silent = true, nowait = true }
-- All of the base chars that will be used for an "input" operation on the
-- prompt
-- stylua: ignore
local chars = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "<", ">", "`", "@", "#", "~", "!",
"\"", "$", "%", "^", "&", "/", "(", ")", "=", "+", "*", "-", "_", ".", ",", ";", ":", "?", "\\", "|", "'", "{", "}",
"[", "]", " ",
}
local function string_to_table(lines)
local items = {}
for line in lines:gmatch "[^\r\n]+" do
table.insert(items, line)
end
return items
end
local function set_items_string(buffer, lines)
vim.api.nvim_buf_set_lines(buffer, 0, 9999, false, string_to_table(lines))
end
local function set_items_array(buffer, lines)
if type(lines[1]) == "string" then
vim.api.nvim_buf_set_lines(buffer, 0, 9999, false, lines)
else
for i = 1, #lines do
vim.api.nvim_buf_set_lines(buffer, i - 1, 9999, false, { lines[i][2] })
end
end
end
local window = {}
window.index = 0
-window.previous = nil
+window.origin = nil
window.window = nil
window.buffer = nil
window.initialize = function()
window.make_buffer()
end
window.make_buffer = function()
- window.previous = vim.api.nvim_get_current_win()
+ window.origin = vim.api.nvim_get_current_win()
vim.api.nvim_command "botright split new"
window.buffer = vim.api.nvim_win_get_buf(0)
window.window = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_option(window.window, "number", false)
vim.api.nvim_win_set_option(window.window, "relativenumber", false)
vim.api.nvim_win_set_option(window.window, "signcolumn", "no")
vim.api.nvim_buf_set_option(window.buffer, "filetype", "ivy")
vim.api.nvim_buf_set_var(window.buffer, "bufftype", "nofile")
for index = 1, #chars do
local char = chars[index]
if char == "'" then
char = "\\'"
end
if char == "\\" then
char = "\\\\\\\\"
end
vim.api.nvim_buf_set_keymap(window.buffer, "n", chars[index], "<cmd>lua vim.ivy.input('" .. char .. "')<CR>", opts)
end
vim.api.nvim_buf_set_keymap(window.buffer, "n", "<C-c>", "<cmd>lua vim.ivy.destroy()<CR>", opts)
vim.api.nvim_buf_set_keymap(window.buffer, "n", "<C-w>", "<cmd>lua vim.ivy.search('')<CR>", opts)
vim.api.nvim_buf_set_keymap(window.buffer, "n", "<C-n>", "<cmd>lua vim.ivy.next()<CR>", opts)
vim.api.nvim_buf_set_keymap(window.buffer, "n", "<C-p>", "<cmd>lua vim.ivy.previous()<CR>", opts)
vim.api.nvim_buf_set_keymap(window.buffer, "n", "<C-M-n>", "<cmd>lua vim.ivy.next(); vim.ivy.checkpoint()<CR>", opts)
vim.api.nvim_buf_set_keymap(
window.buffer,
"n",
"<C-M-p>",
"<cmd>lua vim.ivy.previous(); vim.ivy.checkpoint()<CR>",
opts
)
vim.api.nvim_buf_set_keymap(window.buffer, "n", "<CR>", "<cmd>lua vim.ivy.complete()<CR>", opts)
vim.api.nvim_buf_set_keymap(window.buffer, "n", "<BS>", "<cmd>lua vim.ivy.input('BACKSPACE')<CR>", opts)
end
window.get_current_selection = function()
local line = vim.api.nvim_buf_get_lines(window.buffer, window.index, window.index + 1, true)
if line == nil then
line = { "" }
end
return line[1]
end
window.get_buffer = function()
if window.buffer == nil then
window.make_buffer()
end
return window.buffer
end
window.update = function()
-- TODO(ade): Add a guard in so we can not go out of range on the results buffer
vim.api.nvim_win_set_cursor(window.window, { window.index + 1, 0 })
end
window.set_items = function(items)
if #items == 0 then
vim.api.nvim_buf_set_lines(window.get_buffer(), 0, 9999, false, { "-- No Items --" })
elseif type(items) == "string" then
set_items_string(window.get_buffer(), items)
elseif type(items) == "table" then
set_items_array(window.get_buffer(), items)
end
local line_count = vim.api.nvim_buf_line_count(window.buffer)
window.index = line_count - 1
if line_count > 10 then
line_count = 10
end
vim.api.nvim_win_set_height(window.window, line_count)
window.update()
end
window.destroy = function()
if type(window.buffer) == "number" then
vim.api.nvim_buf_delete(window.buffer, { force = true })
end
window.buffer = nil
window.window = nil
- window.previous = nil
+ window.origin = nil
window.index = 0
end
return window
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Sep 10, 5:13 PM (7 h, 36 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4497
Default Alt Text
(6 KB)
Attached To
Mode
R1 ivy.nvim
Attached
Detach File
Event Timeline
Log In to Comment