@@ -120,3 +120,58 @@ function print_codeinfo(io::IO, frame::Frame)
120
120
end
121
121
println (io)
122
122
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