-
Notifications
You must be signed in to change notification settings - Fork 67
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
Error message always shows last 5 lines of buffer #183
Comments
CC @airportyh Looks like the |
Oh dear! That's definitely not expected. We might have to revert that PR 😞 |
For those interested, here is a workaround (which could work as a fix, btw): myLexer.formatError = function (token, message) {
if (token == null) {
// An undefined token indicates EOF
var text = this.buffer.slice(this.index)
token = {
value: '',
text: text,
offset: this.index,
lineBreaks: text.indexOf('\n') === -1 ? 0 : 1,
line: this.line,
col: this.col,
}
}
const numLinesAround = 2
const firstDisplayedLine = Math.max(token.line - numLinesAround, 1)
const lastDisplayedLine = token.line + numLinesAround
const lastLineDigits = String(lastDisplayedLine).length
const displayedLines = nLines(
this.buffer,
firstDisplayedLine,
lastDisplayedLine,
)
.slice(0, 5)
const errorLines = []
errorLines.push(message + " at line " + token.line + " col " + token.col + ":")
errorLines.push("")
for (let i = 0; i < displayedLines.length; i++) {
var line = displayedLines[i]
var lineNo = firstDisplayedLine + i
errorLines.push(pad(String(lineNo), lastLineDigits) + " " + line);
if (lineNo === token.line) {
errorLines.push(pad("", lastLineDigits + token.col + 1) + "^")
}
}
return errorLines.join("\n")
}
function nLines(string, from, to) {
const reg = new RegExp(`^([^\\n]*\\n){${from - 1}}(([^\\n]*\\n){${to - from}})`);
const res = reg.exec(string);
return res?.[2]?.split('\n') ?? [];
}
function pad(s, length) {
if (s.length > length) {
return s
}
return Array(length - s.length + 1).join(" ") + s
} |
oguimbal
added a commit
to oguimbal/moo
that referenced
this issue
May 16, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error messages seem to always show the last 5 lines of the buffer, even when the error occurs earlier in the buffer.
This line seems to always grab the last N lines from
buffer
, not considering the actual position of the error within the buffer.The text was updated successfully, but these errors were encountered: