UNPKG

1.12 kBtext/coffeescriptView Raw
1export default (source, filename, err) ->
2
3 {first_line, last_line, first_column, last_column} = err.location
4
5 # last_line no longer exists randomly
6 last_line = first_line unless last_line?
7
8 # get lines from source
9 lines = source.split '\n'
10
11 # insert start of highlighting
12 l = lines[first_line]
13 lines[first_line] = (l.substring 0, first_column) + '\x1B[91m' + l.substring first_column
14
15 # insert end
16
17 l = lines[last_line]
18
19 if first_line is last_line
20 # error only spans one line and overlaps with current cursor
21 # skip a bit farther due to insertion of highlighting
22 col = last_column + 6
23 else
24 col = last_column
25
26 # end highlighting
27 lines[last_line] = (l.substring 0, col) + '\x1B[39m' + (l.substring col)
28
29 # get subset of lines with error to display
30 lines = lines.slice first_line, last_line + 1
31
32 # underline with carets
33 carets = Array(first_column+1).join(' ') +
34 "\x1B[91m#{Array(last_column + 2 - first_column).join('^')}\x1B[39m"
35
36 lines.push carets
37
38 """
39 #{filename}:#{first_line+1}:#{first_column+1}: #{err.name}: #{err.message}
40 #{lines.join '\n'}\n
41 """
\No newline at end of file