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_comments #16

Merged
merged 5 commits into from Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions apic.go
Expand Up @@ -206,6 +206,14 @@ func yaml_emitter_set_indentless_block_sequence(emitter *yaml_emitter_t, indentl
emitter.indentless_block_sequence = indentless_block_sequence
}

// Set indent line comments.
func yaml_emitter_set_indent_line_comments(emitter *yaml_emitter_t, indent_line_comments int) {
if indent_line_comments < 0 {
indent_line_comments = 1
}
emitter.indent_line_comments = indent_line_comments
}

///*
// * Destroy a token object.
// */
Expand Down
7 changes: 5 additions & 2 deletions emitterc.go
Expand Up @@ -1147,8 +1147,11 @@ func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
return true
}
if !emitter.whitespace {
if !put(emitter, ' ') {
return false
// Insert as many spaces before the line comment as requested.
for i := 0; i < emitter.indent_line_comments; i++ {
if !put(emitter, ' ') {
return false
}
}
}
if !yaml_emitter_write_comment(emitter, emitter.line_comment) {
Expand Down
2 changes: 2 additions & 0 deletions encode.go
Expand Up @@ -41,6 +41,7 @@ type encoder struct {
func newEncoder() *encoder {
e := &encoder{}
yaml_emitter_initialize(&e.emitter)
yaml_emitter_set_indent_line_comments(&e.emitter, 1)
yaml_emitter_set_output_string(&e.emitter, &e.out)
yaml_emitter_set_unicode(&e.emitter, true)
return e
Expand All @@ -49,6 +50,7 @@ func newEncoder() *encoder {
func newEncoderWithWriter(w io.Writer) *encoder {
e := &encoder{}
yaml_emitter_initialize(&e.emitter)
yaml_emitter_set_indent_line_comments(&e.emitter, 1)
yaml_emitter_set_output_writer(&e.emitter, w)
yaml_emitter_set_unicode(&e.emitter, true)
return e
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 TestIndentLineComments(t *testing.T) {
formatTestCase{
name: "indent line comments",
folder: "indent_line_comments",
configureDecoder: noopDecoder,
configureEncoder: func(enc *yaml.Encoder) {
enc.SetIndentLineComments(2)
},
}.Run(t)
}
1 change: 1 addition & 0 deletions formattest/testdata/indent_line_comments/expected.yaml
@@ -0,0 +1 @@
a: 1 # comment
1 change: 1 addition & 0 deletions formattest/testdata/indent_line_comments/input.yaml
@@ -0,0 +1 @@
a: 1 # comment
badouralix marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions yaml.go
Expand Up @@ -323,6 +323,11 @@ func (e *Encoder) SetDropMergeTag(dropMergeTag bool) {
e.encoder.optDropMergeTag = dropMergeTag
}

// SetIndentLineComments changes the number of indentation spaces before line comments.
func (e *Encoder) SetIndentLineComments(indentLineComments int) {
yaml_emitter_set_indent_line_comments(&e.encoder.emitter, indentLineComments)
}

// 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
indent_line_comments int // The number of indentation spaces before line comments.
badouralix marked this conversation as resolved.
Show resolved Hide resolved

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