Faster Neovim Plugin Development with plenary.nvim
Overview
In this article, let’s walk through plenary.nvim
which is a Neovim Lua library that provides essential Lua functions we need to develop Neovim plugins.
Popup
A popup menu can be easily created using the library.
Default Popup
local popup = require "plenary.popup"local function create_default_popup()
local win_id = popup.create({ "menu item 1", "menu item 2", "menu item 3" }, {})
print(win_id)
endcreate_default_popup()
Custom Popup
I can also specify the line, column, minimum width, border, and highlight.
vim.cmd [[highlight PopupColor ctermbg=black ctermfg=blue guifg=blue guibg=green]]local function create_highlight_popup()
local win_id = popup.create({ "item 1", "item 2", "item 3" }, {
line = 15,
col = 45,
minwidth = 20,
border = true,
highlight = "PopupColor",
})
print(win_id)
endcreate_highlight_popup()
Custom Border Popup
This creates a popup with a custom border.
local function create_border_popup(borderchars)
local _, config = popup.create("popup with custom border", {
line = 8,
col = 55,
padding = { 0, 3, 0, 3 },
borderchars = borderchars,
}) local border_id = config.border.win_id
local border_bufnr = vim.api.nvim_win_get_buf(border_id)
print(border_id)
print(border_bufnr)
endcreate_border_popup { "+" }
Check out the code snippet for more examples.
Job
Job module allows us to run system processes or commands easily.
Sync Job
local Job = require "plenary.job"local function sync_job()
local job = Job:new {
command = "ls",
args = { "-l" },
} job:sync()
print(vim.inspect.inspect(job:result()))
end