Skip to content

Commit a244c94

Browse files
authored
Use TextEdit struct instead of map string key (#650)
Change the text `myers_diff_to_text_edits` in formatting provider to use `TextEdit` struct instead of map string key to be the same as `ManipulatePipes` does.
1 parent 1fd0fc3 commit a244c94

File tree

2 files changed

+78
-76
lines changed

2 files changed

+78
-76
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule ElixirLS.LanguageServer.Providers.Formatting do
22
import ElixirLS.LanguageServer.Protocol, only: [range: 4]
3+
alias ElixirLS.LanguageServer.Protocol.TextEdit
34
alias ElixirLS.LanguageServer.SourceFile
45

56
def format(%SourceFile{} = source_file, uri, project_dir) when is_binary(project_dir) do
@@ -97,17 +98,17 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
9798
myers_diff_to_text_edits(rest, advance_pos({line, col}, str), edits)
9899

99100
{{:ins, str}, _} ->
100-
edit = %{"range" => range(line, col, line, col), "newText" => str}
101+
edit = %TextEdit{range: range(line, col, line, col), newText: str}
101102
myers_diff_to_text_edits(rest, {line, col}, [edit | edits])
102103

103104
{{:del, del_str}, [{:ins, ins_str} | rest]} ->
104105
{end_line, end_col} = advance_pos({line, col}, del_str)
105-
edit = %{"range" => range(line, col, end_line, end_col), "newText" => ins_str}
106+
edit = %TextEdit{range: range(line, col, end_line, end_col), newText: ins_str}
106107
myers_diff_to_text_edits(rest, {end_line, end_col}, [edit | edits])
107108

108109
{{:del, str}, _} ->
109110
{end_line, end_col} = advance_pos({line, col}, str)
110-
edit = %{"range" => range(line, col, end_line, end_col), "newText" => ""}
111+
edit = %TextEdit{range: range(line, col, end_line, end_col), newText: ""}
111112
myers_diff_to_text_edits(rest, {end_line, end_col}, [edit | edits])
112113
end
113114
end

apps/language_server/test/providers/formatting_test.exs

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
22
use ElixirLS.Utils.MixTest.Case, async: false
33
import ElixirLS.LanguageServer.Test.PlatformTestHelpers
44
alias ElixirLS.LanguageServer.Providers.Formatting
5+
alias ElixirLS.LanguageServer.Protocol.TextEdit
56
alias ElixirLS.LanguageServer.SourceFile
67
alias ElixirLS.LanguageServer.Test.FixtureHelpers
78

@@ -32,25 +33,25 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
3233
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
3334

3435
assert changes == [
35-
%{
36-
"newText" => ")",
37-
"range" => %{
36+
%TextEdit{
37+
newText: ")",
38+
range: %{
3839
"end" => %{"character" => 23, "line" => 4},
3940
"start" => %{"character" => 23, "line" => 4}
4041
}
4142
},
42-
%{
43-
"newText" => "(",
44-
"range" => %{
43+
%TextEdit{
44+
newText: "(",
45+
range: %{
4546
"end" => %{"character" => 16, "line" => 4},
4647
"start" => %{"character" => 15, "line" => 4}
4748
}
4849
}
4950
]
5051

5152
assert Enum.all?(changes, fn change ->
52-
assert_position_type(change["range"]["end"]) and
53-
assert_position_type(change["range"]["start"])
53+
assert_position_type(change.range["end"]) and
54+
assert_position_type(change.range["start"])
5455
end)
5556
end)
5657
end
@@ -82,25 +83,25 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
8283
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
8384

8485
assert changes == [
85-
%{
86-
"newText" => ")",
87-
"range" => %{
86+
%TextEdit{
87+
newText: ")",
88+
range: %{
8889
"end" => %{"character" => 23, "line" => 4},
8990
"start" => %{"character" => 23, "line" => 4}
9091
}
9192
},
92-
%{
93-
"newText" => "(",
94-
"range" => %{
93+
%TextEdit{
94+
newText: "(",
95+
range: %{
9596
"end" => %{"character" => 16, "line" => 4},
9697
"start" => %{"character" => 15, "line" => 4}
9798
}
9899
}
99100
]
100101

101102
assert Enum.all?(changes, fn change ->
102-
assert_position_type(change["range"]["end"]) and
103-
assert_position_type(change["range"]["start"])
103+
assert_position_type(change.range["end"]) and
104+
assert_position_type(change.range["start"])
104105
end)
105106
end)
106107
end
@@ -132,25 +133,25 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
132133
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
133134

134135
assert changes == [
135-
%{
136-
"newText" => ")",
137-
"range" => %{
136+
%TextEdit{
137+
newText: ")",
138+
range: %{
138139
"end" => %{"character" => 23, "line" => 4},
139140
"start" => %{"character" => 23, "line" => 4}
140141
}
141142
},
142-
%{
143-
"newText" => "(",
144-
"range" => %{
143+
%TextEdit{
144+
newText: "(",
145+
range: %{
145146
"end" => %{"character" => 16, "line" => 4},
146147
"start" => %{"character" => 15, "line" => 4}
147148
}
148149
}
149150
]
150151

151152
assert Enum.all?(changes, fn change ->
152-
assert_position_type(change["range"]["end"]) and
153-
assert_position_type(change["range"]["start"])
153+
assert_position_type(change.range["end"]) and
154+
assert_position_type(change.range["start"])
154155
end)
155156
end)
156157
end
@@ -184,60 +185,60 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
184185
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
185186

186187
assert changes == [
187-
%{
188-
"newText" => "\n",
189-
"range" => %{
188+
%TextEdit{
189+
newText: "\n",
190+
range: %{
190191
"end" => %{"character" => 0, "line" => 7},
191192
"start" => %{"character" => 3, "line" => 6}
192193
}
193194
},
194-
%{
195-
"newText" => "\n",
196-
"range" => %{
195+
%TextEdit{
196+
newText: "\n",
197+
range: %{
197198
"end" => %{"character" => 0, "line" => 6},
198199
"start" => %{"character" => 5, "line" => 5}
199200
}
200201
},
201-
%{
202-
"newText" => ")\n",
203-
"range" => %{
202+
%TextEdit{
203+
newText: ")\n",
204+
range: %{
204205
"end" => %{"character" => 0, "line" => 5},
205206
"start" => %{"character" => 23, "line" => 4}
206207
}
207208
},
208-
%{
209-
"newText" => "(",
210-
"range" => %{
209+
%TextEdit{
210+
newText: "(",
211+
range: %{
211212
"end" => %{"character" => 16, "line" => 4},
212213
"start" => %{"character" => 15, "line" => 4}
213214
}
214215
},
215-
%{
216-
"newText" => "\n",
217-
"range" => %{
216+
%TextEdit{
217+
newText: "\n",
218+
range: %{
218219
"end" => %{"character" => 0, "line" => 4},
219220
"start" => %{"character" => 25, "line" => 3}
220221
}
221222
},
222-
%{
223-
"newText" => "\n\n",
224-
"range" => %{
223+
%TextEdit{
224+
newText: "\n\n",
225+
range: %{
225226
"end" => %{"character" => 0, "line" => 3},
226227
"start" => %{"character" => 16, "line" => 1}
227228
}
228229
},
229-
%{
230-
"newText" => "\n",
231-
"range" => %{
230+
%TextEdit{
231+
newText: "\n",
232+
range: %{
232233
"end" => %{"character" => 0, "line" => 1},
233234
"start" => %{"character" => 21, "line" => 0}
234235
}
235236
}
236237
]
237238

238239
assert Enum.all?(changes, fn change ->
239-
assert_position_type(change["range"]["end"]) and
240-
assert_position_type(change["range"]["start"])
240+
assert_position_type(change.range["end"]) and
241+
assert_position_type(change.range["start"])
241242
end)
242243
end)
243244
end
@@ -302,25 +303,25 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
302303
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
303304

304305
assert changes == [
305-
%{
306-
"newText" => ")",
307-
"range" => %{
306+
%TextEdit{
307+
newText: ")",
308+
range: %{
308309
"end" => %{"character" => 2, "line" => 7},
309310
"start" => %{"character" => 2, "line" => 7}
310311
}
311312
},
312-
%{
313-
"newText" => "(",
314-
"range" => %{
313+
%TextEdit{
314+
newText: "(",
315+
range: %{
315316
"end" => %{"character" => 20, "line" => 4},
316317
"start" => %{"character" => 15, "line" => 4}
317318
}
318319
}
319320
]
320321

321322
assert Enum.all?(changes, fn change ->
322-
assert_position_type(change["range"]["end"]) and
323-
assert_position_type(change["range"]["start"])
323+
assert_position_type(change.range["end"]) and
324+
assert_position_type(change.range["start"])
324325
end)
325326
end)
326327
end
@@ -378,16 +379,16 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
378379
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
379380

380381
assert changes == [
381-
%{
382-
"newText" => ")",
383-
"range" => %{
382+
%TextEdit{
383+
newText: ")",
384+
range: %{
384385
"end" => %{"character" => 12, "line" => 0},
385386
"start" => %{"character" => 12, "line" => 0}
386387
}
387388
},
388-
%{
389-
"newText" => "(",
390-
"range" => %{
389+
%TextEdit{
390+
newText: "(",
391+
range: %{
391392
"end" => %{"character" => 8, "line" => 0},
392393
"start" => %{"character" => 7, "line" => 0}
393394
}
@@ -417,16 +418,16 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
417418
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
418419

419420
assert changes == [
420-
%{
421-
"newText" => ")",
422-
"range" => %{
421+
%TextEdit{
422+
newText: ")",
423+
range: %{
423424
"end" => %{"character" => 16, "line" => 0},
424425
"start" => %{"character" => 16, "line" => 0}
425426
}
426427
},
427-
%{
428-
"newText" => "(",
429-
"range" => %{
428+
%TextEdit{
429+
newText: "(",
430+
range: %{
430431
"end" => %{"character" => 8, "line" => 0},
431432
"start" => %{"character" => 7, "line" => 0}
432433
}
@@ -456,16 +457,16 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
456457
assert {:ok, changes} = Formatting.format(source_file, uri, project_dir)
457458

458459
assert changes == [
459-
%{
460-
"newText" => ")",
461-
"range" => %{
460+
%TextEdit{
461+
newText: ")",
462+
range: %{
462463
"end" => %{"character" => 213, "line" => 0},
463464
"start" => %{"character" => 213, "line" => 0}
464465
}
465466
},
466-
%{
467-
"newText" => "(",
468-
"range" => %{
467+
%TextEdit{
468+
newText: "(",
469+
range: %{
469470
"end" => %{"character" => 8, "line" => 0},
470471
"start" => %{"character" => 7, "line" => 0}
471472
}

0 commit comments

Comments
 (0)