Skip to content

Commit

Permalink
scanner: fix position calculation for literal, folded and raw folded …
Browse files Browse the repository at this point in the history
…strings
  • Loading branch information
efd6 committed Dec 11, 2022
1 parent dc3fed8 commit 358b0ba
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
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

0 comments on commit 358b0ba

Please sign in to comment.