Fix :cpp:any: fix_paren with explicit title.

This commit is contained in:
Jakob Lykke Andersen 2016-01-28 13:19:23 +09:00
parent 678f6066f5
commit 5c363a683e
5 changed files with 90 additions and 46 deletions

View File

@ -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

View File

@ -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 <paren_3>`
* ref function with parens, explicit title :cpp:any:`paren_4_title <paren_4()>`

View File

@ -35,3 +35,12 @@ directives
.. cpp:enum-struct:: protected MyScopedVisibilityEnum : std::underlying_type<MySpecificEnum>::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)

View File

@ -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 <paren_3>`
* ref function with parens, explicit title :cpp:func:`paren_4_title <paren_4()>`

View File

@ -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('<li><a .*?><code .*?><span .*?>Sphinx</span></code></a></li>', roles)
assert re.search(('<li>ref function without parens <a .*?><code .*?><span .*?>'
'hello\(\)</span></code></a>\.</li>'), roles)
assert re.search(('<li>ref function with parens <a .*?><code .*?><span .*?>'
'hello\(\)</span></code></a>\.</li>'), roles)
assert re.search('<li><a .*?><code .*?><span .*?>Sphinx::version</span></code></a></li>',
roles)
assert re.search('<li><a .*?><code .*?><span .*?>version</span></code></a></li>', roles)
assert re.search('<li><a .*?><code .*?><span .*?>List</span></code></a></li>', roles)
assert re.search('<li><a .*?><code .*?><span .*?>MyEnum</span></code></a></li>', roles)
def check(spec, text, file):
pattern = '<li>%s<a .*?><code .*?><span .*?>%s</span></code></a></li>' % 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('<li><a .*?><code .*?><span .*?>Sphinx</span></code></a></li>', any_role)
assert re.search(('<li>ref function without parens <a .*?><code .*?><span .*?>'
'hello\(\)</span></code></a>\.</li>'), any_role)
assert re.search(('<li>ref function with parens <a .*?><code .*?><span .*?>'
'hello\(\)</span></code></a>\.</li>'), any_role)
assert re.search('<li><a .*?><code .*?><span .*?>Sphinx::version</span></code></a></li>',
any_role)
assert re.search('<li><a .*?><code .*?><span .*?>version</span></code></a></li>', any_role)
assert re.search('<li><a .*?><code .*?><span .*?>List</span></code></a></li>', any_role)
assert re.search('<li><a .*?><code .*?><span .*?>MyEnum</span></code></a></li>', 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(('<li>ref function without parens <a .*?><code .*?><span .*?>'
'hello</span></code></a>\.</li>'), roles)
assert re.search(('<li>ref function with parens <a .*?><code .*?><span .*?>'
'hello</span></code></a>\.</li>'), roles)
def check(spec, text, file):
pattern = '<li>%s<a .*?><code .*?><span .*?>%s</span></code></a></li>' % 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(('<li>ref function without parens <a .*?><code .*?><span .*?>'
'hello</span></code></a>\.</li>'), any_role)
assert re.search(('<li>ref function with parens <a .*?><code .*?><span .*?>'
'hello</span></code></a>\.</li>'), 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)