Skip to content

Commit

Permalink
refactor: integrate highlights into new function
Browse files Browse the repository at this point in the history
  • Loading branch information
seflue committed May 28, 2024
1 parent 377b5eb commit 093e762
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
32 changes: 4 additions & 28 deletions lua/orgmode/objects/calendar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lua/orgmode/objects/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 093e762

Please sign in to comment.