Rewrite entire plugin management system

- This encapsulates all plugin related functionalities into the plugins/
  folder (and pluginconfig/) instead of relying on utility file.
- It also renames utility/plugins.lua to a more sensible
  plugins/packer.lua as it actually only applies for packer plugin
  manager.
- The plugins.lua file is now split into 2 files, first holding the list
  of all plugins plugins/plugin_list.lua and second one holding the
  logic of bootstrapping and running startup on packer.
This commit is contained in:
ItsDrike 2021-12-07 22:17:56 +01:00
parent b2214eb7af
commit 8350dc7926
No known key found for this signature in database
GPG key ID: FB8CA11A2CF3A843
5 changed files with 212 additions and 191 deletions

View file

@ -1,69 +0,0 @@
local plugins = require("utility.plugins")
local vim = require("vim")
local cmd = vim.cmd
local fn = vim.fn
-- Automatically run :PackerCompile if plugins.lua is updated
cmd[[
augroup packer_user_config
autocmd!
autocmd BufWritePost ~/.config/nvim/lua/plugins.lua source <afile> | PackerCompile
augroup end
]]
-- Extend plugins.get_plugin_file function and automatically pass a plugin_directory
-- into it. Expects a relative path to the plugin file settings from this directory
local function plug_cfg(plugin_file)
local plugin_directory = fn.stdpath("config") .. "lua/pluginconf"
plugins.get_plugin_file(plugin_file, plugin_directory)
end
-- Define packer plugins
local plugin_configs = {
{ "airblade/vim-gitgutter" },
{ "dhruvasagar/vim-table-mode" },
{ "tmhedberg/SimpylFold" },
{ "wakatime/vim-wakatime" },
{ "mhinz/vim-startify" },
{ "ryanoasis/vim-devicons" },
{ "vimwiki/vimwiki", config = plug_cfg("vimwiki.lua") },
{ "sheerun/vim-polyglot", setup = plug_cfg("polyglot.lua") },
{ "tpope/vim-commentary", config = plug_cfg("commentary.lua") },
{ "junegunn/fzf", run = function() fn['fzf#install']() end },
{ "tomasiser/vim-code-dark", config = plug_cfg("vim-code-dark.lua") },
{
"vim-airline/vim-airline",
config = plug_cfg("airline.lua"),
requires = { "vim-airline/vim-airline-themes", opt = true },
},
{
"preservim/nerdtree",
config = plug_cfg("nerdtree.lua"),
requires = {
{ "Xuyuanp/nerdtree-git-plugin", opt = true },
{ "tiagofumo/vim-nerdtree-syntax-highlight", opt = true },
},
},
{
"mfussenegger/nvim-dap",
config = plug_cfg("nvim-dap.lua"),
requires = { "mfussenegger/nvim-dap-python", opt = true },
},
{
"junegunn/fzf.vim",
config = plug_cfg("fzf.lua"),
after = "fzf",
requires = { "stsewd/fzf-checkout.vim", opt = true },
},
{
"neoclide/coc.nvim",
branch = "release",
config = plug_cfg("coc.vim"),
requires = { "antoinemadec/coc-fzf", opt = true },
},
}
-- Set up packer and use given plugins
plugins.packer_setup(plugin_configs)

View file

@ -0,0 +1,36 @@
local vim = require("vim")
local packer_m = require("plugins.packer")
-- Require packer_compiled to lazy-load all of the plugins and their settings
-- automatically. If this fails, it means we probably didn't yet compile
-- packer. This file is generated upon running :PackerCompile, so if we didn't
-- find it, we inform the user to run it. We can't run it here directly,
-- because packer may not yet be installed, bootstrapping happens only after
-- this to allow this lazy loadning behavior. If we required packer before
-- this, the lazy-loading would have no effect.
local packer_compiled_ok, _ = pcall(require, "compiled.packer_compiled")
if not packer_compiled_ok then
vim.notify(
"Run :PackerCompile or :PackerSync",
vim.log.levels.WARN,
{ title = "Notification" }
)
end
-- If packer isn't present, install it automatically it
local present, packer = pcall(require, "packer")
local first_install = false
if not present then
first_install = packer_m.bootstrap_packer()
if first_install then
-- We know this will work now that packer was bootstrapped
-- Otherwise we'd receive false in first_install
---@diagnostic disable-next-line:different-requires
packer = require("packer")
end
end
-- Obtain the plugins defined in plugin_list.ua
local plugin_list = require("plugins.plugin_list")
packer_m.startup(packer, plugin_list, first_install)

View file

@ -0,0 +1,114 @@
local vim = require("vim")
local cmd = vim.cmd
local fn = vim.fn
local M = {}
-- Define some paths used in the functions
M.packer_install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
M.packer_compile_path = fn.stdpath("config") .. "/lua/compiled/packer_compiled.lua"
-- Define basic default settings for packer
M.packer_settings = {
display = {
open_fn = function()
return require('packer.util').float({ border = "rounded" })
end,
prompt_border = "rounded",
},
git = {
-- Timeout for git clones in seconds
clone_timeout = 600,
},
profile = {
enable = true,
-- The time that a pluign's load time must surpass for it to be included
-- in the profile (in miliseconds)
threshold = 1,
},
compile_path = M.packer_compile_path,
auto_clean = true,
compile_on_sync = true
}
-- Define default plugins which should always be used
M.default_plugin_list = {
-- Let packer manager itself, so that it gets updates
{ "wbthomason/packer.nvim" },
-- Add plugin for speeding up `require` in lua by caching
--{ "lewis6991/impatient.nvim" },
-- Replaces default filetype.vim sourced on startup, which includes
-- 800+ autocommands setting the filetype based on the filename. This
-- is very slow and this plugin merges them into single autocommand,
-- which is 175x faster, improving startup time
--{ "nathom/filetype.nvim" },
}
-- Download and load packer plugin manager
function M.bootstrap_packer()
print("Clonning pakcer plugin manager, please wait...")
-- First remove the directory in case it already exists but packer isn't present
fn.delete(M.packer_install_path, "rf")
fn.system({
"git", "clone", "--depth", "1",
"https://github.com/wbthomason/packer.nvim",
M.packer_install_path
})
-- Add packer plugin via nvim's internal plugin system
-- and make sure that we can now require it.
cmd("packadd packer.nvim")
local present, packer = pcall(require, "packer")
if present then
print("Packer clonned successfully.")
return true
else
print("Couldn't clone packer! Packer path: " .. M.packer_install_path .. "\n" .. packer)
return false
end
end
-- Run packer startup with the default config and given plugin settings
-- Expects: `packer`, `plugin_list`, `run_sync`, `settings_override`
function M.startup(packer, plugin_list, run_sync, settings_override)
-- Initialize packer with default settings extended by
-- the given settings override
local settings = M.packer_settings
if settings_override then
settings = vim.tbl_extend("foce", settings, settings_override)
end
packer.reset()
packer.init(settings)
-- Run packer startup and use all given plugins with their settings
-- including the default plugins
local use = packer.use
return packer.startup(function()
-- Use the default plugins (should be first)
for _, plugin_settings in pairs(M.default_plugin_list) do
use(plugin_settings)
end
-- Use the obtained plugins
if plugin_list and not vim.tbl_isempty(plugin_list) then
for _, plugin_settings in pairs(plugin_list) do
use(plugin_settings)
end
end
-- We can also automatically run sync to install all specified plugins
-- immediately, this is useful if we've just bootstrapped packer.
if run_sync then
vim.notify(
"Make sure to restart after packer sync!",
vim.log.levels.WARN,
{ title = "Notification" }
)
packer.sync()
end
end)
end
return M

View file

@ -0,0 +1,62 @@
local vim = require("vim")
local fn = vim.fn
local plugin_directory = fn.stdpath("config") .. "/lua/pluginconf"
-- Return the line (string) to be executed with lua that loads in given plugin file.
-- This is useful for the `config` or `setup` parameters of packer's use to source
-- both `.vim` and `.lua` files.
-- Expects a `plugin_file` which is a relative path from the `plugin_directory` folder.
local function get_plugin_file(plugin_file)
local source_line = string.format('source %s/%s', plugin_directory, plugin_file)
return string.format("vim.fn.execute('%s')", source_line)
end
-- Define packer plugins
-- The individual tables will get passed into the packer's use function
local plugin_list = {
{ "airblade/vim-gitgutter" },
{ "dhruvasagar/vim-table-mode" },
{ "tmhedberg/SimpylFold" },
{ "wakatime/vim-wakatime" },
{ "mhinz/vim-startify" },
{ "ryanoasis/vim-devicons" },
{ "sheerun/vim-polyglot", setup = get_plugin_file("polyglot.lua") },
{ "vimwiki/vimwiki", config = get_plugin_file("vimwiki.lua") },
{ "tpope/vim-commentary", config = get_plugin_file("commentary.lua") },
{ "junegunn/fzf", run = function() fn['fzf#install']() end },
{ "tomasiser/vim-code-dark", config = get_plugin_file("vim-code-dark.lua") },
{
"vim-airline/vim-airline",
config = get_plugin_file("airline.lua"),
requires = { "vim-airline/vim-airline-themes", opt = true },
},
{
"preservim/nerdtree",
config = get_plugin_file("nerdtree.lua"),
requires = {
{ "Xuyuanp/nerdtree-git-plugin", opt = true },
{ "tiagofumo/vim-nerdtree-syntax-highlight", opt = true },
},
},
{
"mfussenegger/nvim-dap",
config = get_plugin_file("nvim-dap.lua"),
requires = { "mfussenegger/nvim-dap-python", opt = true },
},
{
"junegunn/fzf.vim",
config = get_plugin_file("fzf.lua"),
after = "fzf",
requires = { "stsewd/fzf-checkout.vim", opt = true },
},
{
"neoclide/coc.nvim",
branch = "release",
config = get_plugin_file("coc.vim"),
requires = { "antoinemadec/coc-fzf", opt = true },
},
}
return plugin_list

View file

@ -1,122 +0,0 @@
local vim = require("vim")
local cmd = vim.cmd
local api = vim.api
local fn = vim.fn
-- This module contains several functions regarding packer plugin manager
-- Most notably the get_plugin_file and packer_setup fucntions
local M = {}
-- Drectory containing the individual plugin configuration files.
-- File to hold the compiled packer binary
M.packer_compiled_path = fn.stdpath("config") .. "plugin/packer_compiled.lua"
-- Return the line (string) to be executed with lua that loads in given plugin file.
-- This is useful for the `config` or `setup` parameters of packer's use to either
-- source `.vim` files, or require `.lua` files.
-- Expects a `plugin_file` which is a relative path from the `plugin_directory` folder.
function M.get_plugin_file(plugin_file, plugin_directory)
local filename, extension = plugin_file:match("^(.+)(%..+)$")
if (extension == ".vim") then
local source_line = string.format('source "%s/%s"', plugin_directory, plugin_file)
return string.format("vim.fn.execute('%s')", source_line)
else
return string.format('require("%s/%s")', plugin_directory, filename)
end
end
-- Download packer plugin manager in case it isn't already installed
function M.packer_bootstrap()
local first_install = false
local present, packer = pcall(require, "packer")
if not present then
local packer_install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
print("Clonning pakcer plugin manager, please wait...")
-- First remove the directory in case it already exists but packer isn't present
fn.delete(packer_install_path, "rf")
fn.system({
"git", "clone", "--depth", "1",
"https://github.com/wbthomason/packer.nvim",
packer_install_path
})
-- Make sure packer was installed properly
cmd("packadd packer.nvim")
present, packer = pcall(require, "packer")
if present then
print("Packer clonned successfully.")
first_install = true
else
print("Couldn't clone packer! Packer path: " .. packer_install_path .. "\n" .. packer)
end
end
return { present = present, first_install = first_install, packer = packer }
end
-- Initialize packer with our desired configuration
-- If packer isn't instaleld, this also performs the installation
function M.packer_init()
local details = M.packer_bootstrap()
-- Only continue if we actually managed to install packer
if not details.present then
return details
end
details.packer.init({
display = {
open_fn = function()
return require('packer.util').float({ border = "rounded" })
end,
prompt_border = "rounded",
},
git = {
-- Timeout for git clones in seconds
clone_timeout = 600,
},
profile = {
enable = true,
-- The time that a pluign's load time must surpass for it to be included
-- in the profile (in miliseconds)
threshold = 1,
},
compile_path = M.packer_compiled_path,
})
return details
end
-- Run packer's setup function and define all of the used plugins
-- Expects table/list of tables holding the individual plugin settings
function M.packer_setup(plugin_configs)
local details = M.packer_init()
if not details.present then
return false
end
local packer = details.packer
local use = packer.use
local first_install = details.first_install
-- Make sure to add packer here, even if it's opt
api.nvim_command("packadd packer.nvim")
return packer.startup(function()
-- We always want to let packer manage itself
use("wbthomason/packer.nvim")
-- Load the obtained plugins (in any)
if plugin_configs and not vim.tbl_isempty(plugin_configs) then
for _, plugin in pairs(plugin_configs) do
use(plugin)
end
end
if first_install then
packer.sync()
end
end)
end
return M