From 4ef8100961498ef69171363da7c1937049338dc5 Mon Sep 17 00:00:00 2001 From: xunjieli Date: Fri, 5 Aug 2016 06:39:59 -0700 Subject: [PATCH] Modify java_cpp_enum.py to preserve comments This CL modifies java_cpp_enum.py to preserve comments on the enums in the generated Java file. The comments are useful for developers who usually read the JavaDoc and not the C++ files. BUG=634092 Review-Url: https://codereview.chromium.org/2210633002 Cr-Original-Commit-Position: refs/heads/master@{#410047} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: ea986397d77dc45b5b272ad68d00cc6063ee6987 --- android/gyp/java_cpp_enum.py | 30 +++++++++++++++++++++++++++--- android/gyp/java_cpp_enum_tests.py | 28 +++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/android/gyp/java_cpp_enum.py b/android/gyp/java_cpp_enum.py index 21913da76..ffab05c91 100755 --- a/android/gyp/java_cpp_enum.py +++ b/android/gyp/java_cpp_enum.py @@ -27,11 +27,12 @@ ENUM_FIXED_TYPE_WHITELIST = ['char', 'unsigned char', class EnumDefinition(object): def __init__(self, original_enum_name=None, class_name_override=None, - enum_package=None, entries=None, fixed_type=None): + enum_package=None, entries=None, comments=None, fixed_type=None): self.original_enum_name = original_enum_name self.class_name_override = class_name_override self.enum_package = enum_package self.entries = collections.OrderedDict(entries or []) + self.comments = collections.OrderedDict(comments or []) self.prefix_to_strip = None self.fixed_type = fixed_type @@ -40,6 +41,11 @@ class EnumDefinition(object): raise Exception('Multiple definitions of key %s found.' % key) self.entries[key] = value + def AppendEntryComment(self, key, value): + if key in self.comments: + raise Exception('Multiple definitions of key %s found.' % key) + self.comments[key] = value + @property def class_name(self): return self.class_name_override or self.original_enum_name @@ -124,7 +130,7 @@ class DirectiveSet(object): class HeaderParser(object): - single_line_comment_re = re.compile(r'\s*//') + single_line_comment_re = re.compile(r'\s*//\s*([^\n]+)') multi_line_comment_start_re = re.compile(r'\s*/\*') enum_line_re = re.compile(r'^\s*(\w+)(\s*\=\s*([^,\n]+))?,?') enum_end_re = re.compile(r'^\s*}\s*;\.*$') @@ -150,6 +156,7 @@ class HeaderParser(object): self._enum_definitions = [] self._in_enum = False self._current_definition = None + self._current_comments = [] self._generator_directives = DirectiveSet() self._multi_line_generator_directive = None @@ -171,7 +178,9 @@ class HeaderParser(object): self._ParseEnumLine(line) def _ParseEnumLine(self, line): - if HeaderParser.single_line_comment_re.match(line): + enum_comment = HeaderParser.single_line_comment_re.match(line) + if enum_comment: + self._current_comments.append(enum_comment.groups()[0]) return if HeaderParser.multi_line_comment_start_re.match(line): raise Exception('Multi-line comments in enums are not supported in ' + @@ -187,6 +196,10 @@ class HeaderParser(object): enum_key = enum_entry.groups()[0] enum_value = enum_entry.groups()[2] self._current_definition.AppendEntry(enum_key, enum_value) + if self._current_comments: + self._current_definition.AppendEntryComment( + enum_key, ' '.join(self._current_comments)) + self._current_comments = [] def _ParseMultiLineDirectiveLine(self, line): multi_line_directive_continuation = ( @@ -289,6 +302,17 @@ ${ENUM_ENTRIES} 'NAME': enum_name, 'VALUE': enum_value, } + enum_comments = enum_definition.comments.get(enum_name) + if enum_comments: + enum_comments_indent = ' * ' + comments_line_wrapper = textwrap.TextWrapper( + initial_indent=enum_comments_indent, + subsequent_indent=enum_comments_indent, + width=100) + enum_entries_string.append(' /**') + enum_entries_string.append( + '\n'.join(comments_line_wrapper.wrap(enum_comments))) + enum_entries_string.append(' */') enum_entries_string.append(enum_template.substitute(values)) enum_names.append(enum_name) enum_entries_string = '\n'.join(enum_entries_string) diff --git a/android/gyp/java_cpp_enum_tests.py b/android/gyp/java_cpp_enum_tests.py index 3444cfa25..643a410d5 100755 --- a/android/gyp/java_cpp_enum_tests.py +++ b/android/gyp/java_cpp_enum_tests.py @@ -26,7 +26,13 @@ class TestPreprocess(unittest.TestCase): def testOutput(self): definition = EnumDefinition(original_enum_name='ClassName', enum_package='some.package', - entries=[('E1', 1), ('E2', '2 << 2')]) + entries=[('E1', 1), ('E2', '2 << 2')], + comments=[('E2', 'This is a comment.'), + ('E1', 'This is a multiple line ' + 'comment that is really long. ' + 'This is a multiple line ' + 'comment that is really ' + 'really long.')]) output = GenerateOutput('path/to/file', definition) expected = """ // Copyright %d The Chromium Authors. All rights reserved. @@ -51,11 +57,22 @@ public class ClassName { }) @Retention(RetentionPolicy.SOURCE) public @interface ClassNameEnum {} + /** + * %s + * really really long. + */ public static final int E1 = 1; + /** + * This is a comment. + */ public static final int E2 = 2 << 2; } """ - self.assertEqual(expected % (date.today().year, GetScriptName()), output) + long_comment = ('This is a multiple line comment that is really long. ' + 'This is a multiple line comment that is') + self.assertEqual( + expected % (date.today().year, GetScriptName(), long_comment), + output) def testParseSimpleEnum(self): test_data = """ @@ -134,6 +151,8 @@ public class ClassName { // GENERATED_JAVA_PREFIX_TO_STRIP: P_ enum EnumTwo { P_A, + // This comment spans + // two lines. P_B }; """.split('\n') @@ -145,10 +164,13 @@ public class ClassName { self.assertEqual(collections.OrderedDict([('A', '1'), ('B', 'A')]), definition.entries) - + self.assertEqual(collections.OrderedDict([('ENUM_ONE_B', 'Comment there')]), + definition.comments) definition = definitions[1] self.assertEqual('EnumTwo', definition.class_name) self.assertEqual('other.package', definition.enum_package) + self.assertEqual(collections.OrderedDict( + [('P_B', 'This comment spans two lines.')]), definition.comments) self.assertEqual(collections.OrderedDict([('A', 0), ('B', 1)]), definition.entries)