Skip to content

Commit

Permalink
fix: after review
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacervello committed Apr 2, 2024
1 parent 15e9b98 commit ee80f06
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 70 deletions.
4 changes: 2 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ defmodule NextLS do
"""
## #{reference.module}
#{NextLS.HoverHelpers.to_markdown(content_type, mod_doc)}
#{NextLS.DocsHelpers.to_markdown(content_type, mod_doc)}
"""

"function" ->
Expand All @@ -419,7 +419,7 @@ defmodule NextLS do
"""
## #{Macro.to_string(mod)}.#{reference.identifier}/#{reference.arity}
#{NextLS.HoverHelpers.to_markdown(content_type, fdoc)}
#{NextLS.DocsHelpers.to_markdown(content_type, fdoc)}
"""

_ ->
Expand Down
2 changes: 1 addition & 1 deletion lib/next_ls/autocomplete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ defmodule NextLS.Autocomplete do
"""
## #{Macro.to_string(mod)}.#{name}/#{arity}
#{NextLS.HoverHelpers.to_markdown(content_type, fdoc)}
#{NextLS.DocsHelpers.to_markdown(content_type, fdoc)}
"""

_ ->
Expand Down
30 changes: 30 additions & 0 deletions lib/next_ls/helpers/ast_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,34 @@ defmodule NextLS.ASTHelpers do
end
end)
end

defmodule Function do
@moduledoc false

alias Sourceror.Zipper

def find_aliased_function_call_within(ast, {line, column}) do
position = [line: line, column: column]

result =
ast
|> Zipper.zip()
|> Zipper.find(fn
{{:., _, _}, _metadata, _} = node ->
range = Sourceror.get_range(node)

Sourceror.compare_positions(range.start, position) == :lt &&
Sourceror.compare_positions(range.end, position) == :gt

_ ->
false
end)

if result do
{:ok, Zipper.node(result)}
else
{:error, :not_found}
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule NextLS.HoverHelpers do
defmodule NextLS.DocsHelpers do
@moduledoc false

@spec to_markdown(String.t(), String.t() | list()) :: String.t()
Expand Down
86 changes: 28 additions & 58 deletions lib/next_ls/signature_help.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,59 @@ defmodule NextLS.SignatureHelp do
alias GenLSP.Structures.ParameterInformation
alias GenLSP.Structures.SignatureHelp
alias GenLSP.Structures.SignatureInformation
alias Sourceror.Zipper
alias NextLS.ASTHelpers

def fetch_mod_and_name(uri, position) do
with {:ok, text} <- File.read(URI.parse(uri).path),
ast =
text
|> Spitfire.parse()
|> Spitfire.parse(literal_encoder: &{:ok, {:__literal__, &2, [&1]}})
|> then(fn
{:ok, ast} -> ast
{:error, ast, _} -> ast
end),
{:ok, result} <- find_node(ast, position) do
{:ok, result} <- ASTHelpers.Function.find_aliased_function_call_within(ast, position) do
case result do
{{:., _, [{:__aliases__, _, modules}, name]}, _, _} -> {:ok, {Module.concat(modules), name}}
end
end
end

def format({:ok, {:docs_v1, _, :elixir, _, _, _, docs}}, func_name) do
docs
|> Enum.filter(fn
{{_, name, _arity}, _, _, _, _} -> name == func_name
end)
|> Enum.map(fn
{{_, _name, _arity}, _, [signature], _, _} ->
params_info =
signature
|> Spitfire.parse!()
|> then(fn {_, _, args} ->
Enum.map(args, fn {name, _, _} -> name end)
end)
|> Enum.map(fn name ->
def format({:ok, {:docs_v1, _, _lang, content_type, _, _, docs}}, func_name) do
for {{_, name, _arity}, _, [signature], fdoc, _} <- docs, name == func_name do
params_info =
signature
|> Spitfire.parse!()
|> then(fn {_, _, args} ->
Enum.map(args, fn {name, _, _} ->
%ParameterInformation{
label: Atom.to_string(name)
}
end)
end)

%SignatureHelp{
signatures: [
%SignatureInformation{
label: signature,
parameters: params_info,
documentation: %MarkupContent{
kind: MarkupKind.markdown(),
value: ""
}
}
]
}

# {{_, _name, _arity}, _, [], _, _} ->
# []

_otherwise ->
[]
end)
%SignatureHelp{
signatures: [
%SignatureInformation{
label: signature,
parameters: params_info,
documentation: maybe_doc(content_type, fdoc)
}
]
}
end
end

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

defp find_node(ast, {line, column}) do
position = [line: line, column: column]

result =
ast
|> Zipper.zip()
|> Zipper.find(fn
{{:., _, _}, _metadata, _} = node ->
range = Sourceror.get_range(node)

Sourceror.compare_positions(range.start, position) == :lt &&
Sourceror.compare_positions(range.end, position) == :gt

_ ->
false
end)

if result do
{:ok, Zipper.node(result)}
else
{:error, :not_found}
end
defp maybe_doc(content_type, %{"en" => fdoc}) do
%MarkupContent{
kind: MarkupKind.markdown(),
value: NextLS.DocsHelpers.to_markdown(content_type, fdoc)
}
end

defp maybe_doc(_content_type, _fdoc), do: nil
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule NextLS.HoverHelpersTest do
defmodule NextLS.DocsHelpersTest do
use ExUnit.Case, async: true

alias NextLS.HoverHelpers
alias NextLS.DocsHelpers

describe "converts erlang html format to markdown" do
test "some divs and p and code" do
Expand Down Expand Up @@ -35,7 +35,7 @@ defmodule NextLS.HoverHelpersTest do
]}
]

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

assert actual ==
String.trim("""
Expand All @@ -60,7 +60,7 @@ defmodule NextLS.HoverHelpersTest do
]}
]

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

assert actual ==
String.trim("""
Expand Down Expand Up @@ -103,7 +103,7 @@ defmodule NextLS.HoverHelpersTest do
{:p, [], ["Allowed in guard tests."]}
]

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

assert actual ==
String.trim("""
Expand Down Expand Up @@ -191,7 +191,7 @@ defmodule NextLS.HoverHelpersTest do
]}
]

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

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

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

assert String.trim(actual) ==
String.trim("""
Expand Down
7 changes: 6 additions & 1 deletion test/next_ls/signature_help_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule NextLS.SignatureHelpTest do

File.write!(remote, """
defmodule Remote do
@doc "doc example"
def bang!(bang) do
bang
end
Expand Down Expand Up @@ -116,7 +117,11 @@ defmodule NextLS.SignatureHelpTest do
"parameters" => [
%{"label" => "bang"}
],
"label" => "bang!(bang)"
"label" => "bang!(bang)",
"documentation" => %{
"kind" => "markdown",
"value" => "doc example"
}
}
]
}
Expand Down

0 comments on commit ee80f06

Please sign in to comment.