Skip to content
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

emitter: add pad_line_comment #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -6,9 +6,18 @@ FORK
This is specifically a fork maintained by @braydonk particularly for
the interests of [yamlfmt](https://www.github.com/google/yamlfmt).

The focus of the fixes to this repo are specifically on the workflow of
Unmarshalling to `yaml.Node` and immediately encoding. Many of the fixes
will likely work for any workflow through the library and can potentially
be adopted, but there may be cases where it won't.
This repo also adds tests to `formattest` which are test cases focused on
this workflow.

The following is the documentation as stated upstream. I will change it
when necessary.

-------------------------------------------------------------------------

Introduction
------------

Expand Down
5 changes: 5 additions & 0 deletions apic.go
Expand Up @@ -206,6 +206,11 @@ func yaml_emitter_set_indentless_block_sequence(emitter *yaml_emitter_t, indentl
emitter.indentless_block_sequence = indentless_block_sequence
}

// Set pad line comment
func yaml_emitter_set_pad_line_comment(emitter *yaml_emitter_t, pad_line_comment int) {
emitter.pad_line_comment = pad_line_comment
}

///*
// * Destroy a token object.
// */
Expand Down
18 changes: 13 additions & 5 deletions emitterc.go
Expand Up @@ -1118,7 +1118,7 @@ func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
if !yaml_emitter_write_indent(emitter) {
return false
}
if !yaml_emitter_write_comment(emitter, emitter.tail_comment) {
if !yaml_emitter_write_comment(emitter, emitter.tail_comment, 0) {
return false
}
emitter.tail_comment = emitter.tail_comment[:0]
Expand All @@ -1134,7 +1134,7 @@ func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
if !yaml_emitter_write_indent(emitter) {
return false
}
if !yaml_emitter_write_comment(emitter, emitter.head_comment) {
if !yaml_emitter_write_comment(emitter, emitter.head_comment, 0) {
return false
}
emitter.head_comment = emitter.head_comment[:0]
Expand All @@ -1151,7 +1151,7 @@ func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
return false
}
}
if !yaml_emitter_write_comment(emitter, emitter.line_comment) {
if !yaml_emitter_write_comment(emitter, emitter.line_comment, emitter.pad_line_comment) {
return false
}
emitter.line_comment = emitter.line_comment[:0]
Expand All @@ -1166,7 +1166,7 @@ func yaml_emitter_process_foot_comment(emitter *yaml_emitter_t) bool {
if !yaml_emitter_write_indent(emitter) {
return false
}
if !yaml_emitter_write_comment(emitter, emitter.foot_comment) {
if !yaml_emitter_write_comment(emitter, emitter.foot_comment, 0) {
return false
}
emitter.foot_comment = emitter.foot_comment[:0]
Expand Down Expand Up @@ -1980,9 +1980,17 @@ func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) boo
return true
}

func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte) bool {
func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte, padding int) bool {
breaks := false
pound := false

// [Go] Start by adding any additional padding to the line comment.
for i := 0; i < padding; i++ {
if !put(emitter, ' ') {
return false
}
}

Comment on lines -1983 to +1993

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to inject the extra spaces here instead of directly in yaml_emitter_process_line_comment ?
That would avoid having to provide a 0 any time yaml_emitter_write_comment is called

yaml/emitterc.go

Lines 1149 to 1153 in 875dd3d

if !emitter.whitespace {
if !put(emitter, ' ') {
return false
}
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason is that then I would have to change the logic at line 2007 which checks for the beginning of the line comment to be a #, and if it's not then it inserts # . It's sorta weird logic imo, but in general I tend to prefer stuff like this that simply adds an override to existing logic over messing with it directly. Just want to keep my involvement with the original library to a minimum.

Copy link

@badouralix badouralix Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it ! My understanding of this logic is that it applies to non-line comments, since line comments always have comment[0] == "#" and never have breaks, whereas non-line comments can span over multiple lines and handling them requires this logic
Overall I wouldn't have to worry much about this logic for anything related to inserting spaces before writing the comment itself

I have been experimenting with this alternative implementation badouralix@f2c7845 and couldn't find any edge case breaking the behavior of line comments ( haven't opened the pull request though since you opened yours first )
One interesting thing is that the config is explicitly 2 to insert 2 spaces before a line comment, whereas the padding config wants 1 to insert 2 spaces
Wouldn't it be easier to keep an alignment between the config and the number of spaces rather than having a discrepancy ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I had kept with the idea of "padding" and making it a padding of 1 space over the 1 space that automatically gets inserted. But I can see how that would be harder to explain that in documentation vs the default of 1.

That being said, I like your implementation better. I have a couple of comments, but I think overall yours is better and I'd rather accept that in favour of my version. Could you open a PR with it and add me as a reviewer?

Copy link

@badouralix badouralix Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just opened #16 but it seems I cannot add you as a reviewer 🙈

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Not sure why it doesn't let you add me as a reviewer, but I just requested myself instead.

for i := 0; i < len(comment); {
if is_break(comment, i) {
if !write_break(emitter, comment, &i) {
Expand Down
11 changes: 11 additions & 0 deletions formattest/encode_test.go
Expand Up @@ -62,3 +62,14 @@ func TestDropMergeTag(t *testing.T) {
},
}.Run(t)
}

func TestPadLineComment(t *testing.T) {
formatTestCase{
name: "pad line comment",
folder: "pad_line_comment",
configureDecoder: noopDecoder,
configureEncoder: func(enc *yaml.Encoder) {
enc.SetPadLineComment(1)
},
}.Run(t)
}
2 changes: 2 additions & 0 deletions formattest/testdata/pad_line_comment/expected.yaml
@@ -0,0 +1,2 @@
# comment
a: 1 # comment
2 changes: 2 additions & 0 deletions formattest/testdata/pad_line_comment/input.yaml
@@ -0,0 +1,2 @@
# comment
a: 1 # comment
5 changes: 5 additions & 0 deletions yaml.go
Expand Up @@ -323,6 +323,11 @@ func (e *Encoder) SetDropMergeTag(dropMergeTag bool) {
e.encoder.optDropMergeTag = dropMergeTag
}

// SetPadLineComment sets the option to add padding to a LineComment spacing.
func (e *Encoder) SetPadLineComment(padding int) {
yaml_emitter_set_pad_line_comment(&e.encoder.emitter, padding)
}

// Close closes the encoder by writing any remaining data.
// It does not write a stream terminating string "...".
func (e *Encoder) Close() (err error) {
Expand Down
1 change: 1 addition & 0 deletions yamlh.go
Expand Up @@ -738,6 +738,7 @@ type yaml_emitter_t struct {
explicit_document_start bool // Force an explicit document start
assume_folded_as_literal bool // Assume blocks were scanned as literals
indentless_block_sequence bool // Do not indent block sequences
pad_line_comment int // Pad line comment by requested amount

state yaml_emitter_state_t // The current emitter state.
states []yaml_emitter_state_t // The stack of states.
Expand Down