Skip to content

Commit

Permalink
Merge pull request #43 from oxinabox/ox/breadcrumbs
Browse files Browse the repository at this point in the history
Fix breadcrumbs
  • Loading branch information
oxinabox authored Mar 22, 2019
2 parents 3fe0aae + c7cfc3a commit 1a7bec5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/break_action.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ end

function breadcrumbs(meth, statement_ind)
printstyled("\nBreakpoint Hit: "; color=:blue)
printstyled(string(meth); color=:light_blue)
#TODO: Translate the statement_ind into a line number
printstyled(string(meth, "\n"); color=:light_blue)
line_num = statement_ind2src_linenum(meth, statement_ind)
breadcrumbs(string(meth.file), line_num)
println()
Expand All @@ -31,7 +30,7 @@ function breadcrumbs(io, file::AbstractString, line_num; nbefore=2, nafter=2)
@assert(nbefore >= 0)
@assert(nafter >= 0)

all_lines = readlines(file)
all_lines = loc_for_file(file)
first_line_num = max(1, line_num - nbefore)
last_line_num = min(length(all_lines), line_num + nafter)

Expand Down
2 changes: 1 addition & 1 deletion src/inner_repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ function eval_and_display(code_ast, eval_module)
catch err
printstyled("ERROR: ", color=:red)
showerror(stdout, err)
println()
end
println()
end


Expand Down
24 changes: 24 additions & 0 deletions src/locate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,27 @@ function before_body_linenum(ir)
first_lineinfo = ir.linetable[first(ir.codelocs)]
return first_lineinfo.line - 1
end

######################################################

"""
loc_for_file(file)
Returns a vector of lines of code for the file.
This special cases the name `"REPL[\\d]"`, as being REPL history
and so returns that REPL history cell instead.
If for some reason the file can not be found,
this returns a vector with a single line and a message explaining as such.
"""
function loc_for_file(file::AbstractString)
if isfile(file)
return readlines(file)
elseif startswith(file, "REPL[") && isdefined(Base, :active_repl)
# extract the number from "REPL[123]"
hist_idx = parse(Int,string(file)[6:end-1])
hist = Base.active_repl.interface.modes[1].hist
source_code = hist.history[hist.start_idx+hist_idx]
return split(source_code, "\n")
else
return ["-- source for $file not found --"]
end
end
13 changes: 12 additions & 1 deletion test/test_locate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ using Test
using MagneticReadHead:
source_paths,
containing_methods,
src_line2ir_statement_ind
src_line2ir_statement_ind,
loc_for_file

@testset "src_line2ir_statement_ind" begin
ir1line = first(methods(()->1)) |> Base.uncompressed_ast
Expand Down Expand Up @@ -46,3 +47,13 @@ end
end
end
end

@testset "loc_for_file" begin
failoutput = loc_for_file("NOT REAL")
@test failoutput isa Vector{String}
@test length(failoutput)==1

output = loc_for_file(@__FILE__)
@test output isa Vector{String}
@test length(output) > 1
end

0 comments on commit 1a7bec5

Please sign in to comment.