aboutsummaryrefslogtreecommitdiffstats
path: root/mako/pygen.py
diff options
context:
space:
mode:
Diffstat (limited to 'mako/pygen.py')
-rw-r--r--mako/pygen.py36
1 files changed, 31 insertions, 5 deletions
diff --git a/mako/pygen.py b/mako/pygen.py
index d6559e5..62967e3 100644
--- a/mako/pygen.py
+++ b/mako/pygen.py
@@ -37,10 +37,36 @@ class PythonPrinter(object):
self._reset_multi_line_flags()
- def write_blanks(self, num=1):
- self.stream.write("\n" * num)
+ # marker for template source lines; this
+ # is part of source/template line mapping
+ self.last_source_line = -1
+
+ self.last_boilerplate_line = -1
+
+ # mapping of generated python lines to template
+ # source lines
+ self.source_map = {}
+
+ # list of "boilerplate" lines, these are lines
+ # that precede/follow a set of template source-mapped lines
+ self.boilerplate_map = []
+
+
+ def _update_lineno(self, num):
+ if self.last_boilerplate_line <= self.last_source_line:
+ self.boilerplate_map.append(self.lineno)
+ self.last_boilerplate_line = self.lineno
self.lineno += num
+ def start_source(self, lineno):
+ if self.last_source_line != lineno:
+ self.source_map[self.lineno] = lineno
+ self.last_source_line = lineno
+
+ def write_blanks(self, num):
+ self.stream.write("\n" * num)
+ self._update_lineno(num)
+
def write_indented_block(self, block):
"""print a line or lines of python which already contain indentation.
@@ -98,7 +124,7 @@ class PythonPrinter(object):
# write the line
self.stream.write(self._indent_line(line) + "\n")
- self.lineno += 1
+ self._update_lineno(1)
# see if this line should increase the indentation level.
# note that a line can both decrase (before printing) and
@@ -218,13 +244,13 @@ class PythonPrinter(object):
for entry in self.line_buffer:
if self._in_multi_line(entry):
self.stream.write(entry + "\n")
- self.lineno += 1
+ self._update_lineno(1)
else:
entry = entry.expandtabs()
if stripspace is None and re.search(r"^[ \t]*[^# \t]", entry):
stripspace = re.match(r"^([ \t]*)", entry).group(1)
self.stream.write(self._indent_line(entry, stripspace) + "\n")
- self.lineno += 1
+ self._update_lineno(1)
self.line_buffer = []
self._reset_multi_line_flags()