Skip to content

Commit 64397be

Browse files
author
Kristijan Husak
committed
Consider properties drawer valid only if it's positioned after headline or planning date.
1 parent b76cf61 commit 64397be

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

lua/orgmode/parser/headline.lua

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,24 +247,25 @@ end
247247
---@param content Content
248248
function Headline:_parse_properties(content)
249249
if content:is_properties_start() then
250-
if not self.properties.range then
251-
self.properties.range = Range.from_line(content.range.start_line)
250+
local is_valid_position = #self.content == 1 or #self.content == 2 and self.content[1]:is_planning()
251+
if not is_valid_position then
252+
return
252253
end
254+
self.properties.range = Range.from_line(content.range.start_line)
255+
self.properties.valid = true
256+
self.properties.unfinished = true
253257
end
254-
if content:is_parent_end() then
255-
if self.properties.range and self.properties.range:is_same_line() then
256-
self.properties.range.end_line = content.range.start_line
257-
self.properties.valid = true
258-
local start_index = self.properties.range.start_line - self.range.start_line
259-
local end_index = self.properties.range.end_line - self.range.start_line
260-
while start_index < end_index do
261-
local entry = self.content[start_index]
262-
if entry.drawer and entry.drawer.properties then
263-
self.properties.items = vim.tbl_extend('force', self.properties.items, entry.drawer.properties or {})
264-
end
265-
start_index = start_index + 1
258+
if content:is_parent_end() and self.properties.valid and self.properties.unfinished then
259+
self.properties.range.end_line = content.range.start_line
260+
local start_index = self.properties.range.start_line - self.range.start_line
261+
local end_index = self.properties.range.end_line - self.range.start_line
262+
local entries = { unpack(self.content, start_index, end_index) }
263+
for _, entry in ipairs(entries) do
264+
if entry.drawer and entry.drawer.properties then
265+
self.properties.items = vim.tbl_extend('force', self.properties.items, entry.drawer.properties or {})
266266
end
267267
end
268+
self.properties.unfinished = nil
268269
end
269270
end
270271

tests/plenary/parser/parser_spec.lua

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ describe('Parser', function()
550550
))
551551
end)
552552

553-
it('should parse drawer', function()
553+
it('should parse properties drawer', function()
554554
local lines = {
555555
'* TODO Test orgmode :WORK:',
556556
'DEADLINE: <2021-05-10 11:00>',
@@ -711,6 +711,60 @@ describe('Parser', function()
711711
assert.are.same({ items = {} }, parsed:get_item(1).properties)
712712
end)
713713

714+
it('should parse properties only if its positioned after headline or planning date', function()
715+
local lines = {
716+
'* TODO Test orgmode :WORK:',
717+
'DEADLINE: <2021-05-10 11:00>',
718+
'Some content in between',
719+
':PROPERTIES:',
720+
':SOME_PROP: some value',
721+
':END:',
722+
'* TODO Another todo',
723+
}
724+
725+
local parsed = parser.parse(lines, 'work')
726+
local headline = parsed:get_item(1)
727+
assert.are.same({}, headline.properties.items)
728+
729+
lines = {
730+
'* TODO Test orgmode :WORK:',
731+
'Some content in between',
732+
':PROPERTIES:',
733+
':SOME_PROP: some value',
734+
':END:',
735+
'* TODO Another todo',
736+
}
737+
738+
parsed = parser.parse(lines, 'work')
739+
headline = parsed:get_item(1)
740+
assert.are.same({}, headline.properties.items)
741+
742+
lines = {
743+
'* TODO Test orgmode :WORK:',
744+
':PROPERTIES:',
745+
':SOME_PROP: some value',
746+
':END:',
747+
'* TODO Another todo',
748+
}
749+
750+
parsed = parser.parse(lines, 'work')
751+
headline = parsed:get_item(1)
752+
assert.are.same({ SOME_PROP = 'some value' }, headline.properties.items)
753+
754+
lines = {
755+
'* TODO Test orgmode :WORK:',
756+
'DEADLINE: <2021-05-10 11:00>',
757+
':PROPERTIES:',
758+
':SOME_PROP: some value',
759+
':END:',
760+
'* TODO Another todo',
761+
}
762+
763+
parsed = parser.parse(lines, 'work')
764+
headline = parsed:get_item(1)
765+
assert.are.same({ SOME_PROP = 'some value' }, headline.properties.items)
766+
end)
767+
714768
it('should override headline category from property', function()
715769
local lines = {
716770
'* TODO Test orgmode :WORK:',

0 commit comments

Comments
 (0)