From 657d3f2ae6efe3cf110d53170232978bc13210bc Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Fri, 13 Jan 2023 16:29:21 +0000 Subject: [PATCH] Added streamer.lua Steamers are akin to that of Telescope's Pickers. A "steamer" is the class structure used by builtins to create new windows/buffers for the user to interact with. This code was simply migrated across from presser@new and refactored to adopt a class-like OOP paradigm. steamers@new is a wrapper function accessed by builtins to invoke the class constructor Steamers@new. Allows to greater control and flexibility of window/buffer options. --- presser/steamers.lua | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 presser/steamers.lua diff --git a/presser/steamers.lua b/presser/steamers.lua new file mode 100644 index 0000000..e35a93e --- /dev/null +++ b/presser/steamers.lua @@ -0,0 +1,78 @@ +local popup = require'plenary.popup' +local gcm = require'presser.context_manager' + +local a = vim.api + +local steamers = {} + +local Steamer = {} +Steamer.__index = Steamer + +-- @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. +-- +-- @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. +-- However, it's used by all internal built-ins which are meant to be used by users. Consider a class constructor in +-- C++, this function is akin to that behaviour and is responsible for constructing the window/buffer and ensuring it +-- can be tracked by Vim itself. +function Steamer:new ( _ctx, label, opts ) + local opts = opts or {} + if not opts.placeholder then + opts["placeholder"] = "" + end + + local buf_opts = { + minwidth = 80, -- getwin_w() / 2 - ((getwin_w() % 2) / 2), + borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }, + } + + if opts.window then + for k,v in pairs( opts.window ) do + buf_opts[k] = v + end + end + + print("building window: ", opts.placeholder, buf_opts ) + local obj = popup.create( opts.placeholder, buf_opts ) + gcm.update( _ctx, { id = obj, what = label, allowed = opts.allowed } ) + -- :TODO: call update function in context manager +-- 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 a.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 = a.nvim_replace_termcodes(key_bind, true, false, true), + a.nvim_buf_set_keymap(buf_id(), mode, key_bind, key_action, + { noremap = true, silent = true } + ) + end + end + end +end + +steamers.new = function ( ctx, opts ) + -- error if the provided option is not type string + assert( type(opts.label) == "string", "Should be string, found type: " .. type(opts.label) ) + + -- @Dev: function can provide greater wrapper options in future. + + return Steamer:new( ctx, opts.label, opts.steamer ) +end + +steamers._Steamer = Steamer + +return steamers