浏览代码

Completion: bug fix when cursor is at the end of a word

Le Tan 7 年之前
父节点
当前提交
3f5ccf6b6e
共有 3 个文件被更改,包括 13 次插入5 次删除
  1. 10 3
      src/utils/veditutils.cpp
  2. 2 1
      src/utils/veditutils.h
  3. 1 1
      src/veditor.cpp

+ 10 - 3
src/utils/veditutils.cpp

@@ -919,14 +919,21 @@ void VEditUtils::insertTitleMark(QTextCursor &p_cursor,
 
 void VEditUtils::findCurrentWord(QTextCursor p_cursor,
                                  int &p_start,
-                                 int &p_end)
+                                 int &p_end,
+                                 bool p_findPrecedingWord)
 {
     QString text = p_cursor.block().text();
     int pib = p_cursor.positionInBlock();
 
     if (pib < text.size() && text[pib].isSpace()) {
-        p_start = p_end = p_cursor.position();
-        return;
+        if (!p_findPrecedingWord
+            || pib == 0
+            || text[pib - 1].isSpace()) {
+            p_start = p_end = p_cursor.position();
+            return;
+        }
+
+        p_cursor.movePosition(QTextCursor::PreviousCharacter);
     }
 
     p_cursor.movePosition(QTextCursor::StartOfWord);

+ 2 - 1
src/utils/veditutils.h

@@ -182,7 +182,8 @@ public:
     // @p_start will equals to @p_end if @p_cursor is a space.
     static void findCurrentWord(QTextCursor p_cursor,
                                 int &p_start,
-                                int &p_end);
+                                int &p_end,
+                                bool p_findPrecedingWord = false);
 
     // Find the start and end of the WORD @p_cursor locates in (within a single block).
     // @p_start and @p_end will be the global position of the start and end of the WORD.

+ 1 - 1
src/veditor.cpp

@@ -1187,7 +1187,7 @@ QStringList VEditor::generateCompletionCandidates() const
     QString content = getContent();
     QTextCursor cursor = textCursorW();
     int start, end;
-    VEditUtils::findCurrentWord(cursor, start, end);
+    VEditUtils::findCurrentWord(cursor, start, end, true);
 
     QRegExp reg("\\W+");