Getting documentation programmatically?

Hello,

I know that I can get the documentation of a symbol in iex by typing h whatever, but is there a way to get this documentation programmatically? For context, I use Neovim and I would like a way of reading the documentation inside an editor buffer instead of a REPL running inside a terminal shell. I have already written info.vim which does similar thing for GNU Info documents. Neovim makes it possible to write remote plugins in Elixir, so if there is some Elixir module to get the documentation as a text string it should be fairly simple to pull off.

2 Likes

Take a look at h implementation: elixir/introspection.ex at d8d89b431e197dd230ce01bc20f4656d9ef24e5c · elixir-lang/elixir · GitHub

It seems to be using Code.fetch_docs underneath

2 Likes

Thanks, that looks like what I need. I tried calling it by hand, but I get an error instead:

iex(10)> IEx.Introspection.h(:Kernel)
Could not load module :Kernel, got: nofile
iex(11)> Code.ensure_loaded(:Kernel)
{:error, :nofile}

I looked up the definition of h, but it uses macros, which I am not yet familiar with.

:Kernel is not the same as Kernel. Kernel is the same as :'Elixir.Kernel'. Try IEx.Introspection.h(Kernel)

2 Likes

Great, that works! Code.fetch_docs(:'Elixir.Kernel') gives me structured data which I can work with. One more question, how do I specify a function? I have tried Code.fetch_docs(:'Elixir.Kernel.is_atom/1'), but I get {:error, :module_not_found}, same thing without the /1 part.

EDIT: Never mind, I need to look at the implementation of Ex.Introspection.h/1, not Ex.Introspection.h/1.

You can have a look at ExDoc source code, paritcularly to the retriever module.

Other Elixir modules of interest are:

Have fun!

1 Like

You can use Kernel, no need for cumbersome and non-standard :'Elixir.Kernel'. Btw Kernel is an alias that resolves to :"Elixir.Kernel". :Kernel is just an atom.

iex(2)> :"Elixir.Kernel"
Kernel
iex(3)> :Kernel
:Kernel
2 Likes

I just wanted to make sure you’re aware that there are existing ElixirLS-powered vim/neovim extension that already support fetching Elixir documentation:

But if you want to create to create this extension for learning purposes, or as an improvement (such as being lighter-weight), then please go ahead :+1:

3 Likes

Yes, I am aware of it and I’m using it, it’s a fantastic tool.

That’s the plan, I am working through the guide right now and I want to try my hands at something useful next. Plus, I prefer reading documentation in a separate buffer I can have open to the side rather than a floating window. I could have probably hacked something using the Lua API of the built-in client, but this seems more interesting. Plus it would work even without a language server running.

3 Likes

If you are looking for something more lightweight than a full language server than you could try elixir_sense, the lib utilised by elixirLS - elixir_sense/elixir_sense.ex at 28f8467a349c6ed004aac66a2e8d7020cdde2e0d · elixir-lsp/elixir_sense · GitHub

2 Likes