-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSScomb.py
129 lines (97 loc) · 4.07 KB
/
CSScomb.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
# coding: utf-8
import sublime
import sublime_plugin
from csscomb import HerokuSort, LocalSort
def to_unicode_or_bust(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
class BaseSorter(sublime_plugin.TextCommand):
"""Base Sorter"""
def __init__(self, view):
self.view = view
def run(self, edit):
sortorder = ''
self.settings = sublime.load_settings("CSScomb.sublime-settings")
if not self.settings.has('sorter'):
self.settings.set('sorter', 'local')
sublime.save_settings('CSScomb.sublime-settings')
if self.settings.get('custom_sort_order') == True:
self.order_settings = sublime.load_settings("Order.sublime-settings")
sortorder = self.order_settings.get('sort_order')
sublime.status_message('Sorting with custom sort order...')
selections = self.get_selections()
SorterCall = self.get_sorter()
threads = []
for sel in selections:
selbody = self.view.substr(sel)
selbody = selbody.encode('utf-8')
thread = SorterCall(sel, selbody, sortorder)
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 get_sorter(self):
sorter = self.settings.get('sorter', "hosted")
sorters = {
'heroku': HerokuSort,
'hosted': LocalSort
}
return sorters[sorter] if sorter in sorters else sorters['hosted']
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 sorted')
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 sorting CSS.')
return
return thread
class CssSorter(BaseSorter):
def handle_result(self, edit, thread, selections, offset):
result = super(CssSorter, self).handle_result(edit, thread, selections, offset)
sel = thread.sel
result = to_unicode_or_bust(thread.result)
# if offset:
# sel = sublime.Region(thread.sel.begin() + offset, thread.sel.end() + offset)
if not thread.error:
self.view.replace(edit, sel, result)
class ChangeSorterToLocalCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
self.settings = sublime.load_settings("CSScomb.sublime-settings")
self.settings.set("sorter", "local")
sublime.save_settings('CSScomb.sublime-settings')
sublime.status_message('Switched to local php sorting')
class ChangeSorterToHerokuCommand(sublime_plugin.WindowCommand):
def run(self, paths=[]):
self.settings = sublime.load_settings("CSScomb.sublime-settings")
self.settings.set("sorter", "heroku")
sublime.save_settings('CSScomb.sublime-settings')
sublime.status_message('Switched to server sorting')