@@ -6,7 +6,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
6
6
alias ElixirLS.LanguageServer.Test.FixtureHelpers
7
7
8
8
@ tag :fixture
9
- test "Formats a file" do
9
+ test "Formats a file with LF line endings " do
10
10
in_fixture ( Path . join ( __DIR__ , ".." ) , "formatter" , fn ->
11
11
path = "lib/file.ex"
12
12
uri = SourceFile . path_to_uri ( path )
@@ -55,6 +55,176 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
55
55
end )
56
56
end
57
57
58
+ @ tag :fixture
59
+ test "Formats a file with CRLF line endings" do
60
+ in_fixture ( Path . join ( __DIR__ , ".." ) , "formatter" , fn ->
61
+ path = "lib/file.ex"
62
+ uri = SourceFile . path_to_uri ( path )
63
+
64
+ text = """
65
+ defmodule MyModule do
66
+ require Logger
67
+
68
+ def dummy_function() do
69
+ Logger.info "dummy"
70
+ end
71
+ end
72
+ """
73
+
74
+ text = text |> String . replace ( "\n " , "\r \n " )
75
+
76
+ source_file = % SourceFile {
77
+ text: text ,
78
+ version: 1 ,
79
+ dirty?: true
80
+ }
81
+
82
+ project_dir = maybe_convert_path_separators ( FixtureHelpers . get_path ( "formatter" ) )
83
+
84
+ assert { :ok , changes } = Formatting . format ( source_file , uri , project_dir )
85
+
86
+ assert changes == [
87
+ % {
88
+ "newText" => "\n " ,
89
+ "range" => % {
90
+ "end" => % { "character" => 0 , "line" => 7 } ,
91
+ "start" => % { "character" => 3 , "line" => 6 }
92
+ }
93
+ } ,
94
+ % {
95
+ "newText" => "\n " ,
96
+ "range" => % {
97
+ "end" => % { "character" => 0 , "line" => 6 } ,
98
+ "start" => % { "character" => 5 , "line" => 5 }
99
+ }
100
+ } ,
101
+ % {
102
+ "newText" => ")\n " ,
103
+ "range" => % {
104
+ "end" => % { "character" => 0 , "line" => 5 } ,
105
+ "start" => % { "character" => 23 , "line" => 4 }
106
+ }
107
+ } ,
108
+ % {
109
+ "newText" => "(" ,
110
+ "range" => % {
111
+ "end" => % { "character" => 16 , "line" => 4 } ,
112
+ "start" => % { "character" => 15 , "line" => 4 }
113
+ }
114
+ } ,
115
+ % {
116
+ "newText" => "\n " ,
117
+ "range" => % {
118
+ "end" => % { "character" => 0 , "line" => 4 } ,
119
+ "start" => % { "character" => 25 , "line" => 3 }
120
+ }
121
+ } ,
122
+ % {
123
+ "newText" => "\n \n " ,
124
+ "range" => % {
125
+ "end" => % { "character" => 0 , "line" => 3 } ,
126
+ "start" => % { "character" => 16 , "line" => 1 }
127
+ }
128
+ } ,
129
+ % {
130
+ "newText" => "\n " ,
131
+ "range" => % {
132
+ "end" => % { "character" => 0 , "line" => 1 } ,
133
+ "start" => % { "character" => 21 , "line" => 0 }
134
+ }
135
+ }
136
+ ]
137
+
138
+ assert Enum . all? ( changes , fn change ->
139
+ assert_position_type ( change [ "range" ] [ "end" ] ) and
140
+ assert_position_type ( change [ "range" ] [ "start" ] )
141
+ end )
142
+ end )
143
+ end
144
+
145
+ @ tag :fixture
146
+ test "elixir formatter does not support CR line endings" do
147
+ in_fixture ( Path . join ( __DIR__ , ".." ) , "formatter" , fn ->
148
+ path = "lib/file.ex"
149
+ uri = SourceFile . path_to_uri ( path )
150
+
151
+ text = """
152
+ defmodule MyModule do
153
+ require Logger
154
+
155
+ def dummy_function() do
156
+ Logger.info "dummy"
157
+ end
158
+ end
159
+ """
160
+
161
+ text = text |> String . replace ( "\n " , "\r " )
162
+
163
+ source_file = % SourceFile {
164
+ text: text ,
165
+ version: 1 ,
166
+ dirty?: true
167
+ }
168
+
169
+ project_dir = maybe_convert_path_separators ( FixtureHelpers . get_path ( "formatter" ) )
170
+
171
+ assert { :error , :internal_error , msg } = Formatting . format ( source_file , uri , project_dir )
172
+ assert String . contains? ( msg , "Unable to format" )
173
+ end )
174
+ end
175
+
176
+ @ tag :fixture
177
+ test "formatting preserves line indings inside a string" do
178
+ in_fixture ( Path . join ( __DIR__ , ".." ) , "formatter" , fn ->
179
+ path = "lib/file.ex"
180
+ uri = SourceFile . path_to_uri ( path )
181
+
182
+ text = """
183
+ defmodule MyModule do
184
+ require Logger
185
+
186
+ def dummy_function() do
187
+ Logger.info "dummy"
188
+ end
189
+ end
190
+ """
191
+
192
+ text = text |> String . replace ( "\" dummy\" " , " \" du\n m\r m\r \n y\" " )
193
+
194
+ source_file = % SourceFile {
195
+ text: text ,
196
+ version: 1 ,
197
+ dirty?: true
198
+ }
199
+
200
+ project_dir = maybe_convert_path_separators ( FixtureHelpers . get_path ( "formatter" ) )
201
+
202
+ assert { :ok , changes } = Formatting . format ( source_file , uri , project_dir )
203
+
204
+ assert changes == [
205
+ % {
206
+ "newText" => ")" ,
207
+ "range" => % {
208
+ "end" => % { "character" => 2 , "line" => 7 } ,
209
+ "start" => % { "character" => 2 , "line" => 7 }
210
+ }
211
+ } ,
212
+ % {
213
+ "newText" => "(" ,
214
+ "range" => % {
215
+ "end" => % { "character" => 20 , "line" => 4 } ,
216
+ "start" => % { "character" => 15 , "line" => 4 }
217
+ }
218
+ }
219
+ ]
220
+
221
+ assert Enum . all? ( changes , fn change ->
222
+ assert_position_type ( change [ "range" ] [ "end" ] ) and
223
+ assert_position_type ( change [ "range" ] [ "start" ] )
224
+ end )
225
+ end )
226
+ end
227
+
58
228
defp assert_position_type ( % { "character" => ch , "line" => line } ) ,
59
229
do: is_integer ( ch ) and is_integer ( line )
60
230
0 commit comments