Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewport: accept and insert spacing lines below header #400

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions taskwiki/viewport.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,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 @@ -367,15 +370,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 @@ -393,9 +409,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)