From c25317344b91379ad7cbb2a7f6af7ea050002341 Mon Sep 17 00:00:00 2001 From: Jason Stiebs Date: Mon, 19 Oct 2026 08:45:27 -0500 Subject: [PATCH] Parse ~GPUI templates without Phoenix LiveView GPUI only used LiveView's TagEngine.Parser to tokenize HEEx-style markup; compilation to %GPUI.Element{} was already GPUI's own. Replace it with a small parser for the subset GPUI supports (tags, remote/local components, slots, {expr} attributes and children) and drop phoenix_live_view, which also removes phoenix, phoenix_html, phoenix_pubsub, phoenix_template and websock from the dependency tree. Output is unchanged for every existing ~GPUI template in the repo. HTML comments are now dropped instead of rendered as text, and unsupported syntax (EEx tags, root attributes, value-less attributes) raises a CompileError with the template line and column. --- CHANGELOG.md | 9 + apps/gpui/CHANGELOG.md | 8 + apps/gpui/lib/gpui/template.ex | 141 +++++------ apps/gpui/lib/gpui/template/engine.ex | 31 --- apps/gpui/lib/gpui/template/parser.ex | 322 ++++++++++++++++++++++++++ apps/gpui/mix.exs | 1 - apps/gpui/test/gpui/template_test.exs | 36 +++ mix.lock | 7 - 8 files changed, 427 insertions(+), 128 deletions(-) delete mode 100644 apps/gpui/lib/gpui/template/engine.ex create mode 100644 apps/gpui/lib/gpui/template/parser.ex diff --git a/CHANGELOG.md b/CHANGELOG.md index 5920c7b1..c73c14dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ## Unreleased +### Changed + +- Parse `~GPUI` templates with GPUI's own parser and drop the + `phoenix_live_view` dependency (and with it Phoenix, Phoenix HTML, and + WebSock). Template output is unchanged, except that HTML comments are now + dropped instead of rendered as literal text. Unsupported syntax (`<%= %>`, + root attributes like `{@rest}`, and value-less attributes) now fails with a + `CompileError` naming the template line and column. + ## 0.2.0 - 2026-09-08 ### Breaking changes diff --git a/apps/gpui/CHANGELOG.md b/apps/gpui/CHANGELOG.md index 6bf2efec..8bb65eaf 100644 --- a/apps/gpui/CHANGELOG.md +++ b/apps/gpui/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +### Changed + +- Parse `~GPUI` templates with GPUI's own parser and drop the + `phoenix_live_view` dependency. Template output is unchanged, except that + HTML comments are now dropped instead of rendered as literal text. + Unsupported syntax (`<%= %>`, root attributes like `{@rest}`, and value-less + attributes) now fails with a `CompileError` naming the line and column. + ## 0.2.0-rc.1 - 2026-09-02 ### Breaking changes diff --git a/apps/gpui/lib/gpui/template.ex b/apps/gpui/lib/gpui/template.ex index 8f2fb34d..aa1ecff1 100644 --- a/apps/gpui/lib/gpui/template.ex +++ b/apps/gpui/lib/gpui/template.ex @@ -2,9 +2,11 @@ defmodule GPUI.Template do @moduledoc """ HEEx-style template support for GPUI. - This module intentionally reuses Phoenix LiveView's tag parser for the - HEEx-compatible tokenizer/tree builder, then compiles the parsed tree into - `%GPUI.Element{}` values instead of `%Phoenix.LiveView.Rendered{}`. + Templates use the element subset of HEEx syntax: tags, `` + and `<.local_component>` calls, `<:slot>` entries, and `{expression}` + attribute values and children. They compile into `%GPUI.Element{}` trees at + compile time. EEx tags (`<%= %>`), `:if`/`:for`, and root attributes such as + `{@rest}` are not supported; use ordinary Elixir expressions instead. Lowercase tags are renderer primitives. Native components use `GPUI.UI` or `GPUI.UI.Overlay`; their internal `ui_*` element tags are intentionally not a @@ -12,7 +14,7 @@ defmodule GPUI.Template do """ alias GPUI.Element - alias Phoenix.LiveView.TagEngine.Parser + alias GPUI.Template.Parser @primitive_tag_lookup GPUI.Schema.components() |> Enum.reject(&String.starts_with?(Atom.to_string(&1.tag), "ui_")) @@ -25,75 +27,63 @@ defmodule GPUI.Template do @spec compile(String.t(), Macro.Env.t(), keyword()) :: Macro.t() def compile(source, caller, meta \\ []) do - %Parser{nodes: nodes} = + nodes = Parser.parse!(source, - caller: caller, file: caller.file || "nofile", - line: Keyword.get(meta, :line, caller.line), - tag_handler: GPUI.Template.Engine + line: Keyword.get(meta, :line, caller.line) ) - case Enum.reject(nodes, &blank_text?/1) do + case nodes do [node] -> compile_node(node, caller) [] -> raise ArgumentError, "GPUI templates require one root element" _nodes -> raise ArgumentError, "GPUI templates require exactly one root element" end end - defp compile_node({:block, :tag, name, attrs, children, open_meta, _close_meta}, caller) do - quote do - %Element{ - type: unquote(tag_atom(name, caller, open_meta)), - attrs: unquote(compile_attrs(attrs, caller)), - children: - List.flatten( - unquote( - children - |> Enum.reject(&blank_text?/1) - |> Enum.map(&compile_node(&1, caller)) - ) - ) - } - end - end - - defp compile_node({:self_close, :tag, name, attrs, meta}, caller) do + defp compile_node({:tag, name, attrs, children, meta}, caller) do quote do %Element{ type: unquote(tag_atom(name, caller, meta)), attrs: unquote(compile_attrs(attrs, caller)), - children: [] + children: unquote(flat_children(children, caller)) } end end - defp compile_node( - {:block, component_type, name, attrs, children, _open_meta, _close_meta}, - caller - ) - when component_type in [:remote_component, :local_component] do - children = Enum.reject(children, &blank_text?/1) - {slots, children} = Enum.split_with(children, &slot_node?/1) - children = Enum.map(children, &compile_node(&1, caller)) - component_call(component_type, name, attrs, children, compile_slots(slots, caller), caller) + defp compile_node({:component, kind, name, attrs, children, _meta}, caller) do + {slots, children} = Enum.split_with(children, &match?({:slot, _, _, _, _}, &1)) + + component_call( + kind, + name, + attrs, + compile_children(children, caller), + compile_slots(slots, caller), + caller + ) end - defp compile_node({:self_close, component_type, name, attrs, _meta}, caller) - when component_type in [:remote_component, :local_component] do - component_call(component_type, name, attrs, [], [], caller) - end + defp compile_node({:text, text}, _caller), do: text + + defp compile_node({:expr, code, meta}, caller), do: compile_expr(code, meta, caller) - defp compile_node({:text, text, _meta}, _caller), do: text + defp compile_children(children, caller), do: Enum.map(children, &compile_node(&1, caller)) - defp compile_node({:body_expr, expr, meta}, _caller) do - Code.string_to_quoted!(expr, line: meta.line, column: meta.column) + defp flat_children([], _caller), do: [] + + defp flat_children(children, caller) do + quote do: List.flatten(unquote(compile_children(children, caller))) end - defp compile_node({:eex, _type, expr, meta}, _caller) do - Code.string_to_quoted!(expr, line: meta.line, column: meta.column) + defp compile_expr(code, meta, caller) do + Code.string_to_quoted!(code, + file: caller.file || "nofile", + line: meta.line, + column: meta.column + ) end - defp component_call(:remote_component, name, attrs, children, slots, caller) do + defp component_call(:remote, name, attrs, children, slots, caller) do {module, function} = remote_component_target(name, caller) quote do @@ -107,7 +97,7 @@ defmodule GPUI.Template do end end - defp component_call(:local_component, name, attrs, children, slots, caller) do + defp component_call(:local, name, attrs, children, slots, caller) do function = compile_time_atom(name) module = caller.module @@ -123,33 +113,16 @@ defmodule GPUI.Template do end defp compile_slots(slots, caller) do - Enum.map(slots, fn - {:block, :slot, name, attrs, children, _open_meta, _close_meta} -> - children = - children - |> Enum.reject(&blank_text?/1) - |> Enum.map(&compile_node(&1, caller)) - - slot = - quote do - %GPUI.Template.Component.Slot{ - attrs: unquote(compile_attrs(attrs, caller)), - children: List.flatten(unquote(children)) - } - end - - {compile_time_atom(name), slot} - - {:self_close, :slot, name, attrs, _meta} -> - slot = - quote do - %GPUI.Template.Component.Slot{ - attrs: unquote(compile_attrs(attrs, caller)), - children: [] - } - end - - {compile_time_atom(name), slot} + Enum.map(slots, fn {:slot, name, attrs, children, _meta} -> + slot = + quote do + %GPUI.Template.Component.Slot{ + attrs: unquote(compile_attrs(attrs, caller)), + children: unquote(flat_children(children, caller)) + } + end + + {compile_time_atom(name), slot} end) end @@ -172,10 +145,6 @@ defmodule GPUI.Template do |> Macro.expand(caller) end - defp slot_node?({:self_close, :slot, _name, _attrs, _meta}), do: true - defp slot_node?({:block, :slot, _name, _attrs, _children, _open_meta, _close_meta}), do: true - defp slot_node?(_node), do: false - defp function_name?(<>), do: first in ?a..?z or first == ?_ defp function_name?(""), do: false @@ -230,14 +199,14 @@ defmodule GPUI.Template do attrs |> Enum.map(fn {name, value, _meta} -> - {attr_atom(to_string(name)), compile_attr_value(value)} + {attr_atom(name), compile_attr_value(value, caller)} end) |> normalize_class_attr() end defp validate_unique_attrs!(attrs, caller) do attrs - |> Enum.group_by(fn {name, _value, _meta} -> to_string(name) end) + |> Enum.group_by(fn {name, _value, _meta} -> name end) |> Enum.each(fn {_name, [_attr]} -> :ok @@ -295,12 +264,6 @@ defmodule GPUI.Template do defp put_unknown_class_attr(attrs, unknown), do: Keyword.put(attrs, :class, Enum.join(unknown, " ")) - defp compile_attr_value({:string, value, _meta}), do: value - - defp compile_attr_value({:expr, expr, meta}) do - Code.string_to_quoted!(expr, line: meta.line, column: meta.column) - end - - defp blank_text?({:text, text, _meta}), do: String.trim(text) == "" - defp blank_text?(_node), do: false + defp compile_attr_value({:string, value}, _caller), do: value + defp compile_attr_value({:expr, code, meta}, caller), do: compile_expr(code, meta, caller) end diff --git a/apps/gpui/lib/gpui/template/engine.ex b/apps/gpui/lib/gpui/template/engine.ex deleted file mode 100644 index d51bfda4..00000000 --- a/apps/gpui/lib/gpui/template/engine.ex +++ /dev/null @@ -1,31 +0,0 @@ -defmodule GPUI.Template.Engine do - @moduledoc "Phoenix HTML engine that lowers HEEx nodes into GPUI element values." - - @behaviour Phoenix.LiveView.TagEngine - - @impl Phoenix.LiveView.TagEngine - def classify_type(":inner_block"), do: {:error, "the slot name :inner_block is reserved"} - def classify_type(":" <> name), do: {:slot, name} - - def classify_type(<> = name) when first in ?A..?Z, - do: {:remote_component, name} - - def classify_type("."), do: {:error, "a component name is required after ."} - def classify_type("." <> name), do: {:local_component, name} - def classify_type(name), do: {:tag, name} - - @impl Phoenix.LiveView.TagEngine - def void?(_name), do: false - - @impl Phoenix.LiveView.TagEngine - def handle_attributes(ast, _meta), do: {:quoted, ast} - - @impl Phoenix.LiveView.TagEngine - def annotate_body(_caller), do: nil - - @impl Phoenix.LiveView.TagEngine - def annotate_slot(_name, _tag_meta, _close_tag_meta, _caller), do: nil - - @impl Phoenix.LiveView.TagEngine - def annotate_caller(_file, _line, _caller), do: nil -end diff --git a/apps/gpui/lib/gpui/template/parser.ex b/apps/gpui/lib/gpui/template/parser.ex new file mode 100644 index 00000000..5f75f222 --- /dev/null +++ b/apps/gpui/lib/gpui/template/parser.ex @@ -0,0 +1,322 @@ +defmodule GPUI.Template.Parser do + @moduledoc false + + # Parses the HEEx subset accepted by `GPUI.Template` into a node tree: + # + # {:tag, name, attrs, children, meta} + # {:component, :remote | :local, name, attrs, children, meta} + # {:slot, name, attrs, children, meta} + # {:text, binary} + # {:expr, code, meta} + # + # Attributes are `{name, {:string, value} | {:expr, code, meta}, meta}`. + # Whitespace-only text and HTML comments are dropped. Expressions end at the + # first unbalanced `}`; like HEEx, braces inside Elixir strings count unless + # escaped as `\{` or `\}`. + + @space_chars ~c"\s\t\f" + @name_stop_chars ~c">/=\r\n\"'" ++ @space_chars + + @spec parse!(String.t(), keyword()) :: list() + def parse!(source, opts) do + state = %{file: Keyword.fetch!(opts, :file)} + {nodes, "", _pos} = children(source, {Keyword.get(opts, :line, 1), 1}, nil, state, [], []) + nodes + end + + # Children are parsed until the parent's closing tag (or the end of input at + # the root). `text` collects the current text run in reverse. + defp children(" rest, pos, parent, state, text, acc) do + acc = flush_text(text, acc) + {name, rest, name_end} = name!(rest, advance(pos, 2), state, "expected tag name after " <> rest -> rest + _rest -> syntax_error!(state, name_end, "expected closing `>`") + end + + case parent do + %{name: ^name} -> + {Enum.reverse(acc), rest, advance(name_end, 1)} + + %{name: open, meta: meta} -> + syntax_error!( + state, + pos, + "unmatched closing tag. Expected for <#{open}> at line #{meta.line}, got: " + ) + + nil -> + syntax_error!(state, pos, "missing opening tag for ") + end + end + + defp children("<%" <> _rest, pos, _parent, state, _text, _acc) do + syntax_error!(state, pos, "EEx tags are not supported in GPUI templates; use {expression}") + end + + defp children("" <> rest, pos, _open_pos, _state), do: {rest, advance(pos, 3)} + + defp skip_comment("", _pos, open_pos, state), + do: syntax_error!(state, open_pos, "expected closing `-->` for comment") + + defp skip_comment(rest, pos, open_pos, state) do + {_char, rest, pos} = next_char(rest, pos) + skip_comment(rest, pos, open_pos, state) + end + + defp name!(rest, pos, state, message) do + case take_name(rest, pos, []) do + {[], _rest, _pos} -> syntax_error!(state, pos, message) + {name, rest, pos} -> {reverse_string(name), rest, pos} + end + end + + defp take_name(<> = rest, pos, acc) when char in @name_stop_chars, + do: {acc, rest, pos} + + defp take_name("", pos, acc), do: {acc, "", pos} + + defp take_name(rest, pos, acc) do + {char, rest, pos} = next_char(rest, pos) + take_name(rest, pos, [char | acc]) + end + + defp skip_space(<>, pos) when char in @space_chars, + do: skip_space(rest, advance(pos, 1)) + + defp skip_space(<> = rest, pos) when char in [?\r, ?\n] do + {_char, rest, pos} = next_char(rest, pos) + skip_space(rest, pos) + end + + defp skip_space(rest, pos), do: {rest, pos} + + defp next_char("\r\n" <> rest, {line, _column}), do: {"\r\n", rest, {line + 1, 1}} + defp next_char("\n" <> rest, {line, _column}), do: {?\n, rest, {line + 1, 1}} + defp next_char(<>, pos), do: {<>, rest, advance(pos, 1)} + + defp next_char(<<_byte, _rest::binary>>, _pos), + do: raise(ArgumentError, "GPUI templates must be valid UTF-8") + + defp flush_text([], acc), do: acc + + defp flush_text(text, acc) do + text = reverse_string(text) + + cond do + String.trim(text) == "" -> acc + # HEEx trims the text that follows a slot entry. + match?([{:slot, _, _, _, _} | _], acc) -> [{:text, String.trim_leading(text)} | acc] + true -> [{:text, text} | acc] + end + end + + defp reverse_string(chars), do: chars |> Enum.reverse() |> IO.iodata_to_binary() + + defp advance({line, column}, count), do: {line, column + count} + + defp meta({line, column}), do: %{line: line, column: column} + + defp syntax_error!(state, {line, column}, message) do + raise CompileError, file: state.file, line: line, description: "#{message} (column #{column})" + end +end diff --git a/apps/gpui/mix.exs b/apps/gpui/mix.exs index 8d2f5eda..dd3669a2 100644 --- a/apps/gpui/mix.exs +++ b/apps/gpui/mix.exs @@ -48,7 +48,6 @@ defmodule GPUI.MixProject do defp deps do [ {:file_system, "~> 1.0", optional: true}, - {:phoenix_live_view, "~> 1.2.8"}, {:safe_rpc, "~> 0.1.14"}, {:ex_doc, "~> 0.40.3", only: :dev, runtime: false} ] diff --git a/apps/gpui/test/gpui/template_test.exs b/apps/gpui/test/gpui/template_test.exs index 7e1fec3b..3e65c3f6 100644 --- a/apps/gpui/test/gpui/template_test.exs +++ b/apps/gpui/test/gpui/template_test.exs @@ -43,6 +43,42 @@ defmodule GPUI.TemplateTest do """ end + test "parses quoted, multi-line, and nested-brace attribute values" do + styles = %{gap: "2"} + + assert %GPUI.Element{ + type: :div, + attrs: [id: "single", title: "two\nlines", value: "2"], + children: [ + %GPUI.Element{type: :text, children: ["a } brace: ", "{", "!"]} + ] + } = + ~GPUI""" +
+ + a } brace: {"\{"}! +
+ """ + end + + test "template syntax errors report the template position" do + for {source, message} <- [ + {"
", ~r/t.ex:1: unmatched closing tag. Expected <\/text>.*column 12/}, + {"
\n ", ~r/t.ex:2: end of template reached without closing tag for /}, + {"
<%= 1 %>
", ~r/EEx tags are not supported/}, + {"
", ~r/root attributes like \{@rest\} are not supported/}, + {"