Skip to content

Commit 2155b61

Browse files
authored
small refactor to printing (#75)
1 parent 561d646 commit 2155b61

File tree

3 files changed

+155
-100
lines changed

3 files changed

+155
-100
lines changed

src/locationinfo.jl

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -66,55 +66,3 @@ function locdesc(frame::Frame)
6666
end
6767
end
6868
end
69-
70-
"""
71-
Determine the offsets in the source code to print, based on the offset of the
72-
currently highlighted part of the code, and the start and stop line of the
73-
entire function.
74-
"""
75-
function compute_source_offsets(code::String, offset::Integer, startline::Integer, stopline::Integer; file::SourceFile = SourceFile(code))
76-
offsetline = compute_line(file, offset)
77-
if offsetline - 3 > length(file.offsets) || startline > length(file.offsets)
78-
return -1, -1
79-
end
80-
startoffset = max(file.offsets[max(offsetline-3,1)], file.offsets[startline])
81-
stopoffset = lastindex(code)-1
82-
if offsetline + 3 < lastindex(file.offsets)
83-
stopoffset = min(stopoffset, file.offsets[offsetline + 3]-1)
84-
end
85-
if stopline + 1 < lastindex(file.offsets)
86-
stopoffset = min(stopoffset, file.offsets[stopline + 1]-1)
87-
end
88-
startoffset, stopoffset
89-
end
90-
91-
function print_sourcecode(io::IO, code::String, line::Integer, defline::Integer; file::SourceFile = SourceFile(code))
92-
startoffset, stopoffset = compute_source_offsets(code, file.offsets[line], defline, line+3; file=file)
93-
94-
if startoffset == -1
95-
printstyled(io, "Line out of file range (bad debug info?)", color=:bold)
96-
return
97-
end
98-
99-
# Compute necessary data for line numbering
100-
startline = compute_line(file, startoffset)
101-
stopline = compute_line(file, stopoffset)
102-
current_line = line
103-
stoplinelength = length(string(stopline))
104-
105-
code = split(code[(startoffset+1):(stopoffset+1)],'\n')
106-
lineno = startline
107-
108-
if !isempty(code) && isempty(code[end])
109-
pop!(code)
110-
end
111-
112-
for textline in code
113-
printstyled(io,
114-
string(lineno, " "^(stoplinelength-length(lineno)+1));
115-
color = lineno == current_line ? :yellow : :bold)
116-
println(io, textline)
117-
lineno += 1
118-
end
119-
println(io)
120-
end

src/printing.jl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,58 @@ function print_codeinfo(io::IO, frame::Frame)
120120
end
121121
println(io)
122122
end
123+
124+
125+
const NUM_SOURCE_LINES_UP_DOWN = Ref(5)
126+
127+
"""
128+
Determine the offsets in the source code to print, based on the offset of the
129+
currently highlighted part of the code, and the start and stop line of the
130+
entire function.
131+
"""
132+
function compute_source_offsets(code::String, offset::Integer, startline::Integer, stopline::Integer; file::SourceFile = SourceFile(code))
133+
offsetline = compute_line(file, offset)
134+
if offsetline - NUM_SOURCE_LINES_UP_DOWN[] > length(file.offsets) || startline > length(file.offsets)
135+
return -1, -1
136+
end
137+
startoffset = max(file.offsets[max(offsetline - NUM_SOURCE_LINES_UP_DOWN[], 1)], file.offsets[startline])
138+
stopoffset = lastindex(code)-1
139+
if offsetline + NUM_SOURCE_LINES_UP_DOWN[] < lastindex(file.offsets)
140+
stopoffset = min(stopoffset, file.offsets[offsetline + NUM_SOURCE_LINES_UP_DOWN[]] - 1)
141+
end
142+
if stopline + 1 < lastindex(file.offsets)
143+
stopoffset = min(stopoffset, file.offsets[stopline + 1] - 1)
144+
end
145+
startoffset, stopoffset
146+
end
147+
148+
function print_sourcecode(io::IO, code::String, line::Integer, defline::Integer; file::SourceFile = SourceFile(code))
149+
startoffset, stopoffset = compute_source_offsets(code, file.offsets[line], defline, line+NUM_SOURCE_LINES_UP_DOWN[]; file=file)
150+
151+
if startoffset == -1
152+
printstyled(io, "Line out of file range (bad debug info?)", color=:bold)
153+
return
154+
end
155+
156+
# Compute necessary data for line numbering
157+
startline = compute_line(file, startoffset)
158+
stopline = compute_line(file, stopoffset)
159+
current_line = line
160+
stoplinelength = length(string(stopline))
161+
162+
code = split(code[(startoffset+1):(stopoffset+1)],'\n')
163+
lineno = startline
164+
165+
if !isempty(code) && isempty(code[end])
166+
pop!(code)
167+
end
168+
169+
for textline in code
170+
printstyled(io,
171+
string(lineno, " "^(stoplinelength-length(lineno)+1));
172+
color = lineno == current_line ? :yellow : :bold)
173+
println(io, textline)
174+
lineno += 1
175+
end
176+
println(io)
177+
end

0 commit comments

Comments
 (0)