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

scanner: fix position calculation for literal, folded and raw folded strings #330

Merged
merged 1 commit into from Dec 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions lexer/lexer_test.go
Expand Up @@ -543,6 +543,49 @@ c: true`,
},
},
},
{
name: "issue326",
src: `a: |
Text
b: 1`,
expect: []testToken{
{
line: 1,
column: 1,
value: "a",
},
{
line: 1,
column: 2,
value: ":",
},
{
line: 1,
column: 4,
value: "|",
},
{
line: 2,
column: 3,
value: "Text\n",
},
{
line: 3,
column: 1,
value: "b",
},
{
line: 3,
column: 2,
value: ":",
},
{
line: 3,
column: 4,
value: "1",
},
},
},
}

for _, tc := range tests {
Expand Down
22 changes: 17 additions & 5 deletions scanner/scanner.go
Expand Up @@ -61,13 +61,25 @@ func (s *Scanner) bufferedToken(ctx *Context) *token.Token {
s.savedPos = nil
return tk
}
size := len(ctx.buf)
line := s.line
column := s.column - len(ctx.buf)
level := s.indentLevel
if ctx.isSaveIndentMode() {
line -= s.newLineCount(ctx.buf)
column = strings.Index(string(ctx.obuf), string(ctx.buf)) + 1
// Since we are in a literal, folded or raw folded
// we can use the indent level from the last token.
last := ctx.lastToken()
if last != nil { // The last token should never be nil here.
level = last.Position.IndentLevel + 1
}
}
return ctx.bufferedToken(&token.Position{
Line: s.line,
Column: s.column - size,
Offset: s.offset - size,
Line: line,
Column: column,
Offset: s.offset - len(ctx.buf),
IndentNum: s.indentNum,
IndentLevel: s.indentLevel,
IndentLevel: level,
})
}

Expand Down