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 2 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
168 changes: 164 additions & 4 deletions apps/language_server/lib/language_server/providers/hover.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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 +44,174 @@ 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
IO.inspect(subject)

[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

cond do
func?(t) ->
mod_name = module_name(t)

if elixir_module?(mod_name) do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can go even further.
With mod_name.__info__(:compile) we can determine wether the module is from a dependency or not. Therefore, generate hexdocs for modules from dependencies as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i have fixed it.

"https://hexdocs.pm/elixir/#{module_name(subject)}.html##{func_name(subject)}/#{params_cnt(t)}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its better to place the base hexdocs url into a module variable

else
""
end

true ->
if elixir_module?(t) do
"https://hexdocs.pm/elixir/#{t}.html"
else
""
end
end
end

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

defp groups_for_modules do
[
Kernel: [Kernel, Kernel.SpecialForms],
"Basic Types": [
Atom,
Base,
Bitwise,
Date,
DateTime,
Exception,
Float,
Function,
Integer,
Module,
NaiveDateTime,
Record,
Regex,
String,
Time,
Tuple,
URI,
Version,
Version.Requirement
],
"Collections & Enumerables": [
Access,
Date.Range,
Enum,
Keyword,
List,
Map,
MapSet,
Range,
Stream
],
"IO & System": [
File,
File.Stat,
File.Stream,
IO,
IO.ANSI,
IO.Stream,
OptionParser,
Path,
Port,
StringIO,
System
],
Calendar: [
Calendar,
Calendar.ISO,
Calendar.TimeZoneDatabase,
Calendar.UTCOnlyTimeZoneDatabase
],
"Processes & Applications": [
Agent,
Application,
Config,
Config.Provider,
Config.Reader,
DynamicSupervisor,
GenServer,
Node,
Process,
Registry,
Supervisor,
Task,
Task.Supervisor
],
Protocols: [
Collectable,
Enumerable,
Inspect,
Inspect.Algebra,
Inspect.Opts,
List.Chars,
Protocol,
String.Chars
],
"Code & Macros": [
Code,
Kernel.ParallelCompiler,
Macro,
Macro.Env
]
]
end

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

defp elixir_module?(s) do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

groups_for_modules()
|> Enum.map(fn x -> elem(x, 1) end)
|> Enum.reduce(fn x, y -> x ++ y end)
|> Enum.any?(fn x ->
x == String.to_atom("Elixir." <> s)
end)
end

defp module_name(s) do
[_ | tail] = s |> String.split(".") |> Enum.reverse()
Enum.join(tail, ".")
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
end