diff --git a/language/codecompletion/codecompletion.cpp b/language/codecompletion/codecompletion.cpp index 22aaabfa35..c90da5c60a 100644 --- a/language/codecompletion/codecompletion.cpp +++ b/language/codecompletion/codecompletion.cpp @@ -1,129 +1,133 @@ /* * KDevelop Generic Code Completion Support * * Copyright 2006 Hamish Rodda * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "codecompletion.h" #include #include #include #include #include #include #include "../duchain/duchain.h" #include "../duchain/topducontext.h" #include "util/debug.h" #include "codecompletionmodel.h" #include using namespace KTextEditor; using namespace KDevelop; CodeCompletion::CodeCompletion(QObject *parent, KTextEditor::CodeCompletionModel* aModel, const QString& language) : QObject(parent), m_model(aModel), m_language(language) { KDevelop::CodeCompletionModel* kdevModel = dynamic_cast(aModel); if(kdevModel) kdevModel->initialize(); connect(KDevelop::ICore::self()->documentController(), &IDocumentController::textDocumentCreated, this, &CodeCompletion::textDocumentCreated); connect( ICore::self()->documentController(), &IDocumentController::documentUrlChanged, this, &CodeCompletion::documentUrlChanged ); aModel->setParent(this); // prevent deadlock QMetaObject::invokeMethod(this, "checkDocuments", Qt::QueuedConnection); } CodeCompletion::~CodeCompletion() { } void CodeCompletion::checkDocuments() { foreach( KDevelop::IDocument* doc, KDevelop::ICore::self()->documentController()->openDocuments() ) { if (doc->textDocument()) { checkDocument(doc->textDocument()); } } } void CodeCompletion::viewCreated(KTextEditor::Document * document, KTextEditor::View * view) { Q_UNUSED(document); if (CodeCompletionInterface* cc = dynamic_cast(view)) { cc->registerCompletionModel(m_model); qCDebug(LANGUAGE) << "Registered completion model"; + emit registeredToView(view); } } void CodeCompletion::documentUrlChanged(KDevelop::IDocument* document) { // The URL has changed (might have a different language now), so we re-register the document Document* textDocument = document->textDocument(); if(textDocument) { checkDocument(textDocument); } } void CodeCompletion::textDocumentCreated(KDevelop::IDocument* document) { Q_ASSERT(document->textDocument()); checkDocument(document->textDocument()); } void CodeCompletion::unregisterDocument(Document* textDocument) { - foreach (KTextEditor::View* view, textDocument->views()) - if (CodeCompletionInterface* cc = dynamic_cast(view)) + foreach (KTextEditor::View* view, textDocument->views()) { + if (CodeCompletionInterface* cc = dynamic_cast(view)) { cc->unregisterCompletionModel(m_model); + emit unregisteredFromView(view); + } + } disconnect(textDocument, &Document::viewCreated, this, &CodeCompletion::viewCreated); } void CodeCompletion::checkDocument(Document* textDocument) { unregisterDocument(textDocument); auto langs = ICore::self()->languageController()->languagesForUrl( textDocument->url() ); bool found = false; foreach(const auto& lang, langs) { if(m_language==lang->name()) { found=true; break; } } if(!found && !m_language.isEmpty()) return; foreach (KTextEditor::View* view, textDocument->views()) viewCreated(textDocument, view); connect(textDocument, &Document::viewCreated, this, &CodeCompletion::viewCreated); } diff --git a/language/codecompletion/codecompletion.h b/language/codecompletion/codecompletion.h index 5b0ab83fa9..104be9c22e 100644 --- a/language/codecompletion/codecompletion.h +++ b/language/codecompletion/codecompletion.h @@ -1,74 +1,79 @@ /* * KDevelop Generic Code Completion Support * * Copyright 2006 Hamish Rodda * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KDEVPLATFORM_CODECOMPLETION_H #define KDEVPLATFORM_CODECOMPLETION_H #include #include namespace KParts { class Part; } namespace KTextEditor { class Document; class View; class CodeCompletionModel; } namespace KDevelop { class IDocument; class ILanguage; +// TODO: cleanup this class for 5.1 class KDEVPLATFORMLANGUAGE_EXPORT CodeCompletion : public QObject { Q_OBJECT public: /** CodeCompletion will be the @p aModel parent. * If @p language is empty, the completion model will work for all files, * otherwise only for ones that contain the selected language. */ CodeCompletion(QObject* parent, KTextEditor::CodeCompletionModel* aModel, const QString& language); virtual ~CodeCompletion(); private Q_SLOTS: void textDocumentCreated(KDevelop::IDocument*); void viewCreated(KTextEditor::Document *document, KTextEditor::View *view); void documentUrlChanged(KDevelop::IDocument*); /** * check already opened documents, * needs to be done via delayed call to prevent infinite loop in * checkDocument() -> load lang plugin -> register CodeCompletion -> checkDocument() -> ... */ void checkDocuments(); + signals: + void registeredToView(KTextEditor::View* view); + void unregisteredFromView(KTextEditor::View* view); + private: void unregisterDocument(KTextEditor::Document*); void checkDocument(KTextEditor::Document*); KTextEditor::CodeCompletionModel* m_model; QString m_language; }; } #endif