浏览代码

BUG: ctkConsole: Fix autocomplete during insertion

This commit fixes the following issues:
- Do NOT leave anymore the word we typed when autocomplete in the case of there is something after
- Do NOT erase anymore the parentheses when autocomplete
- Leave the cursor at the end of the word completed (and don't put it at the very end).
  Moreover it put the cursor between the parentheses of the world completed

See Slicer issue http://na-mic.org/Mantis/view.php?id=4236

Co-authored-by: Jean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
Mayeul Chassagnard 8 年之前
父节点
当前提交
3467bc1075
共有 1 个文件被更改,包括 18 次插入4 次删除
  1. 18 4
      Libs/Widgets/ctkConsole.cpp

+ 18 - 4
Libs/Widgets/ctkConsole.cpp

@@ -908,6 +908,10 @@ void ctkConsolePrivate::insertCompletion(const QString& completion)
 {
 {
   Q_Q(ctkConsole);
   Q_Q(ctkConsole);
   QTextCursor tc = this->textCursor();
   QTextCursor tc = this->textCursor();
+  // save the initial cursor position
+  QTextCursor endOfCompletion = this->textCursor();
+  endOfCompletion.setPosition(tc.position());
+  // Select the previous charactere
   tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
   tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
   if (tc.selectedText()==".")
   if (tc.selectedText()==".")
     {
     {
@@ -915,18 +919,28 @@ void ctkConsolePrivate::insertCompletion(const QString& completion)
     }
     }
   else
   else
     {
     {
-    tc = this->textCursor();
+    //can't more autocomplete when cursor right after '(' or ')'
+    if (tc.selectedText()==")" || tc.selectedText()=="(")
+      {
+      return;
+      }
+    tc.clearSelection();
     tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
     tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
     tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
     tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
     tc.insertText(completion);
     tc.insertText(completion);
+    endOfCompletion.setPosition(tc.position());
     this->setTextCursor(tc);
     this->setTextCursor(tc);
     }
     }
-  tc.movePosition(QTextCursor::StartOfBlock);
-  tc.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
+
+  // Get back the whole command line to apply a cursor offset
+  // (moving cursor between parenthsesis if the completion is
+  // a callable object with more than the self argument)
+  // StartOfBlock don't catch the whole command line if multi-line statement
+  tc.movePosition(QTextCursor::StartOfBlock,QTextCursor::KeepAnchor);
   QString shellLine = tc.selectedText();
   QString shellLine = tc.selectedText();
   shellLine.replace(q->ps1(), "");
   shellLine.replace(q->ps1(), "");
   shellLine.replace(q->ps2(), "");
   shellLine.replace(q->ps2(), "");
-  tc.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
+  tc.setPosition(endOfCompletion.position());
   this->setTextCursor(tc);
   this->setTextCursor(tc);
   int cursorOffset = this->Completer->cursorOffset(shellLine);
   int cursorOffset = this->Completer->cursorOffset(shellLine);
   tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, cursorOffset);
   tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, cursorOffset);