Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Position.compare/2 #767

Merged
merged 3 commits into from
Jun 25, 2024
Merged
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
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
28 changes: 28 additions & 0 deletions projects/lexical_shared/lib/lexical/document/position.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ defmodule Lexical.Document.Position do
}
end
end

@doc """
Compares two positions.

Returns `:gt`, `:lt`, or `:eq` depending on the location of the first
position relative to the second.
"""
@spec compare(t | {line, character}, t | {line, character}) :: :lt | :eq | :gt
def compare(%__MODULE__{} = pos1, %__MODULE__{} = pos2) do
compare({pos1.line, pos1.character}, {pos2.line, pos2.character})
end

def compare(%__MODULE__{} = pos1, {_, _} = pos2) do
compare({pos1.line, pos1.character}, pos2)
end

def compare({_, _} = pos1, %__MODULE__{} = pos2) do
compare(pos1, {pos2.line, pos2.character})
end

def compare({l1, c1} = first, {l2, c2} = second)
when is_integer(l1) and is_integer(c1) and is_integer(l2) and is_integer(c2) do
cond do
first < second -> :lt
first > second -> :gt
true -> :eq
end
end
end

defimpl Inspect, for: Lexical.Document.Position do
Expand Down
45 changes: 45 additions & 0 deletions projects/lexical_shared/test/lexical/document/position_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule Lexical.Document.PositionTest do
alias Lexical.Document.Line
alias Lexical.Document.Lines
alias Lexical.Document.Position

import Line

use ExUnit.Case, async: true

describe "compare/2" do
test "positions on the same line" do
assert :eq = Position.compare(position(1, 10), position(1, 10))
assert :gt = Position.compare(position(1, 11), position(1, 10))
assert :lt = Position.compare(position(1, 9), position(1, 10))
end

test "position on earlier line" do
assert :lt = Position.compare(position(1, 10), position(2, 10))
assert :lt = Position.compare(position(1, 11), position(2, 10))
assert :lt = Position.compare(position(1, 9), position(2, 10))
end

test "position on later line" do
assert :gt = Position.compare(position(2, 10), position(1, 10))
assert :gt = Position.compare(position(2, 11), position(1, 10))
assert :gt = Position.compare(position(2, 9), position(1, 10))
end
end

defp position(line, character) do
stub_line = line(text: "", ending: "\n", line_number: line, ascii?: true)

lines =
line
|> empty_lines()
|> put_in([Access.key(:lines), Access.elem(line - 1)], stub_line)

Position.new(lines, line, character)
end

defp empty_lines(length) do
tuple = List.to_tuple(for(x <- 1..length, do: x))
%Lines{lines: tuple, starting_index: 1}
end
end