eric6/QScintilla/DocstringGenerator/PyDocstringGenerator.py

changeset 8000
47b15df088e4
parent 7998
cd41c844862f
child 8205
4a0f1f896341
equal deleted inserted replaced
7999:159689297bc0 8000:47b15df088e4
104 on the line starting the function definition 104 on the line starting the function definition
105 @type bool 105 @type bool
106 """ 106 """
107 if fromStart: 107 if fromStart:
108 self.__functionStartLine = cursorPosition[0] 108 self.__functionStartLine = cursorPosition[0]
109 docstring, insertPos = self.__generateDocstringFromStart() 109 docstring, insertPos, newCursorLine = (
110 self.__generateDocstringFromStart()
111 )
110 else: 112 else:
111 docstring, insertPos = self.__generateDocstringFromBelow( 113 docstring, insertPos, newCursorLine = (
112 cursorPosition) 114 self.__generateDocstringFromBelow(cursorPosition)
113 115 )
116
117 if docstring:
118 self.editor.beginUndoAction()
119 self.editor.insertAt(docstring, *insertPos)
120
121 if not fromStart:
122 # correct triple quote indentation if neccessary
123 functionIndent = self.editor.indentation(
124 self.__functionStartLine)
125 quoteIndent = self.editor.indentation(insertPos[0])
126
127 # step 1: unindent quote line until indentation is zero
128 while quoteIndent > 0:
129 self.editor.unindent(insertPos[0])
130 quoteIndent = self.editor.indentation(insertPos[0])
131
132 # step 2: indent quote line until indentation is one greater
133 # than function definition line
134 while quoteIndent <= functionIndent:
135 self.editor.indent(insertPos[0])
136 quoteIndent = self.editor.indentation(insertPos[0])
137
138 self.editor.endUndoAction()
139 self.editor.setCursorPosition(
140 newCursorLine, len(self.editor.text(newCursorLine)) - 1
141 )
142
143 def insertDocstringFromShortcut(self, cursorPosition):
144 """
145 Public method to insert a docstring for the function at the cursor
146 position initiated via a keyboard shortcut.
147
148 @param cursorPosition position of the cursor (line and index)
149 @type tuple of (int, int)
150 """
151 result = self.__getFunctionDefinitionFromBelow(cursorPosition)
152 if result is not None:
153 # cursor is on the line after the function definition
154 cline = cursorPosition[0] - 1
155 while not self.isFunctionStart(self.editor.text(cline)):
156 cline -= 1
157 self.__functionStartLine = cline
158 elif self.isFunctionStart(self.editor.text(cursorPosition[0])):
159 # cursor is on the start line of the function definition
160 self.__functionStartLine = cursorPosition[0]
161 else:
162 # neither after the function definition nor at the start
163 # just do nothing
164 return
165
166 docstring, insertPos, newCursorLine = (
167 self.__generateDocstringFromStart()
168 )
114 if docstring: 169 if docstring:
115 self.editor.beginUndoAction() 170 self.editor.beginUndoAction()
116 self.editor.insertAt(docstring, *insertPos) 171 self.editor.insertAt(docstring, *insertPos)
117 self.editor.endUndoAction() 172 self.editor.endUndoAction()
173 self.editor.setCursorPosition(
174 newCursorLine, len(self.editor.text(newCursorLine)) - 1
175 )
118 176
119 def __getIndentationInsertString(self, text): 177 def __getIndentationInsertString(self, text):
120 """ 178 """
121 Private method to create the indentation string for the docstring. 179 Private method to create the indentation string for the docstring.
122 180
159 '"', functionDefinition, bodyStart 217 '"', functionDefinition, bodyStart
160 ) 218 )
161 if docstringList: 219 if docstringList:
162 if self.getDocstringType() == "ericdoc": 220 if self.getDocstringType() == "ericdoc":
163 docstringList.insert(0, self.__quote3) 221 docstringList.insert(0, self.__quote3)
222 newCursorLine = insertLine + 1
164 else: 223 else:
165 docstringList[0] = self.__quote3 + docstringList[0] 224 docstringList[0] = self.__quote3 + docstringList[0]
225 newCursorLine = insertLine
166 docstringList.append(self.__quote3) 226 docstringList.append(self.__quote3)
167 return ( 227 return (
168 indentation + 228 indentation +
169 "{0}{1}".format(sep, indentation).join(docstringList) + 229 "{0}{1}".format(sep, indentation).join(docstringList) +
170 sep 230 sep
171 ), (insertLine, 0) 231 ), (insertLine, 0), newCursorLine
172 232
173 return "", (0, 0) 233 return "", (0, 0), 0
174 234
175 def __getFunctionDefinitionFromStart(self): 235 def __getFunctionDefinitionFromStart(self):
176 """ 236 """
177 Private method to extract the function definition based on the cursor 237 Private method to extract the function definition based on the cursor
178 being placed on the first line of the definition. 238 being placed on the first line of the definition.
265 ) 325 )
266 if docstringList: 326 if docstringList:
267 if self.__isTripleQuotesStart(lineTextToCursor): 327 if self.__isTripleQuotesStart(lineTextToCursor):
268 if self.getDocstringType() == "ericdoc": 328 if self.getDocstringType() == "ericdoc":
269 docstringList.insert(0, "") 329 docstringList.insert(0, "")
330 newCursorLine = cursorPosition[0] + 1
331 else:
332 newCursorLine = cursorPosition[0]
270 docstringList.append("") 333 docstringList.append("")
271 else: 334 else:
272 if self.getDocstringType() == "ericdoc": 335 if self.getDocstringType() == "ericdoc":
273 docstringList.insert(0, self.__quote3) 336 docstringList.insert(0, self.__quote3)
337 newCursorLine = cursorPosition[0] + 1
274 else: 338 else:
275 docstringList[0] = self.__quote3 + docstringList[0] 339 docstringList[0] = self.__quote3 + docstringList[0]
340 newCursorLine = cursorPosition[0]
276 docstringList.append(self.__quote3) 341 docstringList.append(self.__quote3)
277 docstring = ( 342 docstring = (
278 "{0}{1}".format(sep, indentation).join(docstringList) 343 "{0}{1}".format(sep, indentation).join(docstringList)
279 ) 344 )
280 return docstring, cursorPosition 345 return docstring, cursorPosition, newCursorLine
281 346
282 return "", (0, 0) 347 return "", (0, 0), 0
283 348
284 def __getFunctionDefinitionFromBelow(self, cursorPosition): 349 def __getFunctionDefinitionFromBelow(self, cursorPosition):
285 """ 350 """
286 Private method to extract the function definition based on the cursor 351 Private method to extract the function definition based on the cursor
287 being placed on the first line after the definition. 352 being placed on the first line after the definition.
311 376
312 functionTextList.insert(0, text) 377 functionTextList.insert(0, text)
313 378
314 if self.isFunctionStart(text): 379 if self.isFunctionStart(text):
315 # start of function definition reached 380 # start of function definition reached
381 self.__functionStartLine = lineNo
382
316 # check, if function is decorated with a supported one 383 # check, if function is decorated with a supported one
317 if lineNo > 0: 384 if lineNo > 0:
318 decoratorLine = self.editor.text(lineNo - 1) 385 decoratorLine = self.editor.text(lineNo - 1)
319 if ( 386 if (
320 "@classmethod" in decoratorLine or 387 "@classmethod" in decoratorLine or

eric ide

mercurial