Browse Source

Editor: refine completion in reverse case

Remove duplicates after reversing the list.
Le Tan 7 years ago
parent
commit
d014842bbf
2 changed files with 32 additions and 7 deletions
  1. 31 6
      src/veditor.cpp
  2. 1 1
      src/veditor.h

+ 31 - 6
src/veditor.cpp

@@ -1504,7 +1504,7 @@ void VEditor::requestCompletion(bool p_reversed)
     cursor.clearSelection();
     setTextCursorW(cursor);
 
-    QStringList words = generateCompletionCandidates();
+    QStringList words = generateCompletionCandidates(p_reversed);
     QString prefix = fetchCompletionPrefix();
     // Smart case.
     Qt::CaseSensitivity cs = completionCaseSensitivity(prefix);
@@ -1526,7 +1526,7 @@ bool VEditor::isCompletionActivated() const
     return false;
 }
 
-QStringList VEditor::generateCompletionCandidates() const
+QStringList VEditor::generateCompletionCandidates(bool p_reversed) const
 {
     QString content = getContent();
     QTextCursor cursor = textCursorW();
@@ -1535,10 +1535,35 @@ QStringList VEditor::generateCompletionCandidates() const
 
     QRegExp reg("\\W+");
 
-    QStringList ret = content.mid(end).split(reg, QString::SkipEmptyParts);
-    ret.append(content.left(start).split(reg, QString::SkipEmptyParts));
-    ret.removeDuplicates();
-    return ret;
+    QStringList above = content.left(start).split(reg, QString::SkipEmptyParts);
+    QStringList below = content.mid(end).split(reg, QString::SkipEmptyParts);
+
+    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

+ 1 - 1
src/veditor.h

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