diff --git a/sphinx_js/__init__.py b/sphinx_js/__init__.py index 8749fc11..242e7adb 100644 --- a/sphinx_js/__init__.py +++ b/sphinx_js/__init__.py @@ -4,11 +4,11 @@ from .directives import (auto_class_directive_bound_to_app, auto_function_directive_bound_to_app, - auto_attribute_directive_bound_to_app) + auto_attribute_directive_bound_to_app, + JSStaticFunction) from .jsdoc import Analyzer as JsAnalyzer from .typedoc import Analyzer as TsAnalyzer - def setup(app): # I believe this is the best place to run jsdoc. I was tempted to use # app.add_source_parser(), but I think the kind of source it's referring to @@ -17,6 +17,9 @@ def setup(app): app.connect('env-before-read-docs', read_all_docs) + app.add_directive_to_domain('js', + 'staticfunction', + JSStaticFunction) app.add_directive_to_domain('js', 'autofunction', auto_function_directive_bound_to_app(app)) diff --git a/sphinx_js/directives.py b/sphinx_js/directives.py index 2d4e4793..2545798f 100644 --- a/sphinx_js/directives.py +++ b/sphinx_js/directives.py @@ -9,8 +9,11 @@ """ from docutils.parsers.rst import Directive from docutils.parsers.rst.directives import flag +from sphinx.domains.javascript import JSCallable -from .renderers import AutoFunctionRenderer, AutoClassRenderer, AutoAttributeRenderer +from .renderers import (AutoFunctionRenderer, + AutoClassRenderer, + AutoAttributeRenderer) class JsDirective(Directive): @@ -83,3 +86,8 @@ def _members_to_exclude(arg): """ return set(a.strip() for a in (arg or '').split(',')) + + +class JSStaticFunction(JSCallable): + """Like a callable but with a different prefix.""" + display_prefix = 'static ' diff --git a/sphinx_js/jsdoc.py b/sphinx_js/jsdoc.py index 9fa33cfe..5f71efad 100644 --- a/sphinx_js/jsdoc.py +++ b/sphinx_js/jsdoc.py @@ -131,7 +131,7 @@ def _doclet_as_function(doclet, full_path): exported_from=None, is_abstract=False, is_optional=False, - is_static=False, + is_static=is_static(doclet), is_private=is_private(doclet), exceptions=exceptions_to_ir(doclet.get('exceptions', [])), returns=returns_to_ir(doclet.get('returns', [])), @@ -156,6 +156,10 @@ def is_private(doclet): return doclet.get('access') == 'private' +def is_static(doclet): + return doclet.get('scope') == 'static' + + def full_path_segments(d, base_dir, longname_field='longname'): """Return the full, unambiguous list of path segments that points to an entity described by a doclet. diff --git a/sphinx_js/renderers.py b/sphinx_js/renderers.py index f2e48526..6b099b5f 100644 --- a/sphinx_js/renderers.py +++ b/sphinx_js/renderers.py @@ -178,6 +178,7 @@ def _template_vars(self, name, obj): examples=obj.examples, deprecated=obj.deprecated, is_optional=obj.is_optional, + is_static=obj.is_static, see_also=obj.see_alsos, content='\n'.join(self._content)) diff --git a/sphinx_js/templates/function.rst b/sphinx_js/templates/function.rst index 105538dc..4a136c76 100644 --- a/sphinx_js/templates/function.rst +++ b/sphinx_js/templates/function.rst @@ -1,6 +1,10 @@ {% import 'common.rst' as common %} +{% if is_static %} +.. js:staticfunction:: {{ name }}{{ '?' if is_optional else '' }}{{ params }} +{% else %} .. js:function:: {{ name }}{{ '?' if is_optional else '' }}{{ params }} +{% endif %} {{ common.deprecated(deprecated)|indent(3) }} diff --git a/tests/test_build_js/source/code.js b/tests/test_build_js/source/code.js index 508d0acb..1c39f9f7 100644 --- a/tests/test_build_js/source/code.js +++ b/tests/test_build_js/source/code.js @@ -234,3 +234,14 @@ function union(fnodeA) { */ function longDescriptions(a, b) { } + +/** + * Class doc. + */ +class SimpleClass { + + /** + * Static. + */ + static noUseOfThis() {} +} diff --git a/tests/test_build_js/source/docs/autofunction_static.rst b/tests/test_build_js/source/docs/autofunction_static.rst new file mode 100644 index 00000000..ed11b695 --- /dev/null +++ b/tests/test_build_js/source/docs/autofunction_static.rst @@ -0,0 +1,2 @@ +.. js:autoclass:: SimpleClass + :members: diff --git a/tests/test_build_js/test_build_js.py b/tests/test_build_js/test_build_js.py index 3aca151c..4a081950 100644 --- a/tests/test_build_js/test_build_js.py +++ b/tests/test_build_js/test_build_js.py @@ -119,6 +119,17 @@ def test_autofunction_see(self): ' * "deprecatedFunction"\n\n' ' * "DeprecatedAttribute"\n') + def test_autofunction_static(self): + """Make sure the static function gets its prefix ``static``.""" + self._file_contents_eq( + 'autofunction_static', + 'class SimpleClass()\n\n' + ' Class doc.\n' + '\n' + ' static SimpleClass.noUseOfThis()\n' + '\n' + ' Static.\n') + def test_autoclass(self): """Make sure classes show their class comment and constructor comment."""