Kaynağa Gözat

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 yıl önce
ebeveyn
işleme
3467bc1075
1 değiştirilmiş dosya ile 18 ekleme ve 4 silme
  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);
   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);
   if (tc.selectedText()==".")
     {
@@ -915,18 +919,28 @@ void ctkConsolePrivate::insertCompletion(const QString& completion)
     }
   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::EndOfWord, QTextCursor::KeepAnchor);
     tc.insertText(completion);
+    endOfCompletion.setPosition(tc.position());
     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();
   shellLine.replace(q->ps1(), "");
   shellLine.replace(q->ps2(), "");
-  tc.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
+  tc.setPosition(endOfCompletion.position());
   this->setTextCursor(tc);
   int cursorOffset = this->Completer->cursorOffset(shellLine);
   tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, cursorOffset);