aboutsummaryrefslogtreecommitdiffstats
path: root/lib/error_highlight/formatter.rb
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2021-06-30 12:28:22 +0900
committergit <svn-admin@ruby-lang.org>2021-06-30 12:49:18 +0900
commitf428ced69c70473b8405aae9c98828aa6f69b254 (patch)
tree9504ebbb01cc7333381c3fd06430c72f99fbbb3f /lib/error_highlight/formatter.rb
parentdb7e9b1aac7752259e60e09b92ea2d2e74b0886d (diff)
downloadruby-f428ced69c70473b8405aae9c98828aa6f69b254.tar.gz
[ruby/error_highlight] Experimentally support a custom formatter
https://github.com/ruby/error_highlight/commit/f40a1de20e
Diffstat (limited to 'lib/error_highlight/formatter.rb')
-rw-r--r--lib/error_highlight/formatter.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/error_highlight/formatter.rb b/lib/error_highlight/formatter.rb
new file mode 100644
index 0000000000..a3d6510dc2
--- /dev/null
+++ b/lib/error_highlight/formatter.rb
@@ -0,0 +1,24 @@
+module ErrorHighlight
+ class DefaultFormatter
+ def message_for(spot)
+ # currently only a one-line code snippet is supported
+ if spot[:first_lineno] == spot[:last_lineno]
+ marker = " " * spot[:first_column] + "^" * (spot[:last_column] - spot[:first_column])
+
+ "\n\n#{ spot[:snippet] }#{ marker }"
+ else
+ ""
+ end
+ end
+ end
+
+ def self.formatter
+ @@formatter
+ end
+
+ def self.formatter=(formatter)
+ @@formatter = formatter
+ end
+
+ self.formatter = DefaultFormatter.new
+end