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

Support suffix keyword for document header option #506

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,29 @@ s: >-3
},
},
},
{
YAML: `
|2-

text
`,
Tokens: token.Tokens{
{
Type: token.LiteralType,
CharacterType: token.CharacterTypeIndicator,
Indicator: token.BlockScalarIndicator,
Value: "|2-",
Origin: "\n|2-\n",
},
{
Type: token.StringType,
CharacterType: token.CharacterTypeMiscellaneous,
Indicator: token.NotIndicator,
Value: "\n text",
Origin: "\n text\n",
},
},
},
}
for _, test := range tests {
t.Run(test.YAML, func(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions scanner/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ func (c *Context) updateDocumentIndentColumn() {
}

func (c *Context) docFirstLineIndentColumnByDocOpt() int {
trimmed := strings.TrimPrefix(c.docOpt, "-")
trimmed = strings.TrimPrefix(trimmed, "+")
i, _ := strconv.ParseInt(trimmed, 10, 64)
opt := c.docOpt
opt = strings.TrimPrefix(opt, "-")
opt = strings.TrimPrefix(opt, "+")
opt = strings.TrimSuffix(opt, "-")
opt = strings.TrimSuffix(opt, "+")
i, _ := strconv.ParseInt(opt, 10, 64)
return int(i)
}

Expand Down Expand Up @@ -270,7 +273,7 @@ func (c *Context) existsBuffer() bool {

func (c *Context) bufferedSrc() []rune {
src := c.buf[:c.notSpaceCharPos]
if c.isDocument() && strings.HasPrefix(c.docOpt, "-") {
if c.isDocument() && (strings.HasPrefix(c.docOpt, "-") || strings.HasSuffix(c.docOpt, "-")) {
// remove end '\n' character and trailing empty lines
// https://yaml.org/spec/1.2.2/#8112-block-chomping-indicator
for {
Expand Down
7 changes: 4 additions & 3 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,10 @@ func (s *Scanner) validateDocumentHeaderOption(opt string) error {
if len(opt) == 0 {
return nil
}
if opt[0] == '+' || opt[0] == '-' {
opt = opt[1:]
}
opt = strings.TrimPrefix(opt, "-")
opt = strings.TrimPrefix(opt, "+")
opt = strings.TrimSuffix(opt, "-")
opt = strings.TrimSuffix(opt, "+")
if len(opt) == 0 {
return nil
}
Expand Down
Loading