Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 9 additions & 33 deletions apps/common/lib/lexical/ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,21 +317,9 @@ defmodule Lexical.Ast do
def contains_position?(ast, %Position{} = position) do
case Sourceror.get_range(ast) do
%{start: start_pos, end: end_pos} ->
on_same_line? = start_pos[:line] == end_pos[:line] and position.line == start_pos[:line]

cond do
on_same_line? ->
position.character >= start_pos[:column] and position.character <= end_pos[:column]

position.line == start_pos[:line] ->
position.character >= start_pos[:column]

position.line == end_pos[:line] ->
position.character <= end_pos[:column]

true ->
position.line > start_pos[:line] and position.line < end_pos[:line]
end
start_pos = {start_pos[:line], start_pos[:column]}
end_pos = {end_pos[:line], end_pos[:column]}
within?(position, start_pos, end_pos)

nil ->
false
Expand Down Expand Up @@ -514,7 +502,7 @@ defmodule Lexical.Ast do
fn %Zipper{node: node} = zipper, {last_position, acc} ->
current_position = node_position(node, last_position)

if within_range?(current_position, range) do
if within?(current_position, range.start, range.end) do
{zipper, new_acc} = fun.(zipper, acc)

{:cont, zipper, {current_position, new_acc}}
Expand All @@ -528,27 +516,15 @@ defmodule Lexical.Ast do
end
end

defp within_range?({current_line, current_column}, %Range{} = range) do
start_pos = %Position{} = range.start
end_pos = %Position{} = range.end

cond do
current_line == start_pos.line ->
current_column >= start_pos.character

current_line == end_pos.line ->
current_column <= end_pos.character

true ->
current_line >= start_pos.line and current_line <= end_pos.line
end
defp within?(pos, start_pos, end_pos) do
Position.compare(pos, start_pos) in [:gt, :eq] and
Position.compare(pos, end_pos) in [:lt, :eq]
end

defp at_or_after?(node, %Position{} = position) do
line = get_line(node, 0)
column = get_column(node, 0)
node_position = node_position(node, {0, 0})

line > position.line or (line == position.line and column >= position.character)
Position.compare(node_position, position) in [:gt, :eq]
end

defp one_line_range(%Document{} = document, line_number) do
Expand Down
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 @@ -82,8 +82,6 @@ defmodule Lexical.RemoteControl.CodeIntelligence.Variable do

definition_children = Map.get(block_id_to_children, definition_entry.block_id, [])

definition_start = definition_entry.range.start

# The algorithm here is to first clean up the entries so they either are definitions or references to a
# variable with the given name. We sort them by their occurrence in the file, working backwards on a line, so
# definitions earlier in the line shadow definitions later in the line.
Expand All @@ -107,14 +105,7 @@ defmodule Lexical.RemoteControl.CodeIntelligence.Variable do
{entries, _, _} =
entries
|> Enum.filter(fn %Entry{} = entry ->
entry_start = entry.range.start

after_definition? =
if entry_start.line == definition_start.line do
entry_start.character > definition_entry.range.end.character
else
entry_start.line > definition_start.line
end
after_definition? = Position.compare(entry.range.start, definition_entry.range.end) == :gt

variable_type? = entry.type == :variable
correct_subject? = entry.subject == definition_entry.subject
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