[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdevplatform] /: Add assistants to the "argument hints" code completion
From:       Olivier JG <olivier.jg () gmail ! com>
Date:       2015-10-10 20:32:42
Message-ID: E1Zl0os-0004E2-4L () scm ! kde ! org
[Download RAW message or body]

Git commit 1647070385ab0e5355f639cc82f94bfad6fa51d7 by Olivier JG.
Committed on 10/10/2015 at 20:36.
Pushed by olivierjg into branch 'master'.

Add assistants to the "argument hints" code completion

M  +106  -0    language/codecompletion/codecompletionmodel.cpp
M  +4    -0    language/codecompletion/codecompletionmodel.h
M  +7    -0    shell/uicontroller.cpp

http://commits.kde.org/kdevplatform/1647070385ab0e5355f639cc82f94bfad6fa51d7

diff --git a/language/codecompletion/codecompletionmodel.cpp \
b/language/codecompletion/codecompletionmodel.cpp index 6ee981b..4bf0c14 100644
--- a/language/codecompletion/codecompletionmodel.cpp
+++ b/language/codecompletion/codecompletionmodel.cpp
@@ -31,6 +31,9 @@
 #include <ktexteditor/view.h>
 #include <ktexteditor/document.h>
 
+#include <language/assistant/staticassistant.h>
+#include <language/assistant/staticassistantsmanager.h>
+
 #include "../duchain/declaration.h"
 #include "../duchain/classfunctiondeclaration.h"
 #include "../duchain/ducontext.h"
@@ -56,6 +59,73 @@ using namespace KTextEditor;
 //Multi-threaded completion creates some multi-threading related crashes, and \
sometimes shows the completions in the wrong position if the cursor was moved  // \
#define SINGLE_THREADED_COMPLETION  
+namespace {
+
+class AssistantItem : public KDevelop::CompletionTreeItem
+{
+public:
+    AssistantItem(const KDevelop::IAssistantAction::Ptr& action)
+        : CompletionTreeItem()
+        , m_action(action)
+    {
+        Q_ASSERT(action);
+    }
+
+    QVariant data(const QModelIndex& index, int role, const \
KDevelop::CodeCompletionModel*) const override +    {
+        switch (role) {
+        case Qt::DisplayRole:
+            if (index.column() == KTextEditor::CodeCompletionModel::Name)
+                return m_action->description();
+            break;
+        }
+        return QVariant();
+    }
+
+    int argumentHintDepth() const override
+    {
+        return 1;
+    }
+
+    void execute(KTextEditor::View*, const KTextEditor::Range&) override
+    {
+        m_action->execute();
+    }
+
+private:
+    KDevelop::IAssistantAction::Ptr m_action;
+};
+
+class AssistantGroup : public KDevelop::CompletionCustomGroupNode
+{
+public:
+    AssistantGroup(const QList<KDevelop::IAssistantAction::Ptr>& actions)
+        : CompletionCustomGroupNode(QObject::tr("Assistants"), 0)
+    {
+        for (const auto& action: actions) {
+            appendChild(KDevelop::CompletionTreeItemPointer(new \
AssistantItem(action))); +        }
+    }
+};
+
+KDevelop::IAssistant* activeAssistant()
+{
+    return KDevelop::ICore::self()->languageController()-> \
staticAssistantsManager()->activeAssistant().data(); +}
+
+KDevelop::CompletionTreeElementPointer assistantActionsGroup(KDevelop::IAssistant* \
assistant) +{
+    if (assistant) {
+        const auto actions = assistant->actions();
+        if (!actions.isEmpty()) {
+            return KDevelop::CompletionTreeElementPointer(new \
AssistantGroup(actions)); +        }
+    }
+    return {};
+}
+
+}
+
 namespace KDevelop {
 
 class CompletionWorkerThread : public QThread
@@ -106,6 +176,10 @@ CodeCompletionModel::CodeCompletionModel( QObject * parent )
   , m_thread(0)
 {
   qRegisterMetaType<KTextEditor::Cursor>();
+
+  connect(ICore::self()->languageController()->staticAssistantsManager(),
+          &StaticAssistantsManager::activeAssistantChanged,
+          this, &CodeCompletionModel::updateAssistantItems);
 }
 
 void CodeCompletionModel::initialize() {
@@ -145,6 +219,35 @@ KDevelop::CodeCompletionWorker* CodeCompletionModel::worker() \
const {  return m_thread->m_worker;
 }
 
+void CodeCompletionModel::updateAssistantItems()
+{
+    const auto assistant = activeAssistant();
+    disconnect(m_connection);
+    if (assistant) {
+        m_connection = connect(assistant, &IAssistant::actionsChanged,
+                               this, &CodeCompletionModel::updateAssistantItems);
+    }
+
+    const bool hasAssistantNode = !m_completionItems.isEmpty() &&
+        dynamic_cast<AssistantGroup*>(m_completionItems.last().data());
+
+    if (!assistant && !hasAssistantNode) {
+        return;
+    }
+    // KateCompletionModel will happily ignore beginRemoveRows/endRemoveRows for \
argument hint items +    // We use argument hints otherwise we have to hack the \
currently typed text into the item's Name column +    // because KateCompletionModel \
offers no way to force an item to skip text filtering (other than arg hints) +    // \
Event if we didn't, KateCompletionModel doesn't correctly balance \
begin/endInsertRows, so... meh +    beginResetModel();
+    if (hasAssistantNode) {
+        m_completionItems.removeLast();
+    }
+    if (auto assists = assistantActionsGroup(assistant)) {
+        m_completionItems.append(assists);
+    }
+    endResetModel();
+}
+
 void CodeCompletionModel::clear()
 {
   beginResetModel();
@@ -264,6 +367,9 @@ void CodeCompletionModel::foundDeclarations(const \
QList<QExplicitlySharedDataPoi  
   beginResetModel();
   m_completionItems = items;
+  if (auto assists = assistantActionsGroup(activeAssistant())) {
+    m_completionItems.append(assists);
+  }
   endResetModel();
 
   if(m_completionContext) {
diff --git a/language/codecompletion/codecompletionmodel.h \
b/language/codecompletion/codecompletionmodel.h index 1c2c581..b141fa5 100644
--- a/language/codecompletion/codecompletionmodel.h
+++ b/language/codecompletion/codecompletionmodel.h
@@ -135,7 +135,11 @@ class KDEVPLATFORMLANGUAGE_EXPORT CodeCompletionModel : public \
KTextEditor::Code  friend class CompletionWorkerThread;
 
     CodeCompletionWorker* worker() const;
+
+    void updateAssistantItems();
+
   private:
+    QMetaObject::Connection m_connection;
     bool m_forceWaitForModel;
     bool m_fullCompletion;
     QMutex* m_mutex;
diff --git a/shell/uicontroller.cpp b/shell/uicontroller.cpp
index e4ecf40..ceb631e 100644
--- a/shell/uicontroller.cpp
+++ b/shell/uicontroller.cpp
@@ -747,6 +747,13 @@ void UiController::popUpAssistant(const \
KDevelop::IAssistant::Ptr& assistant)  d->currentShownAssistant = new AssistantPopup;
         }
         d->currentShownAssistant->reset(editorView, assistant);
+        // TODO: See if we can use CodeCompletionInterface::startCompletion for this
+        // See if we can split the assistant actions into their own model and use \
the model param as well +        // That way we don't get unwanted completions when \
the assistant completions are shown +        if (!assistant->actions().isEmpty()) {
+            // HACK: Internal, unexposed API -- do NOT git blame this line
+            QMetaObject::invokeMethod(editorView, "userInvokedCompletion");
+        }
     }
 }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic