elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Code.Fragment.surround_context returns `:none` within sigils

Open rudiejd opened this issue 10 months ago • 1 comments

Elixir and Erlang/OTP versions

❯ elixir --version Erlang/OTP 27 [erts-15.1.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.17.3 (compiled with Erlang/OTP 27)

Operating system

Arch Linux x86_64 6.12.1-arch1-1

Current behavior

%{context: {:sigil, ~c"D"}, begin: {1, 1}, end: {1, 3}}
iex [ :: 24] > Code.Fragment.surround_context("~D[2025-01-01]", {1, 2})
%{context: {:sigil, ~c"D"}, begin: {1, 1}, end: {1, 3}}
iex [ :: 25] > Code.Fragment.surround_context("~D[2025-01-01]", {1, 3})
:none
iex [ :: 26] > Code.Fragment.surround_context("~D[2025-01-01]", {1, 4})
:none

Expected behavior

%{context: {:sigil, ~c"D"}, begin: {1, 1}, end: {1, 12}}
iex [ :: 24] > Code.Fragment.surround_context("~D[2025-01-01]", {1, 2})
%{context: {:sigil, ~c"D"}, begin: {1, 1}, end: {1, 12}}
iex [ :: 25] > Code.Fragment.surround_context("~D[2025-01-01]", {1, 3})
%{context: {:sigil, ~c"D"}, begin: {1, 1}, end: {1, 12}}
iex [ :: 26] > Code.Fragment.surround_context("~D[2025-01-01]", {1, 4})
%{context: {:sigil, ~c"D"}, begin: {1, 1}, end: {1, 12}}

I would expect the surround_context for a sigil to span the entire body of the sigil. My use case is that I'd like to be able to determine when I'm in a ~H sigil in elixir-ls. Should surround_context return information about the "container" that I am in (this was my take from the docs), or am I mistaking this for another API?

rudiejd avatar Jan 30 '25 03:01 rudiejd

I don't think there is an API to return this today, one would need to be added. The reason why surround_context is not a good candidate for this are two:

  1. Sometimes you want the surround context inside a sigil itself, think autocompletion inside HEEx
  2. surround_context only looks at the current line, so it wouldn't work for a multiline sigil

The best candidate would be to change Code.Fragment.container_cursor_to_quoted("~D[2025-01-01") to return we are inside a sigil if an option is given. We could probably do it somewhat cleanly by returning something like sigil_D(<<"2025-01-01", __cursor__>>.

josevalim avatar Jan 30 '25 07:01 josevalim