Skip to content

Commit

Permalink
Merge pull request #135 from liveview-native/bc-add-annotate-caller
Browse files Browse the repository at this point in the history
Annotate caller
  • Loading branch information
bcardarella authored Feb 27, 2024
2 parents 2fb3330 + d9ddc01 commit 9bbac79
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
5 changes: 1 addition & 4 deletions lib/live_view_native/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,14 @@ defmodule LiveViewNative.Component do
raise "~LVN requires a variable named \"assigns\" to exist and be set to a map"
end

debug_annotations? = Module.get_attribute(__CALLER__.module, :__debug_annotations__)

options = [
engine: Phoenix.LiveView.TagEngine,
file: __CALLER__.file,
line: __CALLER__.line + 1,
caller: __CALLER__,
indentation: meta[:indentation] || 0,
source: expr,
tag_handler: LiveViewNative.TagEngine,
annotate_tagged_content: debug_annotations? && (&LiveViewNative.Engine.annotate_tagged_content/1)
tag_handler: LiveViewNative.TagEngine
]

EEx.compile_string(expr, options)
Expand Down
15 changes: 1 addition & 14 deletions lib/live_view_native/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ defmodule LiveViewNative.Engine do
@doc false
defmacro compile(path) do
trim = Application.get_env(:phoenix, :trim_on_html_eex_engine, true)
debug_annotations? = Module.get_attribute(__CALLER__.module, :__debug_annotations__)
source = File.read!(path)

EEx.compile_string(source,
Expand All @@ -29,8 +28,7 @@ defmodule LiveViewNative.Engine do
trim: trim,
caller: __CALLER__,
source: source,
tag_handler: LiveViewNative.TagEngine,
annotate_tagged_content: debug_annotations? && (&annotate_tagged_content/1)
tag_handler: LiveViewNative.TagEngine
)
end

Expand All @@ -42,15 +40,4 @@ defmodule LiveViewNative.Engine do
def encode_to_iodata!(bin) when is_binary(bin), do: Phoenix.HTML.Engine.html_escape(bin)
def encode_to_iodata!(list) when is_list(list), do: LiveViewNative.Template.Safe.List.to_iodata(list)
def encode_to_iodata!(other), do: LiveViewNative.Template.Safe.to_iodata(other)

@doc false
def annotate_tagged_content(%Macro.Env{} = caller) do
%Macro.Env{module: mod, function: {func, _}, file: file, line: line} = caller
line = if line == 0, do: 1, else: line
file = Path.relative_to_cwd(file)

before = "<#{inspect(mod)}.#{func}> #{file}:#{line}"
aft = "</#{inspect(mod)}.#{func}>"
{"<!-- #{before} -->", "<!-- #{aft} -->"}
end
end
26 changes: 26 additions & 0 deletions lib/live_view_native/tag_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ defmodule LiveViewNative.TagEngine do
end
end

@doc false
@impl true
def annotate_body(%Macro.Env{} = caller) do
if Application.get_env(:phoenix_live_view, :debug_heex_annotations, false) do
%Macro.Env{module: mod, function: {func, _}, file: file, line: line} = caller
line = if line == 0, do: 1, else: line
file = Path.relative_to_cwd(file)
app = Application.get_env(:logger, :compile_time_application)

before = "<#{inspect(mod)}.#{func}> #{file}:#{line}"
aft = "</#{inspect(mod)}.#{func}>"
{"<!-- #{before} (#{app}) -->", "<!-- #{aft} -->"}
end
end

@impl true
def annotate_caller(file, line) do
if Application.get_env(:phoenix_live_view, :debug_heex_annotations, false) do
line = if line == 0, do: 1, else: line
file = Path.relative_to_cwd(file)
app = Application.get_env(:logger, :compile_time_application)

"<!-- @caller #{file}:#{line} (#{app}) -->"
end
end

@doc false
@impl true
def classify_type(":inner_block"), do: {:error, "the slot name :inner_block is reserved"}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule LiveViewNative.MixProject do
[
{:phoenix, "~> 1.7.0"},
{:phoenix_view, "~> 2.0"},
{:phoenix_live_view, github: "phoenixframework/phoenix_live_view", ref: "4939fb8", override: true},
{:phoenix_live_view, github: "phoenixframework/phoenix_live_view", branch: "main", override: true},
{:phoenix_live_reload, "~> 1.4", only: :test},
{:phoenix_template, "~> 1.0.4"},
{:phoenix_html, "~> 3.3 or ~> 4.0 or ~> 4.1"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"phoenix": {:hex, :phoenix, "1.7.11", "1d88fc6b05ab0c735b250932c4e6e33bfa1c186f76dcf623d8dd52f07d6379c7", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "b1ec57f2e40316b306708fe59b92a16b9f6f4bf50ccfa41aa8c7feb79e0ec02a"},
"phoenix_html": {:hex, :phoenix_html, "4.0.0", "4857ec2edaccd0934a923c2b0ba526c44a173c86b847e8db725172e9e51d11d6", [:mix], [], "hexpm", "cee794a052f243291d92fa3ccabcb4c29bb8d236f655fb03bcbdc3a8214b8d13"},
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.4.1", "2aff698f5e47369decde4357ba91fc9c37c6487a512b41732818f2204a8ef1d3", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "9bffb834e7ddf08467fe54ae58b5785507aaba6255568ae22b4d46e2bb3615ab"},
"phoenix_live_view": {:git, "https://github.com/phoenixframework/phoenix_live_view.git", "4939fb878363fe353ec9a68e28b6256c32e696b2", [ref: "4939fb8"]},
"phoenix_live_view": {:git, "https://github.com/phoenixframework/phoenix_live_view.git", "251f0a413568a540d0c9b3405b96a59b453547d9", [branch: "main"]},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
"phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"},
"phoenix_view": {:hex, :phoenix_view, "2.0.3", "4d32c4817fce933693741deeb99ef1392619f942633dde834a5163124813aad3", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}], "hexpm", "cd34049af41be2c627df99cd4eaa71fc52a328c0c3d8e7d4aa28f880c30e7f64"},
Expand Down

0 comments on commit 9bbac79

Please sign in to comment.