Skip to content

Commit 85487e7

Browse files
authored
Merge pull request #810 from scottming/fix-long-line-formatting
Fix long line formatting of the experimental project
2 parents 1e815bd + a115bf3 commit 85487e7

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

apps/language_server/lib/language_server/experimental/code_mod/diff.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ defmodule ElixirLS.LanguageServer.Experimental.CodeMod.Diff do
7070

7171
defp apply_diff(:ins, {line, code_unit} = position, change, {current_line, prev_lines}) do
7272
current_line = [edit(change, line, code_unit, line, code_unit) | current_line]
73-
advance(change, position, {current_line, prev_lines})
73+
# When inserting, the insert itself does not exist in source, so there is no need to advance
74+
{position, {current_line, prev_lines}}
7475
end
7576

7677
defp advance(<<>>, position, edits) do

apps/language_server/test/experimental/code_mod/format_test.exs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,93 @@ defmodule ElixirLS.Experimental.FormatTest do
7171
]t == result
7272
end
7373

74+
test "it can format a long line function definition into multiple lines" do
75+
unformatted = ~q[
76+
defmodule Unformatted do
77+
def very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong(s) do
78+
s
79+
end
80+
end
81+
]t
82+
83+
formatted = ~q[
84+
defmodule Unformatted do
85+
def very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong(
86+
s
87+
) do
88+
s
89+
end
90+
end
91+
]t
92+
93+
assert {:ok, formatted} == modify(unformatted)
94+
end
95+
96+
test "it can format a long line function call into two lines" do
97+
unformatted = ~q[
98+
defmodule Unformatted do
99+
def foo1(s) do
100+
s = very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonooooong(s) |> IO.inputs()
101+
s
102+
end
103+
104+
def very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonooooong(s) do
105+
s
106+
end
107+
end
108+
]t
109+
110+
formatted = ~q[
111+
defmodule Unformatted do
112+
def foo1(s) do
113+
s =
114+
very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonooooong(s) |> IO.inputs()
115+
116+
s
117+
end
118+
119+
def very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooonooooong(s) do
120+
s
121+
end
122+
end
123+
]t
124+
125+
assert {:ok, formatted} == modify(unformatted)
126+
end
127+
128+
test "it can format a long line function definition(with multiple args) into multiple lines" do
129+
unformatted = ~q[
130+
defmodule Unformatted do
131+
def foo(arg1, arg2, arg3, _arg4, _arg5, _arg6, _arg7, _arg8, _arg9, _arg10, _arg11, _arg12, _arg13) do
132+
arg1 <> arg2 <> arg3
133+
end
134+
end
135+
]t
136+
formatted = ~q[
137+
defmodule Unformatted do
138+
def foo(
139+
arg1,
140+
arg2,
141+
arg3,
142+
_arg4,
143+
_arg5,
144+
_arg6,
145+
_arg7,
146+
_arg8,
147+
_arg9,
148+
_arg10,
149+
_arg11,
150+
_arg12,
151+
_arg13
152+
) do
153+
arg1 <> arg2 <> arg3
154+
end
155+
end
156+
]t
157+
158+
assert {:ok, formatted} == modify(unformatted)
159+
end
160+
74161
test "it handles extra lines" do
75162
assert {:ok, result} = ~q[
76163
defmodule Unformatted do

0 commit comments

Comments
 (0)