Skip to content

Commit

Permalink
fix(previewer): fix bad colorscheme name in bat theme (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Feb 14, 2024
1 parent 13ff739 commit 1e187c8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 78 deletions.
1 change: 0 additions & 1 deletion lua/fzfx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ local function setup(opts)
require("fzfx.detail.popup").setup()
require("fzfx.detail.bat_helpers").setup()
require("fzfx.detail.fzf_helpers").setup()
require("fzfx.helper.colorschemes").setup()

local general = require("fzfx.detail.general")

Expand Down
29 changes: 14 additions & 15 deletions lua/fzfx/detail/bat_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local versions = require("fzfx.commons.versions")
local constants = require("fzfx.lib.constants")
local log = require("fzfx.lib.log")

local colorschemes_helper = require("fzfx.helper.colorschemes")
local bat_themes_helper = require("fzfx.helper.bat_themes")

local M = {}
Expand Down Expand Up @@ -816,6 +815,7 @@ M._build_theme = function(colorname)
)

local theme_config_file = bat_themes_helper.get_theme_config_file(colorname)
log.debug("|_build_theme| theme_config_file:%s", vim.inspect(theme_config_file))
log.ensure(
strings.not_empty(theme_config_file),
"|_build_theme| failed to get bat theme config file from nvim colorscheme name:%s",
Expand Down Expand Up @@ -881,6 +881,7 @@ M._patch_theme = function(colorname, lsp_type, lsp_modifiers)
end

local theme_config_file = bat_themes_helper.get_theme_config_file(colorname)
log.debug("|_patch_theme| theme_config_file:%s", vim.inspect(theme_config_file))
log.ensure(
strings.not_empty(theme_config_file),
"|_patch_theme| failed to get bat theme config file from nvim colorscheme name:%s",
Expand Down Expand Up @@ -916,36 +917,34 @@ M.setup = function()
return
end

local color = vim.g.colors_name
if strings.not_empty(color) then
M._build_theme(color)
if strings.not_empty(vim.g.colors_name) then
M._build_theme(vim.g.colors_name)
end

vim.api.nvim_create_autocmd({ "ColorScheme" }, {
callback = function(event)
vim.defer_fn(function()
-- log.debug("|setup| ColorScheme event:%s", vim.inspect(event))
if strings.not_empty(tables.tbl_get(event, "match")) then
M._build_theme(event.match)
log.debug("|setup| ColorScheme event:%s", vim.inspect(event))
vim.schedule(function()
if strings.not_empty(vim.g.colors_name) then
M._build_theme(vim.g.colors_name)
end
end, 10)
end)
end,
})

if versions.ge("0.9") and vim.fn.exists("##LspTokenUpdate") then
vim.api.nvim_create_autocmd("LspTokenUpdate", {
callback = function(event)
-- log.debug("|setup| LspTokenUpdate:%s", vim.inspect(event))
vim.defer_fn(function()
log.debug("|setup| LspTokenUpdate:%s", vim.inspect(event))
vim.schedule(function()
if strings.not_empty(tables.tbl_get(event, "data", "token", "type")) then
local lsp_type = string.format("@lsp.type.%s", event.data.token.type)
local lsp_modifiers = tables.tbl_get(event, "data", "token", "modifiers") or {}
local bufcolor = colorschemes_helper.get_color_name() --[[@as string]]
if strings.not_empty(bufcolor) then
M._patch_theme(bufcolor, lsp_type, lsp_modifiers)
if strings.not_empty(vim.g.colors_name) then
M._patch_theme(vim.g.colors_name, lsp_type, lsp_modifiers)
end
end
end, 10)
end)
end,
})
end
Expand Down
16 changes: 11 additions & 5 deletions lua/fzfx/helper/bat_themes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ end

--- @type string?
local CACHED_THEME_DIR = nil
local theme_dir_cached_reader = nil

--- @return string?
M._cached_theme_dir = function()
if CACHED_THEME_DIR == nil then
CACHED_THEME_DIR = fileios.readfile(M._theme_dir_cache(), { trim = true })
if theme_dir_cached_reader == nil then
theme_dir_cached_reader = fileios.CachedFileReader:open(M._theme_dir_cache())
end
return CACHED_THEME_DIR
return theme_dir_cached_reader:read({ trim = true })
end

--- @param theme_dir string
Expand All @@ -41,7 +42,7 @@ end
--- @return string
M.get_theme_dir = function()
local cached_result = M._cached_theme_dir() --[[@as string]]
-- log.debug("|get_theme_dir| cached_result:%s", vim.inspect(cached_result))
log.debug("|get_theme_dir| cached_result:%s", vim.inspect(cached_result))

if strings.empty(cached_result) then
log.ensure(constants.HAS_BAT, "|get_theme_dir| cannot find 'bat' executable")
Expand All @@ -55,7 +56,7 @@ M.get_theme_dir = function()
on_stderr = function() end,
})
:wait()
-- log.debug("|get_theme_dir| config_dir:%s", vim.inspect(config_dir))
log.debug("|get_theme_dir| config_dir:%s", vim.inspect(config_dir))
local theme_dir = paths.join(config_dir, "themes")
M._create_theme_dir(theme_dir)
fileios.writefile(M._theme_dir_cache(), theme_dir)
Expand Down Expand Up @@ -131,6 +132,11 @@ M.get_theme_config_file = function(colorname)
"|get_theme_config_file| failed to get bat theme name from nvim colorscheme name:%s",
vim.inspect(colorname)
)
log.debug(
"|get_theme_config_file| theme_dir:%s, theme_name:%s",
vim.inspect(theme_dir),
vim.inspect(theme_name)
)
return paths.join(theme_dir, theme_name .. ".tmTheme")
end

Expand Down
26 changes: 0 additions & 26 deletions lua/fzfx/helper/colorschemes.lua

This file was deleted.

9 changes: 4 additions & 5 deletions lua/fzfx/helper/previewers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local log = require("fzfx.lib.log")
local LogLevels = require("fzfx.lib.log").LogLevels

local bat_themes_helper = require("fzfx.helper.bat_themes")
local colorschemes_helper = require("fzfx.helper.colorschemes")
local parsers_helper = require("fzfx.helper.parsers")
local queries_helper = require("fzfx.helper.queries")
local actions_helper = require("fzfx.helper.actions")
Expand All @@ -29,12 +28,12 @@ M._bat_style_theme = function()
end

if constants.HAS_BAT and vim.opt.termguicolors then
local color = colorschemes_helper.get_color_name() --[[@as string]]
if strings.not_empty(color) then
local theme_config_file = bat_themes_helper.get_theme_config_file(color)
local colorname = vim.g.colors_name --[[@as string]]
if strings.not_empty(colorname) then
local theme_config_file = bat_themes_helper.get_theme_config_file(colorname)
if paths.isfile(theme_config_file or "") then
-- print(string.format("bat previewer color:%s", vim.inspect(color)))
local custom_theme_name = bat_themes_helper.get_theme_name(color) --[[@as string]]
local custom_theme_name = bat_themes_helper.get_theme_name(colorname) --[[@as string]]
-- log.debug(
-- "|_bat_style_theme| theme_config_file:%s, custom_theme_name:%s",
-- vim.inspect(theme_config_file),
Expand Down
25 changes: 0 additions & 25 deletions spec/helper/colorschemes_spec.lua

This file was deleted.

2 changes: 1 addition & 1 deletion spec/helper/previewers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("helper.previewers", function()
local previewers = require("fzfx.helper.previewers")
local conf = require("fzfx.config")
local fzf_helpers = require("fzfx.detail.fzf_helpers")
conf.setup()
require("fzfx").setup()

describe("[_bat_style_theme]", function()
if constants.HAS_BAT then
Expand Down

0 comments on commit 1e187c8

Please sign in to comment.