js nodes, update display_prefix

This commit is contained in:
Jakob Lykke Andersen 2021-10-02 13:30:11 +02:00
parent ebea4327e3
commit 37083fcc1a
3 changed files with 19 additions and 9 deletions

View File

@ -15,6 +15,9 @@ Incompatible changes
* #9672: the signature of
:py:meth:`domains.py.PyObject.get_signature_prefix` has changed to
return a list of nodes instead of a plain string.
* ``domains.js.JSObject.display_prefix`` has been changed into a method
``get_display_prefix`` which now returns a list of nodes
instead of a plain string.
Deprecated
----------

View File

@ -41,9 +41,6 @@ class JSObject(ObjectDescription[Tuple[str, str]]):
#: added
has_arguments = False
#: what is displayed right before the documentation entry
display_prefix: str = None
#: If ``allow_nesting`` is ``True``, the object prefixes will be accumulated
#: based on directive nesting
allow_nesting = False
@ -53,6 +50,10 @@ class JSObject(ObjectDescription[Tuple[str, str]]):
'noindexentry': directives.flag,
}
def get_display_prefix(self) -> List[nodes.Node]:
#: what is displayed right before the documentation entry
return []
def handle_signature(self, sig: str, signode: desc_signature) -> Tuple[str, str]:
"""Breaks down construct signatures
@ -91,9 +92,9 @@ class JSObject(ObjectDescription[Tuple[str, str]]):
signode['object'] = prefix
signode['fullname'] = fullname
if self.display_prefix:
signode += addnodes.desc_annotation(self.display_prefix,
self.display_prefix)
display_prefix = self.get_display_prefix()
if display_prefix:
signode += addnodes.desc_annotation('', '', *display_prefix)
if prefix:
signode += addnodes.desc_addname(prefix + '.', prefix + '.')
elif mod_name:
@ -227,9 +228,13 @@ class JSCallable(JSObject):
class JSConstructor(JSCallable):
"""Like a callable but with a different prefix."""
display_prefix = 'class '
allow_nesting = True
def get_display_prefix(self) -> List[nodes.Node]:
return [addnodes.desc_sig_keyword('class', 'class'),
addnodes.desc_sig_space()]
class JSModule(SphinxDirective):
"""

View File

@ -15,7 +15,8 @@ from docutils import nodes
from sphinx import addnodes
from sphinx.addnodes import (desc, desc_annotation, desc_content, desc_name, desc_parameter,
desc_parameterlist, desc_signature)
desc_parameterlist, desc_sig_keyword, desc_sig_space,
desc_signature)
from sphinx.domains.javascript import JavaScriptDomain
from sphinx.testing import restructuredtext
from sphinx.testing.util import assert_node
@ -198,7 +199,8 @@ def test_js_class(app):
text = ".. js:class:: Application"
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_annotation, "class "],
[desc, ([desc_signature, ([desc_annotation, ([desc_sig_keyword, 'class'],
desc_sig_space)],
[desc_name, "Application"],
[desc_parameterlist, ()])],
[desc_content, ()])]))