Skip to content

Commit

Permalink
fix(perf): Memoize few headline methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed May 31, 2024
1 parent 6c03b72 commit 772c7c6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lua/orgmode/api/headline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ end
---@private
function OrgHeadline._build_from_internal_headline(section, index)
local todo, _, type = section:get_todo()
local _, properties = section:get_properties()
local properties = section:get_properties()
return OrgHeadline:_new({
title = section:get_title(),
line = section:get_headline_line_content(),
Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/files/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function OrgFile:apply_search(search, todo_only)
local deadline = item:get_deadline_date()
local scheduled = item:get_scheduled_date()
local closed = item:get_closed_date()
local _, properties = item:get_properties()
local properties = item:get_properties()

return search:check({
props = vim.tbl_extend('keep', {}, properties, {
Expand Down
24 changes: 13 additions & 11 deletions lua/orgmode/files/headline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ end

---@return OrgDate | nil
function Headline:get_closed_date()
return utils.find(self:get_all_dates(), function(date)
return date:is_closed()
end)
local dates = self:get_plan_dates()
return vim.tbl_get(dates, 'CLOSED', 1)
end

function Headline:get_priority_sort_value()
Expand Down Expand Up @@ -361,13 +360,14 @@ function Headline:get_title_with_priority()
return title
end

---@return TSNode | nil, table<string, string>
memoize('get_properties')
---@return table<string, string>, TSNode | nil
function Headline:get_properties()
local section = self:node():parent()
local properties_node = section and section:field('property_drawer')[1]

if not properties_node then
return nil, {}
return {}, nil
end

local properties = {}
Expand All @@ -383,7 +383,7 @@ function Headline:get_properties()
end
end

return properties_node, properties
return properties, properties_node
end

---@param name string
Expand All @@ -397,19 +397,19 @@ function Headline:set_property(name, value)
vim.fn.deletebufline(bufnr, property_node:start() + 1)
end
self:refresh()
local properties_node, properties = self:get_properties()
local properties, properties_node = self:get_properties()
if vim.tbl_isempty(properties) then
self:_set_node_lines(properties_node, {})
end
return self:refresh()
end

local properties = self:get_properties()
local _, properties = self:get_properties()
if not properties then
local append_line = self:get_append_line()
local property_drawer = self:_apply_indent({ ':PROPERTIES:', ':END:' }) --[[ @as string[] ]]
vim.api.nvim_buf_set_lines(bufnr, append_line, append_line, false, property_drawer)
properties = self:refresh():get_properties()
_, properties = self:refresh():get_properties()
end

local property = (':%s: %s'):format(name, value)
Expand Down Expand Up @@ -445,7 +445,7 @@ end
---@param search_parents? boolean
---@return string | nil, TSNode | nil
function Headline:get_property(property_name, search_parents)
local properties = self:get_properties()
local _, properties = self:get_properties()
if properties then
for _, node in ipairs(ts_utils.get_named_children(properties)) do
local name = node:field('name')[1]
Expand Down Expand Up @@ -598,7 +598,7 @@ end

---@return number
function Headline:get_append_line()
local properties = self:get_properties()
local _, properties = self:get_properties()
if properties then
local row = properties:end_()
return row
Expand Down Expand Up @@ -853,6 +853,7 @@ function Headline:get_drawer_append_line(name)
return name_row + 1
end

memoize('get_range')
---@return OrgRange
function Headline:get_range()
return Range.from_node(self:node():parent())
Expand All @@ -863,6 +864,7 @@ function Headline:get_lines()
return self.file:get_node_text_list(self:node():parent())
end

memoize('get_headline_line_content')
---@return string
function Headline:get_headline_line_content()
local line = self.file:get_node_text(self:node()):gsub('\n', '')
Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/org/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ function OrgMappings:_todo_change_state(direction)
)

if log_repeat_enabled then
headline:set_property('LAST_REPEAT', Date.now():to_wrapped_string(false))
item:set_property('LAST_REPEAT', Date.now():to_wrapped_string(false))
end

if not prompt_repeat_note and not prompt_done_note then
Expand Down

0 comments on commit 772c7c6

Please sign in to comment.