-
Notifications
You must be signed in to change notification settings - Fork 0
/
Typographer.py
97 lines (72 loc) · 2.8 KB
/
Typographer.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
import sys
import sublime
import sublime_plugin
is_python3 = sys.version_info[0] > 2
if is_python3:
from .typograph.typograph import Typograph
from .Edit import Edit as Edit
else:
from typograph import Typograph
class BaseTypographer(sublime_plugin.TextCommand):
"""Base Sorter"""
def __init__(self, view):
self.view = view
def run(self, edit):
self.settings = sublime.load_settings("Typographer.sublime-settings")
# sublime.save_settings('Typographer.sublime-settings')
selections = self.get_selections()
threads = []
for sel in selections:
selbody = self.view.substr(sel)
selbody = selbody.encode('utf-8')
thread = Typograph(sel, selbody,
self.settings.get('entity_type'),
self.settings.get('add_br_tags'),
self.settings.get('wrap_in_paragraph'),
self.settings.get('maximum_nobr'))
threads.append(thread)
thread.start()
self.handle_threads(edit, threads, selections, offset=0, i=0)
def get_selections(self):
selections = self.view.sel()
# check if the user has any actual selections
has_selections = False
for sel in selections:
if sel.empty() == False:
has_selections = True
# if not, add the entire file as a selection
if not has_selections:
full_region = sublime.Region(0, self.view.size())
selections.add(full_region)
return selections
def handle_threads(self, edit, threads, selections, offset=0, i=0):
next_threads = []
for thread in threads:
if thread.is_alive():
next_threads.append(thread)
continue
if thread.result == False:
continue
self.handle_result(edit, thread, selections, offset)
threads = next_threads
if len(threads):
sublime.set_timeout(lambda: self.handle_threads(edit, threads, selections, offset, i), 100)
return
self.view.end_edit(edit)
sublime.status_message('Successfully typographed text')
def handle_result(self, edit, thread, selections, offset):
result = thread.result
if thread.error:
sublime.error_message(result)
return
elif result is None:
sublime.error_message('There was an error typographing text.')
return
return thread
class TypographText(BaseTypographer):
def handle_result(self, edit, thread, selections, offset):
result = super(TypographText, self).handle_result(edit, thread, selections, offset)
sel = thread.sel
result = thread.result
with Edit(self.view) as edit:
edit.replace(sel, result);