Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lua/orgmode/files/file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ local clean_empty_line = vim.fn.has('nvim-0.13') == 1 or vim.fn.has('nvim-0.12.3
---@field root TSNode
local OrgFile = {}

-- The root node id only changes on a re-parse, so a buffer edited since the
-- last parse would still hit results built for the old tree. The changedtick
-- in the key turns that into a miss, and the method re-parses when it runs.
local memoize = Memoize:new(OrgFile, function(self)
local tick = 0
local bufnr = self:bufnr()
if bufnr > -1 then
tick = vim.api.nvim_buf_get_changedtick(bufnr)
end
return {
file = self,
id = table.concat({ 'file', self.root and self.root:id() or '' }, '_'),
id = table.concat({ 'file', self.root and self.root:id() or '', tick }, '_'),
}
end)

Expand Down
30 changes: 30 additions & 0 deletions tests/plenary/files/file_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,36 @@ describe('OrgFile', function()
end)
end)

describe('memoized methods', function()
it('should reparse a buffer edited since the last parse', function()
local filename = vim.fn.tempname() .. '.org'
local file = load_file_sync({
'* First',
'* Second',
':PROPERTIES:',
':ID: second-id',
':END:',
'body',
'body',
'body',
}, filename)
vim.cmd('edit ' .. filename)
file:reload_sync()

local headlines = file:get_headlines_including_archived()
assert.are.same(2, #headlines)
assert.are.same('second-id', headlines[2]:get_property('ID'))

-- Unsaved edit shrinks the buffer, no reload in between
vim.api.nvim_buf_set_lines(0, 1, -1, false, {})

headlines = file:get_headlines_including_archived()
assert.are.same(1, #headlines)
assert.are.same('First', headlines[1]:get_title())
vim.cmd('bwipeout!')
end)
end)

describe('get_node_text_list', function()
local file = load_file_sync({
'* Headline 1 :TAG:',
Expand Down
Loading