src/eric7/Utilities/ClassBrowsers/pyclbr.py

branch
server
changeset 10680
306373ccf8fd
parent 10596
ea35c92a3c7c
parent 10677
6ee2e475490c
child 10704
27d21e5163b8
equal deleted inserted replaced
10651:280a53840aa3 10680:306373ccf8fd
185 class Class(ClbrBaseClasses.Class, VisibilityMixin): 185 class Class(ClbrBaseClasses.Class, VisibilityMixin):
186 """ 186 """
187 Class to represent a Python class. 187 Class to represent a Python class.
188 """ 188 """
189 189
190 def __init__(self, module, name, superClasses, file, lineno): 190 def __init__(self, module, name, superClasses, file, lineno, col_offset=0):
191 """ 191 """
192 Constructor 192 Constructor
193 193
194 @param module name of the module containing this class 194 @param module name of the module containing this class
195 @type str 195 @type str
196 @param name name of this class 196 @param name name of this class
197 @type str 197 @type str
198 @param superClasses list of class names this class is inherited from 198 @param superClasses list of class names this class is inherited from
199 @type list of str 199 @type list of str
200 @param file filename containing this class 200 @param file file name containing this class
201 @type str 201 @type str
202 @param lineno linenumber of the class definition 202 @param lineno line number of the class definition
203 @type int 203 @type int
204 """ 204 @param col_offset column number of the class definition (defaults to 0)
205 ClbrBaseClasses.Class.__init__(self, module, name, superClasses, file, lineno) 205 @type int (optional)
206 """
207 ClbrBaseClasses.Class.__init__(
208 self, module, name, superClasses, file, lineno, col_offset=col_offset
209 )
206 VisibilityMixin.__init__(self) 210 VisibilityMixin.__init__(self)
207 211
208 212
209 class Function(ClbrBaseClasses.Function, VisibilityMixin): 213 class Function(ClbrBaseClasses.Function, VisibilityMixin):
210 """ 214 """
215 self, 219 self,
216 module, 220 module,
217 name, 221 name,
218 file, 222 file,
219 lineno, 223 lineno,
224 col_offset=0,
220 signature="", 225 signature="",
221 separator=",", 226 separator=",",
222 modifierType=ClbrBaseClasses.Function.General, 227 modifierType=ClbrBaseClasses.Function.General,
223 annotation="", 228 annotation="",
224 ): 229 ):
227 232
228 @param module name of the module containing this function 233 @param module name of the module containing this function
229 @type str 234 @type str
230 @param name name of this function 235 @param name name of this function
231 @type str 236 @type str
232 @param file filename containing this class 237 @param file file name containing this function
233 @type str 238 @type str
234 @param lineno linenumber of the class definition 239 @param lineno line number of the function definition
235 @type int 240 @type int
236 @param signature parameterlist of the method 241 @param col_offset column number of the function definition (defaults to 0)
242 @type int (optional)
243 @param signature parameter list of the function
237 @type str 244 @type str
238 @param separator string separating the parameters 245 @param separator string separating the parameters
239 @type str 246 @type str
240 @param modifierType type of the function 247 @param modifierType type of the function
241 @type int 248 @type int
246 self, 253 self,
247 module, 254 module,
248 name, 255 name,
249 file, 256 file,
250 lineno, 257 lineno,
251 signature, 258 col_offset=col_offset,
252 separator, 259 signature=signature,
253 modifierType, 260 separator=separator,
254 annotation, 261 modifierType=modifierType,
262 annotation=annotation,
255 ) 263 )
256 VisibilityMixin.__init__(self) 264 VisibilityMixin.__init__(self)
257 265
258 266
259 class Attribute(ClbrBaseClasses.Attribute, VisibilityMixin): 267 class Attribute(ClbrBaseClasses.Attribute, VisibilityMixin):
260 """ 268 """
261 Class to represent a class attribute. 269 Class to represent a class attribute.
262 """ 270 """
263 271
264 def __init__(self, module, name, file, lineno): 272 def __init__(self, module, name, file, lineno, col_offset=0):
265 """ 273 """
266 Constructor 274 Constructor
267 275
268 @param module name of the module containing this class 276 @param module name of the module containing this class
269 @type str 277 @type str
270 @param name name of this class 278 @param name name of this class
271 @type str 279 @type str
272 @param file filename containing this attribute 280 @param file file name containing this attribute
273 @type str 281 @type str
274 @param lineno linenumber of the class definition 282 @param lineno line number of the attribute definition
275 @type int 283 @type int
276 """ 284 @param col_offset column number of the attribute definition (defaults to 0)
277 ClbrBaseClasses.Attribute.__init__(self, module, name, file, lineno) 285 @type int (optional)
286 """
287 ClbrBaseClasses.Attribute.__init__(
288 self, module, name, file, lineno, col_offset=col_offset
289 )
278 VisibilityMixin.__init__(self) 290 VisibilityMixin.__init__(self)
279 291
280 292
281 @dataclass 293 @dataclass
282 class Publics: 294 class Publics:
526 meth_ret = m.captured("MethodReturnAnnotation") 538 meth_ret = m.captured("MethodReturnAnnotation")
527 meth_ret = meth_ret.replace("\\\n", "") 539 meth_ret = meth_ret.replace("\\\n", "")
528 meth_ret = _commentsub("", meth_ret) 540 meth_ret = _commentsub("", meth_ret)
529 lineno += src.count("\n", last_lineno_pos, start) 541 lineno += src.count("\n", last_lineno_pos, start)
530 last_lineno_pos = start 542 last_lineno_pos = start
543 col_offset = m.capturedStart("MethodName") - m.capturedStart()
531 if modifierType and modifierIndent == thisindent: 544 if modifierType and modifierIndent == thisindent:
532 if modifierType == "@staticmethod": 545 if modifierType == "@staticmethod":
533 modifier = ClbrBaseClasses.Function.Static 546 modifier = ClbrBaseClasses.Function.Static
534 elif modifierType == "@classmethod": 547 elif modifierType == "@classmethod":
535 modifier = ClbrBaseClasses.Function.Class 548 modifier = ClbrBaseClasses.Function.Class
562 f = Function( 575 f = Function(
563 None, 576 None,
564 meth_name, 577 meth_name,
565 file, 578 file,
566 lineno, 579 lineno,
567 meth_sig, 580 col_offset=col_offset,
581 signature=meth_sig,
568 annotation=meth_ret, 582 annotation=meth_ret,
569 modifierType=modifier, 583 modifierType=modifier,
570 ) 584 )
571 cur_class._addmethod(meth_name, f) 585 cur_class._addmethod(meth_name, f)
572 else: 586 else:
576 f = Function( 590 f = Function(
577 module, 591 module,
578 meth_name, 592 meth_name,
579 file, 593 file,
580 lineno, 594 lineno,
581 meth_sig, 595 col_offset=col_offset,
596 signature=meth_sig,
582 annotation=meth_ret, 597 annotation=meth_ret,
583 modifierType=modifier, 598 modifierType=modifier,
584 ) 599 )
585 if meth_name in dict_counts: 600 if meth_name in dict_counts:
586 dict_counts[meth_name] += 1 601 dict_counts[meth_name] += 1
606 # close all classes indented at least as much 621 # close all classes indented at least as much
607 while classstack and classstack[-1][1] >= thisindent: 622 while classstack and classstack[-1][1] >= thisindent:
608 classstack.pop() 623 classstack.pop()
609 lineno += src.count("\n", last_lineno_pos, start) 624 lineno += src.count("\n", last_lineno_pos, start)
610 last_lineno_pos = start 625 last_lineno_pos = start
626 col_offset = m.capturedStart("ClassName") - m.capturedStart()
611 class_name = m.captured("ClassName") 627 class_name = m.captured("ClassName")
612 inherit = m.captured("ClassSupers") 628 inherit = m.captured("ClassSupers")
613 if inherit: 629 if inherit:
614 # the class inherits from other classes 630 # the class inherits from other classes
615 inherit = inherit[1:-1].strip() 631 inherit = inherit[1:-1].strip()
648 del conditionalsstack[-1] 664 del conditionalsstack[-1]
649 if deltastack: 665 if deltastack:
650 del deltastack[-1] 666 del deltastack[-1]
651 deltaindentcalculated = False 667 deltaindentcalculated = False
652 # remember this class 668 # remember this class
653 cur_class = Class(module, class_name, inherit, file, lineno) 669 cur_class = Class(
670 module, class_name, inherit, file, lineno, col_offset=col_offset
671 )
654 endlineno = calculateEndline(lineno, srcLines, thisindent) 672 endlineno = calculateEndline(lineno, srcLines, thisindent)
655 cur_class.setEndLine(endlineno) 673 cur_class.setEndLine(endlineno)
656 if not classstack: 674 if not classstack:
657 if class_name in dict_counts: 675 if class_name in dict_counts:
658 dict_counts[class_name] += 1 676 dict_counts[class_name] += 1
665 classstack.append((cur_class, thisindent)) 683 classstack.append((cur_class, thisindent))
666 684
667 elif m.captured("Attribute") or m.captured("TypedAttribute"): 685 elif m.captured("Attribute") or m.captured("TypedAttribute"):
668 if m.captured("Attribute"): 686 if m.captured("Attribute"):
669 attribute_name = m.captured("AttributeName") 687 attribute_name = m.captured("AttributeName")
688 col_offset = m.capturedStart("AttributeName") - m.capturedStart()
670 else: 689 else:
671 attribute_name = m.captured("TypedAttributeName") 690 attribute_name = m.captured("TypedAttributeName")
691 col_offset = m.capturedStart("TypedAttributeName") - m.capturedStart()
672 lineno += src.count("\n", last_lineno_pos, start) 692 lineno += src.count("\n", last_lineno_pos, start)
673 last_lineno_pos = start 693 last_lineno_pos = start
674 index = -1 694 index = -1
675 while index >= -len(classstack): 695 while index >= -len(classstack):
676 if classstack[index][0] is not None and not isinstance( 696 if classstack[index][0] is not None and not isinstance(
677 classstack[index][0], Function 697 classstack[index][0], Function
678 ): 698 ):
679 attr = Attribute(module, attribute_name, file, lineno) 699 attr = Attribute(
700 module, attribute_name, file, lineno, col_offset=col_offset
701 )
680 classstack[index][0]._addattribute(attr) 702 classstack[index][0]._addattribute(attr)
681 break 703 break
682 else: 704 else:
683 index -= 1 705 index -= 1
684 706
690 712
691 elif m.captured("Variable") or m.captured("TypedVariable"): 713 elif m.captured("Variable") or m.captured("TypedVariable"):
692 if m.captured("Variable"): 714 if m.captured("Variable"):
693 thisindent = _indent(m.captured("VariableIndent")) 715 thisindent = _indent(m.captured("VariableIndent"))
694 variable_name = m.captured("VariableName") 716 variable_name = m.captured("VariableName")
717 col_offset = m.capturedStart("VariableName") - m.capturedStart()
695 else: 718 else:
696 thisindent = _indent(m.captured("TypedVariableIndent")) 719 thisindent = _indent(m.captured("TypedVariableIndent"))
697 variable_name = m.captured("TypedVariableName") 720 variable_name = m.captured("TypedVariableName")
698 if keyword.iskeyword(variable_name): 721 if keyword.iskeyword(variable_name):
699 # only if the determined name is not a keyword (e.g. else, except) 722 # only if the determined name is not a keyword (e.g. else, except)
700 continue 723 continue
724 col_offset = m.capturedStart("TypedVariableName") - m.capturedStart()
701 lineno += src.count("\n", last_lineno_pos, start) 725 lineno += src.count("\n", last_lineno_pos, start)
702 last_lineno_pos = start 726 last_lineno_pos = start
703 if thisindent == 0 or not classstack: 727 if thisindent == 0 or not classstack:
704 # global variable, reset class stack first 728 # global variable, reset class stack first
705 classstack = [] 729 classstack = []
707 if "@@Globals@@" not in dictionary: 731 if "@@Globals@@" not in dictionary:
708 dictionary["@@Globals@@"] = ClbrBaseClasses.ClbrBase( 732 dictionary["@@Globals@@"] = ClbrBaseClasses.ClbrBase(
709 module, "Globals", file, lineno 733 module, "Globals", file, lineno
710 ) 734 )
711 dictionary["@@Globals@@"]._addglobal( 735 dictionary["@@Globals@@"]._addglobal(
712 Attribute(module, variable_name, file, lineno) 736 Attribute(
737 module, variable_name, file, lineno, col_offset=col_offset
738 )
713 ) 739 )
714 else: 740 else:
715 index = -1 741 index = -1
716 while index >= -len(classstack): 742 while index >= -len(classstack):
717 if classstack[index][1] >= thisindent: 743 if classstack[index][1] >= thisindent:
718 index -= 1 744 index -= 1
719 else: 745 else:
720 if isinstance(classstack[index][0], Class): 746 if isinstance(classstack[index][0], Class):
721 classstack[index][0]._addglobal( 747 classstack[index][0]._addglobal(
722 Attribute(module, variable_name, file, lineno) 748 Attribute(
749 module,
750 variable_name,
751 file,
752 lineno,
753 col_offset=col_offset,
754 )
723 ) 755 )
724 elif isinstance(classstack[index][0], Function): 756 elif isinstance(classstack[index][0], Function):
725 classstack[index][0]._addattribute( 757 classstack[index][0]._addattribute(
726 Attribute(module, variable_name, file, lineno) 758 Attribute(
759 module,
760 variable_name,
761 file,
762 lineno,
763 col_offset=col_offset,
764 )
727 ) 765 )
728 break 766 break
729 767
730 elif m.captured("Publics"): 768 elif m.captured("Publics"):
731 idents = m.captured("Identifiers") 769 idents = m.captured("Identifiers")

eric ide

mercurial