Skip to content

Commit

Permalink
Always treat ranges as exclusive in Range.contains?/2
Browse files Browse the repository at this point in the history
  • Loading branch information
zachallaun committed Jun 1, 2024
1 parent 70a025a commit c54bb63
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 85 deletions.
8 changes: 4 additions & 4 deletions apps/common/lib/lexical/ast/detection/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Lexical.Ast.Detection.String do
# a string literal
defp do_detect({:__block__, _, [literal]} = ast, %Position{} = position)
when is_binary(literal) do
case fetch_range(ast, 0, -1) do
case fetch_range(ast) do
{:ok, range} -> Range.contains?(range, position)
:error -> false
end
Expand All @@ -56,7 +56,7 @@ defmodule Lexical.Ast.Detection.String do
# String sigils
defp do_detect({sigil, _, _} = ast, %Position{} = position)
when sigil in @string_sigils do
case fetch_range(ast, 0, 0) do
case fetch_range(ast) do
{:ok, range} -> Range.contains?(range, position)
_ -> false
end
Expand All @@ -75,7 +75,7 @@ defmodule Lexical.Ast.Detection.String do
|> Keyword.get(:delimiter, "\"")
|> String.length()

with {:ok, string_range} <- fetch_range(ast, delimiter_length, -1),
with {:ok, string_range} <- fetch_range(ast, delimiter_length, 0),
{:ok, interpolation_ranges} <- collect_interpolation_ranges(interpolations) do
Range.contains?(string_range, position) and
not Enum.any?(interpolation_ranges, &Range.contains?(&1, position))
Expand All @@ -92,7 +92,7 @@ defmodule Lexical.Ast.Detection.String do
{ast, :error}

{:"::", _, _} = interpolation, {:ok, acc} ->
case fetch_range(interpolation, 1, -1) do
case fetch_range(interpolation, 1, 0) do
{:ok, range} ->
{interpolation, {:ok, [range | acc]}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
def func(x), do: Enum.map(x, & &1 + 1)
end
/
assert [%Location{} = location] = references(project, "Enum.map|(a, b)", code)
assert [%Location{} = location] = references(project, "Enum.|map(a, b)", code)
assert decorate(code, location.range) =~ "def func(x), do: «Enum.map(x, & &1 + 1)»"
end

Expand All @@ -69,7 +69,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
defp func(x), do: Enum.map(x, & &1 + 1)
end
/
assert [%Location{} = location] = references(project, "Enum.map|(a, b)", code)
assert [%Location{} = location] = references(project, "Enum.|map(a, b)", code)
assert decorate(code, location.range) =~ "defp func(x), do: «Enum.map(x, & &1 + 1)»"
end

Expand All @@ -80,7 +80,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
defp func(x), do: E.map(x, & &1 + 1)
end
/
assert [%Location{} = location] = references(project, "Enum.map|(a, b)", code)
assert [%Location{} = location] = references(project, "Enum.|map(a, b)", code)
assert decorate(code, location.range) =~ "defp func(x), do: «E.map(x, & &1 + 1)»"
end

Expand All @@ -91,7 +91,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
defp func(x), do: map(x, & &1 + 1)
end
/
assert [%Location{} = location] = references(project, "Enum.map|(a, b)", code)
assert [%Location{} = location] = references(project, "Enum.|map(a, b)", code)
assert decorate(code, location.range) =~ "defp func(x), do: «map(x, & &1 + 1)»"
end

Expand All @@ -104,7 +104,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
end
/
assert [%Location{} = location] = references(project, "Functions.do_map|(a, b)", code)
assert [%Location{} = location] = references(project, "Functions.|do_map(a, b)", code)
assert decorate(code, location.range) =~ "def func(x), do: «do_map(x, & &1 + 1)»"
end
end
Expand All @@ -118,7 +118,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
end
]

assert [%Location{} = location] = references(project, "ReferencedModule|", code)
assert [%Location{} = location] = references(project, "|ReferencedModule", code)
assert decorate(code, location.range) =~ ~s[alias «ReferencedModule»]
end

Expand All @@ -129,7 +129,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
end
]

assert [%Location{} = location] = references(project, "ReferencedModule|", code)
assert [%Location{} = location] = references(project, "|ReferencedModule", code)
assert decorate(code, location.range) =~ ~s[@attr «ReferencedModule»]
end

Expand All @@ -138,7 +138,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
some_module = ReferencedModule
]

assert [%Location{} = location] = references(project, "ReferencedModule|", code)
assert [%Location{} = location] = references(project, "|ReferencedModule", code)
assert decorate(code, location.range) =~ ~s[some_module = «ReferencedModule»]
end

Expand All @@ -148,7 +148,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
end
]

assert [%Location{} = location] = references(project, "ReferencedModule|", code)
assert [%Location{} = location] = references(project, "|ReferencedModule", code)
assert decorate(code, location.range) =~ ~s[def some_fn(«ReferencedModule») do]
end

Expand All @@ -157,7 +157,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
%ReferencedModule{} = something_else
]

assert [%Location{} = location] = references(project, "ReferencedModule|", code)
assert [%Location{} = location] = references(project, "|ReferencedModule", code)
assert decorate(code, location.range) =~ ~s[%«ReferencedModule»{} = something_else]
end

Expand All @@ -171,7 +171,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
end
]

assert [location_1, location_2] = references(project, "DefinedModule|", code, true)
assert [location_1, location_2] = references(project, "|DefinedModule", code, true)
assert decorate(code, location_1.range) =~ ~s[defmodule «DefinedModule» do]
assert decorate(code, location_2.range) =~ ~s[@attr «DefinedModule»]
end
Expand All @@ -185,7 +185,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
end
)

assert [location] = references(project, "%Struct|{}", code, true)
assert [location] = references(project, "%|Struct{}", code, true)
assert decorate(code, location.range) =~ "«defstruct [:field]»"
end

Expand All @@ -198,22 +198,23 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
]

selector = ~q(
defmodule Struct do
defstruc|t [:name, :value]
end
defmodule Struct do
defstruc|t [:name, :value]
end
)

assert [location] = references(project, selector, code)
assert decorate(code, location.range) =~ "def something(«%Struct{}») do"
end

test "excludes their definition", %{project: project} do
code = ~q(
defmodule Struct do
defstruct [:field]
end
defmodule Struct do
defstruct [:field]
end
)

assert [] = references(project, "%Struct|{}", code)
assert [] = references(project, "%|Struct{}", code)
end
end

Expand Down Expand Up @@ -264,7 +265,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
test "are found in a function body", %{project: project} do
query = ~S[
def my_fun do
first| = 4
|first = 4
y = first * 2
z = y * 3 + first
end
Expand All @@ -282,7 +283,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.ReferencesTest do
query = ~S[
def my_fun do
first = 4
y = first| * 2
y = |first * 2
z = y * 3 + first
end
]
Expand Down
Loading

0 comments on commit c54bb63

Please sign in to comment.