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=''): | 66 def Sblock(self, line=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 self.Append(line) | 71 if line is not None: |
| 72 self.Append(line) |
72 self._indent_level += self._indent_size | 73 self._indent_level += self._indent_size |
73 return self | 74 return self |
74 | 75 |
75 def Eblock(self, line=''): | 76 def Eblock(self, line=None): |
76 """Ends a code block by decreasing and then appending a line (or a blank | 77 """Ends a code block by decreasing and then appending a line (or a blank |
77 line if not given). | 78 line if not given). |
78 """ | 79 """ |
79 # TODO(calamity): Decide if type checking is necessary | 80 # TODO(calamity): Decide if type checking is necessary |
80 #if not isinstance(line, basestring): | 81 #if not isinstance(line, basestring): |
81 # raise TypeError | 82 # raise TypeError |
82 self._indent_level -= self._indent_size | 83 self._indent_level -= self._indent_size |
83 self.Append(line) | 84 if line is not None: |
| 85 self.Append(line) |
84 return self | 86 return self |
85 | 87 |
86 def Comment(self, comment, comment_prefix='// '): | 88 def Comment(self, comment, comment_prefix='// '): |
87 """Adds the given string as a comment. | 89 """Adds the given string as a comment. |
88 | 90 |
89 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 |
90 comments. Otherwise just use code.Append('// ...') for comments. | 92 comments. Otherwise just use code.Append('// ...') for comments. |
91 | 93 |
92 Unaffected by code.Substitute(). | 94 Unaffected by code.Substitute(). |
93 """ | 95 """ |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 """Renders Code as a string. | 132 """Renders Code as a string. |
131 """ | 133 """ |
132 return '\n'.join([l.value for l in self._code]) | 134 return '\n'.join([l.value for l in self._code]) |
133 | 135 |
134 class Line(object): | 136 class Line(object): |
135 """A line of code. | 137 """A line of code. |
136 """ | 138 """ |
137 def __init__(self, value, substitute=True): | 139 def __init__(self, value, substitute=True): |
138 self.value = value | 140 self.value = value |
139 self.substitute = substitute | 141 self.substitute = substitute |
OLD | NEW |