-
Notifications
You must be signed in to change notification settings - Fork 26
/
itodo.py
50 lines (44 loc) · 1.9 KB
/
itodo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sublime, sublime_plugin
from datetime import datetime
class ItodoBase(sublime_plugin.TextCommand):
COMPLETED_MARK = "@done"
def run(self, edit):
filename = self.view.file_name()
# list of allowed filetypes
allowed_filetypes = ('.todo', '.txt')
if filename is None or not filename.endswith(allowed_filetypes):
return False
self.runCommand(edit)
class NewCommand(ItodoBase):
def runCommand(self, edit):
for region in self.view.sel():
line = self.view.line(region)
# don't add a newline when creating new item with cursor is at an empty line
if not line:
line_contents = '- '
self.view.insert(edit, line.begin(), line_contents)
# add a newline when creating new item when cursor is at another line
else:
line_contents = self.view.substr(line) + '\n- '
self.view.replace(edit, line, line_contents)
class CompleteCommand(ItodoBase):
def runCommand(self, edit):
for region in self.view.sel():
line = self.view.line(region)
line_head = self.view.find("[-\+]", line.begin())
line_contents = self.view.substr(line).strip()
# prepend @done if item is ongoing
if line_contents.startswith('-'):
self.view.insert(edit, line.end(), " "+ ItodoBase.COMPLETED_MARK +" (%s)" % datetime.now().strftime("%Y-%m-%d %H:%M"))
self.view.replace(edit, line_head, "+")
# undo @todo
elif line_contents.startswith('+'):
subfix = self.view.find('(\s)*'+ ItodoBase.COMPLETED_MARK +'(.)+\)$', line.begin())
self.view.erase(edit, subfix)
self.view.replace(edit, line_head, "-")
class ClearCommand(ItodoBase):
def runCommand(self, edit):
if not sublime.ok_cancel_dialog("Are you sure to clear all completed items?"):
return False
for region in self.view.find_all(ItodoBase.COMPLETED_MARK):
self.view.erase(edit, self.view.full_line(region))