mirror of
https://gitea.invidious.io/iv-org/shard-backtracer.cr.git
synced 2024-08-15 00:53:13 +00:00
Refactor Frame#context to avoid reading whole file at once
This commit is contained in:
parent
1ff793f00b
commit
5e1ea0bd78
1 changed files with 14 additions and 8 deletions
|
@ -119,16 +119,22 @@ module Backtracer
|
|||
return unless (lineno = @lineno) && (lineno > 0)
|
||||
return unless (path = @path) && File.readable?(path)
|
||||
|
||||
lines = File.read_lines(path)
|
||||
lineidx = lineno - 1
|
||||
context_line = nil
|
||||
pre_context, post_context = %w[], %w[]
|
||||
|
||||
if context_line = lines[lineidx]?
|
||||
pre_context_lines =
|
||||
(lineno <= context_lines) ? lineidx : context_lines
|
||||
|
||||
pre_context = lines[(lineidx - context_lines).clamp(0..), pre_context_lines]
|
||||
post_context = lines[(lineidx + 1).clamp(..lines.size), context_lines]
|
||||
i = 0
|
||||
File.each_line(path) do |line|
|
||||
case i += 1
|
||||
when lineno - context_lines...lineno
|
||||
pre_context << line
|
||||
when lineno
|
||||
context_line = line
|
||||
when lineno + 1..lineno + context_lines
|
||||
post_context << line
|
||||
end
|
||||
end
|
||||
|
||||
if context_line
|
||||
@context_cache[context_lines] =
|
||||
Context.new(
|
||||
lineno: lineno,
|
||||
|
|
Loading…
Reference in a new issue