Skip to content

Commit

Permalink
Make ts_xref_formatter apply to all kinds of xref (#81)
Browse files Browse the repository at this point in the history
Before it specifically applied to external xrefs but I think it's more 
convenient to use it for all xrefs.
  • Loading branch information
hoodmane authored Sep 28, 2023
1 parent 68f57ac commit 7d9916b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
15 changes: 4 additions & 11 deletions sphinx_js/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
Type,
TypeParam,
TypeXRef,
TypeXRefExternal,
TypeXRefInternal,
)
from .jsdoc import Analyzer as JsAnalyzer
from .parsers import PathVisitor
Expand All @@ -57,7 +55,7 @@ class JsRenderer:

_renderer_type: Literal["function", "class", "attribute"]
_template: str
_xref_formatter: Callable[[TypeXRefExternal], str]
_xref_formatter: Callable[[TypeXRef], str]
_partial_path: list[str]
_explicit_formal_params: str
_content: list[str]
Expand All @@ -67,13 +65,13 @@ def _template_vars(self, name: str, obj: TopLevel) -> dict[str, Any]:
raise NotImplementedError

def _set_xref_formatter(
self, formatter: Callable[[Config, TypeXRefExternal], str] | None
self, formatter: Callable[[Config, TypeXRef], str] | None
) -> None:
if formatter:
self._xref_formatter = partial(formatter, self._app.config)
return

def default_xref_formatter(xref: TypeXRefExternal) -> str:
def default_xref_formatter(xref: TypeXRef) -> str:
return xref.name

self._xref_formatter = default_xref_formatter
Expand Down Expand Up @@ -313,12 +311,7 @@ def strs() -> Iterator[str]:
return "".join(res)

def render_xref(self, s: TypeXRef, escape: bool = False) -> str:
if isinstance(s, TypeXRefInternal):
name = rst.escape(s.name)
result = f":js:class:`{name}`"
else:
assert isinstance(s, TypeXRefExternal)
result = self._xref_formatter(s)
result = self._xref_formatter(s)
if escape:
result = rst.escape(result)
return result
Expand Down
11 changes: 11 additions & 0 deletions tests/test_build_ts/source/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@

jsdoc_config_path = "../tsconfig.json"
js_language = "typescript"
from sphinx.util import rst

from sphinx_js.ir import TypeXRefInternal


def ts_xref_formatter(config, xref):
if isinstance(xref, TypeXRefInternal):
name = rst.escape(xref.name)
return f":js:class:`{name}`"
else:
return xref.name
11 changes: 10 additions & 1 deletion tests/test_renderers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from textwrap import dedent, indent

import pytest
from sphinx.util import rst

from sphinx_js.ir import (
DescriptionCode,
Expand Down Expand Up @@ -51,6 +52,14 @@ def test_render_description():
)


def ts_xref_formatter(config, xref):
if isinstance(xref, TypeXRefInternal):
name = rst.escape(xref.name)
return f":js:class:`{name}`"
else:
return xref.name


@pytest.fixture()
def function_renderer():
class _config:
Expand All @@ -63,7 +72,7 @@ class _app:
renderer._app = _app
renderer._explicit_formal_params = None
renderer._content = []
renderer._set_xref_formatter(None)
renderer._set_xref_formatter(ts_xref_formatter)
return renderer


Expand Down

0 comments on commit 7d9916b

Please sign in to comment.