-
Notifications
You must be signed in to change notification settings - Fork 37
/
wrap_plus.py
766 lines (670 loc) · 31.7 KB
/
wrap_plus.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
import sublime
import sublime_plugin
from .wrap import py_textwrap as textwrap
import re
import time
try:
import Default.comment as comment
except ImportError:
import comment
def is_quoted_string(scope_r, scope_name):
# string.quoted.double.block.python
# string.quoted.single.block.python
# string.quoted.double.single-line.python
# string.quoted.single.single-line.python
# comment.block.documentation.python
return 'quoted' in scope_name or 'comment.block.documentation' in scope_name
debug_enabled = False
time_start = 0
last_time = 0
def debug_start(enabled):
global debug_enabled, time_start, last_time
debug_enabled = enabled
if debug_enabled:
time_start = time.time()
last_time = time_start
def debug(msg, *args):
if debug_enabled:
global last_time
t = time.time()
d = t - time_start
print('%.3f (+%.3f) ' % (d, t - last_time), end='')
last_time = t
print(msg % args)
def debug_end():
if debug_enabled:
print('Total time: %.3f' % (time.time() - time_start))
class PrefixStrippingView(object):
"""View that strips out prefix characters, like comments.
:ivar str required_comment_prefix: When inside a line comment, we want to
restrict wrapping to just the comment section, and also retain the
comment characters and any initial indentation. This string is the
set of prefix characters a line must have to be a candidate for
wrapping in that case (otherwise it is typically the empty string).
:ivar Pattern required_comment_pattern: Regular expression required for
matching. The pattern is already included from the first line in
`required_comment_prefix`. This pattern is set to check if subsequent
lines have longer matches (in which case `line` will stop reading).
"""
required_comment_prefix = ''
required_comment_pattern = None
def __init__(self, view, min, max):
self.view = view
self.min = min
self.max = max
def _is_c_comment(self, scope_name):
if 'comment' not in scope_name and 'block' not in scope_name:
return False
for c in self.bc:
if c[0] == '/*' and c[1] == '*/':
break
else:
return False
return True
def set_comments(self, lc, bc, pt):
self.lc = lc
self.bc = bc
# If the line at pt is a comment line, set required_comment_prefix to
# the comment prefix.
self.required_comment_prefix = ''
# Grab the line.
line_r = self.view.line(pt)
line = self.view.substr(line_r)
line_strp = line.strip()
if not line_strp:
# Empty line, nothing to do.
debug('Empty line, no comment characters found.')
return
# Determine if pt is inside a "line comment".
# Only whitespace is allowed to the left of the line comment.
# XXX: What is disable_indent?
for c in lc:
start = c[0].rstrip()
if line_strp.startswith(start):
ldiff = len(line) - len(line.lstrip())
p = line[:ldiff + len(start)]
if (self.required_comment_prefix is None
or len(self.required_comment_prefix) < len(p)
):
self.required_comment_prefix = p
# TODO: re.escape required_comment_prefix.
# Handle email-style quoting.
email_quote_pattern = re.compile('^' + self.required_comment_prefix + email_quote)
m = email_quote_pattern.match(line)
if m:
self.required_comment_prefix = m.group()
self.required_comment_pattern = email_quote_pattern
debug('doing email style quoting')
scope_r = self.view.extract_scope(pt)
scope_name = self.view.scope_name(pt)
if 'comment.block.documentation.summary.python' in scope_name:
# Recent versions of the Python syntax have a different scope for
# the first line of a docstring. This causes problems when
# wrapping the first line. This extends the scope to encompass the
# entire docstring. (I'm not sure why there is a summary scope.)
scope_r = self.view.expand_to_scope(pt, 'comment.block')
debug('scope=%r range=%r', scope_name, scope_r)
if self._is_c_comment(scope_name):
# Check for C-style commenting with each line starting with an asterisk.
first_star_prefix = None
lines = self.view.lines(scope_r)
for line_r in lines[1:-1]:
line = self.view.substr(line_r)
m = funny_c_comment_pattern.match(line)
if m is not None:
if first_star_prefix is None:
first_star_prefix = m.group()
else:
first_star_prefix = None
break
if first_star_prefix:
self.required_comment_prefix = first_star_prefix
# Narrow the scope to just the comment contents.
scope_text = self.view.substr(scope_r)
m = re.match(r'^([ \t\n]*/\*).*(\*/[ \t\n]*)$', scope_text, re.DOTALL)
if m:
begin = scope_r.begin() + len(m.group(1))
end = scope_r.end() - len(m.group(2))
self.min = max(self.min, begin)
self.max = min(self.max, end)
debug('Scope narrowed to %i:%i', self.min, self.max)
debug('required_comment_prefix determined to be %r', self.required_comment_prefix,)
# Narrow the min/max range if inside a "quoted" string.
if is_quoted_string(scope_r, scope_name):
# Narrow the range.
self.min = max(self.min, self.view.line(scope_r.begin()).begin())
self.max = min(self.max, self.view.line(scope_r.end()).end())
def line(self, where):
"""Get a line for a point.
:returns: A (region, str) tuple. str has the comment prefix stripped.
Returns None, None if line out of range.
"""
line_r = self.view.line(where)
if line_r.begin() < self.min:
debug('line min increased')
line_r = sublime.Region(self.min, line_r.end())
if line_r.end() > self.max:
debug('line max lowered')
line_r = sublime.Region(line_r.begin(), self.max)
line = self.view.substr(line_r)
debug('line=%r', line)
if self.required_comment_prefix:
debug('checking required comment prefix %r', self.required_comment_prefix)
if line.startswith(self.required_comment_prefix):
# Check for an insufficient prefix.
if self.required_comment_pattern:
m = self.required_comment_pattern.match(line)
if m:
if m.group() != self.required_comment_prefix:
# This might happen, if for example with an email
# comment, we go from one comment level to a
# deeper one (the regex matched more > characters
# than are in required_comment_pattern).
return None, None
else:
# This should never happen (matches the string but not
# the regex?).
return None, None
rcp_len = len(self.required_comment_prefix)
line = line[rcp_len:]
# XXX: Should this also update line_r?
else:
return None, None
return line_r, line
def substr(self, r):
return self.view.substr(r)
def next_line(self, where):
l_r = self.view.line(where)
debug('next line region=%r', l_r)
pt = l_r.end() + 1
if pt >= self.max:
debug('past max at %r', self.max)
return None, None
return self.line(pt)
def prev_line(self, where):
l_r = self.view.line(where)
pt = l_r.begin() - 1
if pt <= self.min:
return None, None
return self.line(pt)
def OR(*args):
return '(?:' + '|'.join(args) + ')'
def CONCAT(*args):
return '(?:' + ''.join(args) + ')'
blank_line_pattern = re.compile(r'^[\t \n]*$')
# This doesn't always work, but seems decent.
numbered_list = r'(?:(?:([0-9#]+)[.)])+[\t ])'
numbered_list_pattern = re.compile(numbered_list)
lettered_list = r'(?:[a-zA-Z][.)][\t ])'
bullet_list = r'(?:[*+#-]+[\t ])'
list_pattern = re.compile(r'^[ \t]*' + OR(numbered_list, lettered_list, bullet_list) + r'[ \t]*')
latex_hack = r'(?:\\)(?!,|;|&|%|text|emph|cite|\w?(page)?ref|url|footnote|(La)*TeX)'
rest_directive = r'(?:\.\.)'
field_start = r'(?:[:@])' # rest, javadoc, jsdoc, etc.
new_paragraph_pattern = re.compile(
r'^[\t ]*' + OR(lettered_list, bullet_list, field_start))
space_prefix_pattern = re.compile(r'^[ \t]*')
# XXX: Does not handle escaped colons in field name.
fields = OR(r':[^:]+:', '@[a-zA-Z]+ ')
field_pattern = re.compile(r'^([ \t]*)' + fields) # rest, javadoc, jsdoc, etc
sep_chars = '!@#$%^&*=+`~\'\":;.,?_-'
sep_line = '[' + sep_chars + r']+[ \t' + sep_chars + ']*'
# Break pattern is a little ambiguous. Something like "# Header" could also be a list element.
break_pattern = re.compile(r'^[\t ]*' + OR(sep_line, OR(latex_hack, rest_directive) + '.*') + '$')
pure_break_pattern = re.compile(r'^[\t ]*' + sep_line + '$')
email_quote = r'[\t ]*>[> \t]*'
funny_c_comment_pattern = re.compile(r'^[\t ]*\*')
class WrapLinesPlusCommand(sublime_plugin.TextCommand):
def _my_full_line(self, region):
# Special case scenario where you select an entire line. The normal
# "full_line" function will extend it to contain the next line
# (because the cursor is actually at the beginning of the next line).
# I would prefer it didn't do that.
if self.view.substr(region.end() - 1) == '\n':
return self.view.full_line(sublime.Region(region.begin(), region.end() - 1))
else:
return self.view.full_line(region)
def _is_real_numbered_list(self, line_r, line, limit=10, indent=False):
"""Returns True if `line` is not a paragraph continuation."""
# We stop checking the list after `limit` lines to avoid quadratic
# runtime. For inputs like 100 lines of "2. ", this function is called
# in a loop over the input and also contains a loop over the input.
# indent tracks whether we came from an indented line
if limit == 0:
return True
m = numbered_list_pattern.search(line)
if m and m.group(1) == '1':
return True
prev_line_r, prev_line = self._strip_view.prev_line(line_r)
if prev_line_r is None:
return not indent
if self._is_paragraph_break(prev_line_r, prev_line):
return not indent
if new_paragraph_pattern.match(prev_line):
return not indent
if prev_line[0] == ' ' or prev_line[0] == '\t':
# prev_line might be a numbered list or a normal paragraph
return self._is_real_numbered_list(prev_line_r, prev_line, limit - 1, indent=True)
if numbered_list_pattern.match(prev_line):
return self._is_real_numbered_list(prev_line_r, prev_line, limit - 1)
return False # previous line appears to be a normal paragraph
def _is_paragraph_start(self, line_r, line):
# Certain patterns at the beginning of the line indicate this is the
# beginning of a paragraph.
if new_paragraph_pattern.match(line):
return True
if numbered_list_pattern.match(line):
result = self._is_real_numbered_list(line_r, line)
debug('is {}a paragraph continuation'.format('not ' if result else ''))
return result
return False
def _is_paragraph_break(self, line_r, line, pure=False):
"""A paragraph "break" is something like a blank line, or a horizontal line,
or anything that should not be wrapped and treated like a blank line
(i.e. ignored).
"""
if self._is_blank_line(line):
return True
scope_name = self.view.scope_name(line_r.begin())
debug('scope_name=%r (%r)', scope_name, line_r)
if 'heading' in scope_name:
return True
if pure:
return pure_break_pattern.match(line) is not None
else:
return break_pattern.match(line) is not None
def _is_blank_line(self, line):
return blank_line_pattern.match(line) is not None
def _find_paragraph_start(self, pt):
"""Start at pt and move up to find where the paragraph starts.
:returns: The (line, line_region) of the start of the paragraph.
"""
view = self._strip_view
current_line_r, current_line = view.line(pt)
if current_line_r is None:
return None, None
started_in_comment = self._started_in_comment(pt)
debug('is_paragraph_break?')
if self._is_paragraph_break(current_line_r, current_line):
return current_line_r, current_line
debug('no')
while 1:
# Check if this line is the start of a paragraph.
debug('start?')
if self._is_paragraph_start(current_line_r, current_line):
debug('current_line is paragraph start: %r', current_line,)
break
# Check if the previous line is a "break" separator.
debug('break?')
prev_line_r, prev_line = view.prev_line(current_line_r)
if prev_line_r is None:
# current_line is as far up as we're allowed to go.
break
if self._is_paragraph_break(prev_line_r, prev_line):
debug('prev line %r is a paragraph break', prev_line,)
break
# If the previous line has a comment, and we started in a
# non-comment scope, stop. No need to check for comment to
# non-comment change because the prefix restrictions should handle
# that.
if (not started_in_comment
and self.view.score_selector(prev_line_r.end(), 'comment')
):
debug('prev line contains a comment, cannot continue.')
break
debug('prev_line %r is part of the paragraph', prev_line,)
# Previous line is a part of this paragraph. Add it, and loop
# around again.
current_line_r = prev_line_r
current_line = prev_line
return current_line_r, current_line
def _find_paragraphs(self, sr):
"""Find and return a list of paragraphs as regions.
:param Region sr: The region where to look for paragraphs. If it is
an empty region, "discover" where the paragraph starts and ends.
Otherwise, the region defines the max and min (with potentially
several paragraphs contained within).
:returns: A list of (region, lines, comment_prefix) of each paragraph.
"""
result = []
debug('find paragraphs sr=%r', sr,)
if sr.empty():
is_empty = True
view_min = 0
view_max = self.view.size()
else:
is_empty = False
full_sr = self._my_full_line(sr)
view_min = full_sr.begin()
view_max = full_sr.end()
started_in_comment = self._started_in_comment(sr.begin())
self._strip_view = PrefixStrippingView(self.view, view_min, view_max)
view = self._strip_view
# Loop for each paragraph (only loops once if sr is empty).
paragraph_start_pt = sr.begin()
while 1:
debug('paragraph scanning start %r.', paragraph_start_pt,)
view.set_comments(self._lc, self._bc, paragraph_start_pt)
lines = []
if is_empty:
# Find the beginning of this paragraph.
debug('empty sel finding paragraph start.')
current_line_r, current_line = self._find_paragraph_start(paragraph_start_pt)
debug('empty sel paragraph start determined to be %r %r',
current_line_r, current_line)
else:
# The selection defines the beginning.
current_line_r, current_line = view.line(paragraph_start_pt)
debug('sel beggining = %r %r', current_line_r, current_line)
if current_line_r is None:
debug('Could not find start.')
return []
# Skip blank and unambiguous break lines.
while 1:
debug('skip blank line')
if not self._is_paragraph_break(current_line_r, current_line, pure=True):
debug('not paragraph break')
break
if is_empty:
debug('empty sel on paragraph break %r', current_line,)
return []
current_line_r, current_line = view.next_line(current_line_r)
paragraph_start_pt = current_line_r.begin()
paragraph_end_pt = current_line_r.end()
# current_line_r now points to the beginning of the paragraph.
# Move down until the end of the paragraph.
debug('Scan until end of paragraph.')
while 1:
debug('current_line_r=%r max=%r', current_line_r, view.max)
# If we started in a non-comment scope, and the end of the
# line contains a comment, include any non-comment text in the
# wrap and stop looking for more.
if (not started_in_comment
and self.view.score_selector(current_line_r.end(), 'comment')
):
debug('end of paragraph hit a comment.')
# Find the start of the comment.
# This assumes comments do not have multiple scopes.
comment_r = self.view.extract_scope(current_line_r.end())
# Just in case something is wonky with the scope.
end_pt = max(comment_r.begin(), current_line_r.begin())
# A substring of current_line.
subline_r = sublime.Region(current_line_r.begin(), end_pt)
subline = self.view.substr(subline_r)
# Do not include whitespace preceding the comment.
m = re.search('([ \t]+$)', subline)
if m:
end_pt -= len(m.group(1))
debug('non-comment contents are: %r', subline)
paragraph_end_pt = end_pt
lines.append(subline)
# Skip over the comment.
current_line_r, current_line = view.next_line(current_line_r)
break
lines.append(current_line)
paragraph_end_pt = current_line_r.end()
current_line_r, current_line = view.next_line(current_line_r)
if current_line_r is None:
# Line is outside of our range.
debug('Out of range, stopping.')
break
debug('current_line = %r %r', current_line_r, current_line)
if self._is_paragraph_break(current_line_r, current_line):
debug('current line is a break, stopping.')
break
if self._is_paragraph_start(current_line_r, current_line):
debug('current line is a paragraph start, stopping.')
break
paragraph_r = sublime.Region(paragraph_start_pt, paragraph_end_pt)
result.append((paragraph_r, lines, view.required_comment_prefix))
if is_empty:
break
# Skip over blank lines and break lines till the next paragraph
# (or end of range).
debug('skip over blank lines')
while current_line_r is not None:
if self._is_paragraph_start(current_line_r, current_line):
break
if not self._is_paragraph_break(current_line_r, current_line):
break
# It's a paragraph break, skip over it.
current_line_r, current_line = view.next_line(current_line_r)
if current_line_r is None:
break
debug('next_paragraph_start is %r %r', current_line_r, current_line)
paragraph_start_pt = current_line_r.begin()
if paragraph_start_pt >= view_max:
break
return result
def _determine_width(self, width):
"""Determine the maximum line width.
:param Int width: The width specified by the command. Normally 0
which means "figure it out".
:returns: The maximum line width.
"""
if width == 0 and self.view.settings().get('wrap_width'):
try:
width = int(self.view.settings().get('wrap_width'))
except TypeError:
pass
if width == 0 and self.view.settings().get('rulers'):
# try and guess the wrap width from the ruler, if any
try:
width = int(self.view.settings().get('rulers')[0])
except ValueError:
pass
except TypeError:
pass
width = self.view.settings().get('WrapPlus.wrap_width', width)
# Value of 0 means 'automatic'.
if width == 0:
width = 78
ile = self.view.settings().get('WrapPlus.include_line_endings', 'auto')
if ile is True:
width -= self._determine_line_ending_size()
elif ile == 'auto':
if self._auto_word_wrap_enabled() and self.view.settings().get('wrap_width', 0) != 0:
width -= self._determine_line_ending_size()
return width
def _determine_line_ending_size(self):
# Sublime always uses 1, regardless of the file type/OS.
return 1
etypes = {
'windows': 2,
'unix': 1,
'cr': 1,
}
return etypes.get(self.view.line_endings().lower(), 1)
def _auto_word_wrap_enabled(self):
ww = self.view.settings().get('word_wrap')
return (ww is True or
(ww == 'auto' and self.view.score_selector(0, 'text')))
def _determine_tab_size(self):
tab_width = 8
if self.view.settings().get('tab_size'):
try:
tab_width = int(self.view.settings().get('tab_size'))
except TypeError:
pass
if tab_width == 0:
tab_width = 8
self._tab_width = tab_width
def _determine_comment_style(self):
# I'm not exactly sure why this function needs a point. It seems to
# return the same value regardless of location for the stuff I've
# tried.
(self._lc, self._bc) = comment.build_comment_data(self.view, 0)
def _started_in_comment(self, point):
if self.view.score_selector(point, 'comment'):
return True
# Check for case where only whitespace is before a comment.
line_r = self.view.line(point)
if self.view.score_selector(line_r.end(), 'comment'):
line = self.view.substr(line_r)
m = re.search('(^[ \t]+)', line)
if m:
pt_past_space = line_r.begin() + len(m.group(1))
if self.view.score_selector(pt_past_space, 'comment'):
return True
return False
def _width_in_spaces(self, text):
tab_count = text.count('\t')
return tab_count * self._tab_width + len(text) - tab_count
def _make_indent(self):
# This is suboptimal.
return ' ' * 4
# if self.view.settings().get('translate_tabs_to_spaces'):
# return ' ' * self._tab_width
# else:
# return '\t'
def _extract_prefix(self, paragraph_r, lines, required_comment_prefix):
# The comment prefix has already been stripped from the lines.
# If the first line starts with a list-like thing, then that will be the initial prefix.
initial_prefix = ''
subsequent_prefix = ''
first_line = lines[0]
m = list_pattern.match(first_line)
if m:
initial_prefix = first_line[0:m.end()]
stripped_prefix = initial_prefix.lstrip()
leading_whitespace = initial_prefix[:len(initial_prefix) - len(stripped_prefix)]
subsequent_prefix = leading_whitespace + ' ' * self._width_in_spaces(stripped_prefix)
else:
m = field_pattern.match(first_line)
if m:
# The spaces in front of the field start.
initial_prefix = m.group(1)
if len(lines) > 1:
# How to handle subsequent lines.
m = space_prefix_pattern.match(lines[1])
if m:
# It's already indented, keep this indent level
# (unless it is less than where the field started).
spaces = m.group(0)
if (self._width_in_spaces(spaces) >=
self._width_in_spaces(initial_prefix) + 1
):
subsequent_prefix = spaces
if not subsequent_prefix:
# Not already indented, make an indent.
subsequent_prefix = initial_prefix + self._make_indent()
else:
m = space_prefix_pattern.match(first_line)
if m:
initial_prefix = first_line[0:m.end()]
if len(lines) > 1:
m = space_prefix_pattern.match(lines[1])
if m:
subsequent_prefix = lines[1][0:m.end()]
else:
subsequent_prefix = ''
else:
subsequent_prefix = initial_prefix
else:
# Should never happen.
initial_prefix = ''
subsequent_prefix = ''
pt = paragraph_r.begin()
scope_r = self.view.extract_scope(pt)
scope_name = self.view.scope_name(pt)
if len(lines) == 1 and is_quoted_string(scope_r, scope_name):
# A multi-line quoted string, that is currently only on one line.
# This is mainly for Python docstrings. Not sure if it's a
# problem in other cases.
true_first_line_r = self.view.line(pt)
true_first_line = self.view.substr(true_first_line_r)
if true_first_line_r.begin() <= scope_r.begin():
m = space_prefix_pattern.match(true_first_line)
debug('single line quoted string triggered')
if m:
subsequent_prefix = m.group() + subsequent_prefix
# Remove the prefixes that are there.
new_lines = []
new_lines.append(first_line[len(initial_prefix):].strip())
for line in lines[1:]:
if line.startswith(subsequent_prefix):
line = line[len(subsequent_prefix):]
new_lines.append(line.strip())
debug('initial_prefix=%r subsequent_prefix=%r', initial_prefix, subsequent_prefix)
return (required_comment_prefix + initial_prefix,
required_comment_prefix + subsequent_prefix,
new_lines)
def run(self, edit, width=0):
debug_start(self.view.settings().get('WrapPlus.debug', False))
debug('#########################################################################')
self._width = self._determine_width(width)
debug('wrap width = %r', self._width)
self._determine_tab_size()
self._determine_comment_style()
# paragraphs is a list of (region, lines, comment_prefix) tuples.
paragraphs = []
for s in self.view.sel():
debug('examine %r', s)
paragraphs.extend(self._find_paragraphs(s))
debug('paragraphs is %r', paragraphs)
if paragraphs:
# Use view selections to handle shifts from the replace() command.
self.view.sel().clear()
for r, l, p in paragraphs:
self.view.sel().add(r)
# Regions fetched from view.sel() will shift appropriately with
# the calls to replace().
for i, s in enumerate(self.view.sel()):
paragraph_r, paragraph_lines, required_comment_prefix = paragraphs[i]
break_long_words = self.view.settings().get('WrapPlus.break_long_words', False)
break_on_hyphens = self.view.settings().get('WrapPlus.break_on_hyphens', False)
wrapper = textwrap.TextWrapper(break_long_words=break_long_words,
break_on_hyphens=break_on_hyphens)
wrapper.width = self._width
init_prefix, subsequent_prefix, paragraph_lines = self._extract_prefix(
paragraph_r, paragraph_lines, required_comment_prefix)
orig_init_prefix = init_prefix
orig_subsequent_prefix = subsequent_prefix
if orig_init_prefix or orig_subsequent_prefix:
# Textwrap is somewhat limited. It doesn't recognize tabs
# in prefixes. Unfortunately, this means we can't easily
# differentiate between the initial and subsequent. This
# is a workaround.
init_prefix = orig_init_prefix.expandtabs(self._tab_width)
subsequent_prefix = orig_subsequent_prefix.expandtabs(self._tab_width)
wrapper.initial_indent = init_prefix
wrapper.subsequent_indent = subsequent_prefix
wrapper.expand_tabs = False
txt = '\n'.join(paragraph_lines)
txt = txt.expandtabs(self._tab_width)
txt = wrapper.fill(txt)
# Put the tabs back to the prefixes.
if orig_init_prefix or orig_subsequent_prefix:
if (init_prefix != orig_subsequent_prefix
or subsequent_prefix != orig_subsequent_prefix
):
lines = txt.splitlines()
if init_prefix != orig_init_prefix:
debug('fix tabs %r', lines[0])
lines[0] = orig_init_prefix + lines[0][len(init_prefix):]
debug('new line is %r', lines[0])
if subsequent_prefix != orig_subsequent_prefix:
for i, line in enumerate(lines[1:]):
rest = lines[i + 1][len(subsequent_prefix):]
lines[i + 1] = orig_subsequent_prefix + rest
txt = '\n'.join(lines)
replaced_txt = self.view.substr(s)
# I can't decide if I prefer it to not make the modification
# if there is no change (and thus don't mark an unmodified
# file as modified), or if it's better to include a "non-
# change" in the undo stack.
self.view.replace(edit, s, txt)
if replaced_txt != txt:
debug('replaced text not the same:\noriginal=%r\nnew=%r', replaced_txt, txt)
else:
debug('replaced text is the same')
# Move cursor below the last paragraph.
s = self.view.sel()
end = s[len(s) - 1].end()
line = self.view.line(end)
end = min(self.view.size(), line.end() + 1)
self.view.sel().clear()
r = sublime.Region(end)
self.view.sel().add(r)
self.view.show(r)
debug_end()