Skip to content

Commit ddcce6d

Browse files
committed
Add :include_comments option to Ast.from/2
Use Sourceror.parse_string/1 when include_comments: true.
1 parent 5a34016 commit ddcce6d

File tree

2 files changed

+97
-5
lines changed
  • apps/language_server

2 files changed

+97
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ defmodule ElixirLS.LanguageServer.Experimental.CodeMod.Ast do
1010
| {any(), any()}
1111
| {atom() | {any(), [any()], atom() | [any()]}, Keyword.t(), atom() | [any()]}
1212

13-
@spec from(source) :: t
14-
def from(%SourceFile{} = source_file) do
13+
def from(source_file, opts \\ [])
14+
15+
@spec from(source, keyword()) :: t
16+
def from(%SourceFile{} = source_file, opts) do
1517
source_file
1618
|> SourceFile.to_string()
17-
|> from()
19+
|> from(opts)
1820
end
1921

20-
def from(s) when is_binary(s) do
21-
ElixirSense.string_to_quoted(s, 1, 6, token_metadata: true)
22+
def from(s, opts) when is_binary(s) do
23+
if opts[:include_comments] do
24+
Sourceror.parse_string(s)
25+
else
26+
ElixirSense.string_to_quoted(s, 1, 6, token_metadata: true)
27+
end
2228
end
2329

2430
@spec to_string(t()) :: String.t()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
defmodule ElixirLS.LanguageServer.Experimental.CodeMod.AstTest do
2+
alias ElixirLS.LanguageServer.Experimental.CodeMod.Ast
3+
4+
use ExUnit.Case
5+
6+
describe "ast" do
7+
test "from\2 / to_string\1" do
8+
string = """
9+
defmodule Bar do
10+
def foo(baz) do
11+
# TODO
12+
end
13+
end
14+
"""
15+
16+
{:ok, ast} = Ast.from(string, include_comments: true)
17+
assert string == Ast.to_string(ast) <> "\n"
18+
19+
{:ok, ast_missing_comments} = Ast.from(string)
20+
21+
assert """
22+
defmodule Bar do
23+
def foo(baz) do
24+
end
25+
end
26+
""" == Ast.to_string(ast_missing_comments) <> "\n"
27+
end
28+
29+
test "from\1" do
30+
assert {:ok,
31+
{:def, [do: [line: 1, column: 16], end: [line: 3, column: 3], line: 1, column: 3],
32+
[
33+
{:foo, [closing: [line: 1, column: 14], line: 1, column: 7],
34+
[{:baz, [line: 1, column: 11], nil}]},
35+
[do: {:__block__, [], []}]
36+
]}} =
37+
Ast.from("""
38+
def foo(baz) do
39+
# TODO
40+
end
41+
""")
42+
end
43+
44+
test "from\2" do
45+
assert {:ok,
46+
{:def,
47+
[
48+
trailing_comments: [
49+
%{column: 5, line: 2, next_eol_count: 1, previous_eol_count: 1, text: "# TODO"}
50+
],
51+
leading_comments: [],
52+
do: [line: 1, column: 16],
53+
end: [line: 3, column: 3],
54+
line: 1,
55+
column: 3
56+
],
57+
[
58+
{:foo,
59+
[
60+
trailing_comments: [],
61+
leading_comments: [],
62+
closing: [line: 1, column: 14],
63+
line: 1,
64+
column: 7
65+
],
66+
[
67+
{:baz, [trailing_comments: [], leading_comments: [], line: 1, column: 11],
68+
nil}
69+
]},
70+
[
71+
{{:__block__,
72+
[trailing_comments: [], leading_comments: [], line: 1, column: 16], [:do]},
73+
{:__block__, [trailing_comments: [], leading_comments: []], []}}
74+
]
75+
]}} =
76+
Ast.from(
77+
"""
78+
def foo(baz) do
79+
# TODO
80+
end
81+
""",
82+
include_comments: true
83+
)
84+
end
85+
end
86+
end

0 commit comments

Comments
 (0)