Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add quick search to hexdocs.pm #574

Merged
merged 8 commits into from
Jan 23, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 113 additions & 4 deletions apps/language_server/lib/language_server/providers/hover.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule ElixirLS.LanguageServer.Providers.Hover do
Hover provider utilizing Elixir Sense
"""

@hex_base_url "https://hexdocs.pm"

def hover(text, line, character) do
response =
case ElixirSense.docs(text, line + 1, character + 1) do
Expand All @@ -15,7 +17,7 @@ defmodule ElixirLS.LanguageServer.Providers.Hover do
line_text = Enum.at(SourceFile.lines(text), line)
range = highlight_range(line_text, line, character, subject)

%{"contents" => contents(docs), "range" => range}
%{"contents" => contents(docs, subject), "range" => range}
end

{:ok, response}
Expand Down Expand Up @@ -44,14 +46,121 @@ defmodule ElixirLS.LanguageServer.Providers.Hover do
end)
end

defp contents(%{docs: "No documentation available\n"}) do
defp contents(%{docs: "No documentation available\n"}, _subject \\ "") do
[]
end

defp contents(%{docs: markdown}) do
defp contents(%{docs: markdown}, subject) do
%{
kind: "markdown",
value: markdown
value: add_hexdocs_link(markdown, subject)
}
end

defp add_hexdocs_link(markdown, subject) do
[hd | tail] = markdown |> String.split("\n\n")

link = hexdocs_link(markdown, subject)

case link do
"" ->
markdown

_ ->
hd <> " [view on hexdocs](#{link})\n\n" <> Enum.join(tail, "")
end
end

defp hexdocs_link(markdown, subject) do
t = markdown |> String.split("\n\n") |> hd() |> String.replace(">", "") |> String.trim()
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
dep = subject |> root_module_name() |> dep_name()

cond do
func?(t) ->
if dep != "" do
"#{@hex_base_url}/#{dep}/#{module_name(subject)}.html##{func_name(subject)}/#{params_cnt(t)}"
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
else
""
end

true ->
if dep != "" do
"#{@hex_base_url}/#{dep}/#{t}.html"
else
""
end
end
end

defp remove_special_symbol(s) do
s |> String.replace("!", "") |> String.replace("?", "")
end

defp func?(s) do
s =~ ~r/.*\..*\(.*\)/
end

defp module_name(s) do
[_ | tail] = s |> String.split(".") |> Enum.reverse()
tail |> Enum.reverse() |> Enum.join(".")
end

defp func_name(s) do
s |> String.split(".") |> Enum.reverse() |> hd()
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
end

defp params_cnt(s) do
cond do
true == (s =~ ~r/\(\)/) -> 0
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
false == String.contains?(s, ",") -> 1
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
true -> s |> String.split(",") |> length()
end
end

defp dep_name(root_mod_name) do
s = root_mod_name |> source()

cond do
third_dep?(s) -> third_dep_name(s)
builtin?(s) -> builtin_dep_name(s)
true -> ""
end
end

defp root_module_name(subject) do
subject |> String.split(".") |> hd()
end

defp source(mod_name) do
dep = ("Elixir." <> mod_name) |> String.to_atom()
dep.__info__(:compile) |> Keyword.get(:source) |> List.to_string()
end

defp third_dep?(source) do
prefix = File.cwd!() <> "/deps"
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
String.starts_with?(source, prefix)
end

defp third_dep_name(source) do
prefix = File.cwd!() <> "/deps/"
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
String.replace(source, prefix, "") |> String.split("/") |> hd()
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
end

defp builtin?(source) do
[
"elixir",
"eex",
"ex_unit",
"iex",
"logger",
"mix"
]
|> Enum.map(fn x -> "lib/#{x}/lib" end)
lukaszsamson marked this conversation as resolved.
Show resolved Hide resolved
|> Enum.any?(fn y -> String.contains?(source, y) end)
end

def builtin_dep_name(source) do
[_, name | _] = String.split(source, "/lib/")
name
end
end