eric6/Utilities/ClassBrowsers/pyclbr.py

changeset 7698
12cb12380a6a
parent 7690
a59680062837
child 7699
d338c533f5f0
--- a/eric6/Utilities/ClassBrowsers/pyclbr.py	Wed Sep 09 18:05:58 2020 +0200
+++ b/eric6/Utilities/ClassBrowsers/pyclbr.py	Wed Sep 09 18:07:21 2020 +0200
@@ -400,8 +400,35 @@
     @return dictionary containing the extracted data
     @rtype dict
     """
+    def calculateEndline(lineno, lines, indent):
+        """
+        Function to calculate the end line of a class or method/function.
+        
+        @param lineno line number to start at
+        @type int
+        @param lines list of source lines
+        @type list of str
+        @param indent indent length the class/method/function definition
+        @type int
+        @return end line of the class/method/function
+        @rtype int
+        """
+        # start with zero based line after start line
+        while lineno < len(lines):
+            line = lines[lineno]
+            if line.strip():
+                # line contains some text
+                lineIndent = _indent(line.replace(line.lstrip(), ""))
+                if lineIndent <= indent:
+                    return lineno
+            lineno += 1
+        
+        # nothing found
+        return -1
+    
     # convert eol markers the Python style
     src = src.replace("\r\n", "\n").replace("\r", "\n")
+    srcLines = src.splitlines()
     
     dictionary = {}
     dict_counts = {}
@@ -413,7 +440,6 @@
     deltaindentcalculated = 0
     
     lineno, last_lineno_pos = 1, 0
-    lastGlobalEntry = None
     cur_obj = None
     i = 0
     modifierType = ClbrBaseClasses.Function.General
@@ -493,10 +519,8 @@
                 else:
                     dict_counts[meth_name] = 0
                 dictionary[meth_name] = f
-            if not classstack:
-                if lastGlobalEntry:
-                    lastGlobalEntry.setEndLine(lineno - 1)
-                lastGlobalEntry = f
+            endlineno = calculateEndline(lineno, srcLines, thisindent)
+            f.setEndLine(endlineno)
             if cur_obj and isinstance(cur_obj, Function):
                 cur_obj.setEndLine(lineno - 1)
             cur_obj = f
@@ -551,6 +575,8 @@
             # remember this class
             cur_class = Class(module, class_name, inherit,
                               file, lineno)
+            endlineno = calculateEndline(lineno, srcLines, thisindent)
+            cur_class.setEndLine(endlineno)
             if not classstack:
                 if class_name in dict_counts:
                     dict_counts[class_name] += 1
@@ -561,10 +587,6 @@
                 dictionary[class_name] = cur_class
             else:
                 classstack[-1][0]._addclass(class_name, cur_class)
-            if not classstack:
-                if lastGlobalEntry:
-                    lastGlobalEntry.setEndLine(lineno - 1)
-                lastGlobalEntry = cur_class
             classstack.append((cur_class, thisindent))
 
         elif m.start("Attribute") >= 0:
@@ -603,9 +625,6 @@
                         module, "Globals", file, lineno)
                 dictionary["@@Globals@@"]._addglobal(
                     Attribute(module, variable_name, file, lineno))
-                if lastGlobalEntry:
-                    lastGlobalEntry.setEndLine(lineno - 1)
-                lastGlobalEntry = None
             else:
                 index = -1
                 while index >= -len(classstack):

eric ide

mercurial