From 5c363a683e61f471c8b6c3311a9ce42aeec09ca5 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Thu, 28 Jan 2016 13:19:23 +0900 Subject: [PATCH] Fix :cpp:any: fix_paren with explicit title. --- sphinx/domains/cpp.py | 17 +++-- tests/roots/test-domain-cpp/any-role.rst | 7 +- tests/roots/test-domain-cpp/index.rst | 9 +++ tests/roots/test-domain-cpp/roles.rst | 7 +- tests/test_domain_cpp.py | 96 +++++++++++++++--------- 5 files changed, 90 insertions(+), 46 deletions(-) diff --git a/sphinx/domains/cpp.py b/sphinx/domains/cpp.py index 8868fe285..10602931d 100644 --- a/sphinx/domains/cpp.py +++ b/sphinx/domains/cpp.py @@ -3925,8 +3925,12 @@ class CPPXRefRole(XRefRole): if parent: refnode['cpp:parentKey'] = parent.get_lookup_key() if refnode['reftype'] == 'any': - # Remove parentheses from the target (not from title) - title, target = self._fix_parens(env, True, title, target) + # Assume the removal part of fix_parens for :any: refs. + # The addition part is done with the reference is resolved. + if not has_explicit_title and title.endswith('()'): + title = title[:-2] + if target.endswith('()'): + target = target[:-2] # TODO: should this really be here? if not has_explicit_title: target = target.lstrip('~') # only has a meaning for the title @@ -4052,13 +4056,10 @@ class CPPDomain(Domain): docname = s.docname assert docname if typ == 'any' and declaration.objectType == 'function': - title = name - if title.endswith('()'): - title = title[:-2] # remove parentheses if env.config.add_function_parentheses: - title += '()' - contnode.pop(0) - contnode.insert(0, nodes.Text(title)) + if not node['refexplicit']: + title = contnode.pop(0).astext() + contnode += nodes.Text(title + '()') return make_refnode(builder, fromdocname, docname, declaration.get_newest_id(), contnode, name ), declaration.objectType diff --git a/tests/roots/test-domain-cpp/any-role.rst b/tests/roots/test-domain-cpp/any-role.rst index dacd2f600..4ca7e34f2 100644 --- a/tests/roots/test-domain-cpp/any-role.rst +++ b/tests/roots/test-domain-cpp/any-role.rst @@ -2,9 +2,12 @@ any role -------- * :cpp:any:`Sphinx` -* ref function without parens :cpp:any:`hello`. -* ref function with parens :cpp:any:`hello()`. * :cpp:any:`Sphinx::version` * :cpp:any:`version` * :cpp:any:`List` * :cpp:any:`MyEnum` + +* ref function without parens :cpp:any:`paren_1` +* ref function with parens :cpp:any:`paren_2()` +* ref function without parens, explicit title :cpp:any:`paren_3_title ` +* ref function with parens, explicit title :cpp:any:`paren_4_title ` diff --git a/tests/roots/test-domain-cpp/index.rst b/tests/roots/test-domain-cpp/index.rst index 9be739f1e..e46545b06 100644 --- a/tests/roots/test-domain-cpp/index.rst +++ b/tests/roots/test-domain-cpp/index.rst @@ -35,3 +35,12 @@ directives .. cpp:enum-struct:: protected MyScopedVisibilityEnum : std::underlying_type::type A scoped enum with non-default visibility, and with a specified underlying type. + + +.. cpp:function:: void paren_1(int, float) + +.. cpp:function:: void paren_2(int, float) + +.. cpp:function:: void paren_3(int, float) + +.. cpp:function:: void paren_4(int, float) diff --git a/tests/roots/test-domain-cpp/roles.rst b/tests/roots/test-domain-cpp/roles.rst index 8baf29b4c..5609ee451 100644 --- a/tests/roots/test-domain-cpp/roles.rst +++ b/tests/roots/test-domain-cpp/roles.rst @@ -2,9 +2,12 @@ roles ----- * :cpp:class:`Sphinx` -* ref function without parens :cpp:func:`hello`. -* ref function with parens :cpp:func:`hello()`. * :cpp:member:`Sphinx::version` * :cpp:var:`version` * :cpp:type:`List` * :cpp:enum:`MyEnum` + +* ref function without parens :cpp:func:`paren_1` +* ref function with parens :cpp:func:`paren_2()` +* ref function without parens, explicit title :cpp:func:`paren_3_title ` +* ref function with parens, explicit title :cpp:func:`paren_4_title ` diff --git a/tests/test_domain_cpp.py b/tests/test_domain_cpp.py index b4c795125..aef7e8ed4 100644 --- a/tests/test_domain_cpp.py +++ b/tests/test_domain_cpp.py @@ -397,47 +397,75 @@ def test_templates(): # raise DefinitionError("") -@with_app(testroot='domain-cpp') -def test_build_domain_cpp(app, status, warning): +@with_app(testroot='domain-cpp', confoverrides={'add_function_parentheses': True}) +def test_build_domain_cpp_with_add_function_parentheses_is_True(app, status, warning): app.builder.build_all() - roles = (app.outdir / 'roles.html').text() - assert re.search('
  • Sphinx
  • ', roles) - assert re.search(('
  • ref function without parens ' - 'hello\(\)\.
  • '), roles) - assert re.search(('
  • ref function with parens ' - 'hello\(\)\.
  • '), roles) - assert re.search('
  • Sphinx::version
  • ', - roles) - assert re.search('
  • version
  • ', roles) - assert re.search('
  • List
  • ', roles) - assert re.search('
  • MyEnum
  • ', roles) + def check(spec, text, file): + pattern = '
  • %s%s
  • ' % spec + res = re.search(pattern, text) + if not res: + print("Pattern\n\t%s\nnot found in %s" % (pattern, file)) + assert False + rolePatterns = [ + ('', 'Sphinx'), + ('', 'Sphinx::version'), + ('', 'version'), + ('', 'List'), + ('', 'MyEnum') + ] + parenPatterns = [ + ('ref function without parens ', 'paren_1\(\)'), + ('ref function with parens ', 'paren_2\(\)'), + ('ref function without parens, explicit title ', 'paren_3_title'), + ('ref function with parens, explicit title ', 'paren_4_title') + ] - any_role = (app.outdir / 'any-role.html').text() - assert re.search('
  • Sphinx
  • ', any_role) - assert re.search(('
  • ref function without parens ' - 'hello\(\)\.
  • '), any_role) - assert re.search(('
  • ref function with parens ' - 'hello\(\)\.
  • '), any_role) - assert re.search('
  • Sphinx::version
  • ', - any_role) - assert re.search('
  • version
  • ', any_role) - assert re.search('
  • List
  • ', any_role) - assert re.search('
  • MyEnum
  • ', any_role) + f = 'roles.html' + t = (app.outdir / f).text() + for s in rolePatterns: + check(s, t, f) + for s in parenPatterns: + check(s, t, f) + + f = 'any-role.html' + t = (app.outdir / f).text() + for s in parenPatterns: + check(s, t, f) @with_app(testroot='domain-cpp', confoverrides={'add_function_parentheses': False}) def test_build_domain_cpp_with_add_function_parentheses_is_False(app, status, warning): app.builder.build_all() - roles = (app.outdir / 'roles.html').text() - assert re.search(('
  • ref function without parens ' - 'hello\.
  • '), roles) - assert re.search(('
  • ref function with parens ' - 'hello\.
  • '), roles) + def check(spec, text, file): + pattern = '
  • %s%s
  • ' % spec + res = re.search(pattern, text) + if not res: + print("Pattern\n\t%s\nnot found in %s" % (pattern, file)) + assert False + rolePatterns = [ + ('', 'Sphinx'), + ('', 'Sphinx::version'), + ('', 'version'), + ('', 'List'), + ('', 'MyEnum') + ] + parenPatterns = [ + ('ref function without parens ', 'paren_1'), + ('ref function with parens ', 'paren_2'), + ('ref function without parens, explicit title ', 'paren_3_title'), + ('ref function with parens, explicit title ', 'paren_4_title') + ] - any_role = (app.outdir / 'any-role.html').text() - assert re.search(('
  • ref function without parens ' - 'hello\.
  • '), any_role) - assert re.search(('
  • ref function with parens ' - 'hello\.
  • '), any_role) + f = 'roles.html' + t = (app.outdir / f).text() + for s in rolePatterns: + check(s, t, f) + for s in parenPatterns: + check(s, t, f) + + f = 'any-role.html' + t = (app.outdir / f).text() + for s in parenPatterns: + check(s, t, f)