Skip to content

Commit 76467cd

Browse files
authored
Fix Formatting.format returns character float number (#250)
This bug happens because of use `/ 2` in advance_pos which is convert into floating number. Fixes by using `trunc/1`. And add test to ensure line and character always integer. Fixes #249
1 parent 5bf39b8 commit 76467cd

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

apps/language_server/lib/language_server/providers/formatting.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
8181
else
8282
# LSP contentChanges positions are based on UTF-16 string representation
8383
# https://microsoft.github.io/language-server-protocol/specification#textDocuments
84-
{line, col + byte_size(:unicode.characters_to_binary(char, :utf8, :utf16)) / 2}
84+
{line, col + div(byte_size(:unicode.characters_to_binary(char, :utf8, :utf16)), 2)}
8585
end
8686
end)
8787
end

apps/language_server/test/providers/formatting_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
4141
}
4242
}
4343
]
44+
45+
assert Enum.all?(changes, fn change ->
46+
assert_position_type(change["range"]["end"]) and
47+
assert_position_type(change["range"]["start"])
48+
end)
4449
end
4550

51+
defp assert_position_type(%{"character" => ch, "line" => line}),
52+
do: is_integer(ch) and is_integer(line)
53+
4654
test "returns an error when formatting a file with a syntax error" do
4755
uri = "file://project/file.ex"
4856

0 commit comments

Comments
 (0)