OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 class Code(object): | 5 class Code(object): |
6 """A convenience object for constructing code. | 6 """A convenience object for constructing code. |
7 | 7 |
8 Logically each object should be a block of code. All methods except |Render| | 8 Logically each object should be a block of code. All methods except |Render| |
9 and |IsEmpty| return self. | 9 and |IsEmpty| return self. |
10 """ | 10 """ |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 line if not given). | 78 line if not given). |
79 """ | 79 """ |
80 # TODO(calamity): Decide if type checking is necessary | 80 # TODO(calamity): Decide if type checking is necessary |
81 #if not isinstance(line, basestring): | 81 #if not isinstance(line, basestring): |
82 # raise TypeError | 82 # raise TypeError |
83 self._indent_level -= self._indent_size | 83 self._indent_level -= self._indent_size |
84 if line is not None: | 84 if line is not None: |
85 self.Append(line) | 85 self.Append(line) |
86 return self | 86 return self |
87 | 87 |
88 def Comment(self, comment, comment_prefix='// '): | 88 def Comment(self, comment, comment_prefix='// ', wrap_indent=0): |
89 """Adds the given string as a comment. | 89 """Adds the given string as a comment. |
90 | 90 |
91 Will split the comment if it's too long. Use mainly for variable length | 91 Will split the comment if it's too long. Use mainly for variable length |
92 comments. Otherwise just use code.Append('// ...') for comments. | 92 comments. Otherwise just use code.Append('// ...') for comments. |
93 | 93 |
94 Unaffected by code.Substitute(). | 94 Unaffected by code.Substitute(). |
95 """ | 95 """ |
96 max_len = self._comment_length - self._indent_level - len(comment_prefix) | 96 # Helper function to trim a comment to the maximum length, and return one |
97 while len(comment) >= max_len: | 97 # line and the remainder of the comment. |
98 def trim_comment(comment, max_len): | |
99 if len(comment) <= max_len: | |
100 return comment, '' | |
98 line = comment[0:max_len] | 101 line = comment[0:max_len] |
Devlin
2015/03/26 22:36:54
Technically, this means treats the max length as 1
Dan Beam
2015/03/26 22:41:48
i think 80 is fine...?
Devlin
2015/03/26 23:13:08
Thought so, but figured good to double check.
| |
99 last_space = line.rfind(' ') | 102 last_space = line.rfind(' ') |
100 if last_space != -1: | 103 if last_space != -1: |
101 line = line[0:last_space] | 104 line = line[0:last_space] |
102 comment = comment[last_space + 1:] | 105 comment = comment[last_space + 1:] |
103 else: | 106 else: |
104 comment = comment[max_len:] | 107 comment = comment[max_len:] |
105 self.Append(comment_prefix + line, substitute=False) | 108 return line, comment |
106 self.Append(comment_prefix + comment, substitute=False) | 109 |
110 # First line has the full maximum length. | |
111 max_len = self._comment_length - self._indent_level - len(comment_prefix) | |
112 line, comment = trim_comment(comment, max_len) | |
113 self.Append(comment_prefix + line, substitute=False) | |
114 | |
115 # Any subsequent lines be subject to the wrap indent. | |
116 max_len = max_len - wrap_indent | |
117 while len(comment): | |
118 line, comment = trim_comment(comment, max_len) | |
119 self.Append(comment_prefix + (' ' * wrap_indent) + line, substitute=False) | |
120 | |
107 return self | 121 return self |
108 | 122 |
109 def Substitute(self, d): | 123 def Substitute(self, d): |
110 """Goes through each line and interpolates using the given dict. | 124 """Goes through each line and interpolates using the given dict. |
111 | 125 |
112 Raises type error if passed something that isn't a dict | 126 Raises type error if passed something that isn't a dict |
113 | 127 |
114 Use for long pieces of code using interpolation with the same variables | 128 Use for long pieces of code using interpolation with the same variables |
115 repeatedly. This will reduce code and allow for named placeholders which | 129 repeatedly. This will reduce code and allow for named placeholders which |
116 are more clear. | 130 are more clear. |
(...skipping 16 matching lines...) Expand all Loading... | |
133 """ | 147 """ |
134 return '\n'.join([l.value for l in self._code]) | 148 return '\n'.join([l.value for l in self._code]) |
135 | 149 |
136 | 150 |
137 class Line(object): | 151 class Line(object): |
138 """A line of code. | 152 """A line of code. |
139 """ | 153 """ |
140 def __init__(self, value, substitute=True): | 154 def __init__(self, value, substitute=True): |
141 self.value = value | 155 self.value = value |
142 self.substitute = substitute | 156 self.substitute = substitute |
OLD | NEW |