Use the node's start_mark to determine line and column.

* Elminates a lot of logic in the AnsibleComposer class.
* Update tests with new column offsets.  The rule should now be
  consistently: Column is the start of the entry's value (so for
  strings, the first non-space after the entry beginning, for dicts, the
  first character of the first key)
This commit is contained in:
Toshio Kuratomi 2015-04-01 13:51:01 -07:00
commit 05f1bed12b
2 changed files with 29 additions and 58 deletions

View file

@ -24,42 +24,15 @@ from yaml.nodes import MappingNode, ScalarNode
class AnsibleComposer(Composer):
def __init__(self):
self.__mapping_starts = []
super(Composer, self).__init__()
def compose_node(self, parent, index):
# the line number where the previous token has ended (plus empty lines)
node = Composer.compose_node(self, parent, index)
if isinstance(node, ScalarNode):
# Scalars are pretty easy -- assume they start on the current
# token's line (what about multiline strings? Perhaps we also
# need to use previous token ended
if isinstance(node, (ScalarNode, MappingNode)):
node.__datasource__ = self.name
node.__line__ = self.line
# Need to investigate why this works...
if self.indents:
node.__column__ = self.indent + 1
else:
node.__column__ = self.column +1
elif isinstance(node, MappingNode):
node.__datasource__ = self.name
# Need extra help to know where the mapping starts
try:
(cur_line, cur_column) = self.__mapping_starts.pop()
except:
cur_line = None
cur_column = None
node.__line__ = cur_line
node.__column__ = cur_column
node.__column__ = node.start_mark.column + 1
node.__line__ = node.start_mark.line + 1
return node
def compose_mapping_node(self, anchor):
# the column here will point at the position in the file immediately
# after the first key is found, which could be a space or a newline.
# We could back this up to find the beginning of the key, but this
# should be good enough to determine the error location.
self.__mapping_starts.append((self.line + 1, self.column + 1))
return Composer.compose_mapping_node(self, anchor)