src/eric7/QScintilla/DocstringGenerator/BaseDocstringGenerator.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
18 18
19 19
20 def getIndentStr(text): 20 def getIndentStr(text):
21 """ 21 """
22 Function to get the indentation of a text. 22 Function to get the indentation of a text.
23 23
24 @param text text to extract indentation from 24 @param text text to extract indentation from
25 @type str 25 @type str
26 @return indentation string 26 @return indentation string
27 @rtype str 27 @rtype str
28 """ 28 """
29 indent = '' 29 indent = ""
30 30
31 ret = re.match(r'(\s*)', text) 31 ret = re.match(r"(\s*)", text)
32 if ret: 32 if ret:
33 indent = ret.group(1) 33 indent = ret.group(1)
34 34
35 return indent 35 return indent
36 36
37 37
38 class BaseDocstringGenerator: 38 class BaseDocstringGenerator:
39 """ 39 """
40 Class implementing a docstring generator base class. 40 Class implementing a docstring generator base class.
41 """ 41 """
42
42 def __init__(self, editor): 43 def __init__(self, editor):
43 """ 44 """
44 Constructor 45 Constructor
45 46
46 @param editor reference to the editor widget 47 @param editor reference to the editor widget
47 @type Editor 48 @type Editor
48 """ 49 """
49 self.editor = editor 50 self.editor = editor
50 51
51 def isFunctionStart(self, text): 52 def isFunctionStart(self, text):
52 """ 53 """
53 Public method to test, if a text is the start of a function or method 54 Public method to test, if a text is the start of a function or method
54 definition. 55 definition.
55 56
56 @param text line of text to be tested 57 @param text line of text to be tested
57 @type str 58 @type str
58 @return flag indicating that the given text starts a function or 59 @return flag indicating that the given text starts a function or
59 method definition (always False) 60 method definition (always False)
60 @rtype bool 61 @rtype bool
61 """ 62 """
62 return False 63 return False
63 64
64 def hasFunctionDefinition(self, cursorPosition): 65 def hasFunctionDefinition(self, cursorPosition):
65 """ 66 """
66 Public method to test, if the cursor is right below a function 67 Public method to test, if the cursor is right below a function
67 definition. 68 definition.
68 69
69 @param cursorPosition current cursor position (line and column) 70 @param cursorPosition current cursor position (line and column)
70 @type tuple of (int, int) 71 @type tuple of (int, int)
71 @return flag indicating cursor is right below a function definition 72 @return flag indicating cursor is right below a function definition
72 @rtype bool 73 @rtype bool
73 """ 74 """
74 return False 75 return False
75 76
76 def isDocstringIntro(self, cursorPosition): 77 def isDocstringIntro(self, cursorPosition):
77 """ 78 """
78 Public function to test, if the line up to the cursor position might be 79 Public function to test, if the line up to the cursor position might be
79 introducing a docstring. 80 introducing a docstring.
80 81
81 @param cursorPosition current cursor position (line and column) 82 @param cursorPosition current cursor position (line and column)
82 @type tuple of (int, int) 83 @type tuple of (int, int)
83 @return flag indicating a potential start of a docstring 84 @return flag indicating a potential start of a docstring
84 @rtype bool 85 @rtype bool
85 """ 86 """
86 return False 87 return False
87 88
88 def insertDocstring(self, cursorPosition, fromStart=True): 89 def insertDocstring(self, cursorPosition, fromStart=True):
89 """ 90 """
90 Public method to insert a docstring for the function at the cursor 91 Public method to insert a docstring for the function at the cursor
91 position. 92 position.
92 93
93 @param cursorPosition position of the cursor (line and index) 94 @param cursorPosition position of the cursor (line and index)
94 @type tuple of (int, int) 95 @type tuple of (int, int)
95 @param fromStart flag indicating that the editor text cursor is placed 96 @param fromStart flag indicating that the editor text cursor is placed
96 on the line starting the function definition 97 on the line starting the function definition
97 @type bool 98 @type bool
98 """ 99 """
99 # just do nothing in the base class 100 # just do nothing in the base class
100 return 101 return
101 102
102 def insertDocstringFromShortcut(self, cursorPosition): 103 def insertDocstringFromShortcut(self, cursorPosition):
103 """ 104 """
104 Public method to insert a docstring for the function at the cursor 105 Public method to insert a docstring for the function at the cursor
105 position initiated via a keyboard shortcut. 106 position initiated via a keyboard shortcut.
106 107
107 @param cursorPosition position of the cursor (line and index) 108 @param cursorPosition position of the cursor (line and index)
108 @type tuple of (int, int) 109 @type tuple of (int, int)
109 """ 110 """
110 # just do nothing in the base class 111 # just do nothing in the base class
111 return 112 return
112 113
113 def getDocstringType(self): 114 def getDocstringType(self):
114 """ 115 """
115 Public method to determine the docstring type to be generated. 116 Public method to determine the docstring type to be generated.
116 117
117 @return docstring type (one of 'ericdoc', 'numpydoc', 'googledoc', 118 @return docstring type (one of 'ericdoc', 'numpydoc', 'googledoc',
118 'sphinxdoc') 119 'sphinxdoc')
119 @rtype str 120 @rtype str
120 """ 121 """
121 docstringStyle = "" 122 docstringStyle = ""
122 123
123 project = ericApp().getObject("Project") 124 project = ericApp().getObject("Project")
124 filename = self.editor.getFileName() 125 filename = self.editor.getFileName()
125 if ( 126 if filename and project.isOpen() and project.isProjectFile(filename):
126 filename and
127 project.isOpen() and
128 project.isProjectFile(filename)
129 ):
130 docstringStyle = project.getDocstringType().lower() 127 docstringStyle = project.getDocstringType().lower()
131 128
132 if docstringStyle == "": 129 if docstringStyle == "":
133 docstringStyle = Preferences.getEditor("DocstringType") 130 docstringStyle = Preferences.getEditor("DocstringType")
134 131
135 return docstringStyle 132 return docstringStyle
136 133
137 def _generateDocstringList(self, functionInfo, docstringType): 134 def _generateDocstringList(self, functionInfo, docstringType):
138 """ 135 """
139 Protected method to generate type specific docstrings based on the 136 Protected method to generate type specific docstrings based on the
140 extracted function information. 137 extracted function information.
141 138
142 @param functionInfo reference to the function info object 139 @param functionInfo reference to the function info object
143 @type FunctionInfo 140 @type FunctionInfo
144 @param docstringType kind of docstring to be generated 141 @param docstringType kind of docstring to be generated
145 @return list of docstring lines 142 @return list of docstring lines
146 @rtype str 143 @rtype str
147 """ 144 """
148 if docstringType == "ericdoc": 145 if docstringType == "ericdoc":
149 from .EricdocGenerator import generateEricDoc 146 from .EricdocGenerator import generateEricDoc
147
150 return generateEricDoc(functionInfo) 148 return generateEricDoc(functionInfo)
151 elif docstringType == "numpydoc": 149 elif docstringType == "numpydoc":
152 from .NumpydocGenerator import generateNumpyDoc 150 from .NumpydocGenerator import generateNumpyDoc
151
153 return generateNumpyDoc(functionInfo) 152 return generateNumpyDoc(functionInfo)
154 elif docstringType == "googledoc": 153 elif docstringType == "googledoc":
155 from .GoogledocGenerator import generateGoogleDoc 154 from .GoogledocGenerator import generateGoogleDoc
155
156 return generateGoogleDoc(functionInfo, self.editor) 156 return generateGoogleDoc(functionInfo, self.editor)
157 elif docstringType == "sphinxdoc": 157 elif docstringType == "sphinxdoc":
158 from .SphinxdocGenerator import generateSphinxDoc 158 from .SphinxdocGenerator import generateSphinxDoc
159
159 return generateSphinxDoc(functionInfo) 160 return generateSphinxDoc(functionInfo)
160 else: 161 else:
161 return [] 162 return []
162 163
163 164
164 class FunctionInfo: 165 class FunctionInfo:
165 """ 166 """
166 Class implementing an object to store function information. 167 Class implementing an object to store function information.
167 168
168 Methods to extract the relevant information need to be implemented in 169 Methods to extract the relevant information need to be implemented in
169 language specific subclasses. 170 language specific subclasses.
170 """ 171 """
172
171 def __init__(self): 173 def __init__(self):
172 """ 174 """
173 Constructor 175 Constructor
174 """ 176 """
175 self.hasInfo = False 177 self.hasInfo = False
176 self.funcionText = "" 178 self.funcionText = ""
177 self.argumentsText = "" 179 self.argumentsText = ""
178 180
179 self.functionIndent = "" 181 self.functionIndent = ""
180 # indentation fo function definition 182 # indentation fo function definition
181 self.argumentsList = [] 183 self.argumentsList = []
182 # list of tuples with name, type and value 184 # list of tuples with name, type and value
183 self.returnTypeAnnotated = None 185 self.returnTypeAnnotated = None
201 203
202 204
203 class DocstringMenuForEnterOnly(QMenu): 205 class DocstringMenuForEnterOnly(QMenu):
204 """ 206 """
205 Class implementing a special menu reacting to the enter/return keys only. 207 Class implementing a special menu reacting to the enter/return keys only.
206 208
207 If a keyboard input is not the "enter key", the menu is closed and the 209 If a keyboard input is not the "enter key", the menu is closed and the
208 input is inserted to the code editor. 210 input is inserted to the code editor.
209 """ 211 """
212
210 def __init__(self, editor): 213 def __init__(self, editor):
211 """ 214 """
212 Constructor 215 Constructor
213 216
214 @param editor reference to the editor 217 @param editor reference to the editor
215 @type Editor 218 @type Editor
216 """ 219 """
217 super().__init__(editor) 220 super().__init__(editor)
218 self.__editor = editor 221 self.__editor = editor
219 222
220 def keyPressEvent(self, evt): 223 def keyPressEvent(self, evt):
221 """ 224 """
222 Protected method to handle key press events. 225 Protected method to handle key press events.
223 226
224 @param evt reference to the key press event object 227 @param evt reference to the key press event object
225 @type QKeyEvent 228 @type QKeyEvent
226 """ 229 """
227 key = evt.key() 230 key = evt.key()
228 if key not in (Qt.Key.Key_Enter, Qt.Key.Key_Return): 231 if key not in (Qt.Key.Key_Enter, Qt.Key.Key_Return):

eric ide

mercurial