Skip to content

Commit

Permalink
Corrent location reporting with big line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Jan 19, 2024
1 parent 63c7166 commit 4613c9a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ export const prettyLineAt = (range: LocationRange, source: Source): string => {
const start = indexToLocation(range.start, source)
const end = indexToLocation(range.end, source)
if (!start || !end) return '<outside of a file>'
const linePad = `${(start.line + 1).toString()} | `.padStart(6)
const pad = '| '.padStart(6)
const lineNumSize = start.line.toString().length
const padSize = Math.max(6, 3 + lineNumSize)
const linePad = `${(start.line + 1).toString()} | `.padStart(padSize)
const pad = '| '.padStart(padSize)
const sourceLine = source.code.split('\n')[start.line]
const line = linePad + sourceLine
// TODO: multiline highlight
// why special logic is needed when token is near EOL?
const highlightLen =
const highlightEnd =
end.column === sourceLine.length
? sourceLine.length - start.column
: (start.line === end.line ? end.column + 1 : sourceLine.length) - start.column
? sourceLine.length
: start.line === end.line
? end.column + 1
: sourceLine.length
const highlightLen = highlightEnd - start.column
const highlight = pad + ' '.repeat(start.column) + '^'.repeat(highlightLen)
return [pad, line, highlight].join('\n')
}
Expand Down

0 comments on commit 4613c9a

Please sign in to comment.