diff options
author | Guilherme Carreiro <[email protected]> | 2024-10-13 20:40:45 +0200 |
---|---|---|
committer | git <[email protected]> | 2024-10-23 00:58:50 +0000 |
commit | e7c9dfb3e913162b7803195f77d1cb9c0cc2b9df (patch) | |
tree | 827f7ed3f421ab3aec58024315c1e3bbf61df2f6 /lib/error_highlight | |
parent | 5aa8b9e3b53fbac7667c6d8ee9a3e85992380321 (diff) |
[ruby/error_highlight] Handle very long lines with errors in the middle of the line
https://github.com/ruby/error_highlight/commit/0657bc1afa
Diffstat (limited to 'lib/error_highlight')
-rw-r--r-- | lib/error_highlight/formatter.rb | 63 |
1 files changed, 27 insertions, 36 deletions
diff --git a/lib/error_highlight/formatter.rb b/lib/error_highlight/formatter.rb index b4fce93e55..4d5c82f460 100644 --- a/lib/error_highlight/formatter.rb +++ b/lib/error_highlight/formatter.rb @@ -2,55 +2,46 @@ module ErrorHighlight class DefaultFormatter def self.message_for(spot) # currently only a one-line code snippet is supported - if spot[:first_lineno] == spot[:last_lineno] - spot = truncate(spot) + return "" unless spot[:first_lineno] == spot[:last_lineno] - indent = spot[:snippet][0...spot[:first_column]].gsub(/[^\t]/, " ") - marker = indent + "^" * (spot[:last_column] - spot[:first_column]) + snippet = spot[:snippet] + first_column = spot[:first_column] + last_column = spot[:last_column] - "\n\n#{ spot[:snippet] }#{ marker }" - else - "" + # truncate snippet to fit in the viewport + if snippet.size > viewport_size + visible_start = [first_column - viewport_size / 2, 0].max + visible_end = visible_start + viewport_size + + # avoid centering the snippet when the error is at the end of the line + visible_start = snippet.size - viewport_size if visible_end > snippet.size + + prefix = visible_start.positive? ? "..." : "" + suffix = visible_end < snippet.size ? "..." : "" + + snippet = prefix + snippet[(visible_start + prefix.size)...(visible_end - suffix.size)] + suffix + snippet << "\n" unless snippet.end_with?("\n") + + first_column = first_column - visible_start + last_column = [last_column - visible_start, snippet.size - 1].min end + + indent = snippet[0...first_column].gsub(/[^\t]/, " ") + marker = indent + "^" * (last_column - first_column) + + "\n\n#{ snippet }#{ marker }" end def self.viewport_size - Ractor.current[:__error_highlight_viewport_size__] || terminal_columns + Ractor.current[:__error_highlight_viewport_size__] ||= terminal_columns end def self.viewport_size=(viewport_size) Ractor.current[:__error_highlight_viewport_size__] = viewport_size end - private - - def self.truncate(spot) - ellipsis = '...' - snippet = spot[:snippet] - diff = snippet.size - (viewport_size - ellipsis.size) - - # snippet fits in the terminal - return spot if diff.negative? - - if spot[:first_column] < diff - snippet = snippet[0...snippet.size - diff] - { - **spot, - snippet: snippet + ellipsis + "\n", - last_column: [spot[:last_column], snippet.size].min - } - else - { - **spot, - snippet: ellipsis + snippet[diff..-1], - first_column: spot[:first_column] - (diff - ellipsis.size), - last_column: spot[:last_column] - (diff - ellipsis.size) - } - end - end - def self.terminal_columns - # lazy load io/console in case viewport_size is set + # lazy load io/console, so it's not loaded when viewport_size is set require "io/console" IO.console.winsize[1] end |