Bladeren bron

ENH: ctkPythonConsole: Enable cursorOffset on multiline statements

This commit enable the method on lines such as >>> mainAttrs =dir().

This commit is based on commit 934fcf5 originally associated with
topic python-autocomplete-parameter-detection from Christopher
Mullins.

It fixes issues reported in http://na-mic.org/Mantis/view.php?id=1227

Co-authored-by: Mayeul Chassagnard <mayeul.chassagnard@kitware.com>
Co-authored-by: Jean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
Christopher Mullins 9 jaren geleden
bovenliggende
commit
35ede172cf
2 gewijzigde bestanden met toevoegingen van 16 en 2 verwijderingen
  1. 15 2
      Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp
  2. 1 0
      Libs/Widgets/ctkConsole.cpp

+ 15 - 2
Libs/Scripting/Python/Widgets/ctkPythonConsole.cpp

@@ -113,7 +113,21 @@ int ctkPythonConsoleCompleter::cursorOffset(const QString& completion)
   if (allTextFromShell.contains("()"))
     {
     allTextFromShell.replace("()", "");
-    QStringList lineSplit = allTextFromShell.split(".", QString::KeepEmptyParts);
+    // Search backward through the string for usable characters
+    QString currentCompletionText;
+    for (int i = allTextFromShell.length()-1; i >= 0; --i)
+      {
+      QChar c = allTextFromShell.at(i);
+      if (c.isLetterOrNumber() || c == '.' || c == '_')
+        {
+        currentCompletionText.prepend(c);
+        }
+      else
+        {
+        break;
+        }
+      }
+    QStringList lineSplit = currentCompletionText.split(".", QString::KeepEmptyParts);
     QString functionName = lineSplit.at(lineSplit.length()-1);
     QStringList builtinFunctionPath = QStringList() << "__main__" << "__builtins__";
     QStringList userDefinedFunctionPath = QStringList() << "__main__";
@@ -156,7 +170,6 @@ bool ctkPythonConsoleCompleter::isBuiltInFunction(const QString &pythonFunctionN
 int ctkPythonConsoleCompleter::parameterCountBuiltInFunction(const QString& pythonFunctionName)
 {
   int parameterCount = 0;
-  qDebug() << "In parameterCountBuiltInFunction";
   PyObject* pFunction = this->PythonManager.pythonModule(pythonFunctionName);
   if (pFunction && PyObject_HasAttrString(pFunction, "__doc__"))
     {

+ 1 - 0
Libs/Widgets/ctkConsole.cpp

@@ -756,6 +756,7 @@ void ctkConsolePrivate::insertCompletion(const QString& completion)
   tc.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
   QString shellLine = tc.selectedText();
   shellLine.replace(q->ps1(), "");
+  shellLine.replace(q->ps2(), "");
   tc.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor);
   this->setTextCursor(tc);
   int cursorOffset = this->Completer->cursorOffset(shellLine);