forked from norcalli/nvim-colorizer.lua
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use lazy.nvim in minimal.lua (#106)
* chore: refactors minimal.lua to use lazy.nvim
- Loading branch information
Showing
2 changed files
with
98 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |