Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/elixir/lib/code/fragment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,16 @@ defmodule Code.Fragment do
maybe_operator(reversed_pre, post, line, opts)

{:identifier, reversed_post, rest} ->
{rest, _} = strip_spaces(rest, 0)
{keyword?, rest} =
case rest do
[?: | tail] when tail == [] or hd(tail) in @space ->
{true, rest}

_ ->
{rest, _} = strip_spaces(rest, 0)
{false, rest}
end

reversed = reversed_post ++ reversed_pre

case codepoint_cursor_context(reversed, opts) do
Expand All @@ -656,6 +665,9 @@ defmodule Code.Fragment do
{{:dot, _, [_ | _]} = dot, offset} ->
build_surround(dot, reversed, line, offset)

{{:local_or_var, acc}, offset} when keyword? ->
build_surround({:keyword, acc}, reversed, line, offset)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think keyword should be returned here. It's already used for reserved words do end after else catch rescue fn true false nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I misunderstood what keyword meant in this context.
keyword_atom / keyword_key?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Macro.inspect_atom uses key. Maybe that's fine? Or we use atom_key or keyword_key.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either sounds good, I went with :key for the consistency.

Last question: in the case of do:, should it be a :key or a :keyword?
It would be :key with the current PR implementation, but I'm not sure which would make sense from a LSP perspective @lukaszsamson

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do: should be :key IMO.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK, let's have do: as key and do as keyword.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks gents will merge as is then 🚀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized: should we call out this "edge case" in the docs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is an edge case, honestly. It is how the grammar behaves everywhere. :) So there is no need imo!


{{:local_or_var, acc}, offset} when hd(rest) == ?( ->
build_surround({:local_call, acc}, reversed, line, offset)

Expand Down
32 changes: 32 additions & 0 deletions lib/elixir/test/elixir/code_fragment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,38 @@ defmodule CodeFragmentTest do

assert CF.surround_context(":", {1, 1}) == :none
end

test "keyword keys" do
for i <- 2..4 do
assert CF.surround_context("[foo:", {1, i}) == %{
context: {:keyword, ~c"foo"},
begin: {1, 2},
end: {1, 5}
}
end

for i <- 10..12 do
assert CF.surround_context("[foo: 1, bar: 2]", {1, i}) == %{
context: {:keyword, ~c"bar"},
begin: {1, 10},
end: {1, 13}
}
end
end

test "keyword false positives" do
assert CF.surround_context("<<foo::", {1, 3}) == %{
context: {:local_or_var, ~c"foo"},
begin: {1, 3},
end: {1, 6}
}

assert CF.surround_context("[foo :atom", {1, 2}) == %{
context: {:local_or_var, ~c"foo"},
begin: {1, 2},
end: {1, 5}
}
end
end

describe "container_cursor_to_quoted/2" do
Expand Down
Loading