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

Fix ErrorWithPosition panic when less than two lines #433

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 12 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,24 @@ func (pe ParseError) ErrorWithPosition() string {
if pe.Position.Line > 2 {
fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-2, expandTab(lines[pe.Position.Line-3]))
}
if pe.Position.Line > 1 {
fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-1, expandTab(lines[pe.Position.Line-2]))
}

/// Expand tabs, so that the ^^^s are at the correct position, but leave
/// "column 10-13" intact. Adjusting this to the visual column would be
/// better, but we don't know the tabsize of the user in their editor, which
/// can be 8, 4, 2, or something else. We can't know. So leaving it as the
/// character index is probably the "most correct".
expanded := expandTab(lines[pe.Position.Line-1])
diff := len(expanded) - len(lines[pe.Position.Line-1])
var (
expanded string
diff int
)
if pe.Position.Line > 1 {
fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-1, expandTab(lines[pe.Position.Line-2]))
expanded = expandTab(lines[pe.Position.Line-1])
diff = len(expanded) - len(lines[pe.Position.Line-1])
} else {
expanded = expandTab(lines[0])
diff = len(expanded) - len(lines[0])
}

fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line, expanded)
fmt.Fprintf(b, "% 10s%s%s\n", "", strings.Repeat(" ", pe.Position.Col-1+diff), strings.Repeat("^", pe.Position.Len))
Expand Down
13 changes: 13 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ func TestParseError(t *testing.T) {
| 15:04:05.856018510
`,
},

{
&struct{ String string }{},
`string = "test`,
`
| toml: error: unexpected EOF; expected '"'
|
| At line 0, column 14:
|
| 0 | string = "test
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is line 1, not 0.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, I didn't notice that. I've updated the test and believe I found where the line number was being incorrectly set to 0.

| ^
`,
},
}

prep := func(s string) string {
Expand Down
Loading