From 093e762f4f6ac621edddfe37d63d35cf088dc9a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Fl=C3=BCgge?= Date: Tue, 28 May 2024 07:35:38 +0200 Subject: [PATCH] refactor: integrate highlights into new function --- lua/orgmode/objects/calendar.lua | 32 ++++---------------------------- lua/orgmode/objects/date.lua | 8 +++++++- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/lua/orgmode/objects/calendar.lua b/lua/orgmode/objects/calendar.lua index 3ff187fa2..0fbebc9ae 100644 --- a/lua/orgmode/objects/calendar.lua +++ b/lua/orgmode/objects/calendar.lua @@ -224,12 +224,6 @@ function Calendar:render() vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 2, 0, -1) vim.api.nvim_buf_add_highlight(self.buf, namespace, 'Comment', #content - 1, 0, -1) - -- highlight the cell of the current day - self:highlight_day(content, Date.today(), 'OrgCalendarToday') - -- highlight selected day - self:highlight_day(content, self.date, 'OrgCalendarSelected') - - -- TODO this new loop (after Kristjans refactoring) is currently not fully understood. for i, line in ipairs(content) do local from = 0 local to, num @@ -256,10 +250,12 @@ end ---@param day OrgDate ---@param opts { from: number, to: number, line: number} function Calendar:on_render_day(day, opts) - local is_today = day:is_today() - if is_today then + if day:is_today() then vim.api.nvim_buf_add_highlight(self.buf, namespace, 'OrgCalendarToday', opts.line - 1, opts.from - 1, opts.to) end + if day:is_same_day(self.date) then + vim.api.nvim_buf_add_highlight(self.buf, namespace, 'OrgCalendarSelected', opts.line - 1, opts.from - 1, opts.to) + end if self.on_day then self.on_day( day, @@ -273,26 +269,6 @@ function Calendar:on_render_day(day, opts) vim.api.nvim_set_option_value('modifiable', false, { buf = self.buf }) end ----@param day OrgDate? ----@param hl_group string -function Calendar:highlight_day(content, day, hl_group) - if not day then - return - end - - if not day:is_same(self.month, 'month') then - return - end - - local day_formatted = day:format('%d') - for i, line in ipairs(content) do - local from, to = line:find('%s' .. day_formatted .. '%s') - if from and to then - vim.api.nvim_buf_add_highlight(self.buf, namespace, hl_group, i - 1, from - 1, to) - end - end -end - function Calendar.left_pad(time_part) return time_part < 10 and '0' .. time_part or time_part end diff --git a/lua/orgmode/objects/date.lua b/lua/orgmode/objects/date.lua index 6e9e147bd..6784f8444 100644 --- a/lua/orgmode/objects/date.lua +++ b/lua/orgmode/objects/date.lua @@ -29,6 +29,7 @@ local time_format = '%H:%M' ---@field related_date_range OrgDate ---@field dayname string ---@field adjustments string[] +---@private is_today_date boolean? local Date = { ---@type fun(this: OrgDate, other: OrgDate): boolean __eq = function(this, other) @@ -605,11 +606,16 @@ end function Date:is_today() if self.is_today_date == nil then local date = now() - self.is_today_date = date.year == self.year and date.month == self.month and date.day == self.day + self.is_today_date = self:is_same_day(date) end return self.is_today_date end +---@return boolean +function Date:is_same_day(date) + return date and date.year == self.year and date.month == self.month and date.day == self.day +end + ---@return boolean function Date:is_obsolete_range_end() return self.is_date_range_end and self.related_date_range:is_same(self, 'day')