-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.py
130 lines (100 loc) · 3.76 KB
/
command.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import sublime
import sublime_plugin
from .data_struct import Book, BooksManager
from .parse_word import parse_word
class TlwAddWord(sublime_plugin.TextCommand):
def run(self, edit):
book = Book("unpack")
def on_done(words):
for word in words.split("\n"):
book.add_word(word)
caption = "Add Word:"
window = sublime.active_window()
window.show_input_panel(caption, "", on_done, None, None)
class TlwRemoveWord(sublime_plugin.TextCommand):
def run(self, edit):
book = Book("unpack")
words = book.get_words()
def on_select(index):
if index < 0:
return
book.remove_word(words[index])
window = sublime.active_window()
window.show_quick_panel(words, on_select)
class TlwRemoveAllWord(sublime_plugin.TextCommand):
def run(self, edit):
book = Book("unpack")
book.remove_all_words()
class TlwCreateBook(sublime_plugin.TextCommand):
def run(self, edit):
print("tlw_create_book")
class TlwRemoveBook(sublime_plugin.TextCommand):
def run(self, edit):
print("tlw_remove_book")
class TlwTypingWord(sublime_plugin.TextCommand):
def run(self, edit):
self.book_manager = BooksManager()
book_list = self.book_manager.get_book_list()
if len(book_list) == 0:
return
if len(book_list) == 1:
return self.typing_book(book_list[0])
def on_select(index):
if index < 0:
return
self.typing_book(book_list[index])
window = sublime.active_window()
window.show_quick_panel(book_list, on_select)
def typing_book(self, book_name):
window = sublime.active_window()
view = window.new_file()
view.set_name("Typing Learn Word")
view.set_scratch(True)
words = self.book_manager.get_word_by_book_name(book_name)
settings = view.settings()
settings.set("isTypingLearnWord", True)
settings.set("words", words)
settings.set("examination", words)
class TlwNewWord(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
settings = view.settings()
examination = settings.get("examination", None)
if examination is None:
return
if len(examination) == 0:
if sublime.ok_cancel_dialog("Try Again?"):
settings.set("examination", settings.get("words", []))
view.run_command("tlw_new_word")
else:
view.close()
return
word = examination.pop()
settings.set("examination", examination)
if view.size() == 0:
view.insert(edit, 0, word + "\n")
else:
view.replace(edit, sublime.Region(0, view.size()), word + "\n")
class TlwViewEventListener(sublime_plugin.EventListener):
def on_modified(self, view):
settings = view.settings()
if not settings.get("isTypingLearnWord"):
return
contents = view.substr(sublime.Region(0, view.size()))
contents = contents.strip()
result = contents.split("\n")
if len(result) == 2:
if result[0].strip() == result[1].strip():
view.run_command("tlw_new_word")
def on_new(self, view):
sublime.set_timeout(lambda: view.run_command("tlw_new_word"), 10)
class TlwParseWord(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
contents = view.substr(sublime.Region(0, view.size()))
result = parse_word(contents)
window = sublime.active_window()
view = window.new_file()
view.set_name("Parse Word")
view.set_scratch(True)
view.insert(edit, 0, result + "\n")