Skip to content

Commit

Permalink
Do not insert end after do: (elixir-editors#426)
Browse files Browse the repository at this point in the history
* do not insert end after do:

Fixes elixir-editors#424

* Add a couple more tests

Co-authored-by: Jason Axelson <[email protected]>
  • Loading branch information
lukaszsamson and axelson authored Nov 30, 2020
1 parent f08f5ab commit 63d10b3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule ElixirLS.LanguageServer.Providers.OnTypeFormatting do
end

defp tokens(line) do
Regex.scan(Regex.recompile!(~r/(?:->)|(?:\w+)/), line) |> List.flatten()
Regex.scan(Regex.recompile!(~r/(?:->)|(?:[\w\:]+)/), line) |> List.flatten()
end

defp indentation(line) do
Expand Down
60 changes: 60 additions & 0 deletions apps/language_server/test/providers/on_type_formatting_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule ElixirLS.LanguageServer.Providers.OnTypeFormattingTest do
use ExUnit.Case, async: true

alias ElixirLS.LanguageServer.Providers.OnTypeFormatting
alias ElixirLS.LanguageServer.SourceFile

test "insert `end` after `do`" do
text = """
for a <- b do
"""

assert {:ok,
[
%{
"newText" => "\nend",
"range" => %{
"start" => %{"character" => 0, "line" => 1},
"end" => %{"character" => 0, "line" => 1}
}
}
]} = OnTypeFormatting.format(%SourceFile{text: text}, 1, 0, "\n", [])
end

test "insert `end` after def `do`" do
text = """
defmodule TestA do
def myfun(a) do
end
"""

assert {:ok,
[
%{
"newText" => "\n end",
"range" => %{
"start" => %{"character" => 0, "line" => 2},
"end" => %{"character" => 0, "line" => 2}
}
}
]} = OnTypeFormatting.format(%SourceFile{text: text}, 2, 0, "\n", [])
end

test "don't insert `end` after `do:`" do
text = """
for a <- b, do:
"""

assert {:ok, nil} = OnTypeFormatting.format(%SourceFile{text: text}, 1, 0, "\n", [])
end

test "don't insert `end` after def `do: a`" do
text = """
defmodule TestA do
def myfun(a), do: a
end
"""

assert {:ok, nil} = OnTypeFormatting.format(%SourceFile{text: text}, 2, 0, "\n", [])
end
end

0 comments on commit 63d10b3

Please sign in to comment.