diff --git a/NUL b/NUL new file mode 100644 index 0000000..e69de29 diff --git a/lua/git-dashboard-nvim/heatmap/utils.lua b/lua/git-dashboard-nvim/heatmap/utils.lua index 7e96b91..abe68a2 100644 --- a/lua/git-dashboard-nvim/heatmap/utils.lua +++ b/lua/git-dashboard-nvim/heatmap/utils.lua @@ -70,20 +70,6 @@ HeatmapUtils.generate_ascii_heatmap = function( ascii_heatmap = ascii_heatmap .. "\n\n" end - -- add highlights to the heatmap based on the config settings - vim.api.nvim_create_autocmd("FileType", { - pattern = { "dashboard", "alpha" }, - callback = function() - Highlights.add_highlights(config, current_date_info, branch_label, title) - - -- hide cursor - if config.hide_cursor == true then - vim.api.nvim_command("hi Cursor blend=100") - vim.api.nvim_command("set guicursor+=a:Cursor/lCursor") - end - end, - }) - -- horizontal heatmap if config.is_horizontal then -- add month labels @@ -204,6 +190,20 @@ HeatmapUtils.generate_ascii_heatmap = function( ascii_heatmap = string.rep("\n", padding) .. ascii_heatmap end + -- add highlights to the heatmap based on the config settings + vim.api.nvim_create_autocmd("FileType", { + pattern = { "dashboard", "alpha" }, + callback = function() + Highlights.add_highlights(config, current_date_info, branch_label, title) + + -- hide cursor + if config.hide_cursor == true then + vim.api.nvim_command("hi Cursor blend=100") + vim.api.nvim_command("set guicursor+=a:Cursor/lCursor") + end + end, + }) + return ascii_heatmap end diff --git a/lua/git-dashboard-nvim/highlights/init.lua b/lua/git-dashboard-nvim/highlights/init.lua index 813ecc2..9f6e200 100644 --- a/lua/git-dashboard-nvim/highlights/init.lua +++ b/lua/git-dashboard-nvim/highlights/init.lua @@ -1,3 +1,14 @@ +--[[ + +Sets the highlights for the dashboard header section +Highlights will only be applied to the DashboardHeader section of the buffer +For for dashboard.nvim the containedin option works well, but for alpha.nvim +it doesn't prevent the highlights from being applied to the entire buffer +so we added temporary whitespace to the beginning of the match to prevent +easily matching other sections of the buffer + +]] + Highlights = {} ---@param group_name string @@ -5,7 +16,13 @@ Highlights = {} ---@param fg_color string Highlights._add_highlight_group = function(group_name, match, fg_color) vim.cmd("highlight " .. group_name .. " guifg=" .. fg_color) - vim.cmd('call matchadd("' .. group_name .. '", "' .. match .. '")') + + -- Ensure proper escaping of special characters in `match` + local pattern = vim.fn.escape(match, "/") + + -- Construct the syntax match command, using containedin ensures that the match is only applied to the DashboardHeader section + local cmd = string.format("syntax match %s /%s/ containedin=DashboardHeader", group_name, pattern) + vim.cmd(cmd) end ---@param config Config @@ -28,8 +45,8 @@ Highlights.add_highlights = function(config, current_date_info, branch_label, ti for i = 1, #config.days do Highlights._add_highlight_group( - "DashboardHeaderDay", - config.days[i]:sub(1, 3), + "DashboardHeaderDay" .. i, + " " .. config.days[i]:sub(1, 3) .. config.day_label_gap, config.colors.days_and_months_labels ) end @@ -37,7 +54,7 @@ Highlights.add_highlights = function(config, current_date_info, branch_label, ti for i = 1, current_date_info.current_month do Highlights._add_highlight_group( "DashboardHeaderMonth", - config.months[i]:sub(1, 3), + config.months[i]:sub(1, 3) .. " ", config.colors.days_and_months_labels ) end @@ -51,20 +68,21 @@ Highlights.add_highlights = function(config, current_date_info, branch_label, ti end -- add highlight to match any number - vim.cmd("call matchadd('DashboardHeaderMonth', '\\d\\+')") + -- vim.cmd("call matchadd('DashboardHeaderMonth', '\\d\\+')") + Highlights._add_highlight_group( + "DashboardHeaderMonth", + "\\d\\+", + config.colors.days_and_months_labels + ) Highlights._add_highlight_group("DashboardHeaderTitle", title, config.colors.dashboard_title) Highlights._add_highlight_group( "DashboardHeaderBranch", - branch_label, + " " .. branch_label, config.colors.branch_highlight ) - vim.cmd.autocmd( - "BufLeave", - "*", - "highlight clear DashboardHeaderEmptySquare | highlight clear DashboardHeaderDay | highlight clear DashboardHeaderMonth | highlight clear DashboardHeaderFilledSquare | highlight clear DashboardHeaderTitle | highlight clear DashboardHeaderBranch" - ) + -- set cursor color to white when leaving the buffer vim.cmd.autocmd("BufLeave", "*", "highlight Cursor blend=0") vim.cmd.autocmd("BufLeave", "*", "set guicursor+=a:Cursor/lCursor") diff --git a/tests/unit/config_spec.lua b/tests/unit/config_spec.lua index 813014f..57d29e9 100644 --- a/tests/unit/config_spec.lua +++ b/tests/unit/config_spec.lua @@ -59,6 +59,8 @@ describe("config", function() branch_highlight = "#8DC07C", dashboard_title = "#a3cc96", }, + basepoints = { "master", "main" }, + use_git_username_as_author = false, }) end) @@ -145,6 +147,8 @@ describe("config", function() branch_highlight = "#8DC07C", dashboard_title = "#a3cc96", }, + basepoints = { "master", "main" }, + use_git_username_as_author = false, }) end) end)