OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import contextlib |
| 6 |
| 7 class CodeWriter(object): |
| 8 """Helper class for code indentation.""" |
| 9 def __init__(self): |
| 10 self.indent = 0 |
| 11 self.value = [] |
| 12 self.errors = [] |
| 13 |
| 14 def GetErrors(self): |
| 15 return self.errors |
| 16 |
| 17 @contextlib.contextmanager |
| 18 def AddIndent(self, indent=2): |
| 19 self.indent += indent |
| 20 yield 0 |
| 21 self.indent -= indent |
| 22 |
| 23 def IncreaseIndent(self, indent=2): |
| 24 self.indent += indent |
| 25 |
| 26 def DecreaseIndent(self, indent=2): |
| 27 self.indent -= indent |
| 28 |
| 29 def Output(self, fmt, **kwargs): |
| 30 s = fmt.format(**kwargs) |
| 31 s = s.rstrip('\n') |
| 32 lines = s.split('\n') |
| 33 lines = map(lambda s: (' ' * self.indent + s).rstrip(), lines) |
| 34 self.value.extend(lines) |
| 35 |
| 36 def AddError(self, fmt, **kwargs): |
| 37 self.errors.append(fmt.format(**kwargs)) |
| 38 |
| 39 def GetValue(self): |
| 40 return '\n'.join(self.value) + '\n' |
| 41 |
| 42 def WriteCStyleHeader(self): |
| 43 self.Output("// GENERATED FILE") |
| 44 self.Output("// This file generated by DomDistillerJs protoc plugin.") |
OLD | NEW |