Skip to content

Commit

Permalink
viewport: accept and insert spacing lines below header
Browse files Browse the repository at this point in the history
This allows the presence of empty lines between the task list and the
header in a viewport, which is especially desirable to abide by standard
Markdown conventions.

The header spacing of existing viewports is preserved. For new Markdown
task lists, the spacing now follows the one configured in vimwiki with
`g:vimwiki_markdown_header_style`.

GitHub: closes tools-life#298
  • Loading branch information
pacien authored and fullstopslash committed Feb 7, 2024
1 parent 4e08a69 commit 2b2592e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
33 changes: 24 additions & 9 deletions taskwiki/viewport.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ def load_tasks(self):
# Load all tasks below the viewport
for i in range(self.line_number + 1, len(self.cache.buffer)):
line = self.cache.buffer[i]
if not line:
continue

match = re.search(regexp.GENERIC_TASK, line)

if match:
Expand Down Expand Up @@ -373,15 +376,28 @@ def sync_with_taskwarrior(self):
self.cache.remove_line(vimwikitask['line_number'])

# Add the tasks that match the filter and are not listed
added_tasks = 0
existing_tasks = len(self.tasks)

sorted_to_add = list(to_add)
sorted_to_add.sort(key=lambda x: x['entry'])

for task in sorted_to_add:
added_tasks += 1
added_at = self.line_number + existing_tasks + added_tasks
# Insert below headers (with separating lines), or below existing tasks
if existing_tasks == 0:
if to_add and self.cache.markup_syntax == 'markdown':
md_h_style = util.get_var('vimwiki_markdown_header_style', 1)
newlines = int(md_h_style)
else:
newlines = 0

for _ in range(newlines):
self.cache.insert_line('', self.line_number + 1)

insert_start_line = self.line_number + newlines + 1
else:
insert_start_line = max(t['line_number'] for t in self.tasks) + 1

for i, task in enumerate(sorted_to_add):
added_at = insert_start_line + i

# Add the task object to cache
self.cache.task[short.ShortUUID(task['uuid'], self.tw)] = task
Expand All @@ -399,9 +415,8 @@ def sync_with_taskwarrior(self):

sort.TaskSorter(self.cache, self.tasks, self.sort).execute()

# Remove excess task lines beyond limit count
if self.count is not None:
for i in range(
self.line_number + self.count,
self.line_number + existing_tasks + added_tasks,
):
self.cache.remove_line(self.line_number + self.count + 1)
task_lines = sorted(t['line_number'] for t in self.tasks)
for excess_line in reversed(task_lines[self.count:]):
self.cache.remove_line(excess_line)
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
'HEADER3': "=== %s ===",
},
'markdown': {
'HEADER1': "# %s",
'HEADER2': "## %s",
'HEADER3': "### %s",
'HEADER1': "# %s\n",
'HEADER2': "## %s\n",
'HEADER3': "### %s\n",
}
}

Expand Down
25 changes: 25 additions & 0 deletions tests/test_viewport.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,28 @@ def execute(self):
# testfile3 only has header, so refresh on open makes changes
self.client.edit(testfile3)
assert self.client.eval('&modified') == '1'


class TestViewportsNoChangePreserving(MultiSyntaxIntegrationTest):

viminput = """
HEADER2(Work tasks | +work)
* [ ] tag work task #{uuid}
HEADER2(Home tasks | +home)
* [ ] tag home task #{uuid}
"""

vimoutput = viminput

tasks = [
dict(description="tag work task", tags=['work']),
dict(description="tag home task", tags=['home']),
]

def execute(self):
self.command("w", regex="written$", lines=1)

0 comments on commit 2b2592e

Please sign in to comment.