| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 return self | 57 return self |
| 58 | 58 |
| 59 def Cblock(self, code): | 59 def Cblock(self, code): |
| 60 """Concatenates another Code object |code| onto this one followed by a | 60 """Concatenates another Code object |code| onto this one followed by a |
| 61 blank line, if |code| is non-empty.""" | 61 blank line, if |code| is non-empty.""" |
| 62 if not code.IsEmpty(): | 62 if not code.IsEmpty(): |
| 63 self.Concat(code).Append() | 63 self.Concat(code).Append() |
| 64 return self | 64 return self |
| 65 | 65 |
| 66 def Sblock(self, line=None): | 66 def Sblock(self, line=None, indent_size=None): |
| 67 """Starts a code block. | 67 """Starts a code block. |
| 68 | 68 |
| 69 Appends a line of code and then increases the indent level. | 69 Appends a line of code and then increases the indent level. |
| 70 """ | 70 """ |
| 71 if indent_size is None: |
| 72 indent_size = self._indent_size; |
| 71 if line is not None: | 73 if line is not None: |
| 72 self.Append(line) | 74 self.Append(line) |
| 73 self._indent_level += self._indent_size | 75 self._indent_level += indent_size |
| 74 return self | 76 return self |
| 75 | 77 |
| 76 def Eblock(self, line=None): | 78 def Eblock(self, line=None, indent_size=None): |
| 77 """Ends a code block by decreasing and then appending a line (or a blank | 79 """Ends a code block by decreasing and then appending a line (or a blank |
| 78 line if not given). | 80 line if not given). |
| 79 """ | 81 """ |
| 82 if indent_size is None: |
| 83 indent_size = self._indent_size; |
| 80 # TODO(calamity): Decide if type checking is necessary | 84 # TODO(calamity): Decide if type checking is necessary |
| 81 #if not isinstance(line, basestring): | 85 #if not isinstance(line, basestring): |
| 82 # raise TypeError | 86 # raise TypeError |
| 83 self._indent_level -= self._indent_size | 87 self._indent_level -= indent_size |
| 84 if line is not None: | 88 if line is not None: |
| 85 self.Append(line) | 89 self.Append(line) |
| 86 return self | 90 return self |
| 87 | 91 |
| 88 def Comment(self, comment, comment_prefix='// '): | 92 def Comment(self, comment, comment_prefix='// '): |
| 89 """Adds the given string as a comment. | 93 """Adds the given string as a comment. |
| 90 | 94 |
| 91 Will split the comment if it's too long. Use mainly for variable length | 95 Will split the comment if it's too long. Use mainly for variable length |
| 92 comments. Otherwise just use code.Append('// ...') for comments. | 96 comments. Otherwise just use code.Append('// ...') for comments. |
| 93 | 97 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 """Renders Code as a string. | 136 """Renders Code as a string. |
| 133 """ | 137 """ |
| 134 return '\n'.join([l.value for l in self._code]) | 138 return '\n'.join([l.value for l in self._code]) |
| 135 | 139 |
| 136 class Line(object): | 140 class Line(object): |
| 137 """A line of code. | 141 """A line of code. |
| 138 """ | 142 """ |
| 139 def __init__(self, value, substitute=True): | 143 def __init__(self, value, substitute=True): |
| 140 self.value = value | 144 self.value = value |
| 141 self.substitute = substitute | 145 self.substitute = substitute |
| OLD | NEW |