@@ -126,6 +126,9 @@ def run(filepath, source)
126
126
delta = ( ( Time . now - start ) * 1000 ) . round
127
127
128
128
puts "\r #{ color } #{ delta } ms"
129
+ rescue
130
+ puts "\r #{ filepath } "
131
+ raise
129
132
end
130
133
end
131
134
@@ -177,25 +180,12 @@ def run(argv)
177
180
action . run ( filepath , source )
178
181
rescue ParseError => error
179
182
warn ( "Error: #{ error . message } " )
180
- lines = source . lines
181
-
182
- maximum = [ error . lineno + 3 , lines . length ] . min
183
- digits = Math . log10 ( maximum ) . ceil
184
-
185
- ( [ error . lineno - 3 , 0 ] . max ...maximum ) . each do |line_index |
186
- line_number = line_index + 1
187
183
188
- if line_number == error . lineno
189
- part1 = Color . red ( ">" )
190
- part2 = Color . gray ( "%#{ digits } d |" % line_number )
191
- warn ( "#{ part1 } #{ part2 } #{ lines [ line_index ] } " )
192
-
193
- part3 = Color . gray ( " %#{ digits } s |" % " " )
194
- warn ( "#{ part3 } #{ " " * error . column } #{ Color . red ( "^" ) } " )
195
- else
196
- prefix = Color . gray ( " %#{ digits } d |" % line_number )
197
- warn ( "#{ prefix } #{ lines [ line_index ] } " )
198
- end
184
+ if error . lineno
185
+ highlight_error ( error , source )
186
+ else
187
+ warn ( error . message )
188
+ warn ( error . backtrace )
199
189
end
200
190
201
191
errored = true
@@ -232,6 +222,30 @@ def source_for(filepath)
232
222
233
223
File . read ( filepath , encoding : encoding )
234
224
end
225
+
226
+ # Highlights a snippet from a source and parse error.
227
+ def highlight_error ( error , source )
228
+ lines = source . lines
229
+
230
+ maximum = [ error . lineno + 3 , lines . length ] . min
231
+ digits = Math . log10 ( maximum ) . ceil
232
+
233
+ ( [ error . lineno - 3 , 0 ] . max ...maximum ) . each do |line_index |
234
+ line_number = line_index + 1
235
+
236
+ if line_number == error . lineno
237
+ part1 = Color . red ( ">" )
238
+ part2 = Color . gray ( "%#{ digits } d |" % line_number )
239
+ warn ( "#{ part1 } #{ part2 } #{ lines [ line_index ] } " )
240
+
241
+ part3 = Color . gray ( " %#{ digits } s |" % " " )
242
+ warn ( "#{ part3 } #{ " " * error . column } #{ Color . red ( "^" ) } " )
243
+ else
244
+ prefix = Color . gray ( " %#{ digits } d |" % line_number )
245
+ warn ( "#{ prefix } #{ lines [ line_index ] } " )
246
+ end
247
+ end
248
+ end
235
249
end
236
250
end
237
251
end
0 commit comments