Skip to content

Commit

Permalink
Add __RELATIVEFILE__ to PLUG_EDITOR replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
pehbehbeh committed Jul 17, 2024
1 parent 9f67dd9 commit 4d6e1ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/plug/debugger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ defmodule Plug.Debugger do
Or, using Visual Studio Code:
vscode://file/__FILE__:__LINE__
You can also use `__RELATIVEFILE__` if your project path is different from
the running application. This is useful when working with Docker containers.
vscode://file//path/to/your/project/__RELATIVEFILE__:__LINE__
"""

@already_sent {:plug_conn, :sent}
Expand Down Expand Up @@ -338,7 +343,7 @@ defmodule Plug.Debugger do
doc: doc,
clauses: clauses,
args: args,
link: editor && get_editor(source, line, editor)
link: editor && get_editor(source, file, line, editor)
}, index + 1}
end

Expand Down Expand Up @@ -458,9 +463,10 @@ defmodule Plug.Debugger do
end
end

defp get_editor(file, line, editor) do
defp get_editor(source, file, line, editor) do
editor
|> :binary.replace("__FILE__", URI.encode(Path.expand(file)))
|> :binary.replace("__FILE__", URI.encode(Path.expand(source)))
|> :binary.replace("__RELATIVEFILE__", URI.encode(file))
|> :binary.replace("__LINE__", to_string(line))
|> h
end
Expand Down
12 changes: 11 additions & 1 deletion test/plug/debugger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ defmodule Plug.DebuggerTest do
assert conn.resp_body =~ "<script>oops</script>"
end

test "uses PLUG_EDITOR" do
test "uses PLUG_EDITOR with __FILE__" do
System.put_env("PLUG_EDITOR", "hello://open?file=__FILE__&line=__LINE__")

conn = stack([{Plug.Conn, :unknown, 1, file: "lib/plug/conn.ex", line: 1}])
Expand All @@ -344,6 +344,16 @@ defmodule Plug.DebuggerTest do
assert conn.resp_body =~ "hello://open?file=#{file}&line=10000"
end

test "uses PLUG_EDITOR with __RELATIVEFILE__" do
System.put_env("PLUG_EDITOR", "hello://open?file=__RELATIVEFILE__&line=__LINE__")

conn = stack([{Plug.Conn, :unknown, 1, file: "lib/plug/conn.ex", line: 1}])
assert conn.resp_body =~ "hello://open?file=lib/plug/conn.ex&line=1"

conn = stack([{GenServer, :call, 2, file: "lib/gen_server.ex", line: 10_000}])
assert conn.resp_body =~ "hello://open?file=lib/gen_server.ex&line=10000"
end

test "styles can be overridden" do
conn = put_req_header(conn(:get, "/boom"), "accept", "text/html")

Expand Down

0 comments on commit 4d6e1ed

Please sign in to comment.