|
| 1 | +local M = {} |
| 2 | +--- |
| 3 | +---@class MinPlugin A plugin to download and register on the package path |
| 4 | +---@alias PluginName string The plugin name, will be used as part of the git clone destination |
| 5 | +---@alias PluginUrl string The git url at which a plugin is located, can be a path. See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols for details |
| 6 | +---@alias MinPlugins table<PluginName, PluginUrl> |
| 7 | + |
| 8 | +-- Gets the current directory of this file |
| 9 | +local base_root_path = vim.fn.fnamemodify(debug.getinfo(1, 'S').source:sub(2), ':p:h') |
| 10 | +---Gets the root directory of the minimal init and if path is specified appends the given path to the root allowing for |
| 11 | +---subdirectories within the current cwd |
| 12 | +---@param path string? The additional path to append to the root, not required |
| 13 | +---@return string root The root path suffixed with the path provided or an empty suffix if none was given |
| 14 | +function M.root(path) |
| 15 | + return base_root_path .. '/.deps/' .. (path or '') |
| 16 | +end |
| 17 | + |
| 18 | +---Downloads a plugin from a given url and registers it on the 'runtimepath' |
| 19 | +---@param plugin_name PluginName |
| 20 | +---@param plugin_url PluginUrl |
| 21 | +function M.load_plugin(plugin_name, plugin_url) |
| 22 | + local package_root = M.root('plugins/') |
| 23 | + local install_destination = package_root .. plugin_name |
| 24 | + vim.opt.runtimepath:append(install_destination) |
| 25 | + |
| 26 | + if not vim.loop.fs_stat(package_root) then |
| 27 | + vim.fn.mkdir(package_root, 'p') |
| 28 | + end |
| 29 | + |
| 30 | + -- If the plugin install path already exists, we don't need to clone it again. |
| 31 | + if not vim.loop.fs_stat(install_destination) then |
| 32 | + print(string.format('>> Downloading plugin "%s" to "%s"', plugin_name, install_destination)) |
| 33 | + vim.fn.system({ |
| 34 | + 'git', |
| 35 | + 'clone', |
| 36 | + '--depth=1', |
| 37 | + plugin_url, |
| 38 | + install_destination, |
| 39 | + }) |
| 40 | + if vim.v.shell_error > 0 then |
| 41 | + error( |
| 42 | + string.format('>> Failed to clone plugin: "%s" to "%s"!', plugin_name, install_destination), |
| 43 | + vim.log.levels.ERROR |
| 44 | + ) |
| 45 | + end |
| 46 | + end |
| 47 | +end |
| 48 | + |
| 49 | +---Do the initial setup. Downloads plugins, ensures the minimal init does not pollute the filesystem by keeping |
| 50 | +---everything self contained to the CWD of the minimal init file. Run prior to running tests, reproducing issues, etc. |
| 51 | +---@param plugins? MinPlugins |
| 52 | +function M.setup(plugins) |
| 53 | + vim.opt.packpath = {} -- Empty the package path so we use only the plugins specified |
| 54 | + vim.opt.runtimepath:append(M.root('.min')) -- Ensure the runtime detects the root min dir |
| 55 | + |
| 56 | + -- Install required plugins |
| 57 | + if plugins ~= nil then |
| 58 | + for plugin_name, plugin_url in pairs(plugins) do |
| 59 | + M.load_plugin(plugin_name, plugin_url) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + vim.env.XDG_CONFIG_HOME = M.root('xdg/config') |
| 64 | + vim.env.XDG_DATA_HOME = M.root('xdg/data') |
| 65 | + vim.env.XDG_STATE_HOME = M.root('xdg/state') |
| 66 | + vim.env.XDG_CACHE_HOME = M.root('xdg/cache') |
| 67 | + |
| 68 | + -- NOTE: Cleanup the xdg cache on exit so new runs of the minimal init doesn't share any previous state, e.g. shada |
| 69 | + vim.api.nvim_create_autocmd('VimLeave', { |
| 70 | + callback = function() |
| 71 | + vim.fn.delete(M.root('xdg'), 'rf') |
| 72 | + end, |
| 73 | + }) |
| 74 | +end |
| 75 | + |
| 76 | +M.setup({ |
| 77 | + plenary = 'https://github.com/nvim-lua/plenary.nvim.git', |
| 78 | + treesitter = 'https://github.com/nvim-treesitter/nvim-treesitter', |
| 79 | +}) |
| 80 | +-- WARN: Do all plugin setup, test runs, reproductions, etc. AFTER calling setup with a list of plugins! |
| 81 | +-- Basically, do all that stuff AFTER this line. |
| 82 | + |
| 83 | +--## Set proper settings ## |
| 84 | +-- Register Orgmode on the runtimepath, base_root_path is the directory where this file exists |
| 85 | +vim.opt.runtimepath:prepend(vim.fn.fnamemodify(base_root_path, ':h')) |
| 86 | +vim.opt.termguicolors = true |
| 87 | +vim.opt.swapfile = false |
| 88 | +vim.cmd.language('en_US.utf-8') |
| 89 | +vim.env.TZ = 'Europe/London' |
| 90 | +vim.g.mapleader = ',' |
| 91 | + |
| 92 | +require('orgmode').setup_ts_grammar() |
| 93 | +require('nvim-treesitter.configs').setup({ |
| 94 | + ensure_installed = { 'org' }, |
| 95 | + sync_install = true, |
| 96 | +}) |
| 97 | + |
| 98 | +require('orgmode').setup({ |
| 99 | + org_agenda_files = { base_root_path .. '/plenary/fixtures/*' }, |
| 100 | + org_default_notes_file = base_root_path .. '/plenary/fixtures/refile.org', |
| 101 | +}) |
0 commit comments