Skip to content

Commit eb311c6

Browse files
committed
javadoc ref can resolve to alternative URLs
#feat
1 parent b3ed6d7 commit eb311c6

File tree

7 files changed

+328
-209
lines changed

7 files changed

+328
-209
lines changed

src/lib/Gen/adoc/DocVisitor.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,24 @@ operator()(doc::Reference const& I) const
244244
{
245245
return (*this)(static_cast<doc::Text const&>(I));
246246
}
247-
auto url = corpus_.getURL(corpus_->get(I.id));
248-
if (url.starts_with('/'))
247+
dom::Object symbolObj = corpus_.construct(corpus_->get(I.id));
248+
if (symbolObj.exists("url"))
249249
{
250-
url.erase(0, 1);
250+
std::string url = symbolObj.get("url").getString().str();
251+
if (url.starts_with('/'))
252+
{
253+
url.erase(0, 1);
254+
}
255+
fmt::format_to(
256+
std::back_inserter(dest_),
257+
"xref:{}[{}]",
258+
url, AdocEscape(I.string));
259+
return;
251260
}
252261
fmt::format_to(
253-
std::back_inserter(dest_),
254-
"xref:{}[{}]",
255-
url, AdocEscape(I.string));
262+
std::back_inserter(dest_),
263+
"`{}`",
264+
AdocEscape(I.string));
256265
}
257266

258267
void

src/lib/Gen/hbs/HandlebarsCorpus.cpp

+1-144
Original file line numberDiff line numberDiff line change
@@ -115,149 +115,6 @@ domCreate(
115115
return corpus.toStringFn(corpus, I);
116116
}
117117

118-
Info const*
119-
resolveTypedef(Corpus const& c, Info const& I)
120-
{
121-
if (I.Kind == InfoKind::Typedef)
122-
{
123-
TypedefInfo const& TI = dynamic_cast<TypedefInfo const&>(I);
124-
std::unique_ptr<TypeInfo> const& T = TI.Type;
125-
MRDOCS_CHECK_OR(T && T->Kind == TypeKind::Named, &I);
126-
NamedTypeInfo const& NT = dynamic_cast<NamedTypeInfo const&>(*T);
127-
MRDOCS_CHECK_OR(NT.Name, &I);
128-
Info const* resolved = c.find(NT.Name->id);
129-
MRDOCS_CHECK_OR(resolved, &I);
130-
if (resolved->Kind == InfoKind::Typedef)
131-
{
132-
return resolveTypedef(c, *resolved);
133-
}
134-
return resolved;
135-
}
136-
return &I;
137-
}
138-
139-
Info const*
140-
findPrimarySiblingWithUrl(Corpus const& c, Info const& I, Info const& parent)
141-
{
142-
// Look for the primary sibling in the parent scope
143-
auto const* parentScope = dynamic_cast<ScopeInfo const*>(&parent);
144-
MRDOCS_CHECK_OR(parentScope, nullptr);
145-
for (auto& siblingIDs = parentScope->Lookups.at(I.Name);
146-
SymbolID const& siblingID: siblingIDs)
147-
{
148-
Info const* sibling = c.find(siblingID);
149-
if (!sibling ||
150-
!shouldGenerate(*sibling) ||
151-
sibling->Name != I.Name)
152-
{
153-
continue;
154-
}
155-
bool const isPrimarySibling = visit(*sibling, [&](auto const& U)
156-
{
157-
if constexpr (requires { U.Template; })
158-
{
159-
std::optional<TemplateInfo> const& Template = U.Template;
160-
MRDOCS_CHECK_OR(Template, false);
161-
return !Template->Params.empty() && Template->Args.empty();
162-
}
163-
return false;
164-
});
165-
if (!isPrimarySibling)
166-
{
167-
continue;
168-
}
169-
return sibling;
170-
}
171-
return nullptr;
172-
}
173-
174-
Info const*
175-
findPrimarySiblingWithUrl(Corpus const& c, Info const& I);
176-
177-
Info const*
178-
findDirectPrimarySiblingWithUrl(Corpus const& c, Info const& I)
179-
{
180-
// If the parent is a scope, look for a primary sibling
181-
// in the parent scope for which we want to generate the URL
182-
Info const* parent = c.find(I.Parent);
183-
MRDOCS_CHECK_OR(parent, nullptr);
184-
if (!shouldGenerate(*parent))
185-
{
186-
parent = findPrimarySiblingWithUrl(c, *parent);
187-
MRDOCS_CHECK_OR(parent, nullptr);
188-
}
189-
return findPrimarySiblingWithUrl(c, I, *parent);
190-
}
191-
192-
Info const*
193-
findResolvedPrimarySiblingWithUrl(Corpus const& c, Info const& I)
194-
{
195-
// Check if this info is a specialization or a typedef to
196-
// a specialization, otherwise there's nothing to resolve
197-
bool const isSpecialization = visit(I, [&]<typename InfoTy>(InfoTy const& U)
198-
{
199-
// The symbol is a specialization
200-
if constexpr (requires { U.Template; })
201-
{
202-
std::optional<TemplateInfo> const& Template = U.Template;
203-
if (Template &&
204-
!Template->Args.empty())
205-
{
206-
return true;
207-
}
208-
}
209-
// The symbol is a typedef to a specialization
210-
if constexpr (std::same_as<InfoTy, TypedefInfo>)
211-
{
212-
std::unique_ptr<TypeInfo> const& T = U.Type;
213-
MRDOCS_CHECK_OR(T && T->Kind == TypeKind::Named, false);
214-
auto const& NT = dynamic_cast<NamedTypeInfo const&>(*T);
215-
MRDOCS_CHECK_OR(NT.Name, false);
216-
MRDOCS_CHECK_OR(NT.Name->Kind == NameKind::Specialization, false);
217-
return true;
218-
}
219-
return false;
220-
});
221-
MRDOCS_CHECK_OR(isSpecialization, nullptr);
222-
223-
// Find the parent scope containing the primary sibling
224-
// for which we want to generate the URL
225-
Info const* parent = c.find(I.Parent);
226-
MRDOCS_CHECK_OR(parent, nullptr);
227-
228-
// If the parent is a typedef, resolve it
229-
// so we can iterate the members of this scope.
230-
// We can't find siblings in a typedef because
231-
// it's not a scope.
232-
if (parent->Kind == InfoKind::Typedef)
233-
{
234-
parent = resolveTypedef(c, *parent);
235-
MRDOCS_CHECK_OR(parent, nullptr);
236-
}
237-
238-
// If the resolved parent is also a specialization or
239-
// a dependency for which there's no URL, we attempt to
240-
// find the primary sibling for the parent so we take
241-
// the URL from it.
242-
if (!shouldGenerate(*parent))
243-
{
244-
parent = findPrimarySiblingWithUrl(c, *parent);
245-
MRDOCS_CHECK_OR(parent, nullptr);
246-
}
247-
248-
return findPrimarySiblingWithUrl(c, I, *parent);
249-
}
250-
251-
Info const*
252-
findPrimarySiblingWithUrl(Corpus const& c, Info const& I)
253-
{
254-
if (Info const* primary = findDirectPrimarySiblingWithUrl(c, I))
255-
{
256-
return primary;
257-
}
258-
return findResolvedPrimarySiblingWithUrl(c, I);
259-
}
260-
261118
} // (anon)
262119

263120
dom::Object
@@ -275,7 +132,7 @@ construct(Info const& I) const
275132
// If the URL is not available because it's a specialization
276133
// or dependency, we still want to generate the URL and anchor
277134
// for the primary template if it's part of the corpus.
278-
if (Info const* primaryInfo = findPrimarySiblingWithUrl(getCorpus(), I))
135+
if (Info const* primaryInfo = findAlternativeURLInfo(getCorpus(), I))
279136
{
280137
obj.set("url", getURL(*primaryInfo));
281138
obj.set("anchor", names_.getQualified(primaryInfo->id, '-'));

0 commit comments

Comments
 (0)