From 6059927ee649f02fcc819efc213ea78ee9eb2a7d Mon Sep 17 00:00:00 2001 From: Meow Honk <21010072+catgoose@users.noreply.github.com> Date: Fri, 22 Nov 2024 07:48:02 -0600 Subject: [PATCH] chore: use lazy.nvim in minimal.lua (#106) * chore: refactors minimal.lua to use lazy.nvim --- test/expect.txt | 2 +- test/minimal.lua | 146 +++++++++++++++++++++++++++++++---------------- 2 files changed, 98 insertions(+), 50 deletions(-) diff --git a/test/expect.txt b/test/expect.txt index 2d831ac..25af6b6 100644 --- a/test/expect.txt +++ b/test/expect.txt @@ -16,7 +16,7 @@ return { css = true, css_fn = true, mode = "background", - tailwind = false, + tailwind = "both", sass = { enable = false, parsers = { css = true } }, virtualtext = "■", virtualtext_inline = false, diff --git a/test/minimal.lua b/test/minimal.lua index ec23a0a..7a52b0c 100644 --- a/test/minimal.lua +++ b/test/minimal.lua @@ -1,75 +1,123 @@ -- Run this file as `nvim --clean -u minimal.lua` -local base_dir = "colorizer_issue" -local use_remote = false -local local_plugin_dir = os.getenv("HOME") .. "/git/nvim-colorizer.lua" -local function clone_repo_if_missing(name, url, dir) - local install_path = vim.fn.fnamemodify(dir .. "/" .. name, ":p") - if vim.fn.isdirectory(install_path) == 0 then - vim.fn.system({ "git", "clone", "--depth=1", url, install_path }) +-- ADD ANY ADDITIONAL PLUGINS TO `plugins` TABLE IN `define_plugins` FUNCTION + +local settings = { + use_remote = false, -- Use colorizer master or local git directory + base_dir = "colorizer_repro", -- Directory to clone lazy.nvim + local_plugin_dir = os.getenv("HOME") .. "/git/nvim-colorizer.lua", -- Local git directory for colorizer. Used if use_remote is false +} + +if not vim.loop.fs_stat(settings.base_dir) then + vim.fn.system({ + "git", + "clone", + "--filter=blob:none", + "https://github.com/folke/lazy.nvim.git", + "--branch=stable", + settings.base_dir, + }) +end +vim.opt.rtp:prepend(settings.base_dir) + +-- Load options returned from lua file +local function load_options(file_path) + local success, opts = pcall(dofile, file_path) + if not success or type(opts) ~= "table" then + vim.notify("Failed to load options from " .. file_path, vim.log.levels.ERROR) + return nil end - vim.opt.runtimepath:append(install_path) + return opts end -if use_remote then - local remote_plugins = { - colorizer = "https://github.com/NvChad/nvim-colorizer.lua", - kanagawa = "https://github.com/rebelot/kanagawa.nvim", - } - for name, url in pairs(remote_plugins) do - clone_repo_if_missing(name, url, base_dir) +-- Configure colorizer plugin +local function configure_colorizer() + vim.opt.termguicolors = true + local opts = load_options("expect.txt") + if opts then + require("colorizer").setup(opts) + else + vim.notify("Could not load colorizer options from expect.txt", vim.log.levels.WARN) end -else - local remote_plugins = { - kanagawa = "https://github.com/rebelot/kanagawa.nvim", +end + +local function add_colorizer_plugin(plugins) + local base_config = { + event = "BufReadPre", + cmd = { + "ColorizerToggle", + "ColorizerAttachToBuffer", + "ColorizerDetachFromBuffer", + "ColorizerReloadAllBuffers", + }, + config = configure_colorizer, } - for name, url in pairs(remote_plugins) do - clone_repo_if_missing(name, url, base_dir) - end - if vim.fn.isdirectory(local_plugin_dir) == 1 then - vim.opt.runtimepath:append(local_plugin_dir) + if settings.use_remote then + table.insert( + plugins, + vim.tbl_extend("force", base_config, { + "NvChad/nvim-colorizer.lua", + url = "https://github.com/NvChad/nvim-colorizer.lua", + }) + ) else - vim.notify("Local plugin directory not found: " .. local_plugin_dir, vim.log.levels.ERROR) + local local_dir = settings.local_plugin_dir + if vim.fn.isdirectory(local_dir) == 1 then + vim.opt.rtp:append(local_dir) + table.insert( + plugins, + vim.tbl_extend("force", base_config, { + dir = local_dir, + lazy = false, + }) + ) + else + vim.notify("Local plugin directory not found: " .. local_dir, vim.log.levels.ERROR) + end end end -local function get_opts(file_path) - local opts - local success, err = pcall(function() - opts = dofile(file_path) - end) - if not success then - vim.notify("Error reloading file: " .. err, vim.log.levels.ERROR) - return - end - if not opts or type(opts) ~= "table" then - vim.notify("Invalid options in " .. file_path, vim.log.levels.ERROR) - return - end - return opts +-- Define additional plugins +local function define_plugins() + local plugins = { + { + "rebelot/kanagawa.nvim", + url = "https://github.com/rebelot/kanagawa.nvim", + config = function() + vim.cmd.colorscheme("kanagawa") + end, + }, + } + add_colorizer_plugin(plugins) + return plugins end --- Configure setup opts -local setup_opts = get_opts("expect.txt") -require("colorizer").setup(setup_opts) +-- Initialize and setup lazy.nvim +local ok, lazy = pcall(require, "lazy") +if not ok then + vim.notify("Failed to require lazy.nvim", vim.log.levels.ERROR) + return +end +lazy.setup(define_plugins()) +-- Automatically reload colorizer configuration on `expect.txt` changes vim.api.nvim_create_autocmd("BufWritePost", { pattern = "expect.txt", callback = function(evt) vim.schedule(function() - local opts = get_opts(evt.match) - if not opts then - return + local opts = load_options(evt.match) + if opts then + require("colorizer").detach_from_buffer(evt.buf) + require("colorizer").attach_to_buffer(evt.buf, opts.user_default_options) + vim.notify( + "Colorizer reloaded with updated options from " .. evt.match, + vim.log.levels.INFO + ) end - opts = opts.user_default_options - require("colorizer").detach_from_buffer(evt.buf) - require("colorizer").attach_to_buffer(evt.buf, opts) - vim.notify("Colorizer reloaded with updated options from " .. evt.match, vim.log.levels.INFO) end) end, }) -vim.cmd.colorscheme("kanagawa") vim.cmd.edit("expect.txt") -- ADD INIT.LUA SETTINGS _NECESSARY_ FOR REPRODUCING THE ISSUE