Fixed bug in file where it would report its location one step ahead

This commit is contained in:
Sasha Koshka 2022-08-18 01:35:46 -04:00
parent bb89009742
commit 4780d9cc28

View File

@ -8,6 +8,8 @@ type File struct {
path string path string
file *os.File file *os.File
reader *bufio.Reader reader *bufio.Reader
realLine int
realColumn int
currentLine int currentLine int
currentColumn int currentColumn int
lines []string lines []string
@ -42,6 +44,9 @@ func (file *File) Read (bytes []byte) (amountRead int, err error) {
// store the character in the file // store the character in the file
for _, char := range bytes { for _, char := range bytes {
file.realLine = file.currentLine
file.realColumn = file.currentColumn
if char == '\n' { if char == '\n' {
file.lines = append(file.lines, "") file.lines = append(file.lines, "")
file.currentLine ++ file.currentLine ++
@ -61,6 +66,9 @@ func (file *File) Read (bytes []byte) (amountRead int, err error) {
func (file *File) ReadRune () (char rune, size int, err error) { func (file *File) ReadRune () (char rune, size int, err error) {
char, size, err = file.reader.ReadRune() char, size, err = file.reader.ReadRune()
file.realLine = file.currentLine
file.realColumn = file.currentColumn
if char == '\n' { if char == '\n' {
file.lines = append(file.lines, "") file.lines = append(file.lines, "")
file.currentLine ++ file.currentLine ++
@ -106,8 +114,8 @@ func (file *File) Close () {
func (file *File) Location (width int) (location Location) { func (file *File) Location (width int) (location Location) {
return Location { return Location {
file: file, file: file,
row: file.currentLine, row: file.realLine,
column: file.currentColumn, column: file.realColumn,
width: width, width: width,
} }
} }