eric6/Utilities/ModuleParser.py

changeset 7699
d338c533f5f0
parent 7676
0f67b4562d98
child 7706
0c6d32ec64f1
diff -r 12cb12380a6a -r d338c533f5f0 eric6/Utilities/ModuleParser.py
--- a/eric6/Utilities/ModuleParser.py	Wed Sep 09 18:07:21 2020 +0200
+++ b/eric6/Utilities/ModuleParser.py	Fri Sep 11 17:28:59 2020 +0200
@@ -524,8 +524,35 @@
         
         @param src the source text to be scanned (string)
         """
+        def calculateEndline(lineno, lines, indent):
+            """
+            Function to calculate the end line of a class or method/function.
+            
+            @param lineno line number to start at (one based)
+            @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 (one based)
+            @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
+        
+        srcLines = src.splitlines()
+        
         lineno, last_lineno_pos = 1, 0
-        lastGlobalEntry = None
         classstack = []  # stack of (class, indent) pairs
         conditionalsstack = []  # stack of indents of conditional defines
         deltastack = []
@@ -596,12 +623,6 @@
                         deltaindentcalculated = 0
                 # close all classes indented at least as much
                 while classstack and classstack[-1][1] >= thisindent:
-                    if (
-                        classstack[-1][0] is not None and
-                        isinstance(classstack[-1][0], (Class, Function))
-                    ):
-                        # record the end line of this class or function
-                        classstack[-1][0].setEndLine(lineno - 1)
                     del classstack[-1]
                 if classstack:
                     csi = -1
@@ -636,12 +657,8 @@
                                  annotation=meth_ret)
                     self.__py_setVisibility(f)
                     self.addFunction(meth_name, f)
-                if not classstack:
-                    if lastGlobalEntry:
-                        lastGlobalEntry.setEndLine(lineno - 1)
-                    lastGlobalEntry = f
-                if cur_obj and isinstance(cur_obj, Function):
-                    cur_obj.setEndLine(lineno - 1)
+                endlineno = calculateEndline(lineno, srcLines, thisindent)
+                f.setEndLine(endlineno)
                 cur_obj = f
                 classstack.append((None, thisindent))  # Marker for nested fns
                 
@@ -693,12 +710,6 @@
                 last_lineno_pos = start
                 # close all classes indented at least as much
                 while classstack and classstack[-1][1] >= thisindent:
-                    if (
-                        classstack[-1][0] is not None and
-                        isinstance(classstack[-1][0], (Class, Function))
-                    ):
-                        # record the end line of this class or function
-                        classstack[-1][0].setEndLine(lineno - 1)
                     del classstack[-1]
                 class_name = m.group("ClassName")
                 inherit = m.group("ClassSupers")
@@ -730,13 +741,11 @@
                 cur_class = Class(self.name, class_name, inherit,
                                   self.file, lineno)
                 self.__py_setVisibility(cur_class)
+                endlineno = calculateEndline(lineno, srcLines, thisindent)
+                cur_class.setEndLine(endlineno)
                 cur_obj = cur_class
-                # add nested classes to the module
                 self.addClass(class_name, cur_class)
-                if not classstack:
-                    if lastGlobalEntry:
-                        lastGlobalEntry.setEndLine(lineno - 1)
-                    lastGlobalEntry = cur_class
+                # add nested classes to the module
                 classstack.append((cur_class, thisindent))
             
             elif m.start("Attribute") >= 0:
@@ -773,9 +782,6 @@
                         isSignal=isSignal)
                     self.__py_setVisibility(attr)
                     self.addGlobal(variable_name, attr)
-                    if lastGlobalEntry:
-                        lastGlobalEntry.setEndLine(lineno - 1)
-                    lastGlobalEntry = None
                 else:
                     index = -1
                     while index >= -len(classstack):

eric ide

mercurial