Rewrite plugin management configuration

This commit is contained in:
ItsDrike 2021-12-06 22:28:32 +01:00
parent 61f619566a
commit 950d19e1a0
No known key found for this signature in database
GPG key ID: FB8CA11A2CF3A843
2 changed files with 174 additions and 107 deletions

View file

@ -1,22 +1,8 @@
local plugins = require("utility.plugins")
local vim = require("vim") local vim = require("vim")
local cmd = vim.cmd local cmd = vim.cmd
local api = vim.api
local fn = vim.fn local fn = vim.fn
-- Automatically download (bootstrap) packer plugin manager
-- if it's not already installed
local packer_install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
local packer_bootstrap
if fn.empty(fn.glob(packer_install_path)) > 0 then
print("Installing packer plugin manager, please wait...")
packer_bootstrap = fn.system({
'git', 'clone', '--depth', '1',
'https://github.com/wbthomason/packer.nvim',
packer_install_path
})
print("Packer installed, reload vim to install plugins")
end
-- Automatically run :PackerCompile if plugins.lua is updated -- Automatically run :PackerCompile if plugins.lua is updated
cmd[[ cmd[[
augroup packer_user_config augroup packer_user_config
@ -25,100 +11,59 @@ augroup packer_user_config
augroup end augroup end
]] ]]
-- Returns the line to be executed after plugin is loaded, this
-- is useful for the `config` parameter of packer's use to -- Extend plugins.get_plugin_file function and automatically pass a plugin_directory
-- source `.vim` files or require `.lua` files -- into it. Expects a relative path to the plugin file settings from this directory
-- Expects a file path from pluginconf/ folder local function plug_cfg(plugin_file)
local function get_plugin_file(pluginconf_file) local plugin_directory = fn.stdpath("config") .. "lua/pluginconf"
local filename, extension = pluginconf_file:match("^(.+)(%..+)$") plugins.get_plugin_file(plugin_file, plugin_directory)
if (extension == ".vim") then
-- Source wants absolute path
local pluginconf_path = fn.stdpath("config") .. "lua/pluginconf"
local source_line = string.format('source "%s/%s"', pluginconf_path, pluginconf_file)
return string.format("vim.fn.execute('%s')", source_line)
else
-- Require wants relative path from lua/
local pluginconf_path = "pluginconf"
return string.format('require("%s/%s")', pluginconf_path, filename)
end
end end
-- Make sure to add packer here, even if it's opt
api.nvim_command("packadd packer.nvim")
-- Define packer plugins -- Define packer plugins
return require("packer").startup({ local plugin_configs = {
function(use) { "airblade/vim-gitgutter" },
use("wbthomason/packer.nvim") { "dhruvasagar/vim-table-mode" },
use('airblade/vim-gitgutter') { "tmhedberg/SimpylFold" },
use('dhruvasagar/vim-table-mode') { "wakatime/vim-wakatime" },
use('tmhedberg/SimpylFold') { "mhinz/vim-startify" },
use('wakatime/vim-wakatime') { "ryanoasis/vim-devicons" },
use('mhinz/vim-startify') { "vimwiki/vimwiki", config = plug_cfg("vimwiki.lua") },
use('ryanoasis/vim-devicons') { "sheerun/vim-polyglot", setup = plug_cfg("polyglot.lua") },
use({ { "tpope/vim-commentary", config = plug_cfg("commentary.lua") },
"tomasiser/vim-code-dark", { "junegunn/fzf", run = function() fn['fzf#install']() end },
config = get_plugin_file("vim-code-dark.lua"), { "tomasiser/vim-code-dark", config = plug_cfg("vim-code-dark.lua") },
}) {
use({
"vim-airline/vim-airline", "vim-airline/vim-airline",
config = get_plugin_file("airline.lua"), config = plug_cfg("airline.lua"),
requires = { "vim-airline/vim-airline-themes", opt = true }, requires = { "vim-airline/vim-airline-themes", opt = true },
}) },
use({ {
"preservim/nerdtree", "preservim/nerdtree",
config = get_plugin_file("nerdtree.lua"), config = plug_cfg("nerdtree.lua"),
requires = { requires = {
{ "Xuyuanp/nerdtree-git-plugin", opt = true }, { "Xuyuanp/nerdtree-git-plugin", opt = true },
{ "tiagofumo/vim-nerdtree-syntax-highlight", opt = true }, { "tiagofumo/vim-nerdtree-syntax-highlight", opt = true },
}, },
}) },
use({ {
"vimwiki/vimwiki",
config = get_plugin_file("vimwiki.lua")
})
use({
"sheerun/vim-polyglot",
setup = get_plugin_file("polyglot.lua")
})
use({
"tpope/vim-commentary",
config = get_plugin_file("commentary.lua")
})
use({
"mfussenegger/nvim-dap", "mfussenegger/nvim-dap",
config = get_plugin_file("nvim-dap.lua"), config = plug_cfg("nvim-dap.lua"),
requires = { "mfussenegger/nvim-dap-python", opt = true }, requires = { "mfussenegger/nvim-dap-python", opt = true },
}) },
use({ {
"junegunn/fzf",
run = function() fn['fzf#install']() end,
})
use({
"junegunn/fzf.vim", "junegunn/fzf.vim",
config = get_plugin_file("fzf.lua"), config = plug_cfg("fzf.lua"),
after = "fzf", after = "fzf",
requires = { "stsewd/fzf-checkout.vim", opt = true }, requires = { "stsewd/fzf-checkout.vim", opt = true },
}) },
use({ {
"neoclide/coc.nvim", "neoclide/coc.nvim",
branch = "release", branch = "release",
config = get_plugin_file("coc.vim"), config = plug_cfg("coc.vim"),
requires = { "antoinemadec/coc-fzf", opt = true }, requires = { "antoinemadec/coc-fzf", opt = true },
}) },
}
-- Run sync if we've just bootstrapped packer -- Set up packer and use given plugins
if packer_bootstrap then plugins.packer_setup(plugin_configs)
require("packer").sync()
end
end,
config = {
display = {
open_fn = require('packer.util').float,
},
profile = {
enable = true,
threshold = 1, -- the amount in ms that a plugins load time must be over for it to be included in the profile
},
}
})

View file

@ -0,0 +1,122 @@
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