ex_doc icon indicating copy to clipboard operation
ex_doc copied to clipboard

Refactor autolinking typespecs

Open wojtekmach opened this issue 8 months ago • 1 comments

As a reminder, we want to replace, say, atom() with <a href="...">atom</a>(). The challenging part was we wanted to also format the type specs (using Code.format_string, erl_pp) and preserve that text "layout", spaces and new lines.

I think I have finally figured out a good way to do this. We are already working on type specs AST since it gives us name/arity of each thing we may want to link. I just realised that we can use the column information from the AST to precisely know where to make the transformation. We just need to do the transformations from the end of the string.

Here's a proof of concept: https://gist.github.com/wojtekmach/bc0cf1f1405ddd41962e7ddcfee12904.

What's nice about this is that the text transformation piece is totally agnostic between Erlang, Elixir type specs and upcoming types (as long as they have AST with line/column). The actual logic is about 40 LOC. :)

This requires a minor change in Elixir, https://github.com/elixir-lang/elixir/pull/13101, so we might have to wait a couple releases before we do this.

WDYT?

wojtekmach avatar Nov 13 '23 16:11 wojtekmach