Commit 8ea16e11 authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

[Extension API Extern Generation] Fix comment indentation

For extern comments, line wrapping sometimes has indentation. For instance:
/**
 * @param {FooBar} This is some foobar
 *     that has the comment wrapped.
 */
We should support this.  Add an optional parameter to the Code Comment method
to allow for comment indentation.

BUG=461039

Review URL: https://codereview.chromium.org/1010603007

Cr-Commit-Position: refs/heads/master@{#322504}
parent 16485532
...@@ -85,7 +85,7 @@ class Code(object): ...@@ -85,7 +85,7 @@ class Code(object):
self.Append(line) self.Append(line)
return self return self
def Comment(self, comment, comment_prefix='// '): def Comment(self, comment, comment_prefix='// ', wrap_indent=0):
"""Adds the given string as a comment. """Adds the given string as a comment.
Will split the comment if it's too long. Use mainly for variable length Will split the comment if it's too long. Use mainly for variable length
...@@ -93,17 +93,31 @@ class Code(object): ...@@ -93,17 +93,31 @@ class Code(object):
Unaffected by code.Substitute(). Unaffected by code.Substitute().
""" """
max_len = self._comment_length - self._indent_level - len(comment_prefix) # Helper function to trim a comment to the maximum length, and return one
while len(comment) >= max_len: # line and the remainder of the comment.
line = comment[0:max_len] def trim_comment(comment, max_len):
last_space = line.rfind(' ') if len(comment) <= max_len:
return comment, ''
last_space = comment.rfind(' ', 0, max_len + 1)
if last_space != -1: if last_space != -1:
line = line[0:last_space] line = comment[0:last_space]
comment = comment[last_space + 1:] comment = comment[last_space + 1:]
else: else:
line = comment[0:max_len]
comment = comment[max_len:] comment = comment[max_len:]
self.Append(comment_prefix + line, substitute=False) return line, comment
self.Append(comment_prefix + comment, substitute=False)
# First line has the full maximum length.
max_len = self._comment_length - self._indent_level - len(comment_prefix)
line, comment = trim_comment(comment, max_len)
self.Append(comment_prefix + line, substitute=False)
# Any subsequent lines be subject to the wrap indent.
max_len = max_len - wrap_indent
while len(comment):
line, comment = trim_comment(comment, max_len)
self.Append(comment_prefix + (' ' * wrap_indent) + line, substitute=False)
return self return self
def Substitute(self, d): def Substitute(self, d):
......
...@@ -117,14 +117,14 @@ class CodeTest(unittest.TestCase): ...@@ -117,14 +117,14 @@ class CodeTest(unittest.TestCase):
self.assertFalse(c.IsEmpty()) self.assertFalse(c.IsEmpty())
def testComment(self): def testComment(self):
long_comment = ('This comment is eighty nine characters in longness, ' long_comment = ('This comment is ninety one characters in longness, '
'that is, to use another word, length') 'that is, using a different word, length.')
c = Code() c = Code()
c.Comment(long_comment) c.Comment(long_comment)
self.assertEquals( self.assertEquals(
'// This comment is eighty nine characters ' '// This comment is ninety one characters '
'in longness, that is, to use another\n' 'in longness, that is, using a different\n'
'// word, length', '// word, length.',
c.Render()) c.Render())
c = Code() c = Code()
c.Sblock('sblock') c.Sblock('sblock')
...@@ -133,13 +133,13 @@ class CodeTest(unittest.TestCase): ...@@ -133,13 +133,13 @@ class CodeTest(unittest.TestCase):
c.Comment(long_comment) c.Comment(long_comment)
self.assertEquals( self.assertEquals(
'sblock\n' 'sblock\n'
' // This comment is eighty nine characters ' ' // This comment is ninety one characters '
'in longness, that is, to use\n' 'in longness, that is, using a\n'
' // another word, length\n' ' // different word, length.\n'
'eblock\n' 'eblock\n'
'// This comment is eighty nine characters in ' '// This comment is ninety one characters in '
'longness, that is, to use another\n' 'longness, that is, using a different\n'
'// word, length', '// word, length.',
c.Render()) c.Render())
long_word = 'x' * 100 long_word = 'x' * 100
c = Code() c = Code()
...@@ -148,6 +148,14 @@ class CodeTest(unittest.TestCase): ...@@ -148,6 +148,14 @@ class CodeTest(unittest.TestCase):
'// ' + 'x' * 77 + '\n' '// ' + 'x' * 77 + '\n'
'// ' + 'x' * 23, '// ' + 'x' * 23,
c.Render()) c.Render())
c = Code(indent_size=2, comment_length=40)
c.Comment('Pretend this is a Closure Compiler style comment, which should '
'both wrap and indent', comment_prefix=' * ', wrap_indent=4)
self.assertEquals(
' * Pretend this is a Closure Compiler\n'
' * style comment, which should both\n'
' * wrap and indent',
c.Render())
def testCommentWithSpecialCharacters(self): def testCommentWithSpecialCharacters(self):
c = Code() c = Code()
......
...@@ -94,7 +94,7 @@ class _Generator(object): ...@@ -94,7 +94,7 @@ class _Generator(object):
is_constructor = self._IsTypeConstructor(js_type) is_constructor = self._IsTypeConstructor(js_type)
if is_constructor: if is_constructor:
c.Comment('@constructor', comment_prefix = ' * ') c.Comment('@constructor', comment_prefix = ' * ', wrap_indent=4)
else: else:
c.Concat(self._GenerateTypedef(js_type.properties)) c.Concat(self._GenerateTypedef(js_type.properties))
...@@ -141,7 +141,8 @@ class _Generator(object): ...@@ -141,7 +141,8 @@ class _Generator(object):
lines = [] lines = []
if function.description: if function.description:
lines.extend(function.description.splitlines()) for line in function.description.splitlines():
c.Comment(line, comment_prefix=' * ')
for param in function.params: for param in function.params:
js_type = self._TypeToJsType(param.type_) js_type = self._TypeToJsType(param.type_)
...@@ -165,7 +166,7 @@ class _Generator(object): ...@@ -165,7 +166,7 @@ class _Generator(object):
lines.append('@deprecated %s' % function.deprecated) lines.append('@deprecated %s' % function.deprecated)
for line in lines: for line in lines:
c.Comment(line, comment_prefix=' * '); c.Comment(line, comment_prefix=' * ', wrap_indent=4);
c.Append(' */') c.Append(' */')
return c return c
......
...@@ -54,6 +54,9 @@ namespace fakeApi { ...@@ -54,6 +54,9 @@ namespace fakeApi {
// |baz| : The baz to use. // |baz| : The baz to use.
static void doSomething(Baz baz, VoidCallback callback); static void doSomething(Baz baz, VoidCallback callback);
// |callback| : The callback which will most assuredly in all cases be
// called; that is, of course, iff such a callback was provided and is
// not at all null.
static void bazGreek(optional BazGreekCallback callback); static void bazGreek(optional BazGreekCallback callback);
[deprecated="Use a new method."] static DOMString returnString(); [deprecated="Use a new method."] static DOMString returnString();
...@@ -117,7 +120,9 @@ var Baz; ...@@ -117,7 +120,9 @@ var Baz;
chrome.fakeApi.doSomething = function(baz, callback) {}; chrome.fakeApi.doSomething = function(baz, callback) {};
/** /**
* @param {function(Baz, !chrome.fakeApi.Greek):void=} callback * @param {function(Baz, !chrome.fakeApi.Greek):void=} callback The callback
* which will most assuredly in all cases be called; that is, of course, iff
* such a callback was provided and is not at all null.
*/ */
chrome.fakeApi.bazGreek = function(callback) {}; chrome.fakeApi.bazGreek = function(callback) {};
......
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