pythonfindpyqt5qscintilla

Backward search using QsciScintilla.findNext not working as expected


If I search for letter x forward (button Next), everything works as it should, but as soon as I change direction (button Previous), this happens:

ATM I'm considering translating logic for find operations from C++ to Python, thus potentially fixing the issue, but it'd be nice to know that I made some novice mistake, thus avoiding all the extra work...

Here's the code:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import *
import sys


class FindAndReplace(QWidget):
    def __init__(self, *arg, **kwarg):
        super(self.__class__, self).__init__(*arg, **kwarg)

        rows = QVBoxLayout()

        self.editor = QsciScintilla()
        self.editor.setText(f'{"x"*40}\n{"y"*40}\n{"z"*40}\n')
        rows.addWidget(self.editor)

        self.text_to_find = ''
        self.state_ = tuple()

        self.find = QLineEdit()
        self.find_previous = QPushButton('&Previous')
        self.find_next = QPushButton('&Next')
        self.find_lbl = QLabel('&Find')
        self.find_lbl.setBuddy(self.find)
        row = QHBoxLayout()
        for w in (self.find_lbl, self.find, self.find_previous, self.find_next):
            row.addWidget(w)
        rows.addLayout(row)

        self.re = QCheckBox('&Regular expressions')
        self.cs = QCheckBox('&Case sensitive')
        self.wo = QCheckBox('Whole &words')
        self.wrap = QCheckBox('Wrap aroun&d')
        self.show_ = QCheckBox('&Unfold folded text')
        self.posix = QCheckBox('POSI&X-compatible RE')
        row = QHBoxLayout()
        for w in (self.re, self.cs, self.wo, self.wrap, self.show_, self.posix):
            row.addWidget(w)
        rows.addLayout(row)

        self.setLayout(rows)

        self.find_previous.clicked.connect(lambda: self.findText(forward = False))
        self.find_next.clicked.connect(lambda: self.findText(forward = True))

    def findText(self, forward):
        text_to_find = self.find.text()
        state_ = ( \
            self.re.isChecked(), self.cs.isChecked(),
            self.wo.isChecked(), self.wrap.isChecked(),
            forward, -1, -1,
            self.show_.isChecked(), self.posix.isChecked(),
        )

        if text_to_find != self.text_to_find or state_ != self.state_:
            self.text_to_find = text_to_find
            self.state_ = state_
            # search with new conditions.
            self.editor.findFirst(text_to_find, *state_)
        else:
            # search with previously set conditions.
            self.editor.findNext()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    FindAndReplace().show()
    sys.exit(app.exec_())

Solution

  • The findNext function looks buggy to me. If I use getSelection to explicitly enter line and index in findFirst, and avoid using findNext altogether, everything works as expected:

    def findText(self, forward):
        text_to_find = self.find.text()
    
        if forward:
            line, index = self.editor.getSelection()[2:]
        else:
            line, index = self.editor.getSelection()[:2]
    
        state_ = (
            self.re.isChecked(), self.cs.isChecked(),
            self.wo.isChecked(), self.wrap.isChecked(),
            forward, line, index,
            self.show_.isChecked(), self.posix.isChecked(),
            )
    
        self.text_to_find = text_to_find
        self.state_ = state_
        self.editor.findFirst(text_to_find, *state_)
    

    Looking at the latest source code (qsciscintilla.cpp, line 1853) I see this:

    // Finally adjust the start position so that we don't find the same one again.
    if (findState.forward)
        findState.startpos = targend;
    else if ((findState.startpos = targstart - 1) < 0)
        findState.startpos = 0;
    

    I may be misunderstanding the intention of the code, but why does it subtract one here? AFAICS, this will create an off-by-one error when searching backwards.