From cbedc7d803809e4cd7d2daa4c0e3eb77441543c7 Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Wed, 15 Nov 2023 16:27:43 +0100 Subject: [PATCH] Extract file from doc entry if available The documentation could be in an include file, then the file in the moduledoc anno would be different than the file in the doc anno. Normally this does not matter except for when we need to create a warning for a broken link. --- lib/ex_doc/formatter/html.ex | 2 ++ lib/ex_doc/retriever.ex | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/ex_doc/formatter/html.ex b/lib/ex_doc/formatter/html.ex index cd2088ccb..8f96ae8d2 100644 --- a/lib/ex_doc/formatter/html.ex +++ b/lib/ex_doc/formatter/html.ex @@ -98,6 +98,7 @@ defmodule ExDoc.Formatter.HTML do [ id: id, line: child_node.doc_line, + file: child_node.source_path, current_kfa: {:function, child_node.name, child_node.arity} ] @@ -115,6 +116,7 @@ defmodule ExDoc.Formatter.HTML do [ id: id, line: child_node.doc_line, + file: child_node.source_path, current_kfa: {child_node.type, child_node.name, child_node.arity} ] diff --git a/lib/ex_doc/retriever.ex b/lib/ex_doc/retriever.ex index 4d6019757..b02f9ca4c 100644 --- a/lib/ex_doc/retriever.ex +++ b/lib/ex_doc/retriever.ex @@ -236,6 +236,7 @@ defmodule ExDoc.Retriever do {:docs_v1, _, _, content_type, _, module_metadata, _} = module_data.docs {{type, name, arity}, anno, signature, source_doc, metadata} = doc_element doc_line = anno_line(anno) + source = anno_file(anno, source) annotations = annotations_for_docs.(metadata) ++ @@ -310,6 +311,7 @@ defmodule ExDoc.Retriever do {:docs_v1, _, _, content_type, _, module_metadata, _} = module_data.docs {{kind, name, arity}, anno, _signature, source_doc, metadata} = callback doc_line = anno_line(anno) + source = anno_file(anno, source) signature = signature(callback_data.signature) specs = callback_data.specs @@ -355,6 +357,7 @@ defmodule ExDoc.Retriever do {:docs_v1, _, _, content_type, _, module_metadata, _} = module_data.docs {{_, name, arity}, anno, _signature, source_doc, metadata} = type_entry doc_line = anno_line(anno) + source = anno_file(anno, source) annotations = annotations_from_metadata(metadata, module_metadata) type_data = module_data.language.type_data(type_entry, module_data) @@ -406,6 +409,15 @@ defmodule ExDoc.Retriever do defp anno_line(line) when is_integer(line), do: abs(line) defp anno_line(anno), do: anno |> :erl_anno.line() |> abs() + defp anno_file(anno, source) do + case :erl_anno.file(anno) do + :undefined -> + source + file -> + %{ url: source.url, path: Path.join(Path.dirname(source.path), file) } + end + end + defp source_link(%{path: _, url: nil}, _line), do: nil defp source_link(source, line) do