Commit 0d1381aa authored by Igor Kushnir's avatar Igor Kushnir
Browse files

Deduplicate consistsOfWhitespace()

Optimize ILanguageSupport::suggestedReparseDelayForChange() along the
way by not checking the `joinedWord` condition if the change contains
non-whitespace characters.

Simply remove unused CodeCompletionContext::containsOnlySpaces().
parent 65f4b23e
......@@ -10,6 +10,8 @@
#include <QString>
#include <QVarLengthArray>
#include <algorithm>
namespace {
bool endsWithWordBoundary(QStringView str)
{
......@@ -105,6 +107,13 @@ int skipComment(QStringView str, int pos)
} // unnamed namespace
namespace KDevelop {
bool consistsOfWhitespace(QStringView str)
{
return std::all_of(str.cbegin(), str.cend(), [](QChar c) {
return c.isSpace();
});
}
class ParamIteratorPrivate
{
Q_DISABLE_COPY_MOVE(ParamIteratorPrivate)
......
......@@ -19,6 +19,11 @@ class QStringView;
namespace KDevelop {
class ParamIteratorPrivate;
/**
* @return true if QChar::isSpace() returns true for all elements of @p str.
*/
bool KDEVPLATFORMLANGUAGE_EXPORT consistsOfWhitespace(QStringView str);
/**
* Searches a fitting closing brace from left to right: a ')' for '(', ']' for '[', ...
*/
......
......@@ -7,6 +7,7 @@
#include "ilanguagesupport.h"
#include "../duchain/duchain.h"
#include "../duchain/stringhelpers.h"
#include <QReadWriteLock>
......@@ -93,13 +94,9 @@ int ILanguageSupport::suggestedReparseDelayForChange(KTextEditor::Document* doc,
const KTextEditor::Range& changedRange,
const QString& /*removedText*/, bool /*removal*/) const
{
auto text = doc->text(changedRange);
bool joinedWord = doc->wordRangeAt(changedRange.start()).isEmpty() ||
doc->wordRangeAt(changedRange.end()).isEmpty();
auto isWhitespace = std::all_of(text.begin(), text.end(), [](const QChar& c) {
return c.isSpace();
});
return (isWhitespace && !joinedWord) ? NoUpdateRequired : DefaultDelay;
const auto joinedWord = [doc, &changedRange] {
return doc->wordRangeAt(changedRange.start()).isEmpty() || doc->wordRangeAt(changedRange.end()).isEmpty();
};
return consistsOfWhitespace(doc->text(changedRange)) && !joinedWord() ? NoUpdateRequired : DefaultDelay;
}
}
......@@ -18,6 +18,7 @@
#include <language/duchain/topducontext.h>
#include <language/duchain/duchainutils.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/stringhelpers.h>
#include <KTextEditor/View>
#include <KTextEditor/Document>
......@@ -28,11 +29,6 @@ using namespace KDevelop;
namespace {
bool isSpaceOnly(const QString& string)
{
return std::find_if(string.begin(), string.end(), [] (const QChar c) { return !c.isSpace(); }) == string.end();
}
bool includePathCompletionRequired(const QString& text)
{
const auto properties = IncludePathProperties::parseText(text);
......@@ -173,7 +169,7 @@ bool ClangCodeCompletionModel::shouldStartCompletion(KTextEditor::View* view, co
{
const QString noCompletionAfter = QStringLiteral(";{}]) ");
if (inserted.isEmpty() || isSpaceOnly(inserted)) {
if (inserted.isEmpty() || consistsOfWhitespace(inserted)) {
return false;
}
const auto lastChar = inserted.at(inserted.size() - 1);
......
......@@ -491,12 +491,4 @@ DeclarationPointer CodeCompletionContext::declarationAtEndOfString(const QString
return visitor.lastDeclaration();
}
bool CodeCompletionContext::containsOnlySpaces(const QString& str)
{
return std::all_of(str.begin(), str.end(), [](const QChar c) {
return c.isSpace();
});
}
}
......@@ -81,7 +81,6 @@ private:
KDevelop::Stack<ExpressionStackEntry> expressionStack(const QString& expression); /*!< @see ExpressionStackEntry */
KDevelop::DeclarationPointer declarationAtEndOfString(const QString& expression);
bool containsOnlySpaces(const QString &str);
private:
enum CompletionKind {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment