Skip to content

Commit 41cea4a

Browse files
committed
sphinxdocs: allow files to be xref
1 parent 9cfdfd8 commit 41cea4a

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

sphinxdocs/src/sphinx_bzl/bzl.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,41 @@ def run(self) -> list[docutils_nodes.Node]:
498498
self.env.ref_context["bzl:file"] = file_label
499499
self.env.ref_context["bzl:object_id_stack"] = []
500500
self.env.ref_context["bzl:doc_id_stack"] = []
501-
return []
501+
502+
_, _, basename = file_label.partition(":")
503+
index_description = f"File {label}"
504+
absolute_label = repo + label
505+
self.env.get_domain('bzl').add_object(
506+
_ObjectEntry(
507+
full_id = absolute_label,
508+
display_name = absolute_label,
509+
# todo: add object_type=file
510+
object_type = 'obj',
511+
search_priority = 1,
512+
index_entry=domains.IndexEntry(
513+
name=basename,
514+
subtype=_INDEX_SUBTYPE_NORMAL,
515+
docname=self.env.docname,
516+
anchor="",
517+
extra="",
518+
qualifier="",
519+
descr=index_description,
520+
)
521+
522+
),
523+
alt_names = [
524+
# Allow xref //foo:bar.bzl
525+
file_label,
526+
# Allow xref bar.bzl
527+
basename,
528+
]
529+
)
530+
index_node = addnodes.index(
531+
entries=[
532+
_index_node_tuple("single", f"File; {label}", ""),
533+
]
534+
)
535+
return [index_node]
502536

503537

504538
class _BzlAttrInfo(sphinx_docutils.SphinxDirective):
@@ -1611,6 +1645,8 @@ def resolve_xref(
16111645
def _find_entry_for_xref(
16121646
self, fromdocname: str, object_type: str, target: str
16131647
) -> typing.Union[_ObjectEntry, None]:
1648+
##if 'pypi-' in fromdocname and 'pip.bzl' in target:
1649+
## breakpoint()
16141650
if target.startswith("--"):
16151651
target = target.strip("-")
16161652
object_type = "flag"

sphinxdocs/tests/sphinx_stardoc/sphinx_output_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def _doc_element(self, doc):
6464
("full_repo_aspect", "@testrepo//lang:aspect.bzl%myaspect", "aspect.html#myaspect"),
6565
("full_repo_target", "@testrepo//lang:relativetarget", "target.html#relativetarget"),
6666
("tag_class_attr_using_attr_role", "myext.mytag.ta1", "module_extension.html#myext.mytag.ta1"),
67+
("tag_class_attr_using_attr_role_just_attr_name", "ta1", "module_extension.html#myext.mytag.ta1"),
68+
("file_without_repo", "//lang:rule.bzl", "rule.html"),
69+
("file_with_repo", "@testrepo//lang:rule.bzl", "rule.html"),
6770
# fmt: on
6871
)
6972
def test_xrefs(self, text, href):

sphinxdocs/tests/sphinx_stardoc/xrefs.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ Various tests of cross referencing support
4545
## Tag class refs
4646

4747
* tag class attribute using attr role: {attr}`myext.mytag.ta1`
48+
* tag class attribute, just attr name, attr role: {attr}`ta1`
49+
50+
## File refs
51+
52+
* without repo {obj}`//lang:rule.bzl`
53+
* with repo {obj}`@testrepo//lang:rule.bzl`

0 commit comments

Comments
 (0)