Skip to content

Commit

Permalink
Rewrite position comparisons in terms of Position.compare/2
Browse files Browse the repository at this point in the history
  • Loading branch information
zachallaun committed Jun 2, 2024
1 parent 037f27f commit 8ab2622
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 56 deletions.
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
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
15 changes: 2 additions & 13 deletions projects/lexical_shared/lib/lexical/document/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,8 @@ defmodule Lexical.Document.Range do
def contains?(%__MODULE__{} = range, %Position{} = position) do
%__MODULE__{start: start_pos, end: end_pos} = range

cond do
position.line == start_pos.line and position.line == end_pos.line ->
position.character >= start_pos.character and position.character < end_pos.character

position.line == start_pos.line ->
position.character >= start_pos.character

position.line == end_pos.line ->
position.character < end_pos.character

true ->
position.line > start_pos.line and position.line < end_pos.line
end
Position.compare(position, start_pos) in [:gt, :eq] and
Position.compare(position, end_pos) == :lt
end
end

Expand Down

0 comments on commit 8ab2622

Please sign in to comment.