-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v3] Improve comment handling #684
Changes from all commits
52a3704
d43cefb
05a4f38
6abbb6e
d2b2a0b
7b36ba5
46fa0f1
193064d
02f3190
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -359,6 +359,7 @@ func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t | |
func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool { | ||
|
||
if event.typ == yaml_DOCUMENT_START_EVENT { | ||
isEmpty := emitter.events[emitter.events_head + 1].typ == yaml_DOCUMENT_END_EVENT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we know here that the +1 is safe? |
||
|
||
if event.version_directive != nil { | ||
if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) { | ||
|
@@ -450,12 +451,15 @@ func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event | |
if !yaml_emitter_process_head_comment(emitter) { | ||
return false | ||
} | ||
if !put_break(emitter) { | ||
if !isEmpty && !put_break(emitter) { | ||
return false | ||
} | ||
} | ||
|
||
emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE | ||
if isEmpty { | ||
emitter.state = yaml_EMIT_DOCUMENT_END_STATE | ||
} | ||
return true | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,6 +305,7 @@ func yaml_parser_parse_document_start(parser *yaml_parser_t, event *yaml_event_t | |
typ: yaml_DOCUMENT_START_EVENT, | ||
start_mark: token.start_mark, | ||
end_mark: token.end_mark, | ||
implicit: true, | ||
|
||
head_comment: head_comment, | ||
} | ||
|
@@ -338,6 +339,7 @@ func yaml_parser_parse_document_start(parser *yaml_parser_t, event *yaml_event_t | |
tag_directives: tag_directives, | ||
implicit: false, | ||
} | ||
yaml_parser_set_event_comments(parser, event) | ||
skip_token(parser) | ||
|
||
} else { | ||
|
@@ -348,6 +350,7 @@ func yaml_parser_parse_document_start(parser *yaml_parser_t, event *yaml_event_t | |
start_mark: token.start_mark, | ||
end_mark: token.end_mark, | ||
} | ||
yaml_parser_set_event_comments(parser, event) | ||
skip_token(parser) | ||
} | ||
|
||
|
@@ -890,6 +893,24 @@ func yaml_parser_parse_block_mapping_value(parser *yaml_parser_t, event *yaml_ev | |
if token.typ == yaml_VALUE_TOKEN { | ||
mark := token.end_mark | ||
skip_token(parser) | ||
// Move foot comment to head comment. This prevents that in some cases, comments | ||
// in maps are moved to the wrong place. In the following map: | ||
// | ||
// a: | ||
// b: | ||
// # comment followed by newline | ||
// | ||
// c: d | ||
// | ||
// (the newline between the comment and the next line is essential!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is stated more succinctly in the body of the yaml right above, so no need to repeat here. Otherwise, thanks for the comment. It provides clear rationale for the non-obvious logic. |
||
// the comment ends up as the foot comment of `c`, which is obviously wrong. | ||
if len(parser.foot_comment) > 0 { | ||
if len(parser.head_comment) > 0 { | ||
parser.head_comment = append(parser.head_comment, '\n') | ||
} | ||
parser.head_comment = append(parser.head_comment, parser.foot_comment...) | ||
parser.foot_comment = nil | ||
} | ||
token = peek_token(parser) | ||
if token == nil { | ||
return false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either the pre-existing comment above is incorrect, or we don't need the new variable. Also, it sounds like we should have enough state available in the parser to tell whether this is a top-level value or not. Can you provide some more details about what's going on here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know / understand the parser well enough to be sure whether the comment is correct or not. That's why I added the parameter
toplevel
, to avoid accidentally introducing a bug here. If you prefer I can remove it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's okay, but we cannot have additional logic in here unless we understand that well, otherwise we may be surprised in other situations, or may have misleading logic that makes it sound like this code executes at times it doesn't. Right now this is labeled as a "stream end event". Either that's true and the new logic is wrong, or it's incorrect and the old comment is wrong.