Skip to content

Commit b453538

Browse files
fredrikekreKristofferC
authored andcommitted
Add o command to open current (file, line) in an editor. (#100)
1 parent 4d295e4 commit b453538

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

Manifest.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
55

66
[[CodeTracking]]
77
deps = ["InteractiveUtils", "Test", "UUIDs"]
8-
git-tree-sha1 = "983f5f7a57c604322917ab8bc5b86ba914d3c345"
8+
git-tree-sha1 = "2c46bc2429c88b43d80caf53151f22b85ba5a4b2"
99
uuid = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
10-
version = "0.3.2"
10+
version = "0.3.4"
1111

1212
[[Crayons]]
1313
deps = ["Test"]
@@ -41,11 +41,11 @@ uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
4141

4242
[[JuliaInterpreter]]
4343
deps = ["CodeTracking", "InteractiveUtils", "Random", "UUIDs"]
44-
git-tree-sha1 = "9c1dfb5bb6b9f5fedcc873e9cbe6fe8765f70701"
44+
git-tree-sha1 = "1d4df77f80cb7ff450ac0c59e0b906a3713537b2"
4545
repo-rev = "master"
4646
repo-url = "https://github.com/JuliaDebug/JuliaInterpreter.jl.git"
4747
uuid = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"
48-
version = "0.2.0"
48+
version = "0.2.1"
4949

5050
[[LibGit2]]
5151
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.1.0"
66
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
77
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
88
Highlights = "eafb193a-b7ab-5a9e-9068-77385905fa72"
9+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
910
JuliaInterpreter = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"
1011
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
1112
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ end
2828
```
2929

3030
This interface allows for manipulating program execution, such as stepping in and
31-
out of functions, line stepping, showing local variables, and evaluating code in
31+
out of functions, line stepping, showing local variables, and evaluating code in
3232
the context of functions.
3333

3434
Basic Commands:
@@ -45,6 +45,7 @@ Basic Commands:
4545
- `w add expr`: add an expression to the watch list
4646
- `w`: show all watch expressions evaluated in the current function's context
4747
- `w rm [i::Int]`: remove all or the `i`:th watch expression
48+
- `o`: open the current line in an editor
4849
- `q`: quit the debugger, returning `nothing`
4950

5051
Advanced commands:
@@ -69,7 +70,7 @@ julia> @run sin(2.0)
6970
Hit breakpoint: abs(x::Float64) in Base at float.jl:522, line 522
7071
In abs(x) at float.jl:522
7172
522 abs(x::Float64) = abs_float(x)
72-
523
73+
523
7374
524 """
7475
7576
About to run: (abs_float)(2.0)
@@ -97,7 +98,7 @@ Breaking on error: string_index_err(s::AbstractString, i::Integer) in Base at st
9798
In string_index_err(s, i) at strings/string.jl:12
9899
12 @noinline string_index_err(s::AbstractString, i::Integer) =
99100
13 throw(StringIndexError(s, Int(i)))
100-
14
101+
14
101102
102103
About to run: (throw)(StringIndexError("αβ", 2))
103104
1|debug> bt

src/Debugger.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Debugger
22

33
using Highlights
44
using Crayons
5+
import InteractiveUtils
56

67
using Markdown
78
using Base.Meta: isexpr

src/commands.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ function execute_command(state::DebuggerState, ::Val{:?}, cmd::AbstractString)
148148
- `w add expr`: add an expression to the watch list\\
149149
- `w`: show all watch expressions evaluated in the current function's context\\
150150
- `w rm [i::Int]`: remove all or the `i`:th watch expression\\
151+
- `o`: open the current line in an editor\\
151152
- `q`: quit the debugger, returning `nothing`\\
152153
Advanced commands:\\
153154
- `nc`: step to the next call\\
@@ -157,3 +158,19 @@ function execute_command(state::DebuggerState, ::Val{:?}, cmd::AbstractString)
157158
""")
158159
return false
159160
end
161+
162+
function execute_command(state::DebuggerState, ::Val{:o}, cmd::AbstractString)
163+
frame = active_frame(state)
164+
loc = JuliaInterpreter.whereis(frame)
165+
if loc === nothing
166+
printstyled(stderr, "Could not find source location\n"; color=:red)
167+
return false
168+
end
169+
file, line = loc
170+
if !isfile(file)
171+
printstyled(stderr, "Could not find file: $(repr(file))\n"; color=:red)
172+
return false
173+
end
174+
InteractiveUtils.edit(file, line)
175+
return false
176+
end

test/misc.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,24 @@ try
6565
st = chomp(sprint(Debugger.print_status, frame; context = :color => true))
6666
x_1_plus_1_colored = "\e[39m\e[97mx\e[39m\e[97m \e[39m\e[91m=\e[39m\e[97m \e[39m1\e[97m \e[39m\e[91m+\e[39m\e[97m \e[39m1\e[97m"
6767
@test occursin(x_1_plus_1_colored, st)
68-
finally
68+
finally
6969
Debugger.set_highlight(Debugger.HIGHLIGHT_OFF)
7070
end
7171

7272
frame = @make_frame Test.eval(1)
7373
desc = Debugger.locdesc(frame)
7474
@test occursin(Sys.STDLIB, desc)
75+
76+
import InteractiveUtils
77+
@testset "`o` command" begin
78+
g() = nothing
79+
LINE = (@__LINE__) - 1
80+
frame = Debugger.@make_frame g()
81+
state = dummy_state(frame)
82+
JuliaInterpreter.breakpoint(InteractiveUtils.edit)
83+
frame, bp = JuliaInterpreter.@interpret execute_command(state, Val{:o}(), "o")
84+
JuliaInterpreter.remove()
85+
locals = JuliaInterpreter.locals(frame)
86+
@test JuliaInterpreter.Variable(@__FILE__, :file, false) in locals
87+
@test JuliaInterpreter.Variable(LINE, :line, false) in locals
88+
end

0 commit comments

Comments
 (0)