Skip to content

Guard struct completion against non-charlist cursor contexts#44

Open
mariari wants to merge 1 commit into
masterfrom
mariari/fix-struct-completion-non-alias
Open

Guard struct completion against non-charlist cursor contexts#44
mariari wants to merge 1 commit into
masterfrom
mariari/fix-struct-completion-non-alias

Conversation

@mariari

@mariari mariari commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Code completion would fail on stuff like %MODULE

with an error like

23:27:24.599 [error] #PID<0.264764.0> running GtBridge.Http.Router (connection #PID<0.264761.0>, stream id 1) terminated
Server: localhost:1212 (http)
Request: POST /COMPLETE
** (exit) exited in: GenServer.call(#PID<0.264748.0>, {:complete, "%__MODULE__", "@doc \"\"\"\nFork a command log\n\"\"\"\n@spec fork(non_neg_integer() | :tip, t()) :: t()\ndef fork(at \\\\ :tip, from = %__MODULE__{id: id} \\\\ head()) do\n  unless from == :main or from in list() do\n    raise ArgumentError, \"cannot fork from unknown branch \#{inspect(from)}\"\n  end\n\n  branch = %__MODULE__:\"fork_\#{System.unique_integer([:positive])}\"\n  AL.Command.create_tables(branch)\n  AL.Command.copy_prefix(from, branch, at_time(at))\n  AL.Object.create_tables(branch)\n  AL.Object.hydrate_since(0, branch)\n  register(branch, from)\n  AL.Scheduler.start(branch)\n  branch\n%__MODULE__"}, :infinity)
    ** (EXIT) an exception was raised:
        ** (FunctionClauseError) no function clause matching in List.to_string/1
            (elixir 1.18.4) lib/list.ex:1119: List.to_string({:local_or_var, ~c"__MODULE__"})
            (gt_bridge 0.17.3) lib/gt_bridge/completion.ex:47: GtBridge.Completion.complete/4
            (gt_bridge 0.17.3) lib/gt_bridge/eval.ex:147: GtBridge.Eval.handle_call/3
            (stdlib 6.1) gen_server.erl:2381: :gen_server.try_handle_call/4
            (stdlib 6.1) gen_server.erl:2410: :gen_server.handle_msg/6
            (stdlib 6.1) proc_lib.erl:329: :proc_lib.init_p_do_apply/3

this patch seeks to rectify this issue

@mariari mariari force-pushed the mariari/fix-struct-completion-non-alias branch from 9cbfa7d to 19e86ca Compare June 26, 2026 08:35
Code.Fragment.cursor_context/1 returns {:struct, hint} where hint is
only a charlist for a plain alias prefix (%MapS). For %__MODULE__ it
returns {:struct, {:local_or_var, _}}, for %File. {:struct, {:dot,
{:alias, _}, _}}, and for %@attr {:struct, {:module_attribute, _}}.
Passing hint straight to List.to_string/1 crashed the /COMPLETE
handler on every non-charlist shape (e.g. typing %__MODULE__).

Flatten each context tuple to a prefix string with struct_prefix/1 and
complete from there: dotted aliases list submodule structs (%File. ->
File.Stat) and the 0-arity Kernel special forms (%__MOD -> __MODULE__)
complete, sourced from Kernel.SpecialForms rather than naming any one.
Shapes with no struct name (a bare variable or module attribute)
complete to nothing instead of crashing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mariari mariari force-pushed the mariari/fix-struct-completion-non-alias branch from 19e86ca to 194cd61 Compare June 26, 2026 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant