From 7b14af2de0825b01b728e4af80a72cf410678ded Mon Sep 17 00:00:00 2001 From: Phil-Bastian Berndt Date: Wed, 17 Jul 2024 20:20:59 +0200 Subject: [PATCH] Add __RELATIVEFILE__ to PLUG_EDITOR replacements (#1240) --- lib/plug/debugger.ex | 12 +++++++++--- test/plug/debugger_test.exs | 12 +++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/plug/debugger.ex b/lib/plug/debugger.ex index 3b936d07..78a36f53 100644 --- a/lib/plug/debugger.ex +++ b/lib/plug/debugger.ex @@ -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} @@ -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 @@ -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 diff --git a/test/plug/debugger_test.exs b/test/plug/debugger_test.exs index 1508781b..09cafb4f 100644 --- a/test/plug/debugger_test.exs +++ b/test/plug/debugger_test.exs @@ -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}]) @@ -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")