From 1b2e5fcc3283ea166fbd966e427dac72d8710344 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Sun, 27 Dec 2020 21:11:22 +0100 Subject: [PATCH] Add Backtrace::Line#context_hash --- src/backtracer/backtrace/line.cr | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/backtracer/backtrace/line.cr b/src/backtracer/backtrace/line.cr index 27f058f..7fb269e 100644 --- a/src/backtracer/backtrace/line.cr +++ b/src/backtracer/backtrace/line.cr @@ -77,5 +77,26 @@ module Backtracer {pre_context, context_line, post_context} end end + + def context_hash(context_lines : Int32? = nil) : Hash(Int32, String)? + return unless context = self.context(context_lines) + return unless lineno = self.number + + pre_context, context_line, post_context = context + + ({} of Int32 => String).tap do |hash| + pre_context.each_with_index do |code, index| + line = (lineno - pre_context.size) + index + hash[line] = code + end + + hash[lineno] = context_line + + post_context.each_with_index do |code, index| + line = lineno + (index + 1) + hash[line] = code + end + end + end end end