Skip to content

Move the xref kind arg onto a field of the TypeXRefInternal #160

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

Merged
merged 1 commit into from
May 7, 2024
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
1 change: 1 addition & 0 deletions sphinx_js/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TypeXRefInternal:
name: str
path: list[str]
type: Literal["internal"] = "internal"
kind: str | None = None


@define
Expand Down
3 changes: 1 addition & 2 deletions sphinx_js/js/convertType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,14 @@ class TypeConverter implements TypeVisitor<Type> {
}

reference(type: ReferenceType): Type {
let res;
if (type.isIntentionallyBroken()) {
// If it's intentionally broken, don't add an xref. It's probably a type
// parameter.
return this.addTypeArguments(type, [type.name]);
} else {
// if we got a reflection use that. It's not all that clear how to deal
// with type arguments here though...
res = this.convertPrivateReferenceToReflection(type);
const res = this.convertPrivateReferenceToReflection(type);
// else use convertReferenceToXRef
if (res) {
return res;
Expand Down
16 changes: 9 additions & 7 deletions sphinx_js/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class HasDepPath(Protocol):


class Renderer:
_type_xref_formatter: Callable[[TypeXRef, str | None], str]
_type_xref_formatter: Callable[[TypeXRef], str]
# We turn the <span class="sphinx_js-type"> in the analyzer tests because it
# makes a big mess.
_add_span: bool
Expand Down Expand Up @@ -235,13 +235,13 @@ def from_directive(cls: type[R], directive: Directive, app: Sphinx) -> R:
)

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

def default_type_xref_formatter(xref: TypeXRef, _: str | None) -> str:
def default_type_xref_formatter(xref: TypeXRef) -> str:
return xref.name

self._type_xref_formatter = default_type_xref_formatter
Expand Down Expand Up @@ -453,11 +453,13 @@ def strs() -> Iterator[str]:

def render_xref(self, s: TypeXRef, escape: bool = False) -> str:
obj = None
kind = None
if isinstance(s, TypeXRefInternal | TypeXRefInternal):
if isinstance(s, TypeXRefInternal):
obj = self.lookup_object(s.path)
kind = type(obj).__name__.lower()
result = self._type_xref_formatter(s, kind)
# Stick the kind on the xref so that the formatter will know what
# xref role to emit. I'm not sure how to compute this earlier. It's
# convenient to do it here.
s.kind = type(obj).__name__.lower()
result = self._type_xref_formatter(s)
if escape:
result = rst.escape(result)
return result
Expand Down
4 changes: 2 additions & 2 deletions tests/test_build_ts/source/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from sphinx_js.ir import TypeXRef, TypeXRefInternal


def ts_type_xref_formatter(config, xref: TypeXRef, kind: str) -> str:
def ts_type_xref_formatter(config, xref: TypeXRef) -> str:
if isinstance(xref, TypeXRefInternal):
name = rst.escape(xref.name)
return f":js:{kind}:`{name}`"
return f":js:{xref.kind}:`{name}`"
else:
return xref.name
10 changes: 7 additions & 3 deletions tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def test_render_description():
)


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

Expand Down Expand Up @@ -339,8 +339,12 @@ def test_render_xref(function_renderer: AutoFunctionRenderer):
assert function_renderer.render_type([xref_external]) == "A"
res = []

def xref_render(config, val, kind):
def xref_render(config, val):
res.append([config, val])
kind = None
if isinstance(val, TypeXRefInternal):
kind = val.kind

return f"{val.package}::{val.name}::{kind}"

function_renderer._set_type_xref_formatter(xref_render)
Expand Down