From 0508e8f34d736822d7fd5601460f0ae3d56abc0c Mon Sep 17 00:00:00 2001 From: Le Tan Date: Wed, 15 Aug 2018 19:55:52 +0800 Subject: [PATCH] Completion: consider word separator --- src/utils/veditutils.cpp | 44 ++++++++++++++++++++++++++++++++++++++++ src/utils/veditutils.h | 2 ++ src/veditor.cpp | 6 +++--- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/utils/veditutils.cpp b/src/utils/veditutils.cpp index 14cb0f33..db7cbe7b 100644 --- a/src/utils/veditutils.cpp +++ b/src/utils/veditutils.cpp @@ -1039,3 +1039,47 @@ bool VEditUtils::isEmptyBlock(const QTextBlock &p_block) { return p_block.length() == 1; } + +// Copy from QTextEngine::atWordSeparator(int position). +bool VEditUtils::isWordSeparator(QChar p_char) +{ + switch (p_char.unicode()) { + case '.': + case ',': + case '?': + case '!': + case '@': + case '#': + case '$': + case ':': + case ';': + case '-': + case '<': + case '>': + case '[': + case ']': + case '(': + case ')': + case '{': + case '}': + case '=': + case '/': + case '+': + case '%': + case '&': + case '^': + case '*': + case '\'': + case '"': + case '`': + case '~': + case '|': + case '\\': + return true; + + default: + break; + } + + return false; +} diff --git a/src/utils/veditutils.h b/src/utils/veditutils.h index 3eee56df..ba861d12 100644 --- a/src/utils/veditutils.h +++ b/src/utils/veditutils.h @@ -209,6 +209,8 @@ public: // Whether @p_block is empty. static bool isEmptyBlock(const QTextBlock &p_block); + static bool isWordSeparator(QChar p_char); + private: VEditUtils() {} }; diff --git a/src/veditor.cpp b/src/veditor.cpp index 0689e8f8..91b3a812 100644 --- a/src/veditor.cpp +++ b/src/veditor.cpp @@ -807,7 +807,7 @@ void VEditor::replaceText(const QString &p_text, QRegExp exp; if (useRegExp) { exp = QRegExp(p_text, - p_options & FindOption::CaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + (p_options & FindOption::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive); } bool found = findTextHelper(p_text, @@ -855,7 +855,7 @@ void VEditor::replaceTextAll(const QString &p_text, QRegExp exp; if (useRegExp) { exp = QRegExp(p_text, - p_options & FindOption::CaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); + (p_options & FindOption::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive); } else { newText = p_replaceText; } @@ -1196,7 +1196,7 @@ QString VEditor::fetchCompletionPrefix() const QString prefix; while (pos >= blockPos) { QChar ch = m_document->characterAt(pos); - if (ch.isSpace()) { + if (ch.isSpace() || VEditUtils::isWordSeparator(ch)) { break; }