Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ts_xref_formatter apply to all kinds of xref #81

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading