Editor: refine completion in reverse case

Remove duplicates after reversing the list.
This commit is contained in:
Le Tan 2018-09-29 18:37:12 +08:00
parent d27d20c4ea
commit d014842bbf
2 changed files with 32 additions and 7 deletions

View File

@ -1504,7 +1504,7 @@ void VEditor::requestCompletion(bool p_reversed)
cursor.clearSelection(); cursor.clearSelection();
setTextCursorW(cursor); setTextCursorW(cursor);
QStringList words = generateCompletionCandidates(); QStringList words = generateCompletionCandidates(p_reversed);
QString prefix = fetchCompletionPrefix(); QString prefix = fetchCompletionPrefix();
// Smart case. // Smart case.
Qt::CaseSensitivity cs = completionCaseSensitivity(prefix); Qt::CaseSensitivity cs = completionCaseSensitivity(prefix);
@ -1526,7 +1526,7 @@ bool VEditor::isCompletionActivated() const
return false; return false;
} }
QStringList VEditor::generateCompletionCandidates() const QStringList VEditor::generateCompletionCandidates(bool p_reversed) const
{ {
QString content = getContent(); QString content = getContent();
QTextCursor cursor = textCursorW(); QTextCursor cursor = textCursorW();
@ -1535,10 +1535,35 @@ QStringList VEditor::generateCompletionCandidates() const
QRegExp reg("\\W+"); QRegExp reg("\\W+");
QStringList ret = content.mid(end).split(reg, QString::SkipEmptyParts); QStringList above = content.left(start).split(reg, QString::SkipEmptyParts);
ret.append(content.left(start).split(reg, QString::SkipEmptyParts)); QStringList below = content.mid(end).split(reg, QString::SkipEmptyParts);
ret.removeDuplicates();
return ret; if (p_reversed) {
QStringList rev;
rev.reserve(above.size() + below.size());
for (auto it = above.rbegin(); it != above.rend(); ++it) {
rev.append(*it);
}
for (auto it = below.rbegin(); it != below.rend(); ++it) {
rev.append(*it);
}
rev.removeDuplicates();
QStringList res;
res.reserve(rev.size());
for (auto it = rev.rbegin(); it != rev.rend(); ++it) {
res.append(*it);
}
return res;
} else {
// below + above.
below.append(above);
below.removeDuplicates();
return below;
}
} }
QString VEditor::fetchCompletionPrefix() const QString VEditor::fetchCompletionPrefix() const

View File

@ -463,7 +463,7 @@ private:
// Highlight @p_cursor as the searched keyword under cursor. // Highlight @p_cursor as the searched keyword under cursor.
void highlightSearchedWordUnderCursor(const QTextCursor &p_cursor); void highlightSearchedWordUnderCursor(const QTextCursor &p_cursor);
QStringList generateCompletionCandidates() const; QStringList generateCompletionCandidates(bool p_reversed) const;
void cleanUp(); void cleanUp();