forked from jonlabelle/Trimmer
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trimmer.py
365 lines (292 loc) · 12.2 KB
/
Trimmer.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# -*- coding: utf-8 -*-
import re
import sublime
import sublime_plugin
def selections(view, default_to_all=True):
regions = [r for r in view.sel() if not r.empty()]
if not regions and default_to_all:
regions = [sublime.Region(0, view.size())]
return regions
def contains(needle, haystack):
if not needle or not haystack:
return False
return needle in haystack
class TrimmerCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
regions = view.find_all('[\t ]+$')
regions.reverse()
for region in regions:
view.erase(edit, region)
has_matches = True
if has_matches:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: trailing whitespace removed.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no trailing whitespace found.'), 0)
class DeleteEmptyLinesCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile('^[ \t]*$\r?\n', re.MULTILINE)
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: empty lines deleted.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no empty lines to delete.'), 0)
class RemoveBlankSpaces(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile(r'[ \t\r\n\v\f]')
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: blanks spaces removed.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no blank spaces to remove.'), 0)
class TrimLeadingWhitespaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile('^[ \t]+', re.MULTILINE)
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: leading whitespace removed.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no leading whitespace to remove.'), 0)
class TrimLeadingTrailingWhitespace(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile('^[ \t]+|[\t ]+$', re.MULTILINE)
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: leading and trailing whitespace removed.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no leading or trailing whitespace to remove.'), 0)
class CollapseLines(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile(r'(?:\s*)(\r?\n)(?:\s*)(?:\r?\n+)')
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub(r'\1\1', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: lines collapsed.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no lines to collapse.'), 0)
class CollapseSpaces(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile('([ ])[ ]+')
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub(r'\1', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: spaces collapsed.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no spaces to collapse.'), 0)
class NormalizeSpaces(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile('(^\\s+)|\\s(?=\\s+)|(\\s+$)')
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: spaces normalized.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no spaces to normalize.'), 0)
class TokenizeString(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile('^ |\t+|( ){2,}|\t| |\t+$', re.MULTILINE)
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: string tokenized.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: nothing to tokenize.'), 0)
class TrimEdges(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile(r'(\A\s+|\s+\Z)')
if view.size() > 0:
region = sublime.Region(0, view.size())
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
return sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: file edges trimmed.'), 0)
else:
return sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no file edges to trim.'), 0)
class DeleteEmptyTags(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
reobj = re.compile(r'<([A-Z][A-Z0-9]*)\b[^>]*>\s*</\1>', re.IGNORECASE)
for region in selections(view):
str_buffer = view.substr(region)
trimmed = reobj.sub('', str_buffer)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: empty tags deleted.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no empty tags to delete.'), 0)
class TrimSelections(sublime_plugin.TextCommand):
def run(self, edit):
"""
Trim leading and trailing whitespace from selections.
Originally from the 'MultiEditUtils' Plug-in
https://github.com/philippotto/Sublime-MultiEditUtils
"""
view = self.view
selection = view.sel()
new_regions = []
for current_region in selection:
text = view.substr(current_region)
l_stripped_text = text.lstrip()
r_stripped_text = l_stripped_text.rstrip()
l_stripped_count = len(text) - len(l_stripped_text)
r_stripped_count = len(l_stripped_text) - len(r_stripped_text)
a = current_region.begin() + l_stripped_count
b = current_region.end() - r_stripped_count
if a == b:
# the region only contained whitespace
# use the old selection end to avoid jumping of cursor
a = b = current_region.b
new_regions.append(sublime.Region(a, b))
selection.clear()
for region in new_regions:
selection.add(region)
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: selections trimmed.'), 0)
class RemoveComments(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
re_single_line_comment = re.compile(r"//.*$", re.MULTILINE)
re_hash_comment = re.compile("#[^!].*$", re.MULTILINE)
re_html_comment = re.compile("<!--.*?-->", re.DOTALL)
re_block_comment = re.compile(r"/\*.*?\*/", re.DOTALL)
re_ini_comment = re.compile(r"^(?:\s+)?;.*$", re.MULTILINE)
for region in selections(view):
str_buffer = view.substr(region)
#
# TODO: re-work this brute force approach and filter by syntax/scope
trimmed = re_single_line_comment.sub('', str_buffer)
trimmed = re_hash_comment.sub('', trimmed)
trimmed = re_html_comment.sub('', trimmed)
trimmed = re_block_comment.sub('', trimmed)
trimmed = re_ini_comment.sub('', trimmed)
if str_buffer != trimmed:
view.replace(edit, region, trimmed)
has_matches = True
if has_matches is True:
view.run_command('collapse_lines')
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: comments removed.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no comments to remove.'), 0)
class ReplaceSmartCharactersCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
has_matches = False
smart_replacements = [
[u'[’‘′‚]', u'\''],
[u'[“”″]', u'"'],
[u'[„]', u'"'],
[u'[…]', u'...'],
[u'[—]', u'---'],
[u'[–]', u'--'],
[u'[•]', u'*'],
[u'[·]', u'-'],
[u'[ ]', u' '],
[u'[ ]', u' '],
[u'[ ]', u' '],
[u'[«]', u'<<'],
[u'[»]', u'>>'],
[u'[©]', u'(C)'],
[u'[®]', u'(R)'],
[u'[™]', u'(TM)']
]
for replacement in smart_replacements:
for region in selections(view):
source_text = view.substr(region)
if len(source_text) > 0:
replaced_text = re.sub(
replacement[0], replacement[1], source_text)
if source_text != replaced_text:
view.replace(edit, region, replaced_text)
has_matches = True
if has_matches is True:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: smart characters replaced.'), 0)
else:
sublime.set_timeout(lambda: sublime.status_message(
'Trimmer: no smart characters to replace.'), 0)