Commit ece1fa9e authored by Mike Frysinger's avatar Mike Frysinger Committed by Commit Bot

externs generator: do not split comments mid-word

If we're appending content and the line is too long, do not split
words in the middle.  In some cases, like JSDoc, the generated
comments are structured data we need to keep intact.  Instead, we
wrap the entire line.

Bug: 1086375
Change-Id: I84087e1586a2837dead61e2d3f6a5689d3bb10d4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2222148
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774750}
parent 3d0fa5d3
......@@ -124,13 +124,18 @@ class Code(object):
def trim_comment(comment, max_len):
if len(comment) <= max_len:
return comment, ''
# If we ran out of space due to existing content, don't try to wrap.
if max_len <= 1:
return '', comment.lstrip()
last_space = comment.rfind(' ', 0, max_len + 1)
if last_space != -1:
line = comment[0:last_space]
comment = comment[last_space + 1:]
else:
line = comment[0:max_len]
comment = comment[max_len:]
# If the line can't be split, then don't try. The comments might be
# important (e.g. JSDoc) where splitting it breaks things.
line = comment
comment = ''
return line, comment.lstrip()
# First line has the full maximum length.
......@@ -145,6 +150,7 @@ class Code(object):
# Any subsequent lines be subject to the wrap indent.
max_len = (self._comment_length - len(''.join(self._line_prefixes)) -
len(comment_prefix) - wrap_indent)
assert max_len > 1
while len(comment):
line, comment = trim_comment(comment, max_len)
self.Append(comment_prefix + (' ' * wrap_indent) + line, substitute=False)
......
......@@ -141,12 +141,16 @@ class CodeTest(unittest.TestCase):
'longness, that is, using a different\n'
'// word, length.',
c.Render())
# Words that cannot be broken up are left as too long.
long_word = 'x' * 100
c = Code()
c.Comment('xxx')
c.Comment(long_word)
c.Comment('xxx')
self.assertEquals(
'// ' + 'x' * 77 + '\n'
'// ' + 'x' * 23,
'// xxx\n'
'// ' + 'x' * 100 + '\n'
'// xxx',
c.Render())
c = Code(indent_size=2, comment_length=40)
c.Comment('Pretend this is a Closure Compiler style comment, which should '
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment