From bf95947e11492f1060819036694daed38237d10e Mon Sep 17 00:00:00 2001 From: Anthon van der Neut Date: Sun, 12 Aug 2018 17:35:24 +0200 Subject: fix issue #163 comment disappearing in list before flow mapping should be fixed in 0.15.54 *When this change indeed resolves your problem, please **Close** this issue*. *(You can do so usingthe WorkFlow pull-down (close to the top right of this page)* --- _test/test_issues.py | 15 ++++++++++----- parser.py | 9 ++++++++- tokens.py | 4 ++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/_test/test_issues.py b/_test/test_issues.py index 1793edb..7e95857 100644 --- a/_test/test_issues.py +++ b/_test/test_issues.py @@ -46,7 +46,7 @@ class TestIssues: """) for comment in ['', ' # no-newline', ' # some comment\n', '\n', ]: s = yaml_str.format(comment) - res = round_trip(s) + res = round_trip(s) # NOQA def test_issue_161a(self): yaml_str = dedent("""\ @@ -56,7 +56,12 @@ class TestIssues: """) for comment in ['\n# between']: s = yaml_str.format(comment) - res = round_trip(s) - # print(s) - # print('------------') - # assert False + res = round_trip(s) # NOQA + + def test_issue_163(self): + s = dedent("""\ + some-list: + # List comment + - {} + """) + x = round_trip(s, preserve_quotes=True) # NOQA diff --git a/parser.py b/parser.py index 0707761..61ff673 100644 --- a/parser.py +++ b/parser.py @@ -434,9 +434,16 @@ class Parser(object): ) self.state = self.parse_flow_sequence_first_entry elif self.scanner.check_token(FlowMappingStartToken): + pt = self.scanner.peek_token() end_mark = self.scanner.peek_token().end_mark event = MappingStartEvent( - anchor, tag, implicit, start_mark, end_mark, flow_style=True + anchor, + tag, + implicit, + start_mark, + end_mark, + flow_style=True, + comment=pt.comment, ) self.state = self.parse_flow_mapping_first_key elif block and self.scanner.check_token(BlockSequenceStartToken): diff --git a/tokens.py b/tokens.py index 72c4f48..5176520 100644 --- a/tokens.py +++ b/tokens.py @@ -27,6 +27,10 @@ class Token(object): arguments += u', line: ' + str(self.start_mark.line) except: # NOQA pass + try: + arguments += u', comment: ' + str(self._comment) + except: # NOQA + pass return u'{}({})'.format(self.__class__.__name__, arguments) def add_post_comment(self, comment): -- cgit v1.2.1