Skip to content

Commit

Permalink
fix(capture): add archive context to subtree headline, not last line
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaswachowski committed May 25, 2024
1 parent c4f11c7 commit aa4a255
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lua/orgmode/capture/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ function Capture:refile_file_headline_to_archive(headline)

local destination_file = self.files:get(archive_location)

local headline_num_lines = #headline:get_lines()

self:_refile_from_org_file({
source_headline = headline,
destination_file = destination_file,
Expand All @@ -287,11 +289,11 @@ function Capture:refile_file_headline_to_archive(headline)
destination_file = self.files:get(archive_location)
self.files:update_file(destination_file.filename, function(archive_file)
local headlines = archive_file:get_headlines_including_archived()
local last_headline = headlines[#headlines]
last_headline:set_property('ARCHIVE_TIME', Date.now():to_string())
last_headline:set_property('ARCHIVE_FILE', file.filename)
last_headline:set_property('ARCHIVE_CATEGORY', headline:get_category())
last_headline:set_property('ARCHIVE_TODO', headline:get_todo() or '')
local subtree_headline = headlines[#headlines - headline_num_lines + 1]
subtree_headline:set_property('ARCHIVE_TIME', Date.now():to_string())
subtree_headline:set_property('ARCHIVE_FILE', file.filename)
subtree_headline:set_property('ARCHIVE_CATEGORY', headline:get_category())
subtree_headline:set_property('ARCHIVE_TODO', headline:get_todo() or '')
end)
end

Expand Down
55 changes: 55 additions & 0 deletions tests/plenary/capture/capture_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Capture = require('orgmode.capture')
local Date = require('orgmode.objects.date')
local Templates = require('orgmode.capture.templates')
local Template = require('orgmode.capture.template')
local helpers = require('tests.plenary.helpers')
Expand Down Expand Up @@ -212,6 +213,60 @@ describe('Refile', function()
'',
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)

describe('when archiving', function()
describe('a subtree with children', function()
it('sets the context on the archived headline', function()
local capture_lines = {
'* foo',
'** bar',
'*** TODO baz',
}
local capture_file = helpers.create_file(capture_lines)
local source_headline = capture_file:get_headlines()[1]

---@diagnostic disable-next-line: invisible
org.capture:refile_file_headline_to_archive(source_headline)

assert.are.same({
'* foo',
' :PROPERTIES:',
' :ARCHIVE_TIME: ' .. Date.now():to_string(),
' :ARCHIVE_FILE: ' .. capture_file.filename,
' :ARCHIVE_CATEGORY: ' .. capture_file:get_category(),
' :ARCHIVE_TODO: ' .. (source_headline:get_todo() or ''),
' :END:',
'** bar',
'*** TODO baz',
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
end)

describe('a leaf headline', function()
it('sets the context on the archived headline', function()
local capture_lines = {
'* foo',
'** bar',
'*** TODO baz',
}
local capture_file = helpers.create_file(capture_lines)
local source_headline = capture_file:get_headlines()[3]

---@diagnostic disable-next-line: invisible
org.capture:refile_file_headline_to_archive(source_headline)

assert.are.same({
'* TODO baz',
' :PROPERTIES:',
' :ARCHIVE_TIME: ' .. Date.now():to_string(),
' :ARCHIVE_FILE: ' .. capture_file.filename,
' :ARCHIVE_CATEGORY: ' .. capture_file:get_category(),
' :ARCHIVE_TODO: ' .. (source_headline:get_todo() or ''),
' :END:',
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
end)
end)
end)

describe('Capture', function()
Expand Down

0 comments on commit aa4a255

Please sign in to comment.