Skip to content

Commit

Permalink
Fix Diagnostic.Result to_lsp with 4-elem tuple position (#502)
Browse files Browse the repository at this point in the history
* fix diagnostic result to_lsp w/ 4-elem position

Signed-off-by: Donghyun <[email protected]>

* chore: remove a line that might be redundant

Signed-off-by: Donghyun <[email protected]>

* remove test tag

Signed-off-by: Donghyun <[email protected]>

---------

Signed-off-by: Donghyun <[email protected]>
  • Loading branch information
bangalcat authored Nov 23, 2023
1 parent 9395c85 commit 7ee08bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ defimpl Lexical.Convertible, for: Lexical.Plugin.V1.Diagnostic.Result do
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
{:ok, Types.Range.new(start: start_pos, end: end_pos)}
end
start_pos = Position.new(document, start_line, max(start_column, 1))
end_pos = Position.new(document, end_line, max(end_column, 1))

range = Range.new(start_pos, end_pos)
Conversions.to_lsp(range)
end

defp position_to_range(%Document{} = document, {line_number, column}) do
line_number = Math.clamp(line_number, 1, Document.size(document))
column = max(column, 1)

document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ defmodule Lexical.Convertibles.Lexical.Plugin.V1.Diagnostic.ResultTest do
assert converted.range == range(:lsp, position(:lsp, 0, 0), position(:lsp, 1, 0))
end

test "it should translate a diagnostic with a four-elements tuple position", %{uri: uri} do
assert {:ok, %Types.Diagnostic{} = converted} =
to_lsp(plugin_diagnostic(uri, {2, 5, 2, 8}), uri)

assert converted.message == "Broken!"
assert converted.range == range(:lsp, position(:lsp, 1, 4), position(:lsp, 1, 7))

assert {:ok, %Types.Diagnostic{} = converted} =
to_lsp(plugin_diagnostic(uri, {1, 0, 3, 0}), uri)

assert converted.message == "Broken!"
assert converted.range == range(:lsp, position(:lsp, 0, 0), position(:lsp, 2, 0))
end

test "it should translate a diagnostic line that is out of bounds (elixir can do this)", %{
uri: uri
} do
Expand Down

1 comment on commit 7ee08bc

@scohen
Copy link
Collaborator

@scohen scohen commented on 7ee08bc Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix and unit test!

Please sign in to comment.