Updated init.lua
Script has been refactor and now easily allows for the creation of new windows/buffers upon function call. Tracking windows/buffers previously was invalid and lead to visual glitches of buffers. Windows/buffers now belong to a context manager found within Vim's global scope, due to ease of accessibility. Created function `new()` which is responsible for constructing new windows/buffers, updating the global context manager of newly added windows/buffers, and allowing for customisation given by plenary.popup.
This commit is contained in:
parent
9472a7b969
commit
5f6839ca43
194
init.lua
194
init.lua
|
|
@ -1,100 +1,130 @@
|
||||||
local plenary = require'plenary'
|
local popup = require'plenary.popup'
|
||||||
local popup = plenary.popup
|
local utils = require'presser.utils'
|
||||||
|
|
||||||
local presser = {}
|
|
||||||
windows = {}
|
|
||||||
|
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
|
|
||||||
local function _iter_table_dump( arr )
|
local presser = {} -- list of functions to be exported
|
||||||
for k, v in pairs(arr) do
|
local g = vim.g
|
||||||
print("Table:", k, v)
|
|
||||||
end
|
g.presser_buf_ctx = {} -- setup a new global context manager for windows/buffers
|
||||||
end
|
|
||||||
|
-- :@Dev: close all Presser windows that may be open for all contexts
|
||||||
local escape_chars = function ( text )
|
presser.close = function ()
|
||||||
return string.gsub(text, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", {
|
for _type, ctx in pairs( g.presser_buf_ctx ) do
|
||||||
["\\"] = "\\\\",
|
for _, win_id in pairs( ctx ) do
|
||||||
["-"] = "\\-",
|
api.nvim_win_close( win_id, true ) -- buffer contents are irrelevant in this context to save
|
||||||
["("] = "\\(",
|
end
|
||||||
[")"] = "\\)",
|
end
|
||||||
["["] = "\\[",
|
|
||||||
["]"] = "\\]",
|
g.presser_buf_ctx = {} -- clear the global context manager
|
||||||
["{"] = "\\{",
|
end
|
||||||
["}"] = "\\}",
|
|
||||||
["?"] = "\\?",
|
-- @Description: Construct a new instance of a window and buffer using the 'plenary.popup' module.
|
||||||
["+"] = "\\+",
|
-- @Params:
|
||||||
["*"] = "\\*",
|
-- + `_type` ~ string denoting which context manager is owner of the window.
|
||||||
["^"] = "\\^",
|
-- + `placeholder` ~ string allowing for placeholder text to be placed within the buffer when constructed.
|
||||||
["$"] = "\\$",
|
-- + `opts` ~ a table of additional options to be provided when constructing the window.
|
||||||
["."] = "\\.",
|
-- @Returns: nil. Constructs a new window and stores the window's ID in a global table.
|
||||||
})
|
--
|
||||||
end
|
-- @Dev: This function is to be as generic for creating a window whilst allowing for as much customisation over the
|
||||||
|
-- window/buffer which are to be constructed. Users are to access this function so is not part of the module export.
|
||||||
-- :@dev: clean the text once it has been fetched from buffer
|
-- However, it's used by all internal built-ins which are meant to be used by users. Consider a class constructor in
|
||||||
local function _clean_buf ( text )
|
-- C++, this function is akin to that behaviour and is responsible for constructing the window/buffer and ensuring it
|
||||||
if not type(text) == "string" or text == nil then
|
-- can be tracked by Vim itself.
|
||||||
print("[DEBUG - _clean_buf] Given input is not of type string.")
|
local new = function ( _type, placeholder, opts )
|
||||||
return
|
-- create new manager for handling windows in the
|
||||||
end
|
local manager = g.presser_buf_ctx
|
||||||
|
-- create specific context manager if it doesn't exist
|
||||||
return escape_chars( text )
|
if not manager[_type] then
|
||||||
:match( "^%s*(.-)%s*$" )
|
manager[_type] = {}
|
||||||
end
|
g.presser_buf_ctx = manager
|
||||||
|
end
|
||||||
function presser.destroy_windows()
|
|
||||||
-- api.nvim_win_close(0, true)
|
local opts = opts or {}
|
||||||
|
local placeholder = placeholder or ""
|
||||||
for k,v in pairs(windows) do
|
|
||||||
-- api.nvim_win_close(v, true)
|
local buf_opts = {
|
||||||
print(k, v)
|
minwidth = 80, -- getwin_w() / 2 - ((getwin_w() % 2) / 2),
|
||||||
table.remove(windows, k)
|
borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.window then
|
||||||
|
for k,v in pairs( opts.window ) do
|
||||||
|
buf_opts[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local obj = popup.create( placeholder, buf_opts )
|
||||||
|
table.insert( manager[_type], obj )
|
||||||
|
g.presser_buf_ctx = manager
|
||||||
|
|
||||||
|
-- :@Dev: handle for keybindings (TODO: any future stuff below here once window is made)
|
||||||
|
local buf_id = function ()
|
||||||
|
return api.nvim_win_get_buf(obj)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- handle for key bindings
|
||||||
|
if opts.keybinds then
|
||||||
|
for mode, mode_map in pairs( opts.keybinds ) do
|
||||||
|
mode = string.lower(mode)
|
||||||
|
|
||||||
|
for key_bind, key_action in pairs(mode_map) do
|
||||||
|
local key_bind = api.nvim_replace_termcodes(key_bind, true, false, true),
|
||||||
|
api.nvim_buf_set_keymap(buf_id(), mode, key_bind, key_action,
|
||||||
|
{ noremap = true, silent = true }
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- @Description: Find and replace words within the current buffer.
|
||||||
|
-- @Params:
|
||||||
|
-- @Returns: nil.
|
||||||
|
--
|
||||||
|
-- @Dev: function is responsible to creating all required buffers to allow full user interaction.
|
||||||
|
--
|
||||||
|
-- @Future: implementation may allow for greater user customisation similar to what's found with
|
||||||
|
-- extensions such as Telescope. For now, it should provide a concrete UI for purpose of design.
|
||||||
function presser.find_replace()
|
function presser.find_replace()
|
||||||
local padding = 128
|
-- define the context which these windows will belong to in the context manager.
|
||||||
local buf_width = api.nvim_win_get_width(0) - padding
|
local ctx = "find_replace"
|
||||||
|
|
||||||
|
local keymap = {
|
||||||
|
n = {
|
||||||
|
["<esc>"] = "<cmd>lua require'presser'.close()<CR>",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
-- options for the title buffer of the built-in
|
||||||
local opts = {
|
local opts = {
|
||||||
minwidth = buf_width,
|
window = {
|
||||||
borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
|
line = 12,
|
||||||
|
border = false,
|
||||||
|
minwidth = 82,
|
||||||
|
padding = { 0, 1, 1, 1 }
|
||||||
|
},
|
||||||
|
keybinds = keymap,
|
||||||
}
|
}
|
||||||
|
local tab_title = new( ctx, "Find & Replace", opts )
|
||||||
|
|
||||||
local win_title_opts = {
|
-- options for the replace buffer
|
||||||
height = 3,
|
local opts = {
|
||||||
minwidth = buf_width + 2,
|
window = {
|
||||||
line = 9,
|
line = 18,
|
||||||
padding = { 1, 1, 1, 1 },
|
title = "Replace",
|
||||||
|
},
|
||||||
|
keybinds = keymap,
|
||||||
}
|
}
|
||||||
|
local replace = new( ctx, nil, opts )
|
||||||
|
|
||||||
if not placeholder == nil then
|
-- modify options for find buffer
|
||||||
vim.tbl_extend( "force", opts, { padding = { 0, buf_padding, 0, } } )
|
opts["window"]["line"] = 15
|
||||||
end
|
opts["window"]["title"] = "Find"
|
||||||
|
local find = new ( ctx, nil, opts )
|
||||||
|
|
||||||
local win_title = popup.create( { "Find & Replace" }, win_title_opts )
|
api.nvim_feedkeys('A', 'n', false)
|
||||||
local win_b = popup.create({ "" }, vim.tbl_extend("force", opts, { title = "Replace", line = 16 }))
|
|
||||||
local win_t = popup.create({ "" }, vim.tbl_extend("force", opts, { title = "Find", line = 13 }))
|
|
||||||
print(win_t, win_b)
|
|
||||||
|
|
||||||
table.insert(windows, win_t)
|
|
||||||
table.insert(windows, win_b)
|
|
||||||
table.insert(windows, win_title)
|
|
||||||
|
|
||||||
--api.nvim_feedkeys(api.nvim_replace_termcodes("<ESC>A", true, false, true), 'n', true)
|
|
||||||
|
|
||||||
local win_t_buf = api.nvim_win_get_buf(win_t)
|
|
||||||
local win_b_buf = api.nvim_win_get_buf(win_b)
|
|
||||||
-- :lua require('presser').destroy_windows()<CR>
|
|
||||||
api.nvim_buf_set_keymap(win_t_buf, "n", "<ESC>", ":lua require('presser').destroy_windows()<CR>", { noremap = true, silent = true })
|
|
||||||
api.nvim_buf_set_keymap(win_b_buf, "n", "<ESC>", ":close!<CR>", { noremap = true, silent = true })
|
|
||||||
print(win_t_buf)
|
|
||||||
|
|
||||||
local text = api.nvim_buf_get_lines(win_t_buf, 0, -1, false)
|
|
||||||
print(_clean_buf(text[1]))
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
presser.find_replace()
|
|
||||||
|
|
||||||
return presser
|
return presser
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user