Skip to content

Commit

Permalink
Add some docs about how -file and the AST works
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi committed Dec 21, 2023
1 parent 4514144 commit 18a6a88
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/ex_doc/language/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule ExDoc.Language.Source do
"""
def get_basedir(abst_code, module) do
## We look for the first -file attribute to see what the source file that
## was compiled is called.
## was compiled is called. Both Erlang and Elixir places one at the top.
filename =
Enum.find_value(abst_code, fn
{:attribute, _anno, :file, {filename, _line}} ->
Expand Down Expand Up @@ -206,6 +206,15 @@ defmodule ExDoc.Language.Source do
filtermap_ast(ast, source_basedir, fun) |> hd()
end

@doc """
Does a filtermap operation over the forms in an abstract syntax tree with
updated anno for each form pointing to the correct file.
"""
# The file which a form belongs to is decided by the previous :file
# attribute in the AST. The :file can be either relative, or absolute
# depending on how the file was included. So when traversing the AST
# we need to keep track of the :file attributes and update the anno
# with the correct file.
def filtermap_ast(ast, source_basedir, fun) do
Enum.reduce(ast, {nil, []}, fn
{:attribute, _anno, :file, {filename, _line}} = entry, {_file, acc} ->
Expand Down

0 comments on commit 18a6a88

Please sign in to comment.