src/eric7/Utilities/ClassBrowsers/pyclbr.py

branch
eric7
changeset 10677
6ee2e475490c
parent 10477
11b5d533e4a2
child 10680
306373ccf8fd
child 10692
9becf9ca115c
diff -r d1479a4f1426 -r 6ee2e475490c src/eric7/Utilities/ClassBrowsers/pyclbr.py
--- a/src/eric7/Utilities/ClassBrowsers/pyclbr.py	Mon Apr 08 17:02:11 2024 +0200
+++ b/src/eric7/Utilities/ClassBrowsers/pyclbr.py	Tue Apr 09 14:27:03 2024 +0200
@@ -185,7 +185,7 @@
     Class to represent a Python class.
     """
 
-    def __init__(self, module, name, superClasses, file, lineno):
+    def __init__(self, module, name, superClasses, file, lineno, col_offset=0):
         """
         Constructor
 
@@ -195,12 +195,16 @@
         @type str
         @param superClasses list of class names this class is inherited from
         @type list of str
-        @param file filename containing this class
+        @param file file name containing this class
         @type str
-        @param lineno linenumber of the class definition
+        @param lineno line number of the class definition
         @type int
+        @param col_offset column number of the class definition (defaults to 0)
+        @type int (optional)
         """
-        ClbrBaseClasses.Class.__init__(self, module, name, superClasses, file, lineno)
+        ClbrBaseClasses.Class.__init__(
+            self, module, name, superClasses, file, lineno, col_offset=col_offset
+        )
         VisibilityMixin.__init__(self)
 
 
@@ -215,6 +219,7 @@
         name,
         file,
         lineno,
+        col_offset=0,
         signature="",
         separator=",",
         modifierType=ClbrBaseClasses.Function.General,
@@ -227,11 +232,13 @@
         @type str
         @param name name of this function
         @type str
-        @param file filename containing this class
+        @param file file name containing this function
         @type str
-        @param lineno linenumber of the class definition
+        @param lineno line number of the function definition
         @type int
-        @param signature parameterlist of the method
+        @param col_offset column number of the function definition (defaults to 0)
+        @type int (optional)
+        @param signature parameter list of the function
         @type str
         @param separator string separating the parameters
         @type str
@@ -246,10 +253,11 @@
             name,
             file,
             lineno,
-            signature,
-            separator,
-            modifierType,
-            annotation,
+            col_offset=col_offset,
+            signature=signature,
+            separator=separator,
+            modifierType=modifierType,
+            annotation=annotation,
         )
         VisibilityMixin.__init__(self)
 
@@ -259,7 +267,7 @@
     Class to represent a class attribute.
     """
 
-    def __init__(self, module, name, file, lineno):
+    def __init__(self, module, name, file, lineno, col_offset=0):
         """
         Constructor
 
@@ -267,12 +275,16 @@
         @type str
         @param name name of this class
         @type str
-        @param file filename containing this attribute
+        @param file file name containing this attribute
         @type str
-        @param lineno linenumber of the class definition
+        @param lineno line number of the attribute definition
         @type int
+        @param col_offset column number of the attribute definition (defaults to 0)
+        @type int (optional)
         """
-        ClbrBaseClasses.Attribute.__init__(self, module, name, file, lineno)
+        ClbrBaseClasses.Attribute.__init__(
+            self, module, name, file, lineno, col_offset=col_offset
+        )
         VisibilityMixin.__init__(self)
 
 
@@ -517,6 +529,7 @@
             meth_ret = _commentsub("", meth_ret)
             lineno += src.count("\n", last_lineno_pos, start)
             last_lineno_pos = start
+            col_offset = m.capturedStart("MethodName") - m.capturedStart()
             if modifierType and modifierIndent == thisindent:
                 if modifierType == "@staticmethod":
                     modifier = ClbrBaseClasses.Function.Static
@@ -553,7 +566,8 @@
                         meth_name,
                         file,
                         lineno,
-                        meth_sig,
+                        col_offset=col_offset,
+                        signature=meth_sig,
                         annotation=meth_ret,
                         modifierType=modifier,
                     )
@@ -567,7 +581,8 @@
                     meth_name,
                     file,
                     lineno,
-                    meth_sig,
+                    col_offset=col_offset,
+                    signature=meth_sig,
                     annotation=meth_ret,
                     modifierType=modifier,
                 )
@@ -597,6 +612,7 @@
                 classstack.pop()
             lineno += src.count("\n", last_lineno_pos, start)
             last_lineno_pos = start
+            col_offset = m.capturedStart("ClassName") - m.capturedStart()
             class_name = m.captured("ClassName")
             inherit = m.captured("ClassSupers")
             if inherit:
@@ -639,7 +655,9 @@
                             del deltastack[-1]
                     deltaindentcalculated = False
             # remember this class
-            cur_class = Class(module, class_name, inherit, file, lineno)
+            cur_class = Class(
+                module, class_name, inherit, file, lineno, col_offset=col_offset
+            )
             endlineno = calculateEndline(lineno, srcLines, thisindent)
             cur_class.setEndLine(endlineno)
             if not classstack:
@@ -656,8 +674,10 @@
         elif m.captured("Attribute") or m.captured("TypedAttribute"):
             if m.captured("Attribute"):
                 attribute_name = m.captured("AttributeName")
+                col_offset = m.capturedStart("AttributeName") - m.capturedStart()
             else:
                 attribute_name = m.captured("TypedAttributeName")
+                col_offset = m.capturedStart("TypedAttributeName") - m.capturedStart()
             lineno += src.count("\n", last_lineno_pos, start)
             last_lineno_pos = start
             index = -1
@@ -665,7 +685,9 @@
                 if classstack[index][0] is not None and not isinstance(
                     classstack[index][0], Function
                 ):
-                    attr = Attribute(module, attribute_name, file, lineno)
+                    attr = Attribute(
+                        module, attribute_name, file, lineno, col_offset=col_offset
+                    )
                     classstack[index][0]._addattribute(attr)
                     break
                 else:
@@ -681,12 +703,14 @@
             if m.captured("Variable"):
                 thisindent = _indent(m.captured("VariableIndent"))
                 variable_name = m.captured("VariableName")
+                col_offset = m.capturedStart("VariableName") - m.capturedStart()
             else:
                 thisindent = _indent(m.captured("TypedVariableIndent"))
                 variable_name = m.captured("TypedVariableName")
                 if keyword.iskeyword(variable_name):
                     # only if the determined name is not a keyword (e.g. else, except)
                     continue
+                col_offset = m.capturedStart("TypedVariableName") - m.capturedStart()
             lineno += src.count("\n", last_lineno_pos, start)
             last_lineno_pos = start
             if thisindent == 0 or not classstack:
@@ -698,7 +722,9 @@
                         module, "Globals", file, lineno
                     )
                 dictionary["@@Globals@@"]._addglobal(
-                    Attribute(module, variable_name, file, lineno)
+                    Attribute(
+                        module, variable_name, file, lineno, col_offset=col_offset
+                    )
                 )
             else:
                 index = -1
@@ -708,11 +734,23 @@
                     else:
                         if isinstance(classstack[index][0], Class):
                             classstack[index][0]._addglobal(
-                                Attribute(module, variable_name, file, lineno)
+                                Attribute(
+                                    module,
+                                    variable_name,
+                                    file,
+                                    lineno,
+                                    col_offset=col_offset,
+                                )
                             )
                         elif isinstance(classstack[index][0], Function):
                             classstack[index][0]._addattribute(
-                                Attribute(module, variable_name, file, lineno)
+                                Attribute(
+                                    module,
+                                    variable_name,
+                                    file,
+                                    lineno,
+                                    col_offset=col_offset,
+                                )
                             )
                         break
 

eric ide

mercurial