Skip to content

Commit

Permalink
feat(properties): add property inheritance for API and search
Browse files Browse the repository at this point in the history
  • Loading branch information
troiganto committed Jan 31, 2025
1 parent 41b9cf2 commit c05873b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lua/orgmode/api/headline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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_own_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 @@ -278,7 +278,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_own_properties()
local priority = item:get_priority()

return search:check({
Expand Down
42 changes: 35 additions & 7 deletions lua/orgmode/files/headline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ function Headline:get_title_with_priority()
return title
end

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

Expand All @@ -416,6 +416,33 @@ function Headline:get_properties()
return properties, properties_node
end

memoize('get_properties')
---@return table<string, string>, TSNode | nil
function Headline:get_properties()
local properties, own_properties_node = self:get_own_properties()

if not config.org_use_property_inheritance then
-- TODO: Do we have to filter non-inherited properties here? (see `Headline:get_tags()`)
return properties, own_properties_node
end

local parent_section = self:node():parent():parent()
while parent_section do
local headline_node = parent_section:field('headline')[1]
if headline_node then
local headline = Headline:new(headline_node, self.file)
for name, value in pairs(headline:get_own_properties()) do
if properties[name] == nil and config:use_property_inheritance(name) then
properties[name] = value
end
end
end
parent_section = parent_section:parent()
end

return properties, own_properties_node
end

---@param name string
---@param value? string
---@return OrgHeadline
Expand All @@ -427,19 +454,19 @@ function Headline:set_property(name, value)
vim.fn.deletebufline(bufnr, property_node:start() + 1)
end
self:refresh()
local properties, properties_node = self:get_properties()
local properties, properties_node = self:get_own_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_own_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_own_properties()
end

local property = (':%s: %s'):format(name, value)
Expand Down Expand Up @@ -478,7 +505,7 @@ end
--- `org_use_property_inheritance`
---@return string | nil, TSNode | nil
function Headline:get_property(property_name, search_parents)
local _, properties = self:get_properties()
local _, properties = self:get_own_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 @@ -550,6 +577,7 @@ memoize('get_tags')
function Headline:get_tags()
local tags, own_tags_node = self:get_own_tags()
if not config.org_use_tag_inheritance then
-- TODO: Why exclude the headline's own tags here?
return config:exclude_tags(tags), own_tags_node
end

Expand Down Expand Up @@ -636,7 +664,7 @@ end

---@return number
function Headline:get_append_line()
local _, properties = self:get_properties()
local _, properties = self:get_own_properties()
if properties then
local row = properties:end_()
return row
Expand Down
23 changes: 23 additions & 0 deletions tests/plenary/files/headline_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ describe('Headline', function()
assert.are.same('some/dir/', file:get_headlines()[2]:get_property('dir'))
assert.is.Nil(file:get_headlines()[2]:get_property('dir', false))
end)
it('does not affect get_own_properties', function()
config:extend({ org_use_property_inheritance = true })
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
assert.are.same({}, file:get_headlines()[2]:get_own_properties())
end)
it('affects get_properties', function()
config:extend({ org_use_property_inheritance = true })
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
local expected = { dir = 'some/dir/', thing = '0', columns = '' }
assert.are.same(expected, file:get_headlines()[2]:get_properties())
end)
it('makes get_properties selective if a list', function()
config:extend({ org_use_property_inheritance = { 'dir' } })
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
local expected = { dir = 'some/dir/' }
assert.are.same(expected, file:get_headlines()[2]:get_properties())
end)
it('makes get_properties selective if a regex', function()
config:extend({ org_use_property_inheritance = '^th...$' })
file.metadata.mtime = file.metadata.mtime + 1 -- invalidate cache
local expected = { thing = '0' }
assert.are.same(expected, file:get_headlines()[2]:get_properties())
end)
end)

describe('get_all_dates', function()
Expand Down

0 comments on commit c05873b

Please sign in to comment.