From b14fadee45f922a832d28f7854de5c296d27eaf0 Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Fri, 13 Jan 2023 16:18:06 +0000 Subject: [PATCH] Added builtins/__modules.lua Script defines all tools available by presser.nvim, which are lazy-loaded and executed when called by the user. Function presser@find_replace has undergone major refactorisation: - Keymaps have been updated to respect new codebase structure. - A new OOP implementation is being experimented for constructing windows, giving future flexibility. Consult presser/steamers.lua. - Function now calls the class@streamers and invokes the streamers@new() method. This constructs the windows/buffers which was originally done via call to presser@new(). - This function is NOT directly accessed, but via proxy table@builtins which lazy-loads function. Any new tools which are created for presser are to be defined in this file and lazy-loaded. Module may be renamed in future for theming consistency. --- presser/builtins/__modules.lua | 83 ++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 presser/builtins/__modules.lua diff --git a/presser/builtins/__modules.lua b/presser/builtins/__modules.lua new file mode 100644 index 0000000..b02fa9e --- /dev/null +++ b/presser/builtins/__modules.lua @@ -0,0 +1,83 @@ +local gcm = require'presser.context_manager' +local steamers = require'presser.steamers' +local actions = require'presser.actions' + +local a = vim.api + +local modules = {} + +-- @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. +modules.find_replace = function () + -- define the context which these windows will belong to in the context manager. + local ctx = "find_replace" + gcm.create( ctx ) + + local keymap = { + n = { + [""] = "lua require'presser.actions'.close()", + [""] = "lua require'presser.actions'.execute()", + }, + i = { + [""] = "lua require'presser.actions'.move_next_buffer()", + [""] = "lua require'presser.actions'.execute()", + } + } + + -- get the centre of the current buffer + local c_cols = math.floor( a.nvim_win_get_width(0) / 2 ) + local c_lines = math.floor( a.nvim_win_get_height(0) / 2 ) + + -- create a title buffer for the steamer + steamers + .new( ctx, { + label = "presser_title", + steamer = { + placeholder = "Find & Replace", + allowed = false, + window = { + line = c_lines - 3, + border = false, + minwidth = 82, + padding = { 0, 1, 1, 1 } + } + } + } ) + + -- create a new buffer for find pattern + steamers + .new ( ctx, { + label = "find_buf", + steamer = { + window = { + line = c_lines, + title = "Find", + }, + keybinds = keymap, + } + } ) + + -- create a new buffer for replace pattern + steamers + .new( ctx, { + label = "replace_buf", + steamer = { + window = { + line = c_lines + 3, + title = "Replace", + }, + keybinds = keymap, + } + } ) + + local result = actions.buf_put_curser_at( "find_buf" ) + a.nvim_feedkeys('A', 'n', false) +end + +return modules