Skip to content

Commit 6efd093

Browse files
authored
implement a "floor" for the printing (#117)
1 parent 05d4caf commit 6efd093

File tree

6 files changed

+133
-198
lines changed

6 files changed

+133
-198
lines changed

src/locationinfo.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ struct FileLocInfo
55
column::Int
66
# The line at which the current context starts, 0 if unknown
77
defline::Int
8+
# typemax(int) if unknown
9+
endline::Int
810
end
911

1012
struct BufferLocInfo
@@ -13,19 +15,21 @@ struct BufferLocInfo
1315
# 0 if unknown
1416
column::Int
1517
defline::Int
18+
# typemax(int) if unknown
19+
endline::Int
1620
end
1721

18-
function loc_for_fname(file::String, line::Integer, defline::Integer)
22+
function loc_for_fname(file::String, line::Integer, defline::Integer, endline::Integer)
1923
if startswith(file, "REPL[")
2024
hist_idx = parse(Int,string(file)[6:end-1])
2125
isdefined(Base, :active_repl) || return nothing, ""
2226
hp = Base.active_repl.interface.modes[1].hist
23-
return BufferLocInfo(hp.history[hp.start_idx+hist_idx], line, 0, defline)
27+
return BufferLocInfo(hp.history[hp.start_idx+hist_idx], line, 0, defline, endline)
2428
else
2529
for path in SEARCH_PATH
2630
fullpath = joinpath(path,string(file))
2731
if isfile(fullpath)
28-
return FileLocInfo(fullpath, line, 0, defline)
32+
return FileLocInfo(fullpath, line, 0, defline, endline)
2933
end
3034
end
3135
end
@@ -37,10 +41,22 @@ function locinfo(frame::Frame)
3741
meth = frame.framecode.scope
3842
def_file, def_line = JuliaInterpreter.whereis(meth)
3943
current_file, current_line = JuliaInterpreter.whereis(frame)
44+
n_stmts = length(frame.framedata.ssavalues)
45+
total_lines = JuliaInterpreter.linenumber(frame, 1n_stmts) - def_line + 1
46+
# We currently cannot see a difference between f(x) = x and
47+
# function f(x)
48+
# x
49+
# end
50+
# If we could, we would do the += 1 below only in the second case
51+
# This means that we now miss printing the end for cases like the second
52+
# (one line bodies)
53+
total_lines == 1 || (total_lines += 1)
54+
end_line = def_line + total_lines - 1
4055
if def_file != current_file || def_line > current_line
4156
def_line = 0 # We are not sure where the context start in cases like these, could be improved?
57+
end_line = typemax(Int)
4258
end
43-
return loc_for_fname(current_file, current_line, def_line)
59+
return loc_for_fname(current_file, current_line, def_line, end_line)
4460
else
4561
println("not yet implemented")
4662
end

src/printing.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function print_status(io::IO, frame::Frame; force_lowered=false)
116116
read(loc.filepath, String)
117117
end
118118
breakpoint_lines = breakpoint_linenumbers(frame)
119-
print_sourcecode(outbuf, data, loc.line, loc.defline, breakpoint_lines)
119+
print_sourcecode(outbuf, data, loc.line, loc.defline, loc.endline, breakpoint_lines)
120120
else
121121
print_codeinfo(outbuf, frame)
122122
end
@@ -157,7 +157,7 @@ function compute_source_offsets(code::String, offset::Integer, startline::Intege
157157
if offsetline + NUM_SOURCE_LINES_UP_DOWN[] < lastindex(file.offsets)
158158
stopoffset = min(stopoffset, file.offsets[offsetline + NUM_SOURCE_LINES_UP_DOWN[]] - 1)
159159
end
160-
if stopline + 1 < lastindex(file.offsets)
160+
if stopline + 1 <= lastindex(file.offsets)
161161
stopoffset = min(stopoffset, file.offsets[stopline + 1] - 1)
162162
end
163163
startoffset, stopoffset
@@ -213,10 +213,11 @@ function breakpoint_char(bp::BreakpointState)
213213
return bp.condition === JuliaInterpreter.falsecondition ? ' ' : ''
214214
end
215215

216-
function print_sourcecode(io::IO, code::String, line::Integer, defline::Integer, breakpoint_lines::Dict{Int, BreakpointState} = Dict{Int, BreakpointState}())
216+
function print_sourcecode(io::IO, code::String, line::Integer, defline::Integer, endline::Integer, breakpoint_lines::Dict{Int, BreakpointState} = Dict{Int, BreakpointState}())
217217
code = highlight_code(code; context=io)
218218
file = SourceFile(code)
219-
startoffset, stopoffset = compute_source_offsets(code, file.offsets[line], defline, line+NUM_SOURCE_LINES_UP_DOWN[]; file=file)
219+
stopline = min(endline, line + NUM_SOURCE_LINES_UP_DOWN[])
220+
startoffset, stopoffset = compute_source_offsets(code, file.offsets[line], defline, stopline; file=file)
220221

221222
if startoffset == -1
222223
printstyled(io, "Line out of file range (bad debug info?)")

test/ui.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ end
5858

5959

6060

61+
62+
function f_end(x)
63+
x = 3 + 3
64+
return sin(x)
65+
end
66+
"we don't want to see this in the source code printing"
67+
68+
6169
@testset "UI" begin
6270
if Sys.isunix() && VERSION >= v"1.1.0"
6371
Debugger._print_full_path[] = false
@@ -87,6 +95,10 @@ end
8795
run_terminal_test(@make_frame(outer(1, 2, 5, 20)),
8896
["s\n", "c\n"],
8997
"ui/history_kw.multiout")
98+
99+
run_terminal_test(@make_frame(f_end(2)),
100+
["n\n", "n\n", "n\n"],
101+
"ui/history_floor.multiout")
90102

91103
if v"1.1">= VERSION < v"1.2"
92104
run_terminal_test(@make_frame(my_gcd_noinfo(10, 20)),

test/ui/history_floor.multiout

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
++++++++++++++++++++++++++++++++++++++++++++++++++
2+
|In f_end(x) at ui.jl:63
3+
|>63 x = 3 + 3
4+
| 64 return sin(x)
5+
| 65 end
6+
|
7+
|About to run: (+)(3, 3)
8+
|1|debug>
9+
--------------------------------------------------
10+
|AAAAAAAAAAAAAAAAAAAAAAA
11+
|BBBBBAAAAAAAAAAAAA
12+
|AAAAAAAAAAAAAAAAAAAAAA
13+
|AAAAAAAA
14+
|
15+
|AAAAAAAAAAAAAAAAAAAAAAA
16+
|CCCCCCCCC
17+
++++++++++++++++++++++++++++++++++++++++++++++++++
18+
|In f_end(x) at ui.jl:63
19+
|>63 x = 3 + 3
20+
| 64 return sin(x)
21+
| 65 end
22+
|
23+
|About to run: (+)(3, 3)
24+
|1|debug> n
25+
|In f_end(x) at ui.jl:63
26+
| 63 x = 3 + 3
27+
|>64 return sin(x)
28+
| 65 end
29+
|
30+
|About to run: (sin)(6)
31+
|1|debug>
32+
--------------------------------------------------
33+
|AAAAAAAAAAAAAAAAAAAAAAA
34+
|BBBBBAAAAAAAAAAAAA
35+
|AAAAAAAAAAAAAAAAAAAAAA
36+
|AAAAAAAA
37+
|
38+
|AAAAAAAAAAAAAAAAAAAAAAA
39+
|CCCCCCCCCA
40+
|AAAAAAAAAAAAAAAAAAAAAAA
41+
|AAAAAAAAAAAAAAAAAA
42+
|BBBBBAAAAAAAAAAAAAAAAA
43+
|AAAAAAAA
44+
|
45+
|AAAAAAAAAAAAAAAAAAAAAA
46+
|CCCCCCCCC
47+
++++++++++++++++++++++++++++++++++++++++++++++++++
48+
|In f_end(x) at ui.jl:63
49+
|>63 x = 3 + 3
50+
| 64 return sin(x)
51+
| 65 end
52+
|
53+
|About to run: (+)(3, 3)
54+
|1|debug> n
55+
|In f_end(x) at ui.jl:63
56+
| 63 x = 3 + 3
57+
|>64 return sin(x)
58+
| 65 end
59+
|
60+
|About to run: (sin)(6)
61+
|1|debug> n
62+
|In f_end(x) at ui.jl:63
63+
| 63 x = 3 + 3
64+
|>64 return sin(x)
65+
| 65 end
66+
|
67+
|About to run: return -0.27941549819892586
68+
|1|debug>
69+
--------------------------------------------------
70+
|AAAAAAAAAAAAAAAAAAAAAAA
71+
|BBBBBAAAAAAAAAAAAA
72+
|AAAAAAAAAAAAAAAAAAAAAA
73+
|AAAAAAAA
74+
|
75+
|AAAAAAAAAAAAAAAAAAAAAAA
76+
|CCCCCCCCCA
77+
|AAAAAAAAAAAAAAAAAAAAAAA
78+
|AAAAAAAAAAAAAAAAAA
79+
|BBBBBAAAAAAAAAAAAAAAAA
80+
|AAAAAAAA
81+
|
82+
|AAAAAAAAAAAAAAAAAAAAAA
83+
|CCCCCCCCCA
84+
|AAAAAAAAAAAAAAAAAAAAAAA
85+
|AAAAAAAAAAAAAAAAAA
86+
|BBBBBAAAAAAAAAAAAAAAAA
87+
|AAAAAAAA
88+
|
89+
|AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
90+
|CCCCCCCCC

0 commit comments

Comments
 (0)