fakevim: slightly better support for regular expressions in search patterns

This commit is contained in:
hjk
2010-08-16 16:46:32 +02:00
parent a8747f32e4
commit 362c41be5b

View File

@@ -3569,9 +3569,14 @@ bool FakeVimHandler::Private::handleExPluginCommand(const ExCommand &cmd)
static void vimPatternToQtPattern(QString *needle, QTextDocument::FindFlags *flags) static void vimPatternToQtPattern(QString *needle, QTextDocument::FindFlags *flags)
{ {
// FIXME: Rough mapping of a common case // FIXME: Rough mapping of a common case.
if (needle->startsWith(_("\\<")) && needle->endsWith(_("\\>"))) if (needle->startsWith(_("\\<")) && needle->endsWith(_("\\>")))
(*flags) |= QTextDocument::FindWholeWords; (*flags) |= QTextDocument::FindWholeWords;
// Half-hearted attempt at removing pitfalls.
if (needle->startsWith(_(".*")))
*needle = needle->mid(2);
if (needle->endsWith(_(".*")))
*needle = needle->left(needle->size() - 2);
needle->remove(_("\\<")); // start of word needle->remove(_("\\<")); // start of word
needle->remove(_("\\>")); // end of word needle->remove(_("\\>")); // end of word
//qDebug() << "NEEDLE " << needle0 << needle; //qDebug() << "NEEDLE " << needle0 << needle;
@@ -3631,10 +3636,11 @@ void FakeVimHandler::Private::search(const SearchData &sd)
sd.forward ? ++startPos : --startPos; sd.forward ? ++startPos : --startPos;
m_searchCursor = QTextCursor(); m_searchCursor = QTextCursor();
QTextCursor tc = m_tc.document()->find(needle, startPos, flags); QRegExp needleExp(needle);
QTextCursor tc = m_tc.document()->find(needleExp, startPos, flags);
if (tc.isNull()) { if (tc.isNull()) {
int startPos = sd.forward ? 0 : lastPositionInDocument(); int startPos = sd.forward ? 0 : lastPositionInDocument();
tc = m_tc.document()->find(needle, startPos, flags); tc = m_tc.document()->find(needleExp, startPos, flags);
if (tc.isNull()) { if (tc.isNull()) {
if (!incSearch) { if (!incSearch) {
highlightMatches(QString()); highlightMatches(QString());
@@ -3652,6 +3658,7 @@ void FakeVimHandler::Private::search(const SearchData &sd)
} }
// Set Cursor. // Set Cursor.
const int size = tc.position() - tc.anchor();
tc.setPosition(qMin(tc.position(), tc.anchor()), MoveAnchor); tc.setPosition(qMin(tc.position(), tc.anchor()), MoveAnchor);
tc.clearSelection(); tc.clearSelection();
m_tc = tc; m_tc = tc;
@@ -3664,7 +3671,7 @@ void FakeVimHandler::Private::search(const SearchData &sd)
if (incSearch && sd.highlightCursor) { if (incSearch && sd.highlightCursor) {
m_searchCursor = m_tc; m_searchCursor = m_tc;
m_searchCursor.setPosition(m_tc.position(), MoveAnchor); m_searchCursor.setPosition(m_tc.position(), MoveAnchor);
m_searchCursor.setPosition(m_tc.position() + needle.size(), KeepAnchor); m_searchCursor.setPosition(m_tc.position() + size, KeepAnchor);
} }
setTargetColumn(); setTargetColumn();
@@ -3689,8 +3696,9 @@ void FakeVimHandler::Private::highlightMatches(const QString &needle0)
QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively; QTextDocument::FindFlags flags = QTextDocument::FindCaseSensitively;
QString needle = needle0; QString needle = needle0;
vimPatternToQtPattern(&needle, &flags); vimPatternToQtPattern(&needle, &flags);
QRegExp needleExp(needle);
while (1) { while (1) {
tc = tc.document()->find(needle, tc.position(), flags); tc = tc.document()->find(needleExp, tc.position(), flags);
if (tc.isNull()) if (tc.isNull())
break; break;
QTextEdit::ExtraSelection sel; QTextEdit::ExtraSelection sel;