eric6/QScintilla/TypingCompleters/CompleterPython.py

changeset 7267
aedc309827c7
parent 7229
53054eb5b15a
child 7281
8087ef6df2e4
equal deleted inserted replaced
7266:d001bc703c29 7267:aedc309827c7
59 """ 59 """
60 Public slot called to reread the configuration parameters. 60 Public slot called to reread the configuration parameters.
61 """ 61 """
62 self.setEnabled( 62 self.setEnabled(
63 Preferences.getEditorTyping("Python/EnabledTypingAids")) 63 Preferences.getEditorTyping("Python/EnabledTypingAids"))
64 self.__insertClosingBrace = \ 64 self.__insertClosingBrace = Preferences.getEditorTyping(
65 Preferences.getEditorTyping("Python/InsertClosingBrace") 65 "Python/InsertClosingBrace")
66 self.__indentBrace = \ 66 self.__indentBrace = Preferences.getEditorTyping(
67 Preferences.getEditorTyping("Python/IndentBrace") 67 "Python/IndentBrace")
68 self.__skipBrace = \ 68 self.__skipBrace = Preferences.getEditorTyping(
69 Preferences.getEditorTyping("Python/SkipBrace") 69 "Python/SkipBrace")
70 self.__insertQuote = \ 70 self.__insertQuote = Preferences.getEditorTyping(
71 Preferences.getEditorTyping("Python/InsertQuote") 71 "Python/InsertQuote")
72 self.__dedentElse = \ 72 self.__dedentElse = Preferences.getEditorTyping(
73 Preferences.getEditorTyping("Python/DedentElse") 73 "Python/DedentElse")
74 self.__dedentExcept = \ 74 self.__dedentExcept = Preferences.getEditorTyping(
75 Preferences.getEditorTyping("Python/DedentExcept") 75 "Python/DedentExcept")
76 self.__insertImport = \ 76 self.__insertImport = Preferences.getEditorTyping(
77 Preferences.getEditorTyping("Python/InsertImport") 77 "Python/InsertImport")
78 self.__importBraceType = \ 78 self.__importBraceType = Preferences.getEditorTyping(
79 Preferences.getEditorTyping("Python/ImportBraceType") 79 "Python/ImportBraceType")
80 self.__insertSelf = \ 80 self.__insertSelf = Preferences.getEditorTyping(
81 Preferences.getEditorTyping("Python/InsertSelf") 81 "Python/InsertSelf")
82 self.__insertBlank = \ 82 self.__insertBlank = Preferences.getEditorTyping(
83 Preferences.getEditorTyping("Python/InsertBlank") 83 "Python/InsertBlank")
84 self.__colonDetection = \ 84 self.__colonDetection = Preferences.getEditorTyping(
85 Preferences.getEditorTyping("Python/ColonDetection") 85 "Python/ColonDetection")
86 self.__dedentDef = \ 86 self.__dedentDef = Preferences.getEditorTyping(
87 Preferences.getEditorTyping("Python/DedentDef") 87 "Python/DedentDef")
88 88
89 def charAdded(self, charNumber): 89 def charAdded(self, charNumber):
90 """ 90 """
91 Public slot called to handle the user entering a character. 91 Public slot called to handle the user entering a character.
92 92
97 '\n', ':']: 97 '\n', ':']:
98 return # take the short route 98 return # take the short route
99 99
100 line, col = self.editor.getCursorPosition() 100 line, col = self.editor.getCursorPosition()
101 101
102 if self.__inComment(line, col) or \ 102 if (
103 (char != '"' and self.__inDoubleQuotedString()) or \ 103 self.__inComment(line, col) or
104 (char != '"' and self.__inTripleDoubleQuotedString()) or \ 104 (char != '"' and self.__inDoubleQuotedString()) or
105 (char != "'" and self.__inSingleQuotedString()) or \ 105 (char != '"' and self.__inTripleDoubleQuotedString()) or
106 (char != "'" and self.__inTripleSingleQuotedString()): 106 (char != "'" and self.__inSingleQuotedString()) or
107 (char != "'" and self.__inTripleSingleQuotedString())
108 ):
107 return 109 return
108 110
109 # open parenthesis 111 # open parenthesis
110 # insert closing parenthesis and self 112 # insert closing parenthesis and self
111 if char == '(': 113 if char == '(':
112 txt = self.editor.text(line)[:col] 114 txt = self.editor.text(line)[:col]
113 if self.__insertSelf and \ 115 if (
114 self.__defRX.exactMatch(txt): 116 self.__insertSelf and
117 self.__defRX.exactMatch(txt)
118 ):
115 if self.__isClassMethodDef(): 119 if self.__isClassMethodDef():
116 self.editor.insert('cls') 120 self.editor.insert('cls')
117 self.editor.setCursorPosition(line, col + 3) 121 self.editor.setCursorPosition(line, col + 3)
118 elif self.__isStaticMethodDef(): 122 elif self.__isStaticMethodDef():
119 # nothing to insert 123 # nothing to insert
120 pass 124 pass
121 elif self.__isClassMethod(): 125 elif self.__isClassMethod():
122 self.editor.insert('self') 126 self.editor.insert('self')
123 self.editor.setCursorPosition(line, col + 4) 127 self.editor.setCursorPosition(line, col + 4)
124 if self.__insertClosingBrace: 128 if self.__insertClosingBrace:
125 if self.__defRX.exactMatch(txt) or \ 129 if (
126 self.__classRX.exactMatch(txt): 130 self.__defRX.exactMatch(txt) or
131 self.__classRX.exactMatch(txt)
132 ):
127 self.editor.insert('):') 133 self.editor.insert('):')
128 else: 134 else:
129 self.editor.insert(')') 135 self.editor.insert(')')
130 136
131 # closing parenthesis 137 # closing parenthesis
269 while ifLine >= 0: 275 while ifLine >= 0:
270 txt = self.editor.text(ifLine) 276 txt = self.editor.text(ifLine)
271 edInd = self.editor.indentation(ifLine) 277 edInd = self.editor.indentation(ifLine)
272 if self.__elseRX.indexIn(txt) == 0 and edInd <= indentation: 278 if self.__elseRX.indexIn(txt) == 0 and edInd <= indentation:
273 indentation = edInd - 1 279 indentation = edInd - 1
274 elif self.__elifRX.indexIn(txt) == 0 and \ 280 elif (
275 edInd == indentation and \ 281 self.__elifRX.indexIn(txt) == 0 and
276 edInd == prevInd: 282 edInd == indentation and
283 edInd == prevInd
284 ):
277 indentation = edInd - 1 285 indentation = edInd - 1
278 elif (self.__ifRX.indexIn(txt) == 0 or 286 elif (
279 self.__whileRX.indexIn(txt) == 0 or 287 (self.__ifRX.indexIn(txt) == 0 or
280 self.__forRX.indexIn(txt) == 0 or 288 self.__whileRX.indexIn(txt) == 0 or
281 self.__tryRX.indexIn(txt) == 0) and \ 289 self.__forRX.indexIn(txt) == 0 or
282 edInd <= indentation: 290 self.__tryRX.indexIn(txt) == 0) and
291 edInd <= indentation
292 ):
283 self.editor.cancelList() 293 self.editor.cancelList()
284 self.editor.setIndentation(line, edInd) 294 self.editor.setIndentation(line, edInd)
285 break 295 break
286 ifLine -= 1 296 ifLine -= 1
287 297
296 indentation = self.editor.indentation(line) 306 indentation = self.editor.indentation(line)
297 tryLine = line - 1 307 tryLine = line - 1
298 while tryLine >= 0: 308 while tryLine >= 0:
299 txt = self.editor.text(tryLine) 309 txt = self.editor.text(tryLine)
300 edInd = self.editor.indentation(tryLine) 310 edInd = self.editor.indentation(tryLine)
301 if (self.__exceptcRX.indexIn(txt) == 0 or 311 if (
302 self.__finallyRX.indexIn(txt) == 0) and \ 312 (self.__exceptcRX.indexIn(txt) == 0 or
303 edInd <= indentation: 313 self.__finallyRX.indexIn(txt) == 0) and
314 edInd <= indentation
315 ):
304 indentation = edInd - 1 316 indentation = edInd - 1
305 elif (self.__exceptRX.indexIn(txt) == 0 or 317 elif (self.__exceptRX.indexIn(txt) == 0 or
306 self.__tryRX.indexIn(txt) == 0) and edInd <= indentation: 318 self.__tryRX.indexIn(txt) == 0) and edInd <= indentation:
307 self.editor.cancelList() 319 self.editor.cancelList()
308 self.editor.setIndentation(line, edInd) 320 self.editor.setIndentation(line, edInd)
320 while tryLine >= 0: 332 while tryLine >= 0:
321 txt = self.editor.text(tryLine) 333 txt = self.editor.text(tryLine)
322 edInd = self.editor.indentation(tryLine) 334 edInd = self.editor.indentation(tryLine)
323 if self.__finallyRX.indexIn(txt) == 0 and edInd <= indentation: 335 if self.__finallyRX.indexIn(txt) == 0 and edInd <= indentation:
324 indentation = edInd - 1 336 indentation = edInd - 1
325 elif (self.__tryRX.indexIn(txt) == 0 or 337 elif (
326 self.__exceptcRX.indexIn(txt) == 0 or 338 (self.__tryRX.indexIn(txt) == 0 or
327 self.__exceptRX.indexIn(txt) == 0) and \ 339 self.__exceptcRX.indexIn(txt) == 0 or
328 edInd <= indentation: 340 self.__exceptRX.indexIn(txt) == 0) and
341 edInd <= indentation
342 ):
329 self.editor.cancelList() 343 self.editor.cancelList()
330 self.editor.setIndentation(line, edInd) 344 self.editor.setIndentation(line, edInd)
331 break 345 break
332 tryLine -= 1 346 tryLine -= 1
333 347
344 edInd = self.editor.indentation(tryLine) 358 edInd = self.editor.indentation(tryLine)
345 newInd = -1 359 newInd = -1
346 if self.__defRX.indexIn(txt) == 0 and edInd < indentation: 360 if self.__defRX.indexIn(txt) == 0 and edInd < indentation:
347 newInd = edInd 361 newInd = edInd
348 elif self.__classRX.indexIn(txt) == 0 and edInd < indentation: 362 elif self.__classRX.indexIn(txt) == 0 and edInd < indentation:
349 newInd = edInd + \ 363 newInd = edInd + (
350 (self.editor.indentationWidth() or self.editor.tabWidth()) 364 self.editor.indentationWidth() or self.editor.tabWidth()
365 )
351 if newInd >= 0: 366 if newInd >= 0:
352 self.editor.cancelList() 367 self.editor.cancelList()
353 self.editor.setIndentation(line, newInd) 368 self.editor.setIndentation(line, newInd)
354 break 369 break
355 tryLine -= 1 370 tryLine -= 1
363 line, col = self.editor.getCursorPosition() 378 line, col = self.editor.getCursorPosition()
364 indentation = self.editor.indentation(line) 379 indentation = self.editor.indentation(line)
365 curLine = line - 1 380 curLine = line - 1
366 while curLine >= 0: 381 while curLine >= 0:
367 txt = self.editor.text(curLine) 382 txt = self.editor.text(curLine)
368 if (self.__defSelfRX.indexIn(txt) == 0 or 383 if (
369 self.__defClsRX.indexIn(txt) == 0) and \ 384 (self.__defSelfRX.indexIn(txt) == 0 or
370 self.editor.indentation(curLine) == indentation: 385 self.__defClsRX.indexIn(txt) == 0) and
386 self.editor.indentation(curLine) == indentation
387 ):
371 return True 388 return True
372 elif self.__classRX.indexIn(txt) == 0 and \ 389 elif (
373 self.editor.indentation(curLine) < indentation: 390 self.__classRX.indexIn(txt) == 0 and
391 self.editor.indentation(curLine) < indentation
392 ):
374 return True 393 return True
375 elif self.__defRX.indexIn(txt) == 0 and \ 394 elif (
376 self.editor.indentation(curLine) <= indentation: 395 self.__defRX.indexIn(txt) == 0 and
396 self.editor.indentation(curLine) <= indentation
397 ):
377 return False 398 return False
378 curLine -= 1 399 curLine -= 1
379 return False 400 return False
380 401
381 def __isClassMethodDef(self): 402 def __isClassMethodDef(self):
386 @return flag indicating the definition of a class method (boolean) 407 @return flag indicating the definition of a class method (boolean)
387 """ 408 """
388 line, col = self.editor.getCursorPosition() 409 line, col = self.editor.getCursorPosition()
389 indentation = self.editor.indentation(line) 410 indentation = self.editor.indentation(line)
390 curLine = line - 1 411 curLine = line - 1
391 if self.__classmethodRX.indexIn(self.editor.text(curLine)) == 0 and \ 412 if (
392 self.editor.indentation(curLine) == indentation: 413 self.__classmethodRX.indexIn(self.editor.text(curLine)) == 0 and
414 self.editor.indentation(curLine) == indentation
415 ):
393 return True 416 return True
394 return False 417 return False
395 418
396 def __isStaticMethodDef(self): 419 def __isStaticMethodDef(self):
397 """ 420 """
401 @return flag indicating the definition of a static method (boolean) 424 @return flag indicating the definition of a static method (boolean)
402 """ 425 """
403 line, col = self.editor.getCursorPosition() 426 line, col = self.editor.getCursorPosition()
404 indentation = self.editor.indentation(line) 427 indentation = self.editor.indentation(line)
405 curLine = line - 1 428 curLine = line - 1
406 if self.__staticmethodRX.indexIn(self.editor.text(curLine)) == 0 and \ 429 if (
407 self.editor.indentation(curLine) == indentation: 430 self.__staticmethodRX.indexIn(self.editor.text(curLine)) == 0 and
431 self.editor.indentation(curLine) == indentation
432 ):
408 return True 433 return True
409 return False 434 return False
410 435
411 def __inComment(self, line, col): 436 def __inComment(self, line, col):
412 """ 437 """
441 quoted string. 466 quoted string.
442 467
443 @return flag indicating, if the cursor is inside a triple double 468 @return flag indicating, if the cursor is inside a triple double
444 quoted string (boolean) 469 quoted string (boolean)
445 """ 470 """
446 return self.editor.currentStyle() == \ 471 return (
472 self.editor.currentStyle() ==
447 QsciLexerPython.TripleDoubleQuotedString 473 QsciLexerPython.TripleDoubleQuotedString
474 )
448 475
449 def __inSingleQuotedString(self): 476 def __inSingleQuotedString(self):
450 """ 477 """
451 Private method to check, if the cursor is within a single quoted 478 Private method to check, if the cursor is within a single quoted
452 string. 479 string.
462 quoted string. 489 quoted string.
463 490
464 @return flag indicating, if the cursor is inside a triple single 491 @return flag indicating, if the cursor is inside a triple single
465 quoted string (boolean) 492 quoted string (boolean)
466 """ 493 """
467 return self.editor.currentStyle() == \ 494 return (
495 self.editor.currentStyle() ==
468 QsciLexerPython.TripleSingleQuotedString 496 QsciLexerPython.TripleSingleQuotedString
497 )

eric ide

mercurial