Skip to content
Draft
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
18 changes: 15 additions & 3 deletions apps/expert/lib/expert/protocol/conversions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@ defmodule Expert.Protocol.Conversions do
end
end

def to_elixir(%LSPosition{} = position, %Document{} = document) do
to_elixir(position, document.lines)
def to_elixir(%LSRange{}, nil) do
{:error, {:invalid_document, nil}}
end

def to_elixir(%ElixirPosition{} = position, _) do
{:ok, position}
end

def to_elixir(%LSPosition{} = position, %Document{} = document) do
to_elixir(position, document.lines)
end

def to_elixir(%LSPosition{line: line} = position, _) when line < 0 do
{:error, {:invalid_position, position}}
end

def to_elixir(%LSPosition{}, nil) do
{:error, {:invalid_document, nil}}
end

def to_elixir(%LSPosition{} = position, %Lines{} = lines) do
line_count = Lines.size(lines)
# we need to handle out of bounds line numbers, because it's possible to build a document
Expand Down Expand Up @@ -68,7 +76,7 @@ defmodule Expert.Protocol.Conversions do
end
end

def to_elixir(%{range: %{start: start_pos, end: end_pos}}, document) do
def to_elixir(%{range: %{start: start_pos, end: end_pos}}, %Document{} = document) do
# this is actually an elixir sense range... note that it's a bare map with
# column keys rather than character keys.
%{line: start_line, column: start_col} = start_pos
Expand All @@ -83,6 +91,10 @@ defmodule Expert.Protocol.Conversions do
{:ok, range}
end

def to_elixir(%{range: %{start: _start_pos, end: _end_pos}}, nil) do
{:error, {:invalid_document, nil}}
end

def to_lsp(%LSRange{start: %LSPosition{}, end: %LSPosition{}} = ls_range) do
{:ok, ls_range}
end
Expand Down
12 changes: 12 additions & 0 deletions apps/expert/test/conversions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ defmodule Expert.Protocol.ConversionsTest do
assert {:ok, pos} = Conversions.to_elixir(lsp_position(8, 2), doc("abcde\n1234"))
assert %ExPosition{line: 3, character: 1} = pos
end

test "document is nil" do
assert Conversions.to_elixir(
%GenLSP.Structures.Range{end: lsp_position(0, 0), start: lsp_position(0, 0)},
nil
) == {:error, {:invalid_document, nil}}

assert Conversions.to_elixir(lsp_position(0, 0), nil) == {:error, {:invalid_document, nil}}

assert Conversions.to_elixir(%{range: %{start: 0, end: 0}}, nil) ==
{:error, {:invalid_document, nil}}
end
end

describe "to_lsp/2 for positions" do
Expand Down
Loading