forked from facelessuser/RegReplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reg_replace.py
executable file
·879 lines (789 loc) · 34.8 KB
/
reg_replace.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
'''
Reg Replace
Licensed under MIT
Copyright (c) 2011 - 2012 Isaac Muse <[email protected]>
'''
import sublime
import sublime_plugin
import re
from fnmatch import fnmatch
DEFAULT_SHOW_PANEL = False
DEFAULT_HIGHLIGHT_COLOR = 'invalid'
DEFAULT_HIGHLIGHT_STYLE = 'outline'
DEFAULT_MULTI_PASS_MAX_SWEEP = 100
MAX_UNFOLD_THRESHOLD = 1000
MODULE_NAME = 'RegReplace'
rrsettings = sublime.load_settings('reg_replace.sublime-settings')
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 RegReplaceInputCommand(sublime_plugin.WindowCommand):
def run_sequence(self, value):
find_only = False
action = None
multi_pass = False
options = {}
# Parse Input
matches = re.match(r"(\?)?\s*([^\+][\w\W]*|\+?)\s*:\s*([\w\W]*)\s*", value)
if matches != None:
# Sequence
value = matches.group(3)
# Find Only?
if matches.group(1) == '?':
find_only = True
# Multi-Pass?
if matches.group(2) == '+':
multi_pass = True
# Action?
elif matches.group(2) != '' and matches.group(2) != None:
# Mark or unmark: parse options?
params = re.match(
r"^(unmark|mark)\s*=\s*([\w\s\.\-]*)\s*(?:,\s*([\w\s\.\-]*)\s*)?(?:,\s*([\w\s\.\-]*))?\s*",
matches.group(2)
)
if params != None:
if params.group(2) != '' and params.group(2) != None:
# Mark options
if params.group(1) == 'mark':
options['key'] = params.group(2).strip()
if params.group(3) != '' and params.group(3) != None:
options['scope'] = params.group(3).strip()
if params.group(4) != '' and params.group(4) != None:
options['style'] = params.group(4).strip()
action = params.group(1)
# Unmark options
elif params.group(1) == 'unmark':
options['key'] = params.group(2)
action = params.group(1)
else:
# All other actions
action = matches.group(2)
# Parse returned regex sequence
sequence = [x.strip() for x in value.split(',')]
view = self.window.active_view()
# Execute sequence
if view != None:
view.run_command(
'reg_replace',
{
'replacements': sequence,
'find_only': find_only,
'action': action,
'multi_pass': multi_pass,
'options': options
}
)
def run(self):
# Display RegReplace input panel for on the fly regex sequences
self.window.show_input_panel(
'Regex Sequence:',
'',
self.run_sequence,
None,
None
)
class RegReplaceListenerCommand(sublime_plugin.EventListener):
def find_replacements(self, view):
match = False
file_name = view.file_name()
if file_name != 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.action = "mark"
self.options["key"] = MODULE_NAME
if scope != None:
self.options["scope"] = scope
if style != 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
if "highlight" in item and bool(item['highlight']):
self.highlights += item['sequence']
else:
multi_pass = True if "multi_pass" in item and bool(item['multi_pass']) else False
self.replacements.append({"sequence": item['sequence'], "multi_pass": multi_pass})
break
if not found and 'file_regex' in item:
for regex in item['file_regex']:
try:
r = re.compile(regex, re.IGNORECASE) if not 'case' in item or not bool(item['case']) else re.compile(regex)
if r.match(file_name) != None:
found = True
if "highlight" in item and bool(item['highlight']):
self.highlights += item['sequence']
else:
multi_pass = True if "multi_pass" in item and bool(item['multi_pass']) else False
self.replacements.append({"sequence": item['sequence'], "multi_pass": multi_pass})
break
except:
pass
match |= found
return match
def on_pre_save(self, view):
self.replacements = []
self.highlights = []
self.action = None
self.multi_pass = False
self.options = {}
if self.find_replacements(view):
edit = view.begin_edit()
reg_replace_cmd = RegReplaceCommand(view)
for replacements in self.replacements:
reg_replace_cmd.run(
edit,
replacements=replacements['sequence'],
multi_pass=replacements['multi_pass'],
no_selection=True
)
if len(self.highlights) > 0:
reg_replace_cmd.run(
edit,
replacements=self.highlights,
action=self.action,
options=self.options,
no_selection=True
)
view.end_edit(edit)
class RegReplaceCommand(sublime_plugin.TextCommand):
handshake = None
def forget_handshake(self):
# Forget current view
self.handshake = None
self.clear_highlights(MODULE_NAME)
def replace_prompt(self):
# Ask if replacements are desired
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):
# 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 != None else None
if view != None:
if self.handshake != 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,
'options': self.options
}
)
else:
self.forget_handshake()
def set_highlights(self, key, style, color):
# Process highlight style
highlight_style = 0
if (self.find_only and self.selection_only) or style == 'underline':
# Use underline if explicity requested,
# or if doing a find only when under a selection only (only underline can be seen through a selection)
self.target_regions = underline(self.target_regions)
highlight_style = sublime.DRAW_EMPTY_AS_OVERWRITE
elif style == 'outline':
highlight_style = sublime.DRAW_OUTLINED
# higlight all of the found regions
self.view.erase_regions(key)
self.view.add_regions(
key,
self.target_regions,
color,
highlight_style
)
def clear_highlights(self, key):
# Clear all highlighted regions
self.view.erase_regions(key)
def ignore_ending_newlines(self, regions):
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):
sublime.status_message(text)
def print_results_panel(self, text):
# Get/create output panel
window = self.view.window()
view = window.get_output_panel('reg_replace_results')
#Turn off stylings 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)
edit = view.begin_edit()
view.replace(edit, sublime.Region(0, view.size()), 'RegReplace Results\n\n' + text)
view.end_edit(edit)
view.set_read_only(True)
view.sel().clear()
def perform_action(self, action, options={}):
status = True
if action == 'fold':
# Ignore newlines at the end of the region; newlines okay in the middle of region
self.view.fold(self.ignore_ending_newlines(self.target_regions))
elif action == 'unfold':
try:
# Unfold regions
self.view.unfold(self.ignore_ending_newlines(self.target_regions))
except:
sublime.error_message("Cannot unfold! Please upgrade to the latest stable beta build to remove this error.")
elif action == 'mark':
# Mark targeted regions
if 'key' in options:
color = options['scope'].strip() if 'scope' in options else DEFAULT_HIGHLIGHT_COLOR
style = options['style'].strip() if 'style' in options else DEFAULT_HIGHLIGHT_STYLE
self.set_highlights(options['key'].strip(), style, color)
elif action == 'unmark':
# Unmark targeted regions
if 'key' in options:
self.clear_highlights(options['key'].strip())
else:
status = False
return status
def get_sel_point(self):
# See if there is a cursor and get the first selections starting point
sel = self.view.sel()
pt = None if len(sel) == 0 else sel[0].begin()
return pt
def qualify_by_scope(self, region, pattern):
for entry in pattern:
# Is there something to qualify?
if len(entry) > 0:
# Initialize qualification parameters
qualify = True
pt = region.begin()
end = region.end()
# Disqualify if entirely of scope
if entry.startswith('-!'):
entry = entry.lstrip('-!')
qualify = False
while pt < end:
if self.view.score_selector(pt, entry) == 0:
qualify = True
break
pt += 1
# Disqualify if one or more instances of scope
elif entry.startswith('-'):
entry = entry.lstrip('-')
while pt < end:
if self.view.score_selector(pt, entry):
qualify = False
break
pt += 1
# Qualify if entirely of scope
elif entry.startswith('!'):
entry = entry.lstrip('!')
while pt < end:
if self.view.score_selector(pt, entry) == 0:
qualify = False
break
pt += 1
# Qualify if one or more instances of scope
else:
qualify = False
while pt < end:
if self.view.score_selector(pt, entry):
qualify = True
break
pt += 1
# If qualificatin of one fails, bail
if qualify == False:
return qualify
# Qualification completed successfully
return True
def greedy_replace(self, find, replace, regions, scope_filter):
# Initialize replace
replaced = 0
count = len(regions) - 1
# Step through all targets and qualify them for replacement
for region in reversed(regions):
# Does the scope qualify?
qualify = self.qualify_by_scope(region, scope_filter) if scope_filter != None else True
if qualify:
replaced += 1
if self.find_only or self.action != None:
# If "find only" or replace action is overridden, just track regions
self.target_regions.append(region)
else:
# Apply replace
self.view.replace(self.edit, region, replace[count])
count -= 1
return replaced
def non_greedy_replace(self, find, replace, regions, scope_filter):
# Initialize replace
replaced = 0
last_region = len(regions) - 1
selected_region = None
selection_index = 0
# See if there is a cursor and get the first selections starting point
pt = self.get_sel_point()
# Intialize with first qualifying region for wrapping and the case of no cursor in view
count = 0
for region in regions:
# Does the scope qualify?
qualify = self.qualify_by_scope(region, scope_filter) if scope_filter != None else True
if qualify:
# Update as new replacement candidate
selected_region = region
selection_index = count
break
else:
count += 1
# If regions were already swept till the end, skip calculation relative to cursor
if selected_region != None and count < last_region and pt != None:
# Try and find the first qualifying match contained withing the first selection or after
reverse_count = last_region
for region in reversed(regions):
# Make sure we are not checking previously checked regions
# And check if region contained after start of selection?
if reverse_count >= count and region.end() - 1 >= pt:
# Does the scope qualify?
qualify = self.qualify_by_scope(region, scope_filter) if scope_filter != None else True
if qualify:
# Update as new replacement candidate
selected_region = region
selection_index = reverse_count
# Walk backwards through replace index
reverse_count -= 1
else:
break
# Did we find a suitable region?
if selected_region != None:
# Show Instance
replaced += 1
self.view.show(selected_region.begin())
if self.find_only or self.action != None:
# If "find only" or replace action is overridden, just track regions
self.target_regions.append(selected_region)
else:
# Apply replace
self.view.replace(self.edit, selected_region, replace[selection_index])
return replaced
def apply_scope_regex(self, string, re_find, replace, greedy_replace, multi):
replaced = 0
extraction = string
if multi and not self.find_only and self.action == None:
extraction, replaced = self.apply_multi_pass_scope_regex(string, extraction, re_find, replace, greedy_replace)
else:
if greedy_replace:
extraction, replaced = re.subn(re_find, replace, string)
else:
extraction, replaced = re.subn(re_find, replace, string, 1)
return extraction, replaced
def apply_multi_pass_scope_regex(self, string, extraction, re_find, replace, greedy_replace):
multi_replaced = 0
count = 0
total_replaced = 0
while count < self.max_sweeps:
count += 1
if greedy_replace:
extraction, multi_replaced = re.subn(re_find, replace, extraction)
else:
extraction, multi_replaced = re.subn(re_find, replace, extraction, 1)
if multi_replaced == 0:
break
total_replaced += multi_replaced
return extraction, total_replaced
def greedy_scope_literal_replace(self, regions, find, replace, greedy_replace):
total_replaced = 0
for region in reversed(regions):
extraction = self.view.substr(region)
replaced = 0
try:
extraction.index(find)
replaced = 1
if greedy_replace:
extraction = extraction.replace(find, replace)
else:
extraction = extraction.replace(find, replace, 1)
except ValueError:
pass
if replaced > 0:
total_replaced += 1
if self.find_only or self.action != None:
self.target_regions.append(region)
else:
self.view.replace(self.edit, region, extraction)
return total_replaced
def non_greedy_scope_literal_replace(self, regions, find, replace, greedy_replace):
# Initialize replace
total_replaced = 0
replaced = 0
last_region = len(regions) - 1
selected_region = None
selected_extraction = None
# See if there is a cursor and get the first selections starting point
pt = self.get_sel_point()
# Intialize with first qualifying region for wrapping and the case of no cursor in view
count = 0
for region in regions:
extraction = self.view.substr(region)
replaced = 0
try:
extraction.index(find)
replaced = 1
if greedy_replace:
extraction = extraction.replace(find, replace)
else:
extraction = extraction.replace(find, replace, 1)
except ValueError:
pass
if replaced > 0:
selected_region = region
selected_extraction = extraction
break
else:
count += 1
# If regions were already swept till the end, skip calculation relative to cursor
if selected_region != None and count < last_region and pt != None:
# Try and find the first qualifying match contained withing the first selection or after
reverse_count = last_region
for region in reversed(regions):
# Make sure we are not checking previously checked regions
# And check if region contained after start of selection?
if reverse_count >= count and region.end() - 1 >= pt:
extraction = self.view.substr(region)
replaced = 0
try:
extraction.index(find)
replaced = 1
if greedy_replace:
extraction = extraction.replace(find, replace)
else:
extraction = extraction.replace(find, replace, 1)
except ValueError:
pass
if replaced > 0:
selected_region = region
selected_extraction = extraction
reverse_count -= 1
else:
break
# Did we find a suitable region?
if selected_region != None:
# Show Instance
total_replaced += 1
self.view.show(selected_region.begin())
if self.find_only or self.action != None:
# If "find only" or replace action is overridden, just track regions
self.target_regions.append(selected_region)
else:
# Apply replace
self.view.replace(self.edit, selected_region, selected_extraction)
return total_replaced
def greedy_scope_replace(self, regions, re_find, replace, greedy_replace, multi):
total_replaced = 0
try:
for region in reversed(regions):
replaced = 0
string = self.view.substr(region)
extraction, replaced = self.apply_scope_regex(string, re_find, replace, greedy_replace, multi)
if replaced > 0:
total_replaced += 1
if self.find_only or self.action != None:
self.target_regions.append(region)
else:
self.view.replace(self.edit, region, extraction)
except Exception, err:
sublime.error_message('REGEX ERROR: %s' % str(err))
return total_replaced
return total_replaced
def non_greedy_scope_replace(self, regions, re_find, replace, greedy_replace, multi):
# Initialize replace
total_replaced = 0
replaced = 0
last_region = len(regions) - 1
selected_region = None
selected_extraction = None
# See if there is a cursor and get the first selections starting point
pt = self.get_sel_point()
# Intialize with first qualifying region for wrapping and the case of no cursor in view
count = 0
try:
for region in regions:
string = self.view.substr(region)
extraction, replaced = self.apply_scope_regex(string, re_find, replace, greedy_replace, multi)
if replaced > 0:
selected_region = region
selected_extraction = extraction
break
else:
count += 1
except Exception, err:
sublime.error_message('REGEX ERROR: %s' % str(err))
return total_replaced
try:
# If regions were already swept till the end, skip calculation relative to cursor
if selected_region != None and count < last_region and pt != None:
# Try and find the first qualifying match contained withing the first selection or after
reverse_count = last_region
for region in reversed(regions):
# Make sure we are not checking previously checked regions
# And check if region contained after start of selection?
if reverse_count >= count and region.end() - 1 >= pt:
string = self.view.substr(region)
extraction, replaced = self.apply_scope_regex(string, re_find, replace, greedy_replace, multi)
if replaced > 0:
selected_region = region
selected_extraction = extraction
reverse_count -= 1
else:
break
except Exception, err:
sublime.error_message('REGEX ERROR: %s' % str(err))
return total_replaced
# Did we find a suitable region?
if selected_region != None:
# Show Instance
total_replaced += 1
self.view.show(selected_region.begin())
if self.find_only or self.action != None:
# If "find only" or replace action is overridden, just track regions
self.target_regions.append(selected_region)
else:
# Apply replace
self.view.replace(self.edit, selected_region, selected_extraction)
return total_replaced
def select_scope_regions(self, regions, greedy_scope):
if greedy_scope:
# Greedy scope; return all scopes
replaced = len(regions)
self.target_regions = regions
else:
# Non-greedy scope; return first valid scope
# If cannot find first valid scope after cursor
number_regions = len(regions)
selected_region = None
first_region = 0
last_region = number_regions - 1
pt = self.get_sel_point()
# Find first scope
if number_regions > 0:
selected_region = regions[0]
# Walk backwards seeing which scope is valid
# Quit if you reach the already selected first scope
if selected_region != None and last_region > first_region and pt != None:
reverse_count = last_region
for region in reversed(regions):
if reverse_count >= first_region and region.end() - 1 >= pt:
selected_region = region
reverse_count -= 1
else:
break
# Store the scope if we found one
if selected_region != None:
replaced += 1
self.view.show(selected_region.begin())
self.target_regions = [selected_region]
return replaced
def scope_apply(self, pattern):
# Initialize replacement variables
replaced = 0
regions = []
# Grab pattern definitions
scope = pattern['scope']
find = pattern['find'] if 'find' in pattern else None
replace = pattern['replace'] if 'replace' in pattern else '\\0'
greedy_scope = bool(pattern['greedy_scope']) if 'greedy_scope' in pattern else True
greedy_replace = bool(pattern['greedy_replace']) if 'greedy_replace' in pattern else True
case = bool(pattern['case']) if 'case' in pattern else True
multi = bool(pattern['multi_pass_regex']) if 'multi_pass_regex' in pattern else False
literal = bool(pattern['literal']) if 'literal' in pattern else False
if scope == None or scope == '':
return replace
regions = self.view.find_by_selector(scope)
if self.selection_only:
regions = self.filter_by_selection(regions)
# Find supplied?
if find != None:
# Compile regex: Ignore case flag?
if not literal:
try:
re_find = re.compile(find, re.IGNORECASE) if case else re.compile(find)
except Exception, err:
sublime.error_message('REGEX ERROR: %s' % str(err))
return replaced
#Greedy Scope?
if greedy_scope:
replaced = self.greedy_scope_replace(regions, re_find, replace, greedy_replace, multi)
else:
replaced = self.non_greedy_scope_replace(regions, re_find, replace, greedy_replace, multi)
else:
if greedy_scope:
replaced = self.greedy_scope_literal_replace(regions, find, replace, greedy_replace)
else:
replaced = self.non_greedy_scope_literal_replace(regions, find, replace, greedy_replace)
else:
replaced = self.select_scope_regions(regions, greedy_scope)
return replaced
def apply(self, pattern):
# Initialize replacement variables
regions = []
flags = 0
replaced = 0
# Grab pattern definitions
find = pattern['find']
replace = pattern['replace'] if 'replace' in pattern else '\\0'
literal = bool(pattern['literal']) if 'literal' in pattern else False
greedy = bool(pattern['greedy']) if 'greedy' in pattern else True
case = bool(pattern['case']) if 'case' in pattern else True
scope_filter = pattern['scope_filter'] if 'scope_filter' in pattern else []
# Ignore Case?
if not case:
flags |= sublime.IGNORECASE
# Literal find?
if literal:
flags |= sublime.LITERAL
# Find and format replacements
extractions = []
try:
regions = self.view.find_all(find, flags, replace, extractions)
except Exception, err:
sublime.error_message('REGEX ERROR: %s' % str(err))
return replaced
if self.selection_only:
regions, extractions = self.filter_by_selection(regions, extractions)
# Where there any regions found?
if len(regions) > 0:
# Greedy or non-greedy search? Get replaced instances.
if greedy:
replaced = self.greedy_replace(find, extractions, regions, scope_filter)
else:
replaced = self.non_greedy_replace(find, extractions, regions, scope_filter)
return replaced
def find_and_replace(self):
replace_list = rrsettings.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 occuring
if self.multi_pass and not self.find_only and self.action == 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]
# Search within a specific scope or search and qualify with scopes
if 'scope' in pattern:
current_replacements += self.scope_apply(pattern)
else:
current_replacements += self.apply(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:
for replacement in self.replacements:
# Is replacement available in the list?
if replacement in replace_list:
pattern = replace_list[replacement]
# Search within a specific scope or search and qualify with scopes
if 'scope' in pattern:
results += result_template % (replacement, self.scope_apply(pattern))
else:
results += result_template % (replacement, self.apply(pattern))
return results
def filter_by_selection(self, regions, extractions=None):
new_regions = []
new_extractions = []
idx = 0
sels = self.view.sel()
for region in regions:
for sel in sels:
if region.begin() >= sel.begin() and region.end() <= sel.end():
new_regions.append(region)
if extractions != None:
new_extractions.append(extractions[idx])
break
idx += 1
if extractions == None:
return new_regions
else:
return new_regions, new_extractions
def is_selection_available(self):
available = False
for sel in self.view.sel():
if sel.size() > 0:
available = True
break
return available
def run(self, edit, replacements=[], find_only=False, clear=False, action=None, multi_pass=False, no_selection=False, options={}):
self.find_only = find_only
self.action = action.strip() if action != None else action
self.target_regions = []
self.replacements = replacements
self.multi_pass = multi_pass
self.options = options
self.selection_only = True if not no_selection and rrsettings.get('selection_only', False) and self.is_selection_available() else False
self.max_sweeps = rrsettings.get('multi_pass_max_sweeps', DEFAULT_MULTI_PASS_MAX_SWEEP)
self.panel_display = rrsettings.get('results_in_panel', DEFAULT_SHOW_PANEL)
self.edit = edit
# Clear regions and exit
if clear:
self.clear_highlights(MODULE_NAME)
return
elif action == 'unmark' and 'key' in options:
self.perform_action(action, options)
# Establish new run
self.handshake = self.view.id()
# Is the sequence empty?
if len(replacements) > 0:
# Find targets and replace if applicable
results = self.find_and_replace()
# Higlight regions
if self.find_only:
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 action != None:
if not self.perform_action(action, options):
results = 'Error: Bad Action!'
# Report results
if self.panel_display:
self.print_results_panel(results)
else:
self.print_results_status_bar(results)