@@ -3,12 +3,14 @@ defmodule ElixirLS.LanguageServer.SourceFile do
3
3
4
4
defstruct [ :text , :version , dirty?: false ]
5
5
6
+ @ endings [ "\r \n " , "\r " , "\n " ]
7
+
6
8
def lines ( % __MODULE__ { text: text } ) do
7
9
lines ( text )
8
10
end
9
11
10
12
def lines ( text ) when is_binary ( text ) do
11
- String . split ( text , [ " \r \n " , " \r " , " \n " ] )
13
+ String . split ( text , @ endings )
12
14
end
13
15
14
16
@ doc """
@@ -20,17 +22,18 @@ defmodule ElixirLS.LanguageServer.SourceFile do
20
22
do_lines_with_endings ( text , "" )
21
23
end
22
24
23
- def do_lines_with_endings ( "" , line ) do
25
+ def do_lines_with_endings ( "" , line ) when is_binary ( line ) do
24
26
[ { line , nil } ]
25
27
end
26
28
27
- for line_ending <- [ "\r \n " , "\r " , "\n " ] do
28
- def do_lines_with_endings ( << unquote ( line_ending ) , rest :: binary >> , line ) do
29
+ for line_ending <- @ endings do
30
+ def do_lines_with_endings ( << unquote ( line_ending ) , rest :: binary >> , line )
31
+ when is_binary ( line ) do
29
32
[ { line , unquote ( line_ending ) } | do_lines_with_endings ( rest , "" ) ]
30
33
end
31
34
end
32
35
33
- def do_lines_with_endings ( << char :: utf8 , rest :: binary >> , line ) do
36
+ def do_lines_with_endings ( << char :: utf8 , rest :: binary >> , line ) when is_binary ( line ) do
34
37
do_lines_with_endings ( rest , line <> << char :: utf8 >> )
35
38
end
36
39
@@ -177,13 +180,15 @@ defmodule ElixirLS.LanguageServer.SourceFile do
177
180
178
181
def line_length_utf16 ( line ) do
179
182
line
180
- |> :unicode . characters_to_binary ( :utf8 , :utf16 )
183
+ |> characters_to_binary! ( :utf8 , :utf16 )
181
184
|> byte_size ( )
182
185
|> div ( 2 )
183
186
end
184
187
185
- defp prepend_line ( line , nil , acc ) , do: [ line | acc ]
186
- defp prepend_line ( line , ending , acc ) , do: [ [ line , ending ] | acc ]
188
+ defp prepend_line ( line , nil , acc ) when is_binary ( line ) , do: [ line | acc ]
189
+
190
+ defp prepend_line ( line , ending , acc ) when is_binary ( line ) and ending in @ endings ,
191
+ do: [ [ line , ending ] | acc ]
187
192
188
193
def apply_edit ( text , range ( start_line , start_character , end_line , end_character ) , new_text ) do
189
194
lines_with_idx =
@@ -201,13 +206,13 @@ defmodule ElixirLS.LanguageServer.SourceFile do
201
206
# LSP contentChanges positions are based on UTF-16 string representation
202
207
# https://microsoft.github.io/language-server-protocol/specification#textDocuments
203
208
beginning_utf8 =
204
- :unicode . characters_to_binary ( line , :utf8 , :utf16 )
209
+ characters_to_binary! ( line , :utf8 , :utf16 )
205
210
|> ( & binary_part (
206
211
& 1 ,
207
212
0 ,
208
213
min ( start_character * 2 , byte_size ( & 1 ) )
209
214
) ) . ( )
210
- |> :unicode . characters_to_binary ( :utf16 , :utf8 )
215
+ |> characters_to_binary! ( :utf16 , :utf8 )
211
216
212
217
[ beginning_utf8 | acc ]
213
218
@@ -228,13 +233,13 @@ defmodule ElixirLS.LanguageServer.SourceFile do
228
233
# LSP contentChanges positions are based on UTF-16 string representation
229
234
# https://microsoft.github.io/language-server-protocol/specification#textDocuments
230
235
ending_utf8 =
231
- :unicode . characters_to_binary ( line , :utf8 , :utf16 )
236
+ characters_to_binary! ( line , :utf8 , :utf16 )
232
237
|> ( & binary_part (
233
238
& 1 ,
234
239
min ( end_character * 2 , byte_size ( & 1 ) ) ,
235
240
max ( byte_size ( & 1 ) - end_character * 2 , 0 )
236
241
) ) . ( )
237
- |> :unicode . characters_to_binary ( :utf16 , :utf8 )
242
+ |> characters_to_binary! ( :utf16 , :utf8 )
238
243
239
244
prepend_line ( ending_utf8 , ending , acc )
240
245
@@ -246,6 +251,12 @@ defmodule ElixirLS.LanguageServer.SourceFile do
246
251
IO . iodata_to_binary ( Enum . reverse ( acc ) )
247
252
end
248
253
254
+ defp characters_to_binary! ( binary , from , to ) do
255
+ case :unicode . characters_to_binary ( binary , from , to ) do
256
+ result when is_binary ( result ) -> result
257
+ end
258
+ end
259
+
249
260
def module_line ( module ) do
250
261
# TODO: Don't call into here directly
251
262
case ElixirSense.Core.Normalized.Code . get_docs ( module , :moduledoc ) do
0 commit comments