-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathrr_sequencer.py
More file actions
491 lines (411 loc) · 17.1 KB
/
rr_sequencer.py
File metadata and controls
491 lines (411 loc) · 17.1 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
"""
Reg Replace.
Licensed under MIT
Copyright (c) 2011 - 2016 Isaac Muse <isaacmuse@gmail.com>
"""
import sublime
import sublime_plugin
import re
from fnmatch import fnmatch
from RegReplace.rr_replacer import FindReplace
from RegReplace.rr_notify import error, deprecated, DEPRECATED_DOTALL
DEFAULT_SHOW_PANEL = False
DEFAULT_HIGHLIGHT_COLOR = 'invalid'
DEFAULT_HIGHLIGHT_STYLE = 'outline'
DEFAULT_MULTI_PASS_MAX_SWEEP = 100
MODULE_NAME = 'RegReplace'
rrsettings = {}
rrsettingsrules = {}
def underline(regions):
"""Convert to empty regions."""
new_regions = []
for region in regions:
start = region.begin()
end = region.end()
while start < end:
new_regions.append(sublime.Region(start))
start += 1
return new_regions
class RegReplaceGlobal(object):
"""Global object to aid in replacing text in a view."""
bfr = None
region = None
@classmethod
def clear(cls):
"""Clear buffer and region."""
cls.bfr = None
cls.region = None
class RegReplaceApplyCommand(sublime_plugin.TextCommand):
"""Command to replace text in a view."""
def run(self, edit):
"""Workaround replace text with info from `RegReplaceGlobal`."""
self.view.replace(edit, RegReplaceGlobal.region, RegReplaceGlobal.bfr)
class RegReplaceListenerCommand(sublime_plugin.EventListener):
"""Event listener command."""
def find_replacements(self, view):
"""Retrieve on save replacement rules."""
match = False
file_name = view.file_name()
if file_name is not None and rrsettings.get('on_save', False):
replacements = rrsettings.get('on_save_sequences', [])
scope = rrsettings.get('on_save_highlight_scope', None)
style = rrsettings.get('on_save_highlight_style', None)
self.options["key"] = MODULE_NAME
if scope is not None:
self.options["scope"] = scope
if style is not None:
self.options["style"] = style
for item in replacements:
found = False
if 'file_pattern' in item:
for pattern in item['file_pattern']:
if fnmatch(file_name, pattern):
found = True
self.select(item)
break
if not found and 'file_regex' in item:
for regex in item['file_regex']:
try:
flags = 0
if 'case' not in item or not bool(item['case']):
flags |= re.IGNORECASE
if 'dotall' in item and bool(item['dotall']):
deprecated(DEPRECATED_DOTALL)
flags |= re.DOTALL
r = re.compile(regex, flags)
if r.match(file_name) is not None:
found = True
self.select(item)
break
except Exception:
pass
match |= found
return match
def select(self, item):
"""Select action to perform."""
if "action" in item:
if item['action'] == "fold":
self.folds += item["sequence"]
elif item['action'] == "unfold":
self.unfolds += item["sequence"]
elif item['action'] == "mark":
self.highlights += item['sequence']
else:
error("action %s is not a valid action" % item["action"])
elif "highlight" in item and bool(item['highlight']):
sublime.message_dialog(
"RegReplace:\n\"on_save_sequence\" setting option '\"highlight\": true' is deprecated!"
"\nPlease use '\"action\": \"mark\"'."
)
self.highlights += item['sequence']
else:
self.replacements.append(
{
"sequence": item['sequence'],
"multi_pass": True if "multi_pass" in item and bool(item['multi_pass']) else False
}
)
def apply(self, view, replacements, options=None, multi_pass=False, action=None):
"""Run the actual RegReplace command."""
if options is None:
options = {}
view.run_command(
'reg_replace',
{
"replacements": replacements,
'action': action,
'options': options,
'multi_pass': multi_pass,
'no_selection': True
}
)
def on_pre_save(self, view):
"""Perform searches and specified action on file save."""
self.replacements = []
self.highlights = []
self.folds = []
self.unfolds = []
self.multi_pass = False
self.options = {}
if self.find_replacements(view):
for replacements in self.replacements:
self.apply(view, replacements['sequence'], multi_pass=replacements["multi_pass"])
if len(self.highlights) > 0:
self.apply(view, self.highlights, action="mark", options=self.options)
if len(self.folds) > 0:
self.apply(view, self.folds, action="fold")
if len(self.unfolds) > 0:
self.apply(view, self.unfolds, action="unfold")
class RegReplaceCommand(sublime_plugin.TextCommand):
"""RegReplace command."""
handshake = None
def forget_handshake(self):
"""Forget current view."""
self.handshake = None
self.clear_highlights(MODULE_NAME)
self.replace_obj.close()
def replace_prompt(self):
"""Prompt the user to see if they wish to replace the highlighted selections."""
# Ask if replacements are desired
self.handshake = self.view.id()
self.view.window().show_input_panel(
'Replace targets / perform action? (yes | no):',
'yes',
self.run_replace,
None,
self.forget_handshake
)
def run_replace(self, answer):
"""
Initiate a find with replace.
Used to initiate replacing when a find is executed with a replace prompt.
"""
# Do we want to replace
if answer.strip().lower() != 'yes':
self.forget_handshake()
return
# See if we know this view
window = sublime.active_window()
view = window.active_view() if window is not None else None
if view is not None:
if self.handshake is not None and self.handshake == view.id():
self.forget_handshake()
# re-run command to actually replace targets
view.run_command(
'reg_replace',
{
'replacements': self.replacements,
'action': self.action,
'multi_pass': self.multi_pass,
'regex_full_file_with_selections': self.full_file,
'options': self.options
}
)
else:
self.forget_handshake()
def clear_regions(self):
"""Clear highlighted regions specified."""
cleared = False
# Clear regions and exit
if self.clear:
self.clear_highlights(MODULE_NAME)
self.replace_obj.close()
cleared = True
elif self.action == 'unmark' and 'key' in self.options:
self.perform_action()
self.replace_obj.close()
cleared = True
return cleared
def set_highlights(self, key, style, color):
"""Mark regions with specified highlight options."""
# Process highlight style
highlight_style = 0
if (self.find_only and self.selection_only) or style == 'underline':
# Use underline if explicitly requested,
# or if doing a find only when under a selection only (only underline can be seen through a selection)
self.replace_obj.target_regions = underline(self.replace_obj.target_regions)
highlight_style = sublime.DRAW_EMPTY_AS_OVERWRITE
elif style == 'outline':
highlight_style = sublime.DRAW_OUTLINED
# highlight all of the found regions
self.view.erase_regions(key)
self.view.add_regions(
key,
self.replace_obj.target_regions,
color,
"",
highlight_style
)
def clear_highlights(self, key):
"""Clear all highlighted regions of given key."""
self.view.erase_regions(key)
def is_selection_available(self):
"""See if there are selections in document."""
available = False
for sel in self.view.sel():
if sel.size() > 0:
available = True
break
return available
def ignore_ending_newlines(self, regions):
"""Ignore newlines at the end of the region; newlines okay in the middle of region."""
new_regions = []
for region in regions:
offset = 0
size = region.size()
if size > offset and self.view.substr(region.end() - 1) == '\n':
offset += 1
if size > offset and self.view.substr(region.end() - offset - 1) == '\r':
offset += 1
new_regions.append(sublime.Region(region.begin(), region.end() - offset))
return new_regions
def print_results_status_bar(self, text):
"""Print results to the status bar."""
sublime.status_message(text)
def print_results_panel(self, text):
"""Print find results to an output panel."""
# Get/create output panel
window = self.view.window()
view = window.get_output_panel('reg_replace_results')
# Turn off styles in panel
view.settings().set('draw_white_space', 'none')
view.settings().set('draw_indent_guides', False)
view.settings().set('gutter', 'none')
view.settings().set('line_numbers', False)
view.set_syntax_file('Packages/Text/Plain text.tmLanguage')
# Show Results in read only panel and clear selection in panel
window.run_command('show_panel', {'panel': 'output.reg_replace_results'})
view.set_read_only(False)
RegReplaceGlobal.bfr = 'RegReplace Results\n\n' + text
RegReplaceGlobal.region = sublime.Region(0, view.size())
view.run_command("reg_replace_apply")
RegReplaceGlobal.clear()
view.set_read_only(True)
view.sel().clear()
def perform_action(self):
"""Perform action on targeted text."""
status = True
if self.action == 'fold':
# Fold regions
self.view.fold(self.ignore_ending_newlines(self.replace_obj.target_regions))
elif self.action == 'unfold':
# Unfold regions
try:
self.view.unfold(self.ignore_ending_newlines(self.replace_obj.target_regions))
except Exception:
error("Cannot unfold! Please upgrade to the latest stable beta build to remove this error.")
elif self.action == 'mark':
# Mark targeted regions
if 'key' in self.options:
color = self.options['scope'].strip() if 'scope' in self.options else DEFAULT_HIGHLIGHT_COLOR
style = self.options['style'].strip() if 'style' in self.options else DEFAULT_HIGHLIGHT_STYLE
self.set_highlights(self.options['key'].strip(), style, color)
elif self.action == 'unmark':
# Unmark targeted regions
if 'key' in self.options:
self.clear_highlights(self.options['key'].strip())
elif self.action == 'select':
self.view.sel().clear()
self.view.sel().add_all(self.replace_obj.target_regions)
else:
# Not a valid action
status = False
return status
def find_and_replace(self):
"""
Walk the sequence finding the targeted regions in the text.
If allowed, replacements will be done as well.
"""
if self.use_test_buffer:
replace_list = sublime.load_settings('reg_replace_test.sublime-settings').get('replacements', {})
else:
replace_list = rrsettingsrules.get('replacements', {})
result_template = '%s: %d regions;\n' if self.panel_display else '%s: %d regions; '
results = ''
# Walk the sequence
# Multi-pass only if requested and will be occurring
if self.multi_pass and not self.find_only and self.action is None:
# Multi-pass initialization
current_replacements = 0
total_replacements = 0
count = 0
# Sweep file until all instances are found
# Avoid infinite loop and break out if sweep threshold is met
while count < self.max_sweeps:
count += 1
current_replacements = 0
for replacement in self.replacements:
# Is replacement available in the list?
if replacement in replace_list:
pattern = replace_list[replacement]
current_replacements += self.replace_obj.search(pattern, 'scope' in pattern)
total_replacements += current_replacements
# No more regions found?
if current_replacements == 0:
break
# Record total regions found
results += 'Regions Found: %d regions;' % total_replacements
else:
not_found = []
for replacement in self.replacements:
# Is replacement available in the list?
if replacement in replace_list:
pattern = replace_list[replacement]
results += result_template % (replacement, self.replace_obj.search(pattern, 'scope' in pattern))
else:
not_found.append(replacement)
if not_found:
error("%d rules not found! See console." % len(not_found))
print('\n'.join(['RegReplace: "%s" not found!' % r for r in not_found]))
return results
def start_sequence(self):
"""Run the replace sequence."""
# Find targets and replace if applicable
results = self.find_and_replace()
if self.find_only:
# Highlight regions
style = rrsettings.get('find_highlight_style', DEFAULT_HIGHLIGHT_STYLE)
color = rrsettings.get('find_highlight_color', DEFAULT_HIGHLIGHT_COLOR)
self.set_highlights(MODULE_NAME, style, color)
self.replace_prompt()
else:
self.clear_highlights(MODULE_NAME)
# Perform action
if self.action is not None:
if not self.perform_action():
results = 'Error: %s - Bad Action!' % self.action
# Report results
if self.panel_display:
self.print_results_panel(results)
else:
self.print_results_status_bar(results)
self.replace_obj.close()
def run(
self, edit, replacements=None,
find_only=False, clear=False, action=None,
multi_pass=False, no_selection=False, regex_full_file_with_selections=False,
options=None, use_test_buffer=False
):
"""Kick off sequence."""
if replacements is None:
replacements = []
if options is None:
options = {}
self.use_test_buffer = bool(use_test_buffer)
self.find_only = bool(find_only)
self.action = action.strip() if action is not None else action
self.full_file = bool(regex_full_file_with_selections)
if not no_selection and rrsettings.get('selection_only', False) and self.is_selection_available():
self.selection_only = True
else:
self.selection_only = False
self.max_sweeps = rrsettings.get('multi_pass_max_sweeps', DEFAULT_MULTI_PASS_MAX_SWEEP)
self.replacements = replacements
self.multi_pass = bool(multi_pass)
self.panel_display = rrsettings.get('results_in_panel', DEFAULT_SHOW_PANEL)
self.options = options
self.clear = clear
self.replace_obj = FindReplace(
self.view,
edit,
self.find_only,
self.full_file,
self.selection_only,
self.max_sweeps,
self.action
)
# Clear regions and exit; no need to run sequences
if self.clear_regions():
self.replace_obj.close()
return
# Is the sequence empty?
if len(self.replacements) > 0:
self.start_sequence()
else:
self.replace_obj.close()
def plugin_loaded():
"""Setup plugin."""
global rrsettings
global rrsettingsrules
rrsettings = sublime.load_settings('reg_replace.sublime-settings')
rrsettingsrules = sublime.load_settings('reg_replace_rules.sublime-settings')