diff -r 9eed155411f0 -r 4a1db75550bd eric6/QScintilla/TypingCompleters/CompleterPython.py --- a/eric6/QScintilla/TypingCompleters/CompleterPython.py Sat Oct 10 16:03:53 2020 +0200 +++ b/eric6/QScintilla/TypingCompleters/CompleterPython.py Sun Oct 11 17:54:52 2020 +0200 @@ -7,15 +7,14 @@ Module implementing a typing completer for Python. """ - import re -from PyQt5.QtCore import QRegExp from PyQt5.Qsci import QsciLexerPython, QsciScintilla from .CompleterBase import CompleterBase import Preferences +from Utilities import rxIndex class CompleterPython(CompleterBase): @@ -31,27 +30,27 @@ """ super(CompleterPython, self).__init__(editor, parent) - self.__defRX = QRegExp(r"""^[ \t]*def \w+\(""") - self.__defSelfRX = QRegExp(r"""^[ \t]*def \w+\([ \t]*self[ \t]*[,)]""") - self.__defClsRX = QRegExp(r"""^[ \t]*def \w+\([ \t]*cls[ \t]*[,)]""") - self.__classRX = QRegExp(r"""^[ \t]*class \w+\(""") - self.__importRX = QRegExp(r"""^[ \t]*from [\w.]+ """) - self.__classmethodRX = QRegExp(r"""^[ \t]*@classmethod""") - self.__staticmethodRX = QRegExp(r"""^[ \t]*@staticmethod""") + self.__defRX = re.compile(r"^[ \t]*def \w+\(") + self.__defSelfRX = re.compile(r"^[ \t]*def \w+\([ \t]*self[ \t]*[,)]") + self.__defClsRX = re.compile(r"^[ \t]*def \w+\([ \t]*cls[ \t]*[,)]") + self.__classRX = re.compile(r"^[ \t]*class \w+\(") + self.__importRX = re.compile(r"^[ \t]*from [\w.]+ ") + self.__classmethodRX = re.compile(r"^[ \t]*@classmethod") + self.__staticmethodRX = re.compile(r"^[ \t]*@staticmethod") - self.__defOnlyRX = QRegExp(r"""^[ \t]*def """) + self.__defOnlyRX = re.compile(r"^[ \t]*def ") - self.__ifRX = QRegExp(r"""^[ \t]*if """) - self.__elifRX = QRegExp(r"""^[ \t]*elif """) - self.__elseRX = QRegExp(r"""^[ \t]*else:""") + self.__ifRX = re.compile(r"^[ \t]*if ") + self.__elifRX = re.compile(r"^[ \t]*elif ") + self.__elseRX = re.compile(r"^[ \t]*else:") - self.__tryRX = QRegExp(r"""^[ \t]*try:""") - self.__finallyRX = QRegExp(r"""^[ \t]*finally:""") - self.__exceptRX = QRegExp(r"""^[ \t]*except """) - self.__exceptcRX = QRegExp(r"""^[ \t]*except:""") + self.__tryRX = re.compile(r"^[ \t]*try:") + self.__finallyRX = re.compile(r"^[ \t]*finally:") + self.__exceptRX = re.compile(r"^[ \t]*except ") + self.__exceptcRX = re.compile(r"^[ \t]*except:") - self.__whileRX = QRegExp(r"""^[ \t]*while """) - self.__forRX = QRegExp(r"""^[ \t]*for """) + self.__whileRX = re.compile(r"^[ \t]*while ") + self.__forRX = re.compile(r"^[ \t]*for ") self.readSettings() @@ -114,7 +113,7 @@ txt = self.editor.text(line)[:col] if ( self.__insertSelf and - self.__defRX.exactMatch(txt) + self.__defRX.fullmatch(txt) is not None ): if self.__isClassMethodDef(): self.editor.insert('cls') @@ -127,8 +126,8 @@ self.editor.setCursorPosition(line, col + 4) if self.__insertClosingBrace: if ( - self.__defRX.exactMatch(txt) or - self.__classRX.exactMatch(txt) + self.__defRX.fullmatch(txt) is not None or + self.__classRX.fullmatch(txt) is not None ): self.editor.insert('):') else: @@ -148,18 +147,18 @@ # dedent def elif char == ' ': txt = self.editor.text(line)[:col] - if self.__insertImport and self.__importRX.exactMatch(txt): + if self.__insertImport and self.__importRX.fullmatch(txt): if self.__importBraceType: self.editor.insert('import ()') self.editor.setCursorPosition(line, col + 8) else: self.editor.insert('import ') self.editor.setCursorPosition(line, col + 7) - elif self.__dedentElse and self.__elifRX.exactMatch(txt): + elif self.__dedentElse and self.__elifRX.fullmatch(txt): self.__dedentToIf() - elif self.__dedentExcept and self.__exceptRX.exactMatch(txt): + elif self.__dedentExcept and self.__exceptRX.fullmatch(txt): self.__dedentExceptToTry(False) - elif self.__dedentDef and self.__defOnlyRX.exactMatch(txt): + elif self.__dedentDef and self.__defOnlyRX.fullmatch(txt): self.__dedentDefStatement() # comma @@ -203,11 +202,11 @@ self.editor.removeSelectedText() else: txt = text[:col] - if self.__dedentElse and self.__elseRX.exactMatch(txt): + if self.__dedentElse and self.__elseRX.fullmatch(txt): self.__dedentElseToIfWhileForTry() - elif self.__dedentExcept and self.__exceptcRX.exactMatch(txt): + elif self.__dedentExcept and self.__exceptcRX.fullmatch(txt): self.__dedentExceptToTry(True) - elif self.__dedentExcept and self.__finallyRX.exactMatch(txt): + elif self.__dedentExcept and self.__finallyRX.fullmatch(txt): self.__dedentFinallyToTry() # new line @@ -253,10 +252,10 @@ while ifLine >= 0: txt = self.editor.text(ifLine) edInd = self.editor.indentation(ifLine) - if self.__elseRX.indexIn(txt) == 0 and edInd <= indentation: + if rxIndex(self.__elseRX, txt) == 0 and edInd <= indentation: indentation = edInd - 1 - elif (self.__ifRX.indexIn(txt) == 0 or - self.__elifRX.indexIn(txt) == 0) and edInd <= indentation: + elif (rxIndex(self.__ifRX, txt) == 0 or + rxIndex(self.__elifRX, txt) == 0) and edInd <= indentation: self.editor.cancelList() self.editor.setIndentation(line, edInd) break @@ -275,19 +274,19 @@ while ifLine >= 0: txt = self.editor.text(ifLine) edInd = self.editor.indentation(ifLine) - if self.__elseRX.indexIn(txt) == 0 and edInd <= indentation: + if rxIndex(self.__elseRX, txt) == 0 and edInd <= indentation: indentation = edInd - 1 elif ( - self.__elifRX.indexIn(txt) == 0 and + rxIndex(self.__elifRX, txt) == 0 and edInd == indentation and edInd == prevInd ): indentation = edInd - 1 elif ( - (self.__ifRX.indexIn(txt) == 0 or - self.__whileRX.indexIn(txt) == 0 or - self.__forRX.indexIn(txt) == 0 or - self.__tryRX.indexIn(txt) == 0) and + (rxIndex(self.__ifRX, txt) == 0 or + rxIndex(self.__whileRX, txt) == 0 or + rxIndex(self.__forRX, txt) == 0 or + rxIndex(self.__tryRX, txt) == 0) and edInd <= indentation ): self.editor.cancelList() @@ -309,13 +308,13 @@ txt = self.editor.text(tryLine) edInd = self.editor.indentation(tryLine) if ( - (self.__exceptcRX.indexIn(txt) == 0 or - self.__finallyRX.indexIn(txt) == 0) and + (rxIndex(self.__exceptcRX, txt) == 0 or + rxIndex(self.__finallyRX, txt) == 0) and edInd <= indentation ): indentation = edInd - 1 - elif (self.__exceptRX.indexIn(txt) == 0 or - self.__tryRX.indexIn(txt) == 0) and edInd <= indentation: + elif (rxIndex(self.__exceptRX, txt) == 0 or + rxIndex(self.__tryRX, txt) == 0) and edInd <= indentation: self.editor.cancelList() self.editor.setIndentation(line, edInd) break @@ -332,12 +331,12 @@ while tryLine >= 0: txt = self.editor.text(tryLine) edInd = self.editor.indentation(tryLine) - if self.__finallyRX.indexIn(txt) == 0 and edInd <= indentation: + if rxIndex(self.__finallyRX, txt) == 0 and edInd <= indentation: indentation = edInd - 1 elif ( - (self.__tryRX.indexIn(txt) == 0 or - self.__exceptcRX.indexIn(txt) == 0 or - self.__exceptRX.indexIn(txt) == 0) and + (rxIndex(self.__tryRX, txt) == 0 or + rxIndex(self.__exceptcRX, txt) == 0 or + rxIndex(self.__exceptRX, txt) == 0) and edInd <= indentation ): self.editor.cancelList() @@ -357,9 +356,9 @@ txt = self.editor.text(tryLine) edInd = self.editor.indentation(tryLine) newInd = -1 - if self.__defRX.indexIn(txt) == 0 and edInd < indentation: + if rxIndex(self.__defRX, txt) == 0 and edInd < indentation: newInd = edInd - elif self.__classRX.indexIn(txt) == 0 and edInd < indentation: + elif rxIndex(self.__classRX, txt) == 0 and edInd < indentation: newInd = edInd + ( self.editor.indentationWidth() or self.editor.tabWidth() ) @@ -381,18 +380,18 @@ while curLine >= 0: txt = self.editor.text(curLine) if ( - (self.__defSelfRX.indexIn(txt) == 0 or - self.__defClsRX.indexIn(txt) == 0) and + (rxIndex(self.__defSelfRX, txt) == 0 or + rxIndex(self.__defClsRX, txt) == 0) and self.editor.indentation(curLine) == indentation ): return True elif ( - self.__classRX.indexIn(txt) == 0 and + rxIndex(self.__classRX, txt) == 0 and self.editor.indentation(curLine) < indentation ): return True elif ( - self.__defRX.indexIn(txt) == 0 and + rxIndex(self.__defRX, txt) == 0 and self.editor.indentation(curLine) <= indentation ): return False @@ -410,7 +409,7 @@ indentation = self.editor.indentation(line) curLine = line - 1 if ( - self.__classmethodRX.indexIn(self.editor.text(curLine)) == 0 and + rxIndex(self.__classmethodRX, self.editor.text(curLine)) == 0 and self.editor.indentation(curLine) == indentation ): return True @@ -427,7 +426,7 @@ indentation = self.editor.indentation(line) curLine = line - 1 if ( - self.__staticmethodRX.indexIn(self.editor.text(curLine)) == 0 and + rxIndex(self.__staticmethodRX, self.editor.text(curLine)) == 0 and self.editor.indentation(curLine) == indentation ): return True