Mon, 07 Nov 2022 17:19:58 +0100
Corrected/acknowledged some bad import style and removed some obsolete code.
7998 | 1 | # -*- coding: utf-8 -*- |
2 | ||
8881
54e42bc2437a
Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8358
diff
changeset
|
3 | # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
7998 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing a docstring generator base class. | |
8 | """ | |
9 | ||
10 | import re | |
11 | ||
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
12 | from PyQt6.QtCore import Qt |
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
13 | from PyQt6.QtWidgets import QMenu |
7998 | 14 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
15 | from eric7 import Preferences |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
16 | from eric7.EricWidgets.EricApplication import ericApp |
7998 | 17 | |
18 | ||
19 | def getIndentStr(text): | |
20 | """ | |
21 | Function to get the indentation of a text. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
22 | |
7998 | 23 | @param text text to extract indentation from |
24 | @type str | |
25 | @return indentation string | |
26 | @rtype str | |
27 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
28 | indent = "" |
7998 | 29 | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
30 | ret = re.match(r"(\s*)", text) |
7998 | 31 | if ret: |
32 | indent = ret.group(1) | |
33 | ||
34 | return indent | |
35 | ||
36 | ||
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
37 | class BaseDocstringGenerator: |
7998 | 38 | """ |
39 | Class implementing a docstring generator base class. | |
40 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
41 | |
7998 | 42 | def __init__(self, editor): |
43 | """ | |
44 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
45 | |
7998 | 46 | @param editor reference to the editor widget |
47 | @type Editor | |
48 | """ | |
49 | self.editor = editor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
50 | |
7998 | 51 | def isFunctionStart(self, text): |
52 | """ | |
53 | Public method to test, if a text is the start of a function or method | |
54 | definition. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | |
7998 | 56 | @param text line of text to be tested |
57 | @type str | |
58 | @return flag indicating that the given text starts a function or | |
59 | method definition (always False) | |
60 | @rtype bool | |
61 | """ | |
62 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
63 | |
7998 | 64 | def hasFunctionDefinition(self, cursorPosition): |
65 | """ | |
66 | Public method to test, if the cursor is right below a function | |
67 | definition. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | |
7998 | 69 | @param cursorPosition current cursor position (line and column) |
70 | @type tuple of (int, int) | |
71 | @return flag indicating cursor is right below a function definition | |
72 | @rtype bool | |
73 | """ | |
74 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
75 | |
7998 | 76 | def isDocstringIntro(self, cursorPosition): |
77 | """ | |
78 | Public function to test, if the line up to the cursor position might be | |
79 | introducing a docstring. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
80 | |
7998 | 81 | @param cursorPosition current cursor position (line and column) |
82 | @type tuple of (int, int) | |
83 | @return flag indicating a potential start of a docstring | |
84 | @rtype bool | |
85 | """ | |
86 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
87 | |
7998 | 88 | def insertDocstring(self, cursorPosition, fromStart=True): |
89 | """ | |
90 | Public method to insert a docstring for the function at the cursor | |
91 | position. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
92 | |
7998 | 93 | @param cursorPosition position of the cursor (line and index) |
94 | @type tuple of (int, int) | |
95 | @param fromStart flag indicating that the editor text cursor is placed | |
96 | on the line starting the function definition | |
97 | @type bool | |
98 | """ | |
99 | # just do nothing in the base class | |
100 | return | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
101 | |
8000
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
102 | def insertDocstringFromShortcut(self, cursorPosition): |
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
103 | """ |
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
104 | Public method to insert a docstring for the function at the cursor |
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
105 | position initiated via a keyboard shortcut. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
106 | |
8000
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
107 | @param cursorPosition position of the cursor (line and index) |
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
108 | @type tuple of (int, int) |
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
109 | """ |
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
110 | # just do nothing in the base class |
47b15df088e4
Editor: extended the docstring functionality to be invocable via a keyboard shortcut (Ctrl+Alt+D).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7998
diff
changeset
|
111 | return |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
112 | |
7998 | 113 | def getDocstringType(self): |
114 | """ | |
115 | Public method to determine the docstring type to be generated. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
116 | |
7998 | 117 | @return docstring type (one of 'ericdoc', 'numpydoc', 'googledoc', |
118 | 'sphinxdoc') | |
119 | @rtype str | |
120 | """ | |
121 | docstringStyle = "" | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
122 | |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8318
diff
changeset
|
123 | project = ericApp().getObject("Project") |
7998 | 124 | filename = self.editor.getFileName() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
125 | if filename and project.isOpen() and project.isProjectFile(filename): |
7998 | 126 | docstringStyle = project.getDocstringType().lower() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
127 | |
7998 | 128 | if docstringStyle == "": |
129 | docstringStyle = Preferences.getEditor("DocstringType") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
130 | |
7998 | 131 | return docstringStyle |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
132 | |
7998 | 133 | def _generateDocstringList(self, functionInfo, docstringType): |
134 | """ | |
135 | Protected method to generate type specific docstrings based on the | |
136 | extracted function information. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
137 | |
7998 | 138 | @param functionInfo reference to the function info object |
139 | @type FunctionInfo | |
140 | @param docstringType kind of docstring to be generated | |
141 | @return list of docstring lines | |
142 | @rtype str | |
143 | """ | |
144 | if docstringType == "ericdoc": | |
9482
a2bc06a54d9d
Corrected/acknowledged some bad import style and removed some obsolete code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
145 | from .EricdocGenerator import generateEricDoc # __IGNORE_WARNING_I101__ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
146 | |
7998 | 147 | return generateEricDoc(functionInfo) |
148 | elif docstringType == "numpydoc": | |
9482
a2bc06a54d9d
Corrected/acknowledged some bad import style and removed some obsolete code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
149 | from .NumpydocGenerator import generateNumpyDoc # __IGNORE_WARNING_I101__ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
150 | |
7998 | 151 | return generateNumpyDoc(functionInfo) |
152 | elif docstringType == "googledoc": | |
9482
a2bc06a54d9d
Corrected/acknowledged some bad import style and removed some obsolete code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
153 | from .GoogledocGenerator import generateGoogleDoc # __IGNORE_WARNING_I101__ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
154 | |
7998 | 155 | return generateGoogleDoc(functionInfo, self.editor) |
156 | elif docstringType == "sphinxdoc": | |
9482
a2bc06a54d9d
Corrected/acknowledged some bad import style and removed some obsolete code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
157 | from .SphinxdocGenerator import generateSphinxDoc # __IGNORE_WARNING_I101__ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
158 | |
7998 | 159 | return generateSphinxDoc(functionInfo) |
160 | else: | |
161 | return [] | |
162 | ||
163 | ||
8207
d359172d11be
Applied some more code simplifications suggested by the new Simplify checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
164 | class FunctionInfo: |
7998 | 165 | """ |
166 | Class implementing an object to store function information. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
167 | |
7998 | 168 | Methods to extract the relevant information need to be implemented in |
169 | language specific subclasses. | |
170 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
171 | |
7998 | 172 | def __init__(self): |
173 | """ | |
174 | Constructor | |
175 | """ | |
176 | self.hasInfo = False | |
177 | self.funcionText = "" | |
178 | self.argumentsText = "" | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
179 | |
7998 | 180 | self.functionIndent = "" |
181 | # indentation fo function definition | |
182 | self.argumentsList = [] | |
183 | # list of tuples with name, type and value | |
184 | self.returnTypeAnnotated = None | |
185 | # return type extracted from type annotation | |
186 | self.returnValueInBody = [] | |
187 | # return values extracted from function body | |
188 | self.raiseList = None | |
189 | # exceptions raised by function | |
190 | self.hasYield = False | |
191 | # function is a generator | |
192 | self.functionType = "" | |
193 | # function type with these values | |
194 | # classmethod, staticmethod, qtslot, constructor or empty (i.e. | |
195 | # standard) | |
196 | self.isAsync = False | |
197 | # function is an asynchronous function, i.e. async def f(): | |
198 | self.visibility = "" | |
199 | # function visibility with allowed values: | |
200 | # public, protected, private or special (i.e. starting and | |
201 | # ending with '__' | |
202 | ||
203 | ||
204 | class DocstringMenuForEnterOnly(QMenu): | |
205 | """ | |
206 | Class implementing a special menu reacting to the enter/return keys only. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
207 | |
7998 | 208 | If a keyboard input is not the "enter key", the menu is closed and the |
209 | input is inserted to the code editor. | |
210 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
211 | |
7998 | 212 | def __init__(self, editor): |
213 | """ | |
214 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
215 | |
7998 | 216 | @param editor reference to the editor |
217 | @type Editor | |
218 | """ | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
219 | super().__init__(editor) |
7998 | 220 | self.__editor = editor |
221 | ||
222 | def keyPressEvent(self, evt): | |
223 | """ | |
224 | Protected method to handle key press events. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
225 | |
7998 | 226 | @param evt reference to the key press event object |
227 | @type QKeyEvent | |
228 | """ | |
229 | key = evt.key() | |
8143
2c730d5fd177
Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8000
diff
changeset
|
230 | if key not in (Qt.Key.Key_Enter, Qt.Key.Key_Return): |
7998 | 231 | self.__editor.keyPressEvent(evt) |
232 | self.close() | |
233 | else: | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8207
diff
changeset
|
234 | super().keyPressEvent(evt) |