Skip to content

Commit 5eb9761

Browse files
committed
viewport: accept and insert spacing lines below header
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 #298
1 parent 925d8ed commit 5eb9761

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

taskwiki/viewport.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ def load_tasks(self):
325325
# Load all tasks below the viewport
326326
for i in range(self.line_number + 1, len(self.cache.buffer)):
327327
line = self.cache.buffer[i]
328+
if not line:
329+
continue
330+
328331
match = re.search(regexp.GENERIC_TASK, line)
329332

330333
if match:
@@ -367,15 +370,28 @@ def sync_with_taskwarrior(self):
367370
self.cache.remove_line(vimwikitask['line_number'])
368371

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

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

376-
for task in sorted_to_add:
377-
added_tasks += 1
378-
added_at = self.line_number + existing_tasks + added_tasks
378+
# Insert below headers (with separating lines), or below existing tasks
379+
if existing_tasks == 0:
380+
if self.cache.markup_syntax == 'markdown':
381+
md_h_style = util.get_var('vimwiki_markdown_header_style', 1)
382+
newlines = int(md_h_style)
383+
else:
384+
newlines = 0
385+
386+
for _ in range(newlines):
387+
self.cache.insert_line('', self.line_number + 1)
388+
389+
insert_start_line = self.line_number + newlines + 1
390+
else:
391+
insert_start_line = max(t['line_number'] for t in self.tasks) + 1
392+
393+
for i, task in enumerate(sorted_to_add):
394+
added_at = insert_start_line + i
379395

380396
# Add the task object to cache
381397
self.cache.task[short.ShortUUID(task['uuid'], self.tw)] = task
@@ -393,9 +409,8 @@ def sync_with_taskwarrior(self):
393409

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

412+
# Remove excess task lines beyond limit count
396413
if self.count is not None:
397-
for i in range(
398-
self.line_number + self.count,
399-
self.line_number + existing_tasks + added_tasks,
400-
):
401-
self.cache.remove_line(self.line_number + self.count + 1)
414+
task_lines = sorted(t['line_number'] for t in self.tasks)
415+
for excess_line in reversed(task_lines[self.count:]):
416+
self.cache.remove_line(excess_line)

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
'HEADER3': "=== %s ===",
1515
},
1616
'markdown': {
17-
'HEADER1': "# %s",
18-
'HEADER2': "## %s",
19-
'HEADER3': "### %s",
17+
'HEADER1': "# %s\n",
18+
'HEADER2': "## %s\n",
19+
'HEADER3': "### %s\n",
2020
}
2121
}
2222

tests/test_viewport.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,28 @@ def execute(self):
667667
# testfile3 only has header, so refresh on open makes changes
668668
self.client.edit(testfile3)
669669
assert self.client.eval('&modified') == '1'
670+
671+
672+
class TestViewportsNoChangePreserving(MultiSyntaxIntegrationTest):
673+
674+
viminput = """
675+
HEADER2(Work tasks | +work)
676+
677+
* [ ] tag work task #{uuid}
678+
679+
680+
HEADER2(Home tasks | +home)
681+
682+
683+
* [ ] tag home task #{uuid}
684+
"""
685+
686+
vimoutput = viminput
687+
688+
tasks = [
689+
dict(description="tag work task", tags=['work']),
690+
dict(description="tag home task", tags=['home']),
691+
]
692+
693+
def execute(self):
694+
self.command("w", regex="written$", lines=1)

0 commit comments

Comments
 (0)