Commit 2d262bd3 authored by Erik Verbruggen's avatar Erik Verbruggen

[C++] Added object pool handling for CompletionAssistProvider.

Change-Id: I89634989a7f360a30f7ed1bde4e67c93551ddfe4
Reviewed-by: default avatarEike Ziller <eike.ziller@nokia.com>
parent f3a3bf78
......@@ -59,7 +59,9 @@ namespace ProjectExplorer {
namespace CppTools {
class AbstractEditorSupport;
class CppCompletionSupport;
class CppCompletionAssistProvider;
class CppHighlightingSupport;
class CppHighlightingSupportFactory;
}
namespace CPlusPlus {
......@@ -216,7 +218,10 @@ public:
const QString &fileName, int key = AllExtraDiagnostics) const = 0;
virtual CppTools::CppCompletionSupport *completionSupport(Core::IEditor *editor) const = 0;
virtual void setCppCompletionAssistProvider(CppTools::CppCompletionAssistProvider *completionAssistProvider) = 0;
virtual CppTools::CppHighlightingSupport *highlightingSupport(Core::IEditor *editor) const = 0;
virtual void setHighlightingSupportFactory(CppTools::CppHighlightingSupportFactory *highlightingFactory) = 0;
Q_SIGNALS:
void documentUpdated(CPlusPlus::Document::Ptr doc);
......
......@@ -82,87 +82,6 @@ using namespace CppTools;
using namespace Internal;
using namespace TextEditor;
namespace {
int activationSequenceChar(const QChar &ch,
const QChar &ch2,
const QChar &ch3,
unsigned *kind,
bool wantFunctionCall)
{
int referencePosition = 0;
int completionKind = T_EOF_SYMBOL;
switch (ch.toLatin1()) {
case '.':
if (ch2 != QLatin1Char('.')) {
completionKind = T_DOT;
referencePosition = 1;
}
break;
case ',':
completionKind = T_COMMA;
referencePosition = 1;
break;
case '(':
if (wantFunctionCall) {
completionKind = T_LPAREN;
referencePosition = 1;
}
break;
case ':':
if (ch3 != QLatin1Char(':') && ch2 == QLatin1Char(':')) {
completionKind = T_COLON_COLON;
referencePosition = 2;
}
break;
case '>':
if (ch2 == QLatin1Char('-')) {
completionKind = T_ARROW;
referencePosition = 2;
}
break;
case '*':
if (ch2 == QLatin1Char('.')) {
completionKind = T_DOT_STAR;
referencePosition = 2;
} else if (ch3 == QLatin1Char('-') && ch2 == QLatin1Char('>')) {
completionKind = T_ARROW_STAR;
referencePosition = 3;
}
break;
case '\\':
case '@':
if (ch2.isNull() || ch2.isSpace()) {
completionKind = T_DOXY_COMMENT;
referencePosition = 1;
}
break;
case '<':
completionKind = T_ANGLE_STRING_LITERAL;
referencePosition = 1;
break;
case '"':
completionKind = T_STRING_LITERAL;
referencePosition = 1;
break;
case '/':
completionKind = T_SLASH;
referencePosition = 1;
break;
case '#':
completionKind = T_POUND;
referencePosition = 1;
break;
}
if (kind)
*kind = completionKind;
return referencePosition;
}
} // Anonymous
namespace CppTools {
namespace Internal {
......@@ -500,31 +419,52 @@ int CppFunctionHintModel::activeArgument(const QString &prefix) const
}
// ---------------------------
// CppCompletionAssistProvider
// InternalCompletionAssistProvider
// ---------------------------
bool CppCompletionAssistProvider::supportsEditor(const Core::Id &editorId) const
{
return editorId == Core::Id(CppEditor::Constants::CPPEDITOR_ID);
}
int CppCompletionAssistProvider::activationCharSequenceLength() const
IAssistProcessor *InternalCompletionAssistProvider::createProcessor() const
{
return 3;
return new CppCompletionAssistProcessor;
}
bool CppCompletionAssistProvider::isActivationCharSequence(const QString &sequence) const
namespace {
class CppCompletionSupportInternal: public CppCompletionSupport
{
const QChar &ch = sequence.at(2);
const QChar &ch2 = sequence.at(1);
const QChar &ch3 = sequence.at(0);
if (activationSequenceChar(ch, ch2, ch3, 0, true) != 0)
return true;
return false;
public:
CppCompletionSupportInternal(TextEditor::ITextEditor *editor)
: CppCompletionSupport(editor)
{}
virtual ~CppCompletionSupportInternal()
{}
virtual TextEditor::IAssistInterface *createAssistInterface(ProjectExplorer::Project *project,
QTextDocument *document,
int position,
TextEditor::AssistReason reason) const
{
CppModelManagerInterface *modelManager = CppModelManagerInterface::instance();
QStringList includePaths;
QStringList frameworkPaths;
if (project) {
includePaths = modelManager->projectInfo(project).includePaths();
frameworkPaths = modelManager->projectInfo(project).frameworkPaths();
}
return new CppTools::Internal::CppCompletionAssistInterface(
document,
position,
editor()->document(),
reason,
modelManager->snapshot(),
includePaths,
frameworkPaths);
}
};
}
IAssistProcessor *CppCompletionAssistProvider::createProcessor() const
CppCompletionSupport *InternalCompletionAssistProvider::completionSupport(ITextEditor *editor)
{
return new CppCompletionAssistProcessor;
return new CppCompletionSupportInternal(editor);
}
// -----------------
......@@ -816,7 +756,7 @@ int CppCompletionAssistProcessor::startOfOperator(int pos,
const QChar ch2 = pos > 0 ? m_interface->characterAt(pos - 2) : QChar();
const QChar ch3 = pos > 1 ? m_interface->characterAt(pos - 3) : QChar();
int start = pos - activationSequenceChar(ch, ch2, ch3, kind, wantFunctionCall);
int start = pos - CppCompletionAssistProvider::activationSequenceChar(ch, ch2, ch3, kind, wantFunctionCall);
if (start != pos) {
QTextCursor tc(m_interface->textDocument());
tc.setPosition(pos);
......
......@@ -33,6 +33,8 @@
#ifndef CPPCOMPLETIONASSIST_H
#define CPPCOMPLETIONASSIST_H
#include "cppcompletionassistprovider.h"
#include <cplusplus/Icons.h>
#include <cplusplus/Overview.h>
#include <cplusplus/TypeOfExpression.h>
......@@ -67,13 +69,13 @@ namespace Internal {
class CppCompletionAssistInterface;
class CppAssistProposalModel;
class CppCompletionAssistProvider : public TextEditor::CompletionAssistProvider
class InternalCompletionAssistProvider : public CppCompletionAssistProvider
{
Q_OBJECT
public:
virtual bool supportsEditor(const Core::Id &editorId) const;
virtual int activationCharSequenceLength() const;
virtual bool isActivationCharSequence(const QString &sequence) const;
virtual TextEditor::IAssistProcessor *createProcessor() const;
virtual CppCompletionSupport *completionSupport(TextEditor::ITextEditor *editor);
};
class CppCompletionAssistProcessor : public TextEditor::IAssistProcessor
......@@ -135,7 +137,7 @@ private:
QScopedPointer<const CppCompletionAssistInterface> m_interface;
QList<TextEditor::BasicProposalItem *> m_completions;
TextEditor::SnippetAssistCollector m_snippetCollector;
const CppCompletionAssistProvider *m_provider;
const InternalCompletionAssistProvider *m_provider;
CPlusPlus::Icons m_icons;
QStringList preprocessorCompletions;
QScopedPointer<CppAssistProposalModel> m_model;
......
#include "cppcompletionassistprovider.h"
#include <cppeditor/cppeditorconstants.h>
#include <Token.h>
using namespace CPlusPlus;
using namespace CppTools;
// ---------------------------
// CppCompletionAssistProvider
// ---------------------------
bool CppCompletionAssistProvider::supportsEditor(const Core::Id &editorId) const
{
return editorId == Core::Id(CppEditor::Constants::CPPEDITOR_ID);
}
int CppCompletionAssistProvider::activationCharSequenceLength() const
{
return 3;
}
bool CppCompletionAssistProvider::isActivationCharSequence(const QString &sequence) const
{
const QChar &ch = sequence.at(2);
const QChar &ch2 = sequence.at(1);
const QChar &ch3 = sequence.at(0);
if (activationSequenceChar(ch, ch2, ch3, 0, true) != 0)
return true;
return false;
}
int CppCompletionAssistProvider::activationSequenceChar(const QChar &ch,
const QChar &ch2,
const QChar &ch3,
unsigned *kind,
bool wantFunctionCall)
{
int referencePosition = 0;
int completionKind = T_EOF_SYMBOL;
switch (ch.toLatin1()) {
case '.':
if (ch2 != QLatin1Char('.')) {
completionKind = T_DOT;
referencePosition = 1;
}
break;
case ',':
completionKind = T_COMMA;
referencePosition = 1;
break;
case '(':
if (wantFunctionCall) {
completionKind = T_LPAREN;
referencePosition = 1;
}
break;
case ':':
if (ch3 != QLatin1Char(':') && ch2 == QLatin1Char(':')) {
completionKind = T_COLON_COLON;
referencePosition = 2;
}
break;
case '>':
if (ch2 == QLatin1Char('-')) {
completionKind = T_ARROW;
referencePosition = 2;
}
break;
case '*':
if (ch2 == QLatin1Char('.')) {
completionKind = T_DOT_STAR;
referencePosition = 2;
} else if (ch3 == QLatin1Char('-') && ch2 == QLatin1Char('>')) {
completionKind = T_ARROW_STAR;
referencePosition = 3;
}
break;
case '\\':
case '@':
if (ch2.isNull() || ch2.isSpace()) {
completionKind = T_DOXY_COMMENT;
referencePosition = 1;
}
break;
case '<':
completionKind = T_ANGLE_STRING_LITERAL;
referencePosition = 1;
break;
case '"':
completionKind = T_STRING_LITERAL;
referencePosition = 1;
break;
case '/':
completionKind = T_SLASH;
referencePosition = 1;
break;
case '#':
completionKind = T_POUND;
referencePosition = 1;
break;
}
if (kind)
*kind = completionKind;
return referencePosition;
}
#ifndef CPPTOOLS_CPPCOMPLETIONASSISTPROVIDER_H
#define CPPTOOLS_CPPCOMPLETIONASSISTPROVIDER_H
#include "cppcompletionsupport.h"
#include "cpptools_global.h"
#include <texteditor/codeassist/completionassistprovider.h>
namespace CppTools {
class CPPTOOLS_EXPORT CppCompletionAssistProvider : public TextEditor::CompletionAssistProvider
{
Q_OBJECT
public:
virtual bool supportsEditor(const Core::Id &editorId) const;
virtual int activationCharSequenceLength() const;
virtual bool isActivationCharSequence(const QString &sequence) const;
virtual CppCompletionSupport *completionSupport(TextEditor::ITextEditor *editor) = 0;
static int activationSequenceChar(const QChar &ch, const QChar &ch2,
const QChar &ch3, unsigned *kind,
bool wantFunctionCall);
};
} // namespace CppTools
#endif // CPPTOOLS_CPPCOMPLETIONASSISTPROVIDER_H
......@@ -43,7 +43,3 @@ CppCompletionSupport::CppCompletionSupport(TextEditor::ITextEditor *editor)
CppCompletionSupport::~CppCompletionSupport()
{
}
CppCompletionSupportFactory::~CppCompletionSupportFactory()
{
}
......@@ -71,14 +71,6 @@ private:
TextEditor::ITextEditor *m_editor;
};
class CPPTOOLS_EXPORT CppCompletionSupportFactory
{
public:
virtual ~CppCompletionSupportFactory() = 0;
virtual CppCompletionSupport *completionSupport(TextEditor::ITextEditor *editor) = 0;
};
} // namespace CppTools
#endif // CPPTOOLS_CPPCOMPLETIONSUPPORT_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "cppcompletionassist.h"
#include "cppcompletionsupportinternal.h"
#include "cppmodelmanager.h"
#include <coreplugin/idocument.h>
#include <projectexplorer/project.h>
#include <texteditor/codeassist/iassistinterface.h>
using namespace CPlusPlus;
using namespace CppTools;
using namespace CppTools::Internal;
CppCompletionSupportInternal::CppCompletionSupportInternal(TextEditor::ITextEditor *editor)
: CppCompletionSupport(editor)
{
}
CppCompletionSupportInternal::~CppCompletionSupportInternal()
{
}
TextEditor::IAssistInterface *CppCompletionSupportInternal::createAssistInterface(
ProjectExplorer::Project *project,
QTextDocument *document,
int position,
TextEditor::AssistReason reason) const
{
CppModelManagerInterface *modelManager = CppModelManagerInterface::instance();
QStringList includePaths;
QStringList frameworkPaths;
if (project) {
includePaths = modelManager->projectInfo(project).includePaths();
frameworkPaths = modelManager->projectInfo(project).frameworkPaths();
}
return new CppTools::Internal::CppCompletionAssistInterface(
document,
position,
editor()->document(),
reason,
modelManager->snapshot(),
includePaths,
frameworkPaths);
}
CppCompletionSupportInternalFactory::~CppCompletionSupportInternalFactory()
{}
CppCompletionSupport *CppCompletionSupportInternalFactory::completionSupport(TextEditor::ITextEditor *editor)
{
return new CppCompletionSupportInternal(editor);
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef CPPTOOLS_CPPCOMPLETIONSUPPORTINTERNAL_H
#define CPPTOOLS_CPPCOMPLETIONSUPPORTINTERNAL_H
#include "cppcompletionsupport.h"
namespace CppTools {
namespace Internal {
class CppCompletionSupportInternal: public CppCompletionSupport
{
public:
CppCompletionSupportInternal(TextEditor::ITextEditor *editor);
virtual ~CppCompletionSupportInternal();
virtual TextEditor::IAssistInterface *createAssistInterface(ProjectExplorer::Project *project,
QTextDocument *document,
int position,
TextEditor::AssistReason reason) const;
};
class CppCompletionSupportInternalFactory: public CppCompletionSupportFactory
{
public:
virtual ~CppCompletionSupportInternalFactory();
virtual CppCompletionSupport *completionSupport(TextEditor::ITextEditor *editor);
};
} // namespace Internal
} // namespace CppTools
#endif // CPPTOOLS_CPPCOMPLETIONSUPPORTINTERNAL_H
......@@ -34,8 +34,7 @@
#include <cplusplus/Overview.h>
#include "cppmodelmanager.h"
#include "cppcompletionsupport.h"
#include "cppcompletionsupportinternal.h"
#include "cppcompletionassist.h"
#include "cpphighlightingsupport.h"
#include "cpphighlightingsupportinternal.h"
#include "abstracteditorsupport.h"
......@@ -735,14 +734,16 @@ CppModelManager::CppModelManager(QObject *parent)
connect(Core::ICore::editorManager(), SIGNAL(editorAboutToClose(Core::IEditor *)),
this, SLOT(editorAboutToClose(Core::IEditor *)));
m_completionFallback = new CppCompletionSupportInternalFactory;
m_completionFactory = m_completionFallback;
m_completionFallback = new InternalCompletionAssistProvider;
m_completionAssistProvider = m_completionFallback;
ExtensionSystem::PluginManager::instance()->addObject(m_completionAssistProvider);
m_highlightingFallback = new CppHighlightingSupportInternalFactory;
m_highlightingFactory = m_highlightingFallback;
}
CppModelManager::~CppModelManager()
{
ExtensionSystem::PluginManager::instance()->removeObject(m_completionAssistProvider);
delete m_completionFallback;
delete m_highlightingFallback;
}
......@@ -1346,17 +1347,19 @@ void CppModelManager::finishedRefreshingSourceFiles(const QStringList &files)
CppCompletionSupport *CppModelManager::completionSupport(Core::IEditor *editor) const
{
if (TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor *>(editor))
return m_completionFactory->completionSupport(textEditor);
return m_completionAssistProvider->completionSupport(textEditor);
else
return 0;
}
void CppModelManager::setCompletionSupportFactory(CppCompletionSupportFactory *completionFactory)
void CppModelManager::setCppCompletionAssistProvider(CppCompletionAssistProvider *completionAssistProvider)
{
if (completionFactory)
m_completionFactory = completionFactory;
ExtensionSystem::PluginManager::instance()->removeObject(m_completionAssistProvider);
if (completionAssistProvider)
m_completionAssistProvider = completionAssistProvider;
else
m_completionFactory = m_completionFallback;
m_completionAssistProvider = m_completionFallback;
ExtensionSystem::PluginManager::instance()->addObject(m_completionAssistProvider);
}
CppHighlightingSupport *CppModelManager::highlightingSupport(Core::IEditor *editor) const
......
......@@ -138,10 +138,10 @@ public:
void finishedRefreshingSourceFiles(const QStringList &files);
virtual CppCompletionSupport *completionSupport(Core::IEditor *editor) const;
void setCompletionSupportFactory(CppCompletionSupportFactory *completionFactory);
virtual void setCppCompletionAssistProvider(CppCompletionAssistProvider *completionAssistProvider);
virtual CppHighlightingSupport *highlightingSupport(Core::IEditor *editor) const;
void setHighlightingSupportFactory(CppHighlightingSupportFactory *highlightingFactory);
virtual void setHighlightingSupportFactory(CppHighlightingSupportFactory *highlightingFactory);
Q_SIGNALS:
void projectPathChanged(const QString &projectPath);
......@@ -247,8 +247,8 @@ private:
QMap<QString, QList<ProjectPart::Ptr> > m_srcToProjectPart;
CppCompletionSupportFactory *m_completionFactory;
CppCompletionSupportFactory *m_completionFallback;
CppCompletionAssistProvider *m_completionAssistProvider;
CppCompletionAssistProvider *m_completionFallback;
CppHighlightingSupportFactory *m_highlightingFactory;
CppHighlightingSupportFactory *m_highlightingFallback;
};
......
......@@ -40,12 +40,12 @@ HEADERS += completionsettingspage.h \
commentssettings.h \
symbolfinder.h \
cppcompletionsupport.h \
cppcompletionsupportinternal.h \
cpphighlightingsupport.h \
cpphighlightingsupportinternal.h \
cppchecksymbols.h \
cpplocalsymbols.h \
cppsemanticinfo.h
cppsemanticinfo.h \
cppcompletionassistprovider.h
SOURCES += completionsettingspage.cpp \
cppclassesfilter.cpp \