Skip to content

Commit b2b0d89

Browse files
committed
Support token_metadata in container_cursor_to_quoted
1 parent 78bbaf8 commit b2b0d89

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

lib/elixir/lib/code/fragment.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,11 @@ defmodule Code.Fragment do
806806
* `:columns` - when `true`, attach a `:column` key to the quoted
807807
metadata. Defaults to `false`.
808808
809+
* `:token_metadata` - when `true`, includes token-related
810+
metadata in the expression AST, such as metadata for `do` and `end`
811+
tokens, for closing tokens, end of expressions, as well as delimiters
812+
for sigils. See `t:Macro.metadata/0`. Defaults to `false`.
813+
809814
"""
810815
@doc since: "1.13.0"
811816
@spec container_cursor_to_quoted(List.Chars.t(), keyword()) ::
@@ -815,12 +820,14 @@ defmodule Code.Fragment do
815820
line = Keyword.get(opts, :line, 1)
816821
column = Keyword.get(opts, :column, 1)
817822
columns = Keyword.get(opts, :columns, false)
823+
token_metadata = Keyword.get(opts, :token_metadata, false)
824+
818825
fragment = to_charlist(fragment)
819826
tokenizer_opts = [file: file, cursor_completion: true, columns: columns]
820827

821828
case :elixir_tokenizer.tokenize(fragment, line, column, tokenizer_opts) do
822829
{:ok, _, _, _warnings, tokens} ->
823-
:elixir.tokens_to_quoted(tokens, file, columns: columns)
830+
:elixir.tokens_to_quoted(tokens, file, columns: columns, token_metadata: token_metadata)
824831

825832
{:error, {line, column, {prefix, suffix}, token}, _rest, _warnings, _so_far} ->
826833
location = [line: line, column: column]

lib/elixir/lib/macro.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,9 +1772,7 @@ defmodule Macro do
17721772
def quoted_literal?({:{}, _, args}), do: quoted_literal?(args)
17731773
def quoted_literal?({left, right}), do: quoted_literal?(left) and quoted_literal?(right)
17741774
def quoted_literal?(list) when is_list(list), do: Enum.all?(list, &quoted_literal?/1)
1775-
1776-
def quoted_literal?(term),
1777-
do: is_atom(term) or is_number(term) or is_binary(term)
1775+
def quoted_literal?(term), do: is_atom(term) or is_number(term) or is_binary(term)
17781776

17791777
@doc """
17801778
Receives an AST node and expands it until it can no longer

lib/elixir/test/elixir/code_fragment_test.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,9 +908,12 @@ defmodule CodeFragmentTest do
908908
assert cc2q("foo(bar do :done end |>") == s2q("foo(__cursor__())")
909909
end
910910

911-
test "returns column information" do
911+
test "options" do
912912
assert cc2q("foo(", columns: true) == s2q("foo(__cursor__())", columns: true)
913913
assert cc2q("foo(123,", columns: true) == s2q("foo(123,__cursor__())", columns: true)
914+
915+
assert cc2q("foo(", token_metadata: true) == s2q("foo(__cursor__())", token_metadata: true)
916+
assert cc2q("foo(123,", token_metadata: true) == s2q("foo(123,__cursor__())", token_metadata: true)
914917
end
915918
end
916919
end

0 commit comments

Comments
 (0)