Exemplo n.º 1
0
    def _insertSnippet(self, body):

        if isinstance(body, list):
            txt = ""
            for i, t in enumerate(body):
                txt = body[i] + "\n"
            body = txt

        body = body.replace("\t", "    ")
        body = body.replace("$0", "")

        # Find patern ${num : text}
        # TODO: nested pattern ${2: ${3:Exception} as ${4:e}}
        d = {}
        pattern = "\$\{\d.*?}"
        match = re.findall(pattern, body)
        for fnd in match:
            fnd_split = fnd.split(":")
            # key
            key = fnd_split[0]
            key = key.replace("{", "")
            body = body.replace(fnd, key)
            # value
            value = fnd_split[1]
            value = value.replace("}", "")
            # dict
            d[key] = value

        # In snippet replace keys with values
        for i in reversed(range(10)):
            try:
                i = "$" + str(i)
                body = body.replace(i, d[i])
            except:
                pass

        # Write in the editor
        editor = pyzo.editors.getCurrentEditor()
        # cursor
        cursor = editor.textCursor()
        # current position
        pos = cursor.position()
        # insert the snippet
        cursor.insertText(body)

        # Find target text in the editor
        text = editor.toPlainText()
        pattern = "$1"

        if pattern in d:
            # find text
            regex = QtCore.QRegExp(d[pattern])
            index = regex.indexIn(text, pos)
            # select text
            cursor.setPosition(index)
            cursor.movePosition(QtGui.QTextCursor.EndOfWord, 1)
            editor.setTextCursor(cursor)
Exemplo n.º 2
0
    def find(self, forward=True, wrapAround=True):
        """ The main find method.
        Returns True if a match was found. """

        # Reset timer
        self.autoHideTimerReset()

        # get editor
        editor = self.parent().getCurrentEditor()
        if not editor:
            return

        # find flags
        flags = QtGui.QTextDocument.FindFlags()
        if self._caseCheck.isChecked():
            flags |= QtGui.QTextDocument.FindCaseSensitively
        if not forward:
            flags |= QtGui.QTextDocument.FindBackward
        #if self._wholeWord.isChecked():
        #    flags |= QtGui.QTextDocument.FindWholeWords

        # focus
        self.selectFindText()

        # get text to find
        needle = self._findText.text()
        if self._regExp.isChecked():
            #Make needle a QRegExp; speciffy case-sensitivity here since the
            #FindCaseSensitively flag is ignored when finding using a QRegExp
            needle = QtCore.QRegExp(
                needle, QtCore.Qt.CaseSensitive
                if self._caseCheck.isChecked() else QtCore.Qt.CaseInsensitive)
        elif self._wholeWord.isChecked():
            # Use regexp, because the default begaviour does not find
            # whole words correctly, see issue #276
            # it should *not* find this in this_word
            needle = QtCore.QRegExp(
                r'\b' + needle + r'\b', QtCore.Qt.CaseSensitive
                if self._caseCheck.isChecked() else QtCore.Qt.CaseInsensitive)

        # estblish start position
        cursor = editor.textCursor()
        result = editor.document().find(needle, cursor, flags)

        if not result.isNull():
            editor.setTextCursor(result)
        elif wrapAround:
            self.notifyPassBeginEnd()
            #Move cursor to start or end of document
            if forward:
                cursor.movePosition(cursor.Start)
            else:
                cursor.movePosition(cursor.End)
            #Try again
            result = editor.document().find(needle, cursor, flags)
            if not result.isNull():
                editor.setTextCursor(result)

        # done
        editor.setFocus()
        return not result.isNull()