Skip to content

Commit ee80f06

Browse files
committed
fix: after review
1 parent 15e9b98 commit ee80f06

File tree

7 files changed

+75
-70
lines changed

7 files changed

+75
-70
lines changed

lib/next_ls.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ defmodule NextLS do
405405
"""
406406
## #{reference.module}
407407
408-
#{NextLS.HoverHelpers.to_markdown(content_type, mod_doc)}
408+
#{NextLS.DocsHelpers.to_markdown(content_type, mod_doc)}
409409
"""
410410

411411
"function" ->
@@ -419,7 +419,7 @@ defmodule NextLS do
419419
"""
420420
## #{Macro.to_string(mod)}.#{reference.identifier}/#{reference.arity}
421421
422-
#{NextLS.HoverHelpers.to_markdown(content_type, fdoc)}
422+
#{NextLS.DocsHelpers.to_markdown(content_type, fdoc)}
423423
"""
424424

425425
_ ->

lib/next_ls/autocomplete.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ defmodule NextLS.Autocomplete do
636636
"""
637637
## #{Macro.to_string(mod)}.#{name}/#{arity}
638638
639-
#{NextLS.HoverHelpers.to_markdown(content_type, fdoc)}
639+
#{NextLS.DocsHelpers.to_markdown(content_type, fdoc)}
640640
"""
641641

642642
_ ->

lib/next_ls/helpers/ast_helpers.ex

+30
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,34 @@ defmodule NextLS.ASTHelpers do
152152
end
153153
end)
154154
end
155+
156+
defmodule Function do
157+
@moduledoc false
158+
159+
alias Sourceror.Zipper
160+
161+
def find_aliased_function_call_within(ast, {line, column}) do
162+
position = [line: line, column: column]
163+
164+
result =
165+
ast
166+
|> Zipper.zip()
167+
|> Zipper.find(fn
168+
{{:., _, _}, _metadata, _} = node ->
169+
range = Sourceror.get_range(node)
170+
171+
Sourceror.compare_positions(range.start, position) == :lt &&
172+
Sourceror.compare_positions(range.end, position) == :gt
173+
174+
_ ->
175+
false
176+
end)
177+
178+
if result do
179+
{:ok, Zipper.node(result)}
180+
else
181+
{:error, :not_found}
182+
end
183+
end
184+
end
155185
end

lib/next_ls/helpers/hover_helpers.ex lib/next_ls/helpers/docs_helpers.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule NextLS.HoverHelpers do
1+
defmodule NextLS.DocsHelpers do
22
@moduledoc false
33

44
@spec to_markdown(String.t(), String.t() | list()) :: String.t()

lib/next_ls/signature_help.ex

+28-58
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,59 @@ defmodule NextLS.SignatureHelp do
66
alias GenLSP.Structures.ParameterInformation
77
alias GenLSP.Structures.SignatureHelp
88
alias GenLSP.Structures.SignatureInformation
9-
alias Sourceror.Zipper
9+
alias NextLS.ASTHelpers
1010

1111
def fetch_mod_and_name(uri, position) do
1212
with {:ok, text} <- File.read(URI.parse(uri).path),
1313
ast =
1414
text
15-
|> Spitfire.parse()
15+
|> Spitfire.parse(literal_encoder: &{:ok, {:__literal__, &2, [&1]}})
1616
|> then(fn
1717
{:ok, ast} -> ast
1818
{:error, ast, _} -> ast
1919
end),
20-
{:ok, result} <- find_node(ast, position) do
20+
{:ok, result} <- ASTHelpers.Function.find_aliased_function_call_within(ast, position) do
2121
case result do
2222
{{:., _, [{:__aliases__, _, modules}, name]}, _, _} -> {:ok, {Module.concat(modules), name}}
2323
end
2424
end
2525
end
2626

27-
def format({:ok, {:docs_v1, _, :elixir, _, _, _, docs}}, func_name) do
28-
docs
29-
|> Enum.filter(fn
30-
{{_, name, _arity}, _, _, _, _} -> name == func_name
31-
end)
32-
|> Enum.map(fn
33-
{{_, _name, _arity}, _, [signature], _, _} ->
34-
params_info =
35-
signature
36-
|> Spitfire.parse!()
37-
|> then(fn {_, _, args} ->
38-
Enum.map(args, fn {name, _, _} -> name end)
39-
end)
40-
|> Enum.map(fn name ->
27+
def format({:ok, {:docs_v1, _, _lang, content_type, _, _, docs}}, func_name) do
28+
for {{_, name, _arity}, _, [signature], fdoc, _} <- docs, name == func_name do
29+
params_info =
30+
signature
31+
|> Spitfire.parse!()
32+
|> then(fn {_, _, args} ->
33+
Enum.map(args, fn {name, _, _} ->
4134
%ParameterInformation{
4235
label: Atom.to_string(name)
4336
}
4437
end)
38+
end)
4539

46-
%SignatureHelp{
47-
signatures: [
48-
%SignatureInformation{
49-
label: signature,
50-
parameters: params_info,
51-
documentation: %MarkupContent{
52-
kind: MarkupKind.markdown(),
53-
value: ""
54-
}
55-
}
56-
]
57-
}
58-
59-
# {{_, _name, _arity}, _, [], _, _} ->
60-
# []
61-
62-
_otherwise ->
63-
[]
64-
end)
40+
%SignatureHelp{
41+
signatures: [
42+
%SignatureInformation{
43+
label: signature,
44+
parameters: params_info,
45+
documentation: maybe_doc(content_type, fdoc)
46+
}
47+
]
48+
}
49+
end
6550
end
6651

6752
def format({:ok, {:error, :module_not_found}}, _func_name) do
6853
[]
6954
end
7055

71-
defp find_node(ast, {line, column}) do
72-
position = [line: line, column: column]
73-
74-
result =
75-
ast
76-
|> Zipper.zip()
77-
|> Zipper.find(fn
78-
{{:., _, _}, _metadata, _} = node ->
79-
range = Sourceror.get_range(node)
80-
81-
Sourceror.compare_positions(range.start, position) == :lt &&
82-
Sourceror.compare_positions(range.end, position) == :gt
83-
84-
_ ->
85-
false
86-
end)
87-
88-
if result do
89-
{:ok, Zipper.node(result)}
90-
else
91-
{:error, :not_found}
92-
end
56+
defp maybe_doc(content_type, %{"en" => fdoc}) do
57+
%MarkupContent{
58+
kind: MarkupKind.markdown(),
59+
value: NextLS.DocsHelpers.to_markdown(content_type, fdoc)
60+
}
9361
end
62+
63+
defp maybe_doc(_content_type, _fdoc), do: nil
9464
end

test/next_ls/helpers/hover_helpers_test.exs test/next_ls/helpers/docs_helpers_test.exs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
defmodule NextLS.HoverHelpersTest do
1+
defmodule NextLS.DocsHelpersTest do
22
use ExUnit.Case, async: true
33

4-
alias NextLS.HoverHelpers
4+
alias NextLS.DocsHelpers
55

66
describe "converts erlang html format to markdown" do
77
test "some divs and p and code" do
@@ -35,7 +35,7 @@ defmodule NextLS.HoverHelpersTest do
3535
]}
3636
]
3737

38-
actual = HoverHelpers.to_markdown("application/erlang+html", html)
38+
actual = DocsHelpers.to_markdown("application/erlang+html", html)
3939

4040
assert actual ==
4141
String.trim("""
@@ -60,7 +60,7 @@ defmodule NextLS.HoverHelpersTest do
6060
]}
6161
]
6262

63-
actual = HoverHelpers.to_markdown("application/erlang+html", html)
63+
actual = DocsHelpers.to_markdown("application/erlang+html", html)
6464

6565
assert actual ==
6666
String.trim("""
@@ -103,7 +103,7 @@ defmodule NextLS.HoverHelpersTest do
103103
{:p, [], ["Allowed in guard tests."]}
104104
]
105105

106-
actual = HoverHelpers.to_markdown("application/erlang+html", html)
106+
actual = DocsHelpers.to_markdown("application/erlang+html", html)
107107

108108
assert actual ==
109109
String.trim("""
@@ -191,7 +191,7 @@ defmodule NextLS.HoverHelpersTest do
191191
]}
192192
]
193193

194-
actual = HoverHelpers.to_markdown("application/erlang+html", html)
194+
actual = DocsHelpers.to_markdown("application/erlang+html", html)
195195

196196
assert String.trim(actual) ==
197197
String.trim("""
@@ -231,7 +231,7 @@ defmodule NextLS.HoverHelpersTest do
231231
{:p, [], ["Returns ", {:code, [], ["error"]}, " if no value is associated with ", {:code, [], ["Flag"]}, "."]}
232232
]
233233

234-
actual = HoverHelpers.to_markdown("application/erlang+html", html)
234+
actual = DocsHelpers.to_markdown("application/erlang+html", html)
235235

236236
assert String.trim(actual) ==
237237
String.trim("""

test/next_ls/signature_help_test.exs

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule NextLS.SignatureHelpTest do
2020

2121
File.write!(remote, """
2222
defmodule Remote do
23+
@doc "doc example"
2324
def bang!(bang) do
2425
bang
2526
end
@@ -116,7 +117,11 @@ defmodule NextLS.SignatureHelpTest do
116117
"parameters" => [
117118
%{"label" => "bang"}
118119
],
119-
"label" => "bang!(bang)"
120+
"label" => "bang!(bang)",
121+
"documentation" => %{
122+
"kind" => "markdown",
123+
"value" => "doc example"
124+
}
120125
}
121126
]
122127
}

0 commit comments

Comments
 (0)