Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __RELATIVEFILE__ to PLUG_EDITOR replacements #1240

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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