Skip to content

Commit db8a1cd

Browse files
committed
Revert "Consider surround context until end whenever possible"
This reverts commit a65dae9.
1 parent d25aacb commit db8a1cd

File tree

2 files changed

+40
-88
lines changed

2 files changed

+40
-88
lines changed

lib/elixir/lib/code/fragment.ex

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -636,15 +636,15 @@ defmodule Code.Fragment do
636636
{reversed_pre, post} = adjust_position(reversed_pre, post)
637637

638638
case take_identifier(post, []) do
639-
:none ->
639+
{_, [], _} ->
640640
maybe_operator(reversed_pre, post, line, opts)
641641

642642
{:identifier, reversed_post, rest} ->
643643
{rest, _} = strip_spaces(rest, 0)
644644
reversed = reversed_post ++ reversed_pre
645645

646646
case codepoint_cursor_context(reversed, opts) do
647-
{{:struct, acc}, offset} when acc != [] ->
647+
{{:struct, acc}, offset} ->
648648
build_surround({:struct, acc}, reversed, line, offset)
649649

650650
{{:alias, acc}, offset} ->
@@ -749,27 +749,11 @@ defmodule Code.Fragment do
749749
do: take_identifier(t, [h | acc])
750750

751751
defp take_identifier(rest, acc) do
752-
{stripped, _} = strip_spaces(rest, 0)
753-
754-
with [?. | t] <- stripped,
752+
with {[?. | t], _} <- strip_spaces(rest, 0),
755753
{[h | _], _} when h in ?A..?Z <- strip_spaces(t, 0) do
756754
take_alias(rest, acc)
757755
else
758-
# Consider it an identifier if we are at the end of line
759-
# or if we have spaces not followed by . (call) or / (arity)
760-
_ when acc == [] and (rest == [] or (hd(rest) in @space and hd(stripped) not in ~c"/.")) ->
761-
{:identifier, acc, rest}
762-
763-
# If we are immediately followed by a container, we are still part of the identifier.
764-
# We don't consider << as it _may_ be an operator.
765-
_ when acc == [] and hd(stripped) in ~c"({[" ->
766-
{:identifier, acc, rest}
767-
768-
_ when acc == [] ->
769-
:none
770-
771-
_ ->
772-
{:identifier, acc, rest}
756+
_ -> {:identifier, acc, rest}
773757
end
774758
end
775759

lib/elixir/test/elixir/code_fragment_test.exs

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -443,50 +443,49 @@ defmodule CodeFragmentTest do
443443
end
444444

445445
test "column out of range" do
446-
assert CF.surround_context("hello", {1, 20}) ==
447-
%{begin: {1, 1}, context: {:local_or_var, ~c"hello"}, end: {1, 6}}
446+
assert CF.surround_context("hello", {1, 20}) == :none
448447
end
449448

450449
test "local_or_var" do
451-
for i <- 1..9 do
450+
for i <- 1..8 do
452451
assert CF.surround_context("hello_wo", {1, i}) == %{
453452
context: {:local_or_var, ~c"hello_wo"},
454453
begin: {1, 1},
455454
end: {1, 9}
456455
}
457456
end
458457

459-
assert CF.surround_context("hello_wo ", {1, 10}) == :none
458+
assert CF.surround_context("hello_wo", {1, 9}) == :none
460459

461-
for i <- 2..10 do
460+
for i <- 2..9 do
462461
assert CF.surround_context(" hello_wo", {1, i}) == %{
463462
context: {:local_or_var, ~c"hello_wo"},
464463
begin: {1, 2},
465464
end: {1, 10}
466465
}
467466
end
468467

469-
assert CF.surround_context(" hello_wo ", {1, 11}) == :none
468+
assert CF.surround_context(" hello_wo", {1, 10}) == :none
470469

471-
for i <- 1..7 do
470+
for i <- 1..6 do
472471
assert CF.surround_context("hello!", {1, i}) == %{
473472
context: {:local_or_var, ~c"hello!"},
474473
begin: {1, 1},
475474
end: {1, 7}
476475
}
477476
end
478477

479-
assert CF.surround_context("hello! ", {1, 8}) == :none
478+
assert CF.surround_context("hello!", {1, 7}) == :none
480479

481-
for i <- 1..6 do
480+
for i <- 1..5 do
482481
assert CF.surround_context("안녕_세상", {1, i}) == %{
483482
context: {:local_or_var, ~c"안녕_세상"},
484483
begin: {1, 1},
485484
end: {1, 6}
486485
}
487486
end
488487

489-
assert CF.surround_context("안녕_세상 ", {1, 6}) == :none
488+
assert CF.surround_context("안녕_세상", {1, 6}) == :none
490489

491490
# Keywords are not local or var
492491
for keyword <- ~w(do end after catch else rescue fn true false nil)c do
@@ -500,77 +499,46 @@ defmodule CodeFragmentTest do
500499
end
501500
end
502501

503-
test "local + operator" do
504-
for i <- 1..8 do
505-
assert CF.surround_context("hello_wo+", {1, i}) == %{
506-
context: {:local_or_var, ~c"hello_wo"},
507-
begin: {1, 1},
508-
end: {1, 9}
509-
}
510-
end
511-
512-
assert CF.surround_context("hello_wo+", {1, 9}) == %{
513-
begin: {1, 9},
514-
context: {:operator, ~c"+"},
515-
end: {1, 10}
516-
}
517-
518-
for i <- 1..9 do
519-
assert CF.surround_context("hello_wo +", {1, i}) == %{
520-
context: {:local_or_var, ~c"hello_wo"},
521-
begin: {1, 1},
522-
end: {1, 9}
523-
}
524-
end
525-
526-
assert CF.surround_context("hello_wo +", {1, 10}) == %{
527-
begin: {1, 10},
528-
context: {:operator, ~c"+"},
529-
end: {1, 11}
530-
}
531-
end
532-
533502
test "local call" do
534-
for i <- 1..9 do
503+
for i <- 1..8 do
535504
assert CF.surround_context("hello_wo(", {1, i}) == %{
536505
context: {:local_call, ~c"hello_wo"},
537506
begin: {1, 1},
538507
end: {1, 9}
539508
}
540509
end
541510

542-
assert CF.surround_context("hello_wo(", {1, 10}) == :none
511+
assert CF.surround_context("hello_wo(", {1, 9}) == :none
543512

544-
for i <- 1..9 do
513+
for i <- 1..8 do
545514
assert CF.surround_context("hello_wo (", {1, i}) == %{
546515
context: {:local_call, ~c"hello_wo"},
547516
begin: {1, 1},
548517
end: {1, 9}
549518
}
550519
end
551520

552-
assert CF.surround_context("hello_wo (", {1, 10}) == :none
553-
assert CF.surround_context("hello_wo (", {1, 11}) == :none
521+
assert CF.surround_context("hello_wo (", {1, 9}) == :none
554522

555-
for i <- 1..7 do
523+
for i <- 1..6 do
556524
assert CF.surround_context("hello!(", {1, i}) == %{
557525
context: {:local_call, ~c"hello!"},
558526
begin: {1, 1},
559527
end: {1, 7}
560528
}
561529
end
562530

563-
assert CF.surround_context("hello!(", {1, 8}) == :none
531+
assert CF.surround_context("hello!(", {1, 7}) == :none
564532

565-
for i <- 1..6 do
533+
for i <- 1..5 do
566534
assert CF.surround_context("안녕_세상(", {1, i}) == %{
567535
context: {:local_call, ~c"안녕_세상"},
568536
begin: {1, 1},
569537
end: {1, 6}
570538
}
571539
end
572540

573-
assert CF.surround_context("안녕_세상(", {1, 7}) == :none
541+
assert CF.surround_context("안녕_세상(", {1, 6}) == :none
574542
end
575543

576544
test "local arity" do
@@ -698,47 +666,47 @@ defmodule CodeFragmentTest do
698666
end
699667

700668
test "alias" do
701-
for i <- 1..9 do
669+
for i <- 1..8 do
702670
assert CF.surround_context("HelloWor", {1, i}) == %{
703671
context: {:alias, ~c"HelloWor"},
704672
begin: {1, 1},
705673
end: {1, 9}
706674
}
707675
end
708676

709-
assert CF.surround_context("HelloWor ", {1, 10}) == :none
677+
assert CF.surround_context("HelloWor", {1, 9}) == :none
710678

711-
for i <- 2..10 do
679+
for i <- 2..9 do
712680
assert CF.surround_context(" HelloWor", {1, i}) == %{
713681
context: {:alias, ~c"HelloWor"},
714682
begin: {1, 2},
715683
end: {1, 10}
716684
}
717685
end
718686

719-
assert CF.surround_context(" HelloWor ", {1, 11}) == :none
687+
assert CF.surround_context(" HelloWor", {1, 10}) == :none
720688

721-
for i <- 1..10 do
689+
for i <- 1..9 do
722690
assert CF.surround_context("Hello.Wor", {1, i}) == %{
723691
context: {:alias, ~c"Hello.Wor"},
724692
begin: {1, 1},
725693
end: {1, 10}
726694
}
727695
end
728696

729-
assert CF.surround_context("Hello.Wor ", {1, 11}) == :none
697+
assert CF.surround_context("Hello.Wor", {1, 10}) == :none
730698

731-
for i <- 1..12 do
699+
for i <- 1..11 do
732700
assert CF.surround_context("Hello . Wor", {1, i}) == %{
733701
context: {:alias, ~c"Hello.Wor"},
734702
begin: {1, 1},
735703
end: {1, 12}
736704
}
737705
end
738706

739-
assert CF.surround_context("Hello . Wor ", {1, 13}) == :none
707+
assert CF.surround_context("Hello . Wor", {1, 12}) == :none
740708

741-
for i <- 1..16 do
709+
for i <- 1..15 do
742710
assert CF.surround_context("Foo . Bar . Baz", {1, i}) == %{
743711
context: {:alias, ~c"Foo.Bar.Baz"},
744712
begin: {1, 1},
@@ -770,15 +738,15 @@ defmodule CodeFragmentTest do
770738
end: {1, 11}
771739
}
772740

773-
for i <- 1..15 do
741+
for i <- 1..14 do
774742
assert CF.surround_context("__MODULE__.Foo", {1, i}) == %{
775743
context: {:alias, {:local_or_var, ~c"__MODULE__"}, ~c"Foo"},
776744
begin: {1, 1},
777745
end: {1, 15}
778746
}
779747
end
780748

781-
for i <- 1..19 do
749+
for i <- 1..18 do
782750
assert CF.surround_context("__MODULE__.Foo.Sub", {1, i}) == %{
783751
context: {:alias, {:local_or_var, ~c"__MODULE__"}, ~c"Foo.Sub"},
784752
begin: {1, 1},
@@ -830,15 +798,15 @@ defmodule CodeFragmentTest do
830798
end
831799

832800
test "attribute submodules" do
833-
for i <- 1..10 do
801+
for i <- 1..9 do
834802
assert CF.surround_context("@some.Foo", {1, i}) == %{
835803
context: {:alias, {:module_attribute, ~c"some"}, ~c"Foo"},
836804
begin: {1, 1},
837805
end: {1, 10}
838806
}
839807
end
840808

841-
for i <- 1..14 do
809+
for i <- 1..13 do
842810
assert CF.surround_context("@some.Foo.Sub", {1, i}) == %{
843811
context: {:alias, {:module_attribute, ~c"some"}, ~c"Foo.Sub"},
844812
begin: {1, 1},
@@ -921,15 +889,15 @@ defmodule CodeFragmentTest do
921889
end: {1, 15}
922890
}
923891

924-
for i <- 2..10 do
892+
for i <- 2..9 do
925893
assert CF.surround_context("%HelloWor", {1, i}) == %{
926894
context: {:struct, ~c"HelloWor"},
927895
begin: {1, 1},
928896
end: {1, 10}
929897
}
930898
end
931899

932-
assert CF.surround_context("%HelloWor ", {1, 11}) == :none
900+
assert CF.surround_context("%HelloWor", {1, 10}) == :none
933901

934902
# With dot
935903
assert CF.surround_context("%Hello.Wor", {1, 1}) == %{
@@ -938,15 +906,15 @@ defmodule CodeFragmentTest do
938906
end: {1, 11}
939907
}
940908

941-
for i <- 2..11 do
909+
for i <- 2..10 do
942910
assert CF.surround_context("%Hello.Wor", {1, i}) == %{
943911
context: {:struct, ~c"Hello.Wor"},
944912
begin: {1, 1},
945913
end: {1, 11}
946914
}
947915
end
948916

949-
assert CF.surround_context("%Hello.Wor ", {1, 12}) == :none
917+
assert CF.surround_context("%Hello.Wor", {1, 11}) == :none
950918

951919
# With spaces
952920
assert CF.surround_context("% Hello . Wor", {1, 1}) == %{
@@ -955,15 +923,15 @@ defmodule CodeFragmentTest do
955923
end: {1, 14}
956924
}
957925

958-
for i <- 2..14 do
926+
for i <- 2..13 do
959927
assert CF.surround_context("% Hello . Wor", {1, i}) == %{
960928
context: {:struct, ~c"Hello.Wor"},
961929
begin: {1, 1},
962930
end: {1, 14}
963931
}
964932
end
965933

966-
assert CF.surround_context("% Hello . Wor ", {1, 15}) == :none
934+
assert CF.surround_context("% Hello . Wor", {1, 14}) == :none
967935
end
968936

969937
test "module attributes" do

0 commit comments

Comments
 (0)