[PATCH] proper parameter formatting better default arg handling in :cpp:function

Anonymous avatarAnonymous created an issue

Hello,

This is a patch for sphinx/domains/cpp.py from Sphinx 1.0.7 that adds :param: directive support to :cpp:function: and also handles the default parameters containing parentheses, e.g. :cpp:function: void MyClass::foo(const string& name=string());

Regards, Vadim

--- /Users/vp/Downloads/Sphinx-1.0.7/sphinx/domains/cpp.py	2011-01-08 18:05:59.000000000 +0300
+++ /Users/vp/Downloads/Sphinx-1.0.7/sphinx/domains/cpp2.py	2011-04-19 16:10:06.000000000 +0400
@@ -21,7 +21,7 @@
 from sphinx.directives import ObjectDescription
 from sphinx.util.nodes import make_refnode
 from sphinx.util.compat import Directive
-
+from sphinx.util.docfields import Field, TypedField
 
 _identifier_re = re.compile(r'(~?\b[a-zA-Z_][a-zA-Z0-9_]*)\b')
 _whitespace_re = re.compile(r'\s+(?u)')
@@ -658,17 +658,33 @@
         self.skip_ws()
         if self.match(_string_re):
             return self.matched_text
-        idx1 = self.definition.find(',', self.pos)
-        idx2 = self.definition.find(')', self.pos)
-        if idx1 < 0:
-            idx = idx2
-        elif idx2 < 0:
-            idx = idx1
-        else:
-            idx = min(idx1, idx2)
-        if idx < 0:
-            self.fail('unexpected end in default expression')
-        rv = self.definition[self.pos:idx]
+        paren_stack_depth = 0
+        max_pos = len(self.definition)
+        rv_start = self.pos
+        while 1:
+            idx0 = self.definition.find('(', self.pos)
+            idx1 = self.definition.find(',', self.pos)
+            idx2 = self.definition.find(')', self.pos)
+            if idx0 < 0:
+                idx0 = max_pos
+            if idx1 < 0:
+                idx1 = max_pos
+            if idx2 < 0:
+                idx2 = max_pos
+            idx = min(idx0, idx1, idx2)
+            if idx >= max_pos:
+                self.fail('unexpected end in default expression')
+            if idx == idx0:
+                paren_stack_depth += 1
+            elif idx == idx2:
+                paren_stack_depth -= 1
+                if paren_stack_depth < 0:
+                    break
+            elif paren_stack_depth == 0:
+                break
+            self.pos = idx+1
+            
+        rv = self.definition[rv_start:idx]
         self.pos = idx
         return rv
 
@@ -788,6 +804,16 @@
 class CPPObject(ObjectDescription):
     """Description of a C++ language object."""
 
+    doc_field_types = [
+        TypedField('parameter', label=l_('Parameters'),
+                   names=('param', 'parameter', 'arg', 'argument'),
+                   typerolename='type', typenames=('type',)),
+        Field('returnvalue', label=l_('Returns'), has_arg=False,
+              names=('returns', 'return')),
+        Field('returntype', label=l_('Return type'), has_arg=False,
+              names=('rtype',)),
+    ]
+
     def attach_name(self, node, name):
         owner, name = name.split_owner()
         varname = unicode(name)

Comments (4)

  1. Jeroen Keiren

    I second that this is a useful modification. It applies mostly cleanly to the current development version, and it would be great to have this change in the next release.

    The patch fixes all issues of functions with default parameters that I have run into so far.

  2. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.