From c656d6a3bdab91fffaa98f7c2fc6837680a4eb29 Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 14 Aug 2021 22:11:20 +0530 Subject: [PATCH] Fix theme switcher reload | Move reload_theme to utils (#252) * rearrange functions in utils.lua alphabetically --- lua/telescope/_extensions/themes.lua | 48 +------ lua/utils.lua | 205 +++++++++++++++++++-------- 2 files changed, 143 insertions(+), 110 deletions(-) diff --git a/lua/telescope/_extensions/themes.lua b/lua/telescope/_extensions/themes.lua index 8957aa92..1c6ec44b 100644 --- a/lua/telescope/_extensions/themes.lua +++ b/lua/telescope/_extensions/themes.lua @@ -1,52 +1,6 @@ -- This file can be loaded as a telescope extension local M = {} --- reload themes without restarting vim --- if no theme name given then reload the current theme -M.reload_theme = function(theme_name) - local reload_plugin = require("utils").reload_plugin - - -- if theme name is empty or nil, then reload the current theme - if (theme_name == nil or theme_name == "") then - theme_name = vim.g.nvchad_theme - end - - if not pcall(require, "themes/" .. theme_name) then - print("No such theme ( " .. theme_name .. " )") - return false - end - - vim.g.nvchad_theme = theme_name - - -- reload the base16 theme - local ok, base16 = pcall(require, "base16") - if not ok then - print("Error: Cannot load base16 plugin!") - return false - end - base16(base16.themes(theme_name), true) - - if - not reload_plugin { - "highlights", - "plugins.bufferline", - "galaxyline", - "plugins.statusline" - } - then - print "Error: Not able to reload all plugins." - return false - end - - -- now send the provider info to actual refresh - require("galaxyline.provider").async_load_providers:send() - - return true - -- open a buffer and close it to reload the statusline - -- vim.cmd("new|bwipeout") - -- commented out here as it will not work with telescope picker -end - -- Custom theme picker -- Most of the code is copied from telescope colorscheme plugin, mostly for preview creation M.theme_switcher = function(opts) @@ -65,7 +19,7 @@ M.theme_switcher = function(opts) end local local_utils = require "utils" - local reload_theme = M.reload_theme + local reload_theme = local_utils.reload_theme -- get a table of available themes local themes = local_utils.list_themes() diff --git a/lua/utils.lua b/lua/utils.lua index ced2dc70..a330d255 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -1,29 +1,59 @@ local M = {} --- reload a plugin ( will try to load even if not loaded) --- can take a string or list ( table ) --- return true or false -M.reload_plugin = function(plugins) - local status = true - local function _reload_plugin(plugin) - local loaded = package.loaded[plugin] - if loaded then - package.loaded[plugin] = nil - end - if not pcall(require, plugin) then - print("Error: Cannot load " .. plugin .. " plugin!") - status = false - end +-- 1st arg as current theme, 2nd as new theme +M.change_theme = function(current_theme, new_theme) + if current_theme == nil or new_theme == nil then + print "Error: Provide current and new theme name" + return false + end + if current_theme == new_theme then + return end - if type(plugins) == "string" then - _reload_plugin(plugins) - elseif type(plugins) == "table" then - for _, plugin in ipairs(plugins) do - _reload_plugin(plugin) + local file = vim.fn.stdpath("config") .. "/lua/chadrc.lua" + -- store in data variable + local data = assert(M.file("r", file)) + local find = "theme = .?" .. current_theme .. ".?" + local replace = 'theme = "' .. new_theme .. '"' + local content = string.gsub(data, find, replace) + -- see if the find string exists in file + if content == data then + print("Error: Cannot change default theme with " .. new_theme .. ", edit " .. file .. " manually") + return false + else + assert(M.file("w", file, content)) + end +end + +M.clear_cmdline = function() + vim.defer_fn( + function() + vim.cmd("echo") + end, + 0 + ) +end + +-- 1st arg - r or w +-- 2nd arg - file path +-- 3rd arg - content if 1st arg is w +-- return file data on read, nothing on write +M.file = function(mode, filepath, content) + local data + local fd = assert(vim.loop.fs_open(filepath, mode, 438)) + local stat = assert(vim.loop.fs_fstat(fd)) + if stat.type ~= "file" then + data = false + else + if mode == "r" then + data = assert(vim.loop.fs_read(fd, stat.size, 0)) + else + assert(vim.loop.fs_write(fd, content, 0)) + data = true end end - return status + assert(vim.loop.fs_close(fd)) + return data end -- return a table of available themes @@ -52,60 +82,109 @@ M.list_themes = function(return_type) return themes end --- 1st arg - r or w --- 2nd arg - file path --- 3rd arg - content if 1st arg is w --- return file data on read, nothing on write -M.file = function(mode, filepath, content) - local data - local fd = assert(vim.loop.fs_open(filepath, mode, 438)) - local stat = assert(vim.loop.fs_fstat(fd)) - if stat.type ~= "file" then - data = false - else - if mode == "r" then - data = assert(vim.loop.fs_read(fd, stat.size, 0)) - else - assert(vim.loop.fs_write(fd, content, 0)) - data = true +-- reload a plugin ( will try to load even if not loaded) +-- can take a string or list ( table ) +-- return true or false +M.reload_plugin = function(plugins) + local status = true + local function _reload_plugin(plugin) + local loaded = package.loaded[plugin] + if loaded then + package.loaded[plugin] = nil + end + if not pcall(require, plugin) then + print("Error: Cannot load " .. plugin .. " plugin!") + status = false end end - assert(vim.loop.fs_close(fd)) - return data + + if type(plugins) == "string" then + _reload_plugin(plugins) + elseif type(plugins) == "table" then + for _, plugin in ipairs(plugins) do + _reload_plugin(plugin) + end + end + return status end --- 1st arg as current theme, 2nd as new theme -M.change_theme = function(current_theme, new_theme) - if current_theme == nil or new_theme == nil then - print "Error: Provide current and new theme name" +-- reload themes without restarting vim +-- if no theme name given then reload the current theme +M.reload_theme = function(theme_name) + local reload_plugin = require("utils").reload_plugin + + -- if theme name is empty or nil, then reload the current theme + if (theme_name == nil or theme_name == "") then + theme_name = vim.g.nvchad_theme + end + + if not pcall(require, "themes/" .. theme_name) then + print("No such theme ( " .. theme_name .. " )") return false end - if current_theme == new_theme then - return + + vim.g.nvchad_theme = theme_name + + -- reload the base16 theme + local ok, base16 = pcall(require, "base16") + if not ok then + print("Error: Cannot load base16 plugin!") + return false end + base16(base16.themes(theme_name), true) - local file = vim.fn.stdpath("config") .. "/lua/chadrc.lua" - -- store in data variable - local data = assert(M.file("r", file)) - local find = "theme = .?" .. current_theme .. ".?" - local replace = 'theme = "' .. new_theme .. '"' - local content = string.gsub(data, find, replace) - -- see if the find string exists in file - if content == data then - print("Error: Cannot change default theme with " .. new_theme .. ", edit " .. file .. " manually") + if + not reload_plugin { + "highlights", + "plugins.bufferline", + "galaxyline", + "plugins.statusline" + } + then + print "Error: Not able to reload all plugins." return false - else - assert(M.file("w", file, content)) end -end -M.clear_cmdline = function() - vim.defer_fn( - function() - vim.cmd("echo") - end, - 0 - ) + -- yes, this is very hacky, but due to new_async in + -- https://github.com/glepnir/galaxyline.nvim/blob/main/lua/galaxyline/provider.lua#L5-L36 + -- it doesn't work properly and some statusline stuff dissapears + local vcs = require("galaxyline.provider_vcs") + local fileinfo = require("galaxyline.provider_fileinfo") + local buffer = require("galaxyline.provider_buffer") + local extension = require("galaxyline.provider_extensions") + local whitespace = require("galaxyline.provider_whitespace") + local lspclient = require("galaxyline.provider_lsp") + _G.galaxyline_providers = { + BufferIcon = buffer.get_buffer_type_icon, + BufferNumber = buffer.get_buffer_number, + FileTypeName = buffer.get_buffer_filetype, + GitBranch = vcs.get_git_branch, + DiffAdd = vcs.diff_add, + DiffModified = vcs.diff_modified, + DiffRemove = vcs.diff_remove, + LineColumn = fileinfo.line_column, + FileFormat = fileinfo.get_file_format, + FileEncode = fileinfo.get_file_encode, + FileSize = fileinfo.get_file_size, + FileIcon = fileinfo.get_file_icon, + FileName = fileinfo.get_current_file_name, + SFileName = fileinfo.filename_in_special_buffer, + LinePercent = fileinfo.current_line_percent, + ScrollBar = extension.scrollbar_instance, + VistaPlugin = extension.vista_nearest, + WhiteSpace = whitespace.get_item, + GetLspClient = lspclient.get_lsp_client + } + local diagnostic = require("galaxyline.provider_diagnostic") + _G.galaxyline_providers.DiagnosticError = diagnostic.get_diagnostic_error + _G.galaxyline_providers.DiagnosticWarn = diagnostic.get_diagnostic_warn + _G.galaxyline_providers.DiagnosticHint = diagnostic.get_diagnostic_hint + _G.galaxyline_providers.DiagnosticInfo = diagnostic.get_diagnostic_info + + return true + -- open a buffer and close it to reload the statusline + -- vim.cmd("new|bwipeout") + -- commented out here as it will not work with telescope picker end return M