Skip to content

Commit 25b2280

Browse files
committed
feat(properties): add property inheritance for API and search
1 parent 52eb2c3 commit 25b2280

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

lua/orgmode/api/headline.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ end
6464
---@private
6565
function OrgHeadline._build_from_internal_headline(section, index)
6666
local todo, _, type = section:get_todo()
67-
local properties = section:get_properties()
67+
local properties = section:get_own_properties()
6868
return OrgHeadline:_new({
6969
title = section:get_title(),
7070
line = section:get_headline_line_content(),

lua/orgmode/files/file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ function OrgFile:apply_search(search, todo_only)
278278
local deadline = item:get_deadline_date()
279279
local scheduled = item:get_scheduled_date()
280280
local closed = item:get_closed_date()
281-
local properties = item:get_properties()
281+
local properties = item:get_own_properties()
282282
local priority = item:get_priority()
283283

284284
return search:check({

lua/orgmode/files/headline.lua

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ function Headline:get_title_with_priority()
390390
return title
391391
end
392392

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

@@ -416,6 +416,32 @@ function Headline:get_properties()
416416
return properties, properties_node
417417
end
418418

419+
memoize('get_properties')
420+
---@return table<string, string>, TSNode | nil
421+
function Headline:get_properties()
422+
local properties, own_properties_node = self:get_own_properties()
423+
424+
if not config.org_use_property_inheritance then
425+
return properties, own_properties_node
426+
end
427+
428+
local parent_section = self:node():parent():parent()
429+
while parent_section do
430+
local headline_node = parent_section:field('headline')[1]
431+
if headline_node then
432+
local headline = Headline:new(headline_node, self.file)
433+
for name, value in pairs(headline:get_own_properties()) do
434+
if properties[name] == nil and config:use_property_inheritance(name) then
435+
properties[name] = value
436+
end
437+
end
438+
end
439+
parent_section = parent_section:parent()
440+
end
441+
442+
return properties, own_properties_node
443+
end
444+
419445
---@param name string
420446
---@param value? string
421447
---@return OrgHeadline
@@ -427,19 +453,19 @@ function Headline:set_property(name, value)
427453
vim.fn.deletebufline(bufnr, property_node:start() + 1)
428454
end
429455
self:refresh()
430-
local properties, properties_node = self:get_properties()
456+
local properties, properties_node = self:get_own_properties()
431457
if vim.tbl_isempty(properties) then
432458
self:_set_node_lines(properties_node, {})
433459
end
434460
return self:refresh()
435461
end
436462

437-
local _, properties = self:get_properties()
463+
local _, properties = self:get_own_properties()
438464
if not properties then
439465
local append_line = self:get_append_line()
440466
local property_drawer = self:_apply_indent({ ':PROPERTIES:', ':END:' }) --[[ @as string[] ]]
441467
vim.api.nvim_buf_set_lines(bufnr, append_line, append_line, false, property_drawer)
442-
_, properties = self:refresh():get_properties()
468+
_, properties = self:refresh():get_own_properties()
443469
end
444470

445471
local property = (':%s: %s'):format(name, value)
@@ -478,7 +504,7 @@ end
478504
--- `org_use_property_inheritance`
479505
---@return string | nil, TSNode | nil
480506
function Headline:get_property(property_name, search_parents)
481-
local _, properties = self:get_properties()
507+
local _, properties = self:get_own_properties()
482508
if properties then
483509
for _, node in ipairs(ts_utils.get_named_children(properties)) do
484510
local name = node:field('name')[1]
@@ -636,7 +662,7 @@ end
636662

637663
---@return number
638664
function Headline:get_append_line()
639-
local _, properties = self:get_properties()
665+
local _, properties = self:get_own_properties()
640666
if properties then
641667
local row = properties:end_()
642668
return row

tests/plenary/files/headline_spec.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@ describe('Headline', function()
9797
assert.are.same('some/dir/', file:get_headlines()[2]:get_property('dir'))
9898
assert.is.Nil(file:get_headlines()[2]:get_property('dir', false))
9999
end)
100+
it('does not affect get_own_properties', function()
101+
config:extend({ org_use_property_inheritance = true })
102+
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
103+
assert.are.same({}, file:get_headlines()[2]:get_own_properties())
104+
end)
105+
it('affects get_properties', function()
106+
config:extend({ org_use_property_inheritance = true })
107+
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
108+
local expected = { dir = 'some/dir/', thing = '0', columns = '' }
109+
assert.are.same(expected, file:get_headlines()[2]:get_properties())
110+
end)
111+
it('makes get_properties selective if a list', function()
112+
config:extend({ org_use_property_inheritance = { 'dir' } })
113+
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
114+
local expected = { dir = 'some/dir/' }
115+
assert.are.same(expected, file:get_headlines()[2]:get_properties())
116+
end)
117+
it('makes get_properties selective if a regex', function()
118+
config:extend({ org_use_property_inheritance = '^th...$' })
119+
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
120+
local expected = { thing = '0' }
121+
assert.are.same(expected, file:get_headlines()[2]:get_properties())
122+
end)
100123
end)
101124

102125
describe('get_all_dates', function()

0 commit comments

Comments
 (0)