Skip to content

Commit

Permalink
Removed context document from to_lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
scohen committed Sep 12, 2023
1 parent 0daf9f9 commit bbad0d1
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 103 deletions.
7 changes: 2 additions & 5 deletions apps/proto/lib/lexical/proto/convert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ defmodule Lexical.Proto.Convert do
alias Lexical.Document

def to_lsp(%_{result: result} = response) do
context_document = Document.Container.context_document(result, nil)

case Convertible.to_lsp(result, context_document) do
case Convertible.to_lsp(result) do
{:ok, converted} ->
{:ok, Map.put(response, :result, converted)}

Expand All @@ -15,8 +13,7 @@ defmodule Lexical.Proto.Convert do
end

def to_lsp(other) do
context_document = Document.Container.context_document(other, nil)
Convertible.to_lsp(other, context_document)
Convertible.to_lsp(other)
end

def to_native(%{lsp: request_or_notification} = original_request) do
Expand Down
39 changes: 18 additions & 21 deletions apps/protocol/lib/lexical/protocol/conversions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,27 @@ defmodule Lexical.Protocol.Conversions do
{:ok, range}
end

def to_lsp(%ElixirRange{} = ex_range, %Document{} = doc) do
with {:ok, start_pos} <- to_lsp(ex_range.start, doc.lines),
{:ok, end_pos} <- to_lsp(ex_range.end, doc.lines) do
{:ok, %LSRange{start: start_pos, end: end_pos}}
end
end

def to_lsp(%LSRange{start: %LSPosition{}, end: %LSPosition{}} = ls_range, _) do
def to_lsp(%LSRange{start: %LSPosition{}, end: %LSPosition{}} = ls_range) do
{:ok, ls_range}
end

def to_lsp(%LSRange{} = ls_range, %Document{} = document) do
with {:ok, start_pos} <- to_lsp(ls_range.start, document),
{:ok, end_pos} <- to_lsp(ls_range.end, document) do
def to_lsp(%LSRange{} = ls_range) do
with {:ok, start_pos} <- to_lsp(ls_range.start),
{:ok, end_pos} <- to_lsp(ls_range.end) do
{:ok, LSRange.new(start: start_pos, end: end_pos)}
end
end

def to_lsp(%ElixirPosition{} = position, %Document{} = document) do
to_lsp(position, document.lines)
def to_lsp(%ElixirRange{} = ex_range) do
with {:ok, start_pos} <- to_lsp(ex_range.start),
{:ok, end_pos} <- to_lsp(ex_range.end) do
{:ok, %LSRange{start: start_pos, end: end_pos}}
end
end

def to_lsp(%ElixirPosition{} = position, %Lines{} = lines) do
def to_lsp(%ElixirPosition{} = position) do
elixir_character = position.character
line_count = Lines.size(lines)
line_count = position.document_line_count
document_line_number = Math.clamp(position.line, 1, line_count)

cond do
Expand All @@ -121,31 +117,32 @@ defmodule Lexical.Protocol.Conversions do
{:ok, LSPosition.new(line: line_count, character: 0)}

true ->
with {:ok, line} <- Lines.fetch_line(lines, position.line),
{:ok, lsp_character} <- extract_lsp_character(position, line) do
with {:ok, lsp_character} <- extract_lsp_character(position) do
ls_pos =
LSPosition.new(
character: lsp_character,
line: position.line - lines.starting_index
line: position.line - position.starting_index
)

{:ok, ls_pos}
end
end
end

def to_lsp(%LSPosition{} = position, _) do
def to_lsp(%LSPosition{} = position) do
{:ok, position}
end

# Private

defp extract_lsp_character(%ElixirPosition{} = position, line(ascii?: true, text: text)) do
defp extract_lsp_character(
%ElixirPosition{context_line: line(ascii?: true, text: text)} = position
) do
character = min(position.character - 1, byte_size(text))
{:ok, character}
end

defp extract_lsp_character(%ElixirPosition{} = position, line(text: utf8_text)) do
defp extract_lsp_character(%ElixirPosition{context_line: line(text: utf8_text)} = position) do
with {:ok, code_unit} <- CodeUnit.to_utf16(utf8_text, position.character - 1) do
character = min(code_unit, CodeUnit.count(:utf16, utf8_text))
{:ok, character}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
defimpl Lexical.Convertible, for: Lexical.Document.Changes do
alias Lexical.Document

def to_lsp(%Document.Changes{} = document_edits, context_document) do
context_document = Document.Container.context_document(document_edits, context_document)
Lexical.Convertible.to_lsp(document_edits.edits, context_document)
def to_lsp(%Document.Changes{} = document_edits) do
Lexical.Convertible.to_lsp(document_edits.edits)
end

def to_native(%Document.Changes{} = document_edits, _) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ defimpl Lexical.Convertible, for: Lexical.Document.Edit do
alias Lexical.Protocol.Conversions
alias Lexical.Protocol.Types

def to_lsp(%Document.Edit{range: nil} = edit, _context_document) do
def to_lsp(%Document.Edit{range: nil} = edit) do
{:ok, Types.TextEdit.new(new_text: edit.text, range: nil)}
end

def to_lsp(%Document.Edit{} = edit, context_document) do
with {:ok, range} <- Conversions.to_lsp(edit.range, context_document) do
def to_lsp(%Document.Edit{} = edit) do
with {:ok, range} <- Conversions.to_lsp(edit.range) do
{:ok, Types.TextEdit.new(new_text: edit.text, range: range)}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defimpl Lexical.Convertible, for: Lexical.Document.Location do
alias Lexical.Protocol.Conversions
alias Lexical.Protocol.Types

def to_lsp(%Document.Location{} = location, %Document{} = context_document) do
with {:ok, range} <- Conversions.to_lsp(location.range, context_document) do
def to_lsp(%Document.Location{} = location) do
with {:ok, range} <- Conversions.to_lsp(location.range) do
uri = Document.Location.uri(location)
{:ok, Types.Location.new(uri: uri, range: range)}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defimpl Lexical.Convertible, for: Lexical.Document.Position do
alias Lexical.Document
alias Lexical.Protocol.Conversions

def to_lsp(%Document.Position{} = position, context_document) do
Conversions.to_lsp(position, context_document)
def to_lsp(%Document.Position{} = position) do
Conversions.to_lsp(position)
end

def to_native(%Document.Position{} = position, _context_document) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defimpl Lexical.Convertible, for: Lexical.Document.Range do
alias Lexical.Document
alias Lexical.Protocol.Conversions

def to_lsp(%Document.Range{} = position, context_document) do
Conversions.to_lsp(position, context_document)
def to_lsp(%Document.Range{} = position) do
Conversions.to_lsp(position)
end

def to_native(%Document.Range{} = position, _context_document) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defimpl Lexical.Convertible, for: Lexical.Protocol.Types.Location do
alias Lexical.Protocol.Conversions
alias Lexical.Protocol.Types

def to_lsp(%Types.Location{} = location, context_document) do
with {:ok, range} <- Conversions.to_lsp(location.range, context_document) do
def to_lsp(%Types.Location{} = location) do
with {:ok, range} <- Conversions.to_lsp(location.range) do
{:ok, %Types.Location{location | range: range}}
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defimpl Lexical.Convertible, for: Lexical.Protocol.Types.Position do
alias Lexical.Protocol.Conversions
alias Lexical.Protocol.Types

def to_lsp(%Types.Position{} = position, context_document) do
Conversions.to_lsp(position, context_document)
def to_lsp(%Types.Position{} = position) do
Conversions.to_lsp(position)
end

def to_native(%Types.Position{} = position, context_document) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defimpl Lexical.Convertible, for: Lexical.Protocol.Types.Range do
alias Lexical.Protocol.Conversions
alias Lexical.Protocol.Types

def to_lsp(%Types.Range{} = range, context_document) do
Conversions.to_lsp(range, context_document)
def to_lsp(%Types.Range{} = range) do
Conversions.to_lsp(range)
end

def to_native(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ alias Lexical.Document.Edit
alias Lexical.Protocol.Types.TextDocument.ContentChangeEvent

defimpl Convertible, for: ContentChangeEvent.TextDocumentContentChangeEvent1 do
def to_lsp(event, _context_document) do
def to_lsp(event) do
{:ok, event}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defimpl Lexical.Convertible, for: Lexical.Protocol.Types.TextEdit do
alias Lexical.Protocol.Conversions
alias Lexical.Protocol.Types

def to_lsp(%Types.TextEdit{} = text_edit, context_document) do
with {:ok, range} <- Conversions.to_lsp(text_edit.range, context_document) do
def to_lsp(%Types.TextEdit{} = text_edit) do
with {:ok, range} <- Conversions.to_lsp(text_edit.range) do
{:ok, %Types.TextEdit{text_edit | range: range}}
end
end
Expand Down
10 changes: 5 additions & 5 deletions apps/protocol/test/lexical/protocol/conversions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,31 @@ defmodule Lexical.Protocol.ConversionsTest do
describe "to_lsp/2 for positions" do
test "empty" do
doc = doc("")
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 1), doc)
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 1))
assert %LSPosition{line: 0, character: 0} == pos
end

test "single line first char" do
doc = doc("abcde")
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 1), doc)
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 1))
assert %LSPosition{line: 0, character: 0} == pos
end

test "single line" do
doc = doc("abcde")
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 2), doc)
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 2))
assert %LSPosition{line: 0, character: 1} == pos
end

test "single line utf8" do
doc = doc("🏳️‍🌈abcde")
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 15), doc)
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 1, 15))
assert %LSPosition{character: 6, line: 0} == pos
end

test "multi line" do
doc = doc("abcde\n1234")
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 2, 2), doc)
assert {:ok, pos} = Conversions.to_lsp(ex_position(doc, 2, 2))
assert %LSPosition{character: 1, line: 1} == pos
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ defmodule Lexical.Test.Protocol.ConvertibleSupport do
defoverridable open_file_contents: 0

def to_lsp(convertible, uri) do
{:ok, doc} = Document.Store.fetch(uri)
Convertible.to_lsp(convertible, doc)
Convertible.to_lsp(convertible)
end

def to_native(converible, uri) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ defimpl Lexical.Convertible, for: Lexical.Plugin.V1.Diagnostic.Result do
alias Lexical.Protocol.Types
alias Lexical.Text

def to_lsp(%Diagnostic.Result{} = diagnostic, _context_document) do
with {:ok, document} <- Document.Store.open_temporary(diagnostic.uri),
{:ok, lsp_range} <- position_to_range(document, diagnostic.position) do
def to_lsp(%Diagnostic.Result{} = diagnostic) do
with {:ok, lsp_range} <- lsp_range(diagnostic) do
proto_diagnostic = %Types.Diagnostic{
message: diagnostic.message,
range: lsp_range,
Expand All @@ -26,6 +25,28 @@ defimpl Lexical.Convertible, for: Lexical.Plugin.V1.Diagnostic.Result do
{:ok, diagnostic}
end

defp lsp_range(%Diagnostic.Result{position: %Position{} = position}) do
with {:ok, lsp_start_pos} <- Conversions.to_lsp(position) do
range =
Types.Range.new(
start: lsp_start_pos,
end: Types.Position.new(line: lsp_start_pos.line + 1, character: 0)
)

{:ok, range}
end
end

defp lsp_range(%Diagnostic.Result{position: %Range{} = range}) do
Conversions.to_lsp(range)
end

defp lsp_range(%Diagnostic.Result{} = diagnostic) do
with {:ok, document} <- Document.Store.open_temporary(diagnostic.uri) do
position_to_range(document, diagnostic.position)
end
end

defp position_to_range(%Document{} = document, {start_line, start_column, end_line, end_column}) do
with {:ok, start_pos} <- position_to_range(document, {start_line, start_column}),
{:ok, end_pos} <- position_to_range(document, {end_line, end_column}) do
Expand All @@ -39,7 +60,7 @@ defimpl Lexical.Convertible, for: Lexical.Plugin.V1.Diagnostic.Result do

document
|> to_lexical_range(line_number, column)
|> Conversions.to_lsp(document)
|> Conversions.to_lsp()
end

defp position_to_range(document, line_number) when is_integer(line_number) do
Expand All @@ -50,23 +71,7 @@ defimpl Lexical.Convertible, for: Lexical.Plugin.V1.Diagnostic.Result do

document
|> to_lexical_range(line_number, column)
|> Conversions.to_lsp(document)
end
end

defp position_to_range(document, %Document.Range{} = range) do
Conversions.to_lsp(range, document)
end

defp position_to_range(document, %Document.Position{} = position) do
with {:ok, lsp_start_pos} <- Conversions.to_lsp(position, document) do
range =
Types.Range.new(
start: lsp_start_pos,
end: Types.Position.new(line: lsp_start_pos.line + 1, character: 0)
)

{:ok, range}
|> Conversions.to_lsp()
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Lexical.Convertibles.Lexical.Plugin.V1.Diagnostic.ResultTest do
def fun_two do
"🎸hello"
end
end
endpl
]t
end

Expand Down
Loading

0 comments on commit bbad0d1

Please sign in to comment.