2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "cppcodecompletion.h"
|
|
|
|
|
#include "cppmodelmanager.h"
|
2009-02-20 12:55:01 +01:00
|
|
|
#include "cppdoxygen.h"
|
2009-10-09 15:22:57 +02:00
|
|
|
#include "cpptoolsconstants.h"
|
2009-06-09 13:52:27 +02:00
|
|
|
#include "cpptoolseditorsupport.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <Control.h>
|
|
|
|
|
#include <AST.h>
|
|
|
|
|
#include <ASTVisitor.h>
|
|
|
|
|
#include <CoreTypes.h>
|
|
|
|
|
#include <Literals.h>
|
|
|
|
|
#include <Names.h>
|
|
|
|
|
#include <NameVisitor.h>
|
|
|
|
|
#include <Symbols.h>
|
|
|
|
|
#include <SymbolVisitor.h>
|
|
|
|
|
#include <Scope.h>
|
|
|
|
|
#include <TranslationUnit.h>
|
2008-12-09 15:25:01 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <cplusplus/ResolveExpression.h>
|
2009-09-22 16:52:27 +02:00
|
|
|
#include <cplusplus/MatchingText.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <cplusplus/Overview.h>
|
|
|
|
|
#include <cplusplus/ExpressionUnderCursor.h>
|
2009-10-19 14:14:28 +02:00
|
|
|
#include <cplusplus/BackwardsScanner.h>
|
2010-05-05 10:59:46 +02:00
|
|
|
#include <cplusplus/LookupContext.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2009-10-09 15:22:57 +02:00
|
|
|
#include <coreplugin/mimedatabase.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2010-05-10 18:30:09 +02:00
|
|
|
#include <texteditor/completionsettings.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <texteditor/itexteditor.h>
|
|
|
|
|
#include <texteditor/itexteditable.h>
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
2010-10-27 17:38:22 +02:00
|
|
|
#include <texteditor/snippets/snippet.h>
|
2009-07-23 16:46:30 +02:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
2010-04-14 17:24:29 +02:00
|
|
|
|
|
|
|
|
#include <utils/faketooltip.h>
|
2009-07-23 16:46:30 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <QtCore/QMap>
|
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
#include <QtGui/QAction>
|
2009-03-26 11:14:05 +01:00
|
|
|
#include <QtGui/QApplication>
|
2009-03-26 15:12:31 +01:00
|
|
|
#include <QtGui/QDesktopWidget>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QKeyEvent>
|
|
|
|
|
#include <QtGui/QLabel>
|
2010-04-14 17:24:29 +02:00
|
|
|
#include <QtGui/QStyle>
|
2009-06-19 15:18:36 +02:00
|
|
|
#include <QtGui/QTextDocument> // Qt::escape()
|
2009-03-26 11:14:05 +01:00
|
|
|
#include <QtGui/QToolButton>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QVBoxLayout>
|
2010-09-24 20:16:34 +02:00
|
|
|
#include <QtAlgorithms>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-12 14:52:24 +02:00
|
|
|
namespace {
|
|
|
|
|
const bool debug = ! qgetenv("CPLUSPLUS_DEBUG").isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
|
|
|
|
namespace CppTools {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2009-01-28 13:56:10 +01:00
|
|
|
class FunctionArgumentWidget : public QLabel
|
|
|
|
|
{
|
2009-03-26 11:14:05 +01:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
public:
|
2009-01-28 13:56:10 +01:00
|
|
|
FunctionArgumentWidget();
|
2009-03-26 12:37:40 +01:00
|
|
|
void showFunctionHint(QList<Function *> functionSymbols,
|
2010-05-05 12:06:38 +02:00
|
|
|
const LookupContext &context,
|
2009-03-26 12:37:40 +01:00
|
|
|
int startPosition);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
bool eventFilter(QObject *obj, QEvent *e);
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
private slots:
|
|
|
|
|
void nextPage();
|
|
|
|
|
void previousPage();
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
private:
|
2009-03-26 14:46:03 +01:00
|
|
|
void updateArgumentHighlight();
|
2008-12-02 12:01:29 +01:00
|
|
|
void updateHintText();
|
2010-09-15 12:42:15 +02:00
|
|
|
void placeInsideScreen();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
Function *currentFunction() const
|
|
|
|
|
{ return m_items.at(m_current); }
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
int m_startpos;
|
|
|
|
|
int m_currentarg;
|
2009-03-26 12:37:40 +01:00
|
|
|
int m_current;
|
2009-04-08 10:26:56 +02:00
|
|
|
bool m_escapePressed;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
TextEditor::ITextEditor *m_editor;
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
QWidget *m_pager;
|
2009-03-26 14:46:03 +01:00
|
|
|
QLabel *m_numberLabel;
|
2010-04-14 17:24:29 +02:00
|
|
|
Utils::FakeToolTip *m_popupFrame;
|
2009-03-26 11:14:05 +01:00
|
|
|
QList<Function *> m_items;
|
2010-05-05 12:06:38 +02:00
|
|
|
LookupContext m_context;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ConvertToCompletionItem: protected NameVisitor
|
|
|
|
|
{
|
|
|
|
|
// The completion collector.
|
|
|
|
|
CppCodeCompletion *_collector;
|
|
|
|
|
|
|
|
|
|
// The completion item.
|
|
|
|
|
TextEditor::CompletionItem _item;
|
|
|
|
|
|
|
|
|
|
// The current symbol.
|
|
|
|
|
Symbol *_symbol;
|
|
|
|
|
|
|
|
|
|
// The pretty printer.
|
|
|
|
|
Overview overview;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ConvertToCompletionItem(CppCodeCompletion *collector)
|
|
|
|
|
: _collector(collector),
|
|
|
|
|
_item(0),
|
|
|
|
|
_symbol(0)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
TextEditor::CompletionItem operator()(Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
if (! symbol || ! symbol->name() || symbol->name()->isQualifiedNameId())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
TextEditor::CompletionItem previousItem = switchCompletionItem(0);
|
|
|
|
|
Symbol *previousSymbol = switchSymbol(symbol);
|
2010-08-26 12:23:09 +02:00
|
|
|
accept(symbol->unqualifiedName());
|
2010-08-03 14:45:11 +02:00
|
|
|
if (_item.isValid())
|
2009-10-05 18:32:08 +02:00
|
|
|
_item.data = QVariant::fromValue(symbol);
|
2008-12-02 12:01:29 +01:00
|
|
|
(void) switchSymbol(previousSymbol);
|
|
|
|
|
return switchCompletionItem(previousItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Symbol *switchSymbol(Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
Symbol *previousSymbol = _symbol;
|
|
|
|
|
_symbol = symbol;
|
|
|
|
|
return previousSymbol;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::CompletionItem switchCompletionItem(TextEditor::CompletionItem item)
|
|
|
|
|
{
|
|
|
|
|
TextEditor::CompletionItem previousItem = _item;
|
|
|
|
|
_item = item;
|
|
|
|
|
return previousItem;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-01 12:46:15 +01:00
|
|
|
TextEditor::CompletionItem newCompletionItem(const Name *name)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
TextEditor::CompletionItem item(_collector);
|
2009-10-05 18:32:08 +02:00
|
|
|
item.text = overview.prettyName(name);
|
|
|
|
|
item.icon = _collector->iconForSymbol(_symbol);
|
2008-12-02 12:01:29 +01:00
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-02 11:59:01 +02:00
|
|
|
virtual void visit(const Identifier *name)
|
2008-12-02 12:01:29 +01:00
|
|
|
{ _item = newCompletionItem(name); }
|
|
|
|
|
|
2009-12-01 12:46:15 +01:00
|
|
|
virtual void visit(const TemplateNameId *name)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
_item = newCompletionItem(name);
|
2009-10-05 18:32:08 +02:00
|
|
|
_item.text = QLatin1String(name->identifier()->chars());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-12-01 12:46:15 +01:00
|
|
|
virtual void visit(const DestructorNameId *name)
|
2008-12-02 12:01:29 +01:00
|
|
|
{ _item = newCompletionItem(name); }
|
|
|
|
|
|
2009-12-01 12:46:15 +01:00
|
|
|
virtual void visit(const OperatorNameId *name)
|
2008-12-02 12:01:29 +01:00
|
|
|
{ _item = newCompletionItem(name); }
|
|
|
|
|
|
2009-12-01 12:46:15 +01:00
|
|
|
virtual void visit(const ConversionNameId *name)
|
2008-12-02 12:01:29 +01:00
|
|
|
{ _item = newCompletionItem(name); }
|
|
|
|
|
|
2009-12-01 12:46:15 +01:00
|
|
|
virtual void visit(const QualifiedNameId *name)
|
2010-07-12 13:41:54 +02:00
|
|
|
{ _item = newCompletionItem(name->name()); }
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2009-11-02 10:31:44 +01:00
|
|
|
struct CompleteFunctionDeclaration
|
|
|
|
|
{
|
|
|
|
|
explicit CompleteFunctionDeclaration(Function *f = 0)
|
|
|
|
|
: function(f)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
Function *function;
|
|
|
|
|
};
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CppTools
|
|
|
|
|
|
|
|
|
|
using namespace CppTools::Internal;
|
|
|
|
|
|
2009-11-02 10:31:44 +01:00
|
|
|
Q_DECLARE_METATYPE(CompleteFunctionDeclaration)
|
2009-06-19 15:18:36 +02:00
|
|
|
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
FunctionArgumentWidget::FunctionArgumentWidget():
|
2009-03-26 12:37:40 +01:00
|
|
|
m_startpos(-1),
|
2009-04-08 10:26:56 +02:00
|
|
|
m_current(0),
|
|
|
|
|
m_escapePressed(false)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-13 15:20:48 +01:00
|
|
|
QObject *editorObject = Core::EditorManager::instance()->currentEditor();
|
2008-12-02 12:01:29 +01:00
|
|
|
m_editor = qobject_cast<TextEditor::ITextEditor *>(editorObject);
|
|
|
|
|
|
2010-04-14 17:24:29 +02:00
|
|
|
m_popupFrame = new Utils::FakeToolTip(m_editor->widget());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-05-13 11:05:39 +02:00
|
|
|
QToolButton *downArrow = new QToolButton;
|
|
|
|
|
downArrow->setArrowType(Qt::DownArrow);
|
|
|
|
|
downArrow->setFixedSize(16, 16);
|
|
|
|
|
downArrow->setAutoRaise(true);
|
2009-03-26 11:14:05 +01:00
|
|
|
|
2009-05-13 11:05:39 +02:00
|
|
|
QToolButton *upArrow = new QToolButton;
|
|
|
|
|
upArrow->setArrowType(Qt::UpArrow);
|
|
|
|
|
upArrow->setFixedSize(16, 16);
|
|
|
|
|
upArrow->setAutoRaise(true);
|
2009-03-26 11:14:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
setParent(m_popupFrame);
|
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
m_pager = new QWidget;
|
|
|
|
|
QHBoxLayout *hbox = new QHBoxLayout(m_pager);
|
|
|
|
|
hbox->setMargin(0);
|
|
|
|
|
hbox->setSpacing(0);
|
2009-05-13 11:05:39 +02:00
|
|
|
hbox->addWidget(upArrow);
|
2009-03-26 14:46:03 +01:00
|
|
|
m_numberLabel = new QLabel;
|
|
|
|
|
hbox->addWidget(m_numberLabel);
|
2009-05-13 11:05:39 +02:00
|
|
|
hbox->addWidget(downArrow);
|
2009-03-26 11:14:05 +01:00
|
|
|
|
2009-03-26 14:46:03 +01:00
|
|
|
QHBoxLayout *layout = new QHBoxLayout;
|
2008-12-02 12:01:29 +01:00
|
|
|
layout->setMargin(0);
|
2009-03-26 11:14:05 +01:00
|
|
|
layout->setSpacing(0);
|
|
|
|
|
layout->addWidget(m_pager);
|
|
|
|
|
layout->addWidget(this);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_popupFrame->setLayout(layout);
|
|
|
|
|
|
2009-05-13 11:05:39 +02:00
|
|
|
connect(upArrow, SIGNAL(clicked()), SLOT(previousPage()));
|
|
|
|
|
connect(downArrow, SIGNAL(clicked()), SLOT(nextPage()));
|
2009-03-26 11:14:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
setTextFormat(Qt::RichText);
|
2009-03-26 12:37:40 +01:00
|
|
|
|
|
|
|
|
qApp->installEventFilter(this);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
void FunctionArgumentWidget::showFunctionHint(QList<Function *> functionSymbols,
|
2010-05-05 12:06:38 +02:00
|
|
|
const LookupContext &context,
|
2009-03-26 12:37:40 +01:00
|
|
|
int startPosition)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-03-26 11:14:05 +01:00
|
|
|
Q_ASSERT(!functionSymbols.isEmpty());
|
|
|
|
|
|
2009-03-26 12:37:40 +01:00
|
|
|
if (m_startpos == startPosition)
|
|
|
|
|
return;
|
|
|
|
|
|
2009-03-26 14:46:03 +01:00
|
|
|
m_pager->setVisible(functionSymbols.size() > 1);
|
2009-02-17 15:11:56 +01:00
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
m_items = functionSymbols;
|
2009-02-16 11:20:11 +01:00
|
|
|
m_context = context;
|
2009-03-26 12:37:40 +01:00
|
|
|
m_startpos = startPosition;
|
2009-03-26 11:14:05 +01:00
|
|
|
m_current = 0;
|
2009-04-08 10:26:56 +02:00
|
|
|
m_escapePressed = false;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// update the text
|
|
|
|
|
m_currentarg = -1;
|
2009-03-26 14:46:03 +01:00
|
|
|
updateArgumentHighlight();
|
2009-03-26 11:14:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
m_popupFrame->show();
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
void FunctionArgumentWidget::nextPage()
|
|
|
|
|
{
|
|
|
|
|
m_current = (m_current + 1) % m_items.size();
|
|
|
|
|
updateHintText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FunctionArgumentWidget::previousPage()
|
|
|
|
|
{
|
|
|
|
|
if (m_current == 0)
|
|
|
|
|
m_current = m_items.size() - 1;
|
|
|
|
|
else
|
|
|
|
|
--m_current;
|
|
|
|
|
|
|
|
|
|
updateHintText();
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-26 14:46:03 +01:00
|
|
|
void FunctionArgumentWidget::updateArgumentHighlight()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
int curpos = m_editor->position();
|
|
|
|
|
if (curpos < m_startpos) {
|
2009-03-26 14:46:03 +01:00
|
|
|
m_popupFrame->close();
|
2008-12-02 12:01:29 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString str = m_editor->textAt(m_startpos, curpos - m_startpos);
|
|
|
|
|
int argnr = 0;
|
|
|
|
|
int parcount = 0;
|
|
|
|
|
SimpleLexer tokenize;
|
2010-06-29 17:57:15 +02:00
|
|
|
QList<Token> tokens = tokenize(str);
|
2008-12-02 12:01:29 +01:00
|
|
|
for (int i = 0; i < tokens.count(); ++i) {
|
2010-06-29 17:57:15 +02:00
|
|
|
const Token &tk = tokens.at(i);
|
2008-12-02 12:01:29 +01:00
|
|
|
if (tk.is(T_LPAREN))
|
|
|
|
|
++parcount;
|
|
|
|
|
else if (tk.is(T_RPAREN))
|
|
|
|
|
--parcount;
|
|
|
|
|
else if (! parcount && tk.is(T_COMMA))
|
|
|
|
|
++argnr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_currentarg != argnr) {
|
|
|
|
|
m_currentarg = argnr;
|
|
|
|
|
updateHintText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parcount < 0)
|
2009-03-26 14:46:03 +01:00
|
|
|
m_popupFrame->close();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FunctionArgumentWidget::eventFilter(QObject *obj, QEvent *e)
|
|
|
|
|
{
|
|
|
|
|
switch (e->type()) {
|
2009-04-08 10:26:56 +02:00
|
|
|
case QEvent::ShortcutOverride:
|
|
|
|
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
|
|
|
|
|
m_escapePressed = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2009-03-26 14:46:03 +01:00
|
|
|
case QEvent::KeyPress:
|
2009-04-08 10:26:56 +02:00
|
|
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape) {
|
|
|
|
|
m_escapePressed = true;
|
|
|
|
|
}
|
2009-03-26 14:46:03 +01:00
|
|
|
if (m_items.size() > 1) {
|
|
|
|
|
QKeyEvent *ke = static_cast<QKeyEvent*>(e);
|
|
|
|
|
if (ke->key() == Qt::Key_Up) {
|
|
|
|
|
previousPage();
|
|
|
|
|
return true;
|
|
|
|
|
} else if (ke->key() == Qt::Key_Down) {
|
|
|
|
|
nextPage();
|
|
|
|
|
return true;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-03-26 14:46:03 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case QEvent::KeyRelease:
|
2009-04-08 10:26:56 +02:00
|
|
|
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape && m_escapePressed) {
|
2009-03-26 14:46:03 +01:00
|
|
|
m_popupFrame->close();
|
|
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-03-26 14:46:03 +01:00
|
|
|
updateArgumentHighlight();
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
case QEvent::WindowDeactivate:
|
|
|
|
|
case QEvent::FocusOut:
|
2009-03-26 14:46:03 +01:00
|
|
|
if (obj != m_editor->widget())
|
|
|
|
|
break;
|
|
|
|
|
m_popupFrame->close();
|
2009-03-26 11:14:05 +01:00
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
|
case QEvent::MouseButtonRelease:
|
|
|
|
|
case QEvent::MouseButtonDblClick:
|
2009-03-26 11:14:05 +01:00
|
|
|
case QEvent::Wheel: {
|
|
|
|
|
QWidget *widget = qobject_cast<QWidget *>(obj);
|
|
|
|
|
if (! (widget == this || m_popupFrame->isAncestorOf(widget))) {
|
2009-03-26 14:46:03 +01:00
|
|
|
m_popupFrame->close();
|
2009-03-26 11:14:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FunctionArgumentWidget::updateHintText()
|
|
|
|
|
{
|
|
|
|
|
Overview overview;
|
|
|
|
|
overview.setShowReturnTypes(true);
|
|
|
|
|
overview.setShowArgumentNames(true);
|
2009-05-27 17:02:35 +02:00
|
|
|
overview.setMarkedArgument(m_currentarg + 1);
|
2009-03-26 11:14:05 +01:00
|
|
|
Function *f = currentFunction();
|
2009-03-26 14:46:03 +01:00
|
|
|
|
2009-05-27 17:02:35 +02:00
|
|
|
const QString prettyMethod = overview(f->type(), f->name());
|
|
|
|
|
const int begin = overview.markedArgumentBegin();
|
|
|
|
|
const int end = overview.markedArgumentEnd();
|
|
|
|
|
|
|
|
|
|
QString hintText;
|
|
|
|
|
hintText += Qt::escape(prettyMethod.left(begin));
|
|
|
|
|
hintText += "<b>";
|
|
|
|
|
hintText += Qt::escape(prettyMethod.mid(begin, end - begin));
|
|
|
|
|
hintText += "</b>";
|
|
|
|
|
hintText += Qt::escape(prettyMethod.mid(end));
|
|
|
|
|
setText(hintText);
|
|
|
|
|
|
2009-03-26 14:46:03 +01:00
|
|
|
m_numberLabel->setText(tr("%1 of %2").arg(m_current + 1).arg(m_items.size()));
|
|
|
|
|
|
2010-09-15 12:42:15 +02:00
|
|
|
placeInsideScreen();
|
|
|
|
|
}
|
2009-03-26 15:12:31 +01:00
|
|
|
|
2010-09-15 12:42:15 +02:00
|
|
|
void FunctionArgumentWidget::placeInsideScreen()
|
|
|
|
|
{
|
2009-03-26 15:12:31 +01:00
|
|
|
const QDesktopWidget *desktop = QApplication::desktop();
|
2009-06-03 20:45:49 +02:00
|
|
|
#ifdef Q_WS_MAC
|
2009-04-06 17:11:32 +02:00
|
|
|
const QRect screen = desktop->availableGeometry(desktop->screenNumber(m_editor->widget()));
|
2009-03-26 15:12:31 +01:00
|
|
|
#else
|
2009-04-06 17:11:32 +02:00
|
|
|
const QRect screen = desktop->screenGeometry(desktop->screenNumber(m_editor->widget()));
|
2009-03-26 15:12:31 +01:00
|
|
|
#endif
|
|
|
|
|
|
2010-09-15 12:42:15 +02:00
|
|
|
m_pager->setFixedWidth(m_pager->minimumSizeHint().width());
|
|
|
|
|
|
|
|
|
|
setWordWrap(false);
|
|
|
|
|
const int maxDesiredWidth = screen.width() - 10;
|
|
|
|
|
const QSize minHint = m_popupFrame->minimumSizeHint();
|
|
|
|
|
if (minHint.width() > maxDesiredWidth) {
|
|
|
|
|
setWordWrap(true);
|
|
|
|
|
m_popupFrame->setFixedWidth(maxDesiredWidth);
|
|
|
|
|
const int extra =
|
|
|
|
|
m_popupFrame->contentsMargins().bottom() + m_popupFrame->contentsMargins().top();
|
|
|
|
|
m_popupFrame->setFixedHeight(heightForWidth(maxDesiredWidth - m_pager->width()) + extra);
|
|
|
|
|
} else {
|
|
|
|
|
m_popupFrame->setFixedSize(minHint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QSize sz = m_popupFrame->size();
|
2009-03-26 15:12:31 +01:00
|
|
|
QPoint pos = m_editor->cursorRect(m_startpos).topLeft();
|
|
|
|
|
pos.setY(pos.y() - sz.height() - 1);
|
|
|
|
|
|
|
|
|
|
if (pos.x() + sz.width() > screen.right())
|
|
|
|
|
pos.setX(screen.right() - sz.width());
|
|
|
|
|
|
|
|
|
|
m_popupFrame->move(pos);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-01-28 13:56:10 +01:00
|
|
|
CppCodeCompletion::CppCodeCompletion(CppModelManager *manager)
|
2008-12-02 12:01:29 +01:00
|
|
|
: ICompletionCollector(manager),
|
|
|
|
|
m_manager(manager),
|
2009-10-21 16:51:54 +02:00
|
|
|
m_editor(0),
|
|
|
|
|
m_startPosition(-1),
|
2010-07-14 12:37:49 +02:00
|
|
|
m_shouldRestartCompletion(false),
|
2010-07-19 10:25:55 +02:00
|
|
|
m_automaticCompletion(false),
|
2009-10-09 15:22:57 +02:00
|
|
|
m_completionOperator(T_EOF_SYMBOL),
|
2010-09-24 20:16:34 +02:00
|
|
|
m_objcEnabled(true),
|
2010-12-02 17:02:23 +01:00
|
|
|
m_snippetProvider(CppEditor::Constants::CPP_SNIPPETS_GROUP_ID,
|
2010-10-27 17:38:22 +02:00
|
|
|
QIcon(QLatin1String(":/texteditor/images/snippet.png")))
|
2008-12-12 16:36:33 +01:00
|
|
|
{
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
QIcon CppCodeCompletion::iconForSymbol(Symbol *symbol) const
|
2008-12-12 16:36:33 +01:00
|
|
|
{
|
|
|
|
|
return m_icons.iconForSymbol(symbol);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
/*
|
2009-08-28 13:43:06 +02:00
|
|
|
Searches backwards for an access operator.
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
2010-06-29 17:47:59 +02:00
|
|
|
static int startOfOperator(TextEditor::ITextEditable *editor,
|
2008-12-02 12:01:29 +01:00
|
|
|
int pos, unsigned *kind,
|
|
|
|
|
bool wantFunctionCall)
|
|
|
|
|
{
|
|
|
|
|
const QChar ch = pos > -1 ? editor->characterAt(pos - 1) : QChar();
|
|
|
|
|
const QChar ch2 = pos > 0 ? editor->characterAt(pos - 2) : QChar();
|
|
|
|
|
const QChar ch3 = pos > 1 ? editor->characterAt(pos - 3) : QChar();
|
|
|
|
|
|
|
|
|
|
int start = pos;
|
2009-10-20 16:37:01 +02:00
|
|
|
int completionKind = T_EOF_SYMBOL;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-08-28 13:43:06 +02:00
|
|
|
switch (ch.toLatin1()) {
|
|
|
|
|
case '.':
|
|
|
|
|
if (ch2 != QLatin1Char('.')) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_DOT;
|
2009-08-28 13:43:06 +02:00
|
|
|
--start;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ',':
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_COMMA;
|
2009-03-26 12:37:40 +01:00
|
|
|
--start;
|
2009-08-28 13:43:06 +02:00
|
|
|
break;
|
|
|
|
|
case '(':
|
|
|
|
|
if (wantFunctionCall) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_LPAREN;
|
2009-08-28 13:43:06 +02:00
|
|
|
--start;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ':':
|
|
|
|
|
if (ch3 != QLatin1Char(':') && ch2 == QLatin1Char(':')) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_COLON_COLON;
|
2009-08-28 13:43:06 +02:00
|
|
|
start -= 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
if (ch2 == QLatin1Char('-')) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_ARROW;
|
2009-08-28 13:43:06 +02:00
|
|
|
start -= 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
if (ch2 == QLatin1Char('.')) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_DOT_STAR;
|
2009-08-28 13:43:06 +02:00
|
|
|
start -= 2;
|
|
|
|
|
} else if (ch3 == QLatin1Char('-') && ch2 == QLatin1Char('>')) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_ARROW_STAR;
|
2009-08-28 13:43:06 +02:00
|
|
|
start -= 3;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '\\':
|
|
|
|
|
case '@':
|
|
|
|
|
if (ch2.isNull() || ch2.isSpace()) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_DOXY_COMMENT;
|
2009-08-28 13:43:06 +02:00
|
|
|
--start;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case '<':
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_ANGLE_STRING_LITERAL;
|
2009-07-23 16:46:30 +02:00
|
|
|
--start;
|
2009-08-28 13:43:06 +02:00
|
|
|
break;
|
|
|
|
|
case '"':
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_STRING_LITERAL;
|
2009-07-23 16:46:30 +02:00
|
|
|
--start;
|
2009-08-28 13:43:06 +02:00
|
|
|
break;
|
|
|
|
|
case '/':
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_SLASH;
|
2009-07-23 16:46:30 +02:00
|
|
|
--start;
|
2009-08-28 13:43:06 +02:00
|
|
|
break;
|
2010-05-18 17:57:49 +02:00
|
|
|
case '#':
|
|
|
|
|
completionKind = T_POUND;
|
|
|
|
|
--start;
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:55:01 +01:00
|
|
|
if (start == pos)
|
|
|
|
|
return start;
|
|
|
|
|
|
|
|
|
|
TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(editor->widget());
|
|
|
|
|
QTextCursor tc(edit->textCursor());
|
|
|
|
|
tc.setPosition(pos);
|
|
|
|
|
|
2009-07-23 16:46:30 +02:00
|
|
|
// Include completion: make sure the quote character is the first one on the line
|
2009-10-20 16:37:01 +02:00
|
|
|
if (completionKind == T_STRING_LITERAL) {
|
2009-07-23 16:46:30 +02:00
|
|
|
QTextCursor s = tc;
|
|
|
|
|
s.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
|
|
|
|
QString sel = s.selectedText();
|
|
|
|
|
if (sel.indexOf(QLatin1Char('"')) < sel.length() - 1) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_EOF_SYMBOL;
|
2009-07-23 16:46:30 +02:00
|
|
|
start = pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-20 16:37:01 +02:00
|
|
|
if (completionKind == T_COMMA) {
|
2010-06-29 17:47:59 +02:00
|
|
|
ExpressionUnderCursor expressionUnderCursor;
|
2009-10-08 15:32:12 +02:00
|
|
|
if (expressionUnderCursor.startOfFunctionCall(tc) == -1) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_EOF_SYMBOL;
|
2009-10-08 15:32:12 +02:00
|
|
|
start = pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-29 17:47:59 +02:00
|
|
|
SimpleLexer tokenize;
|
|
|
|
|
tokenize.setQtMocRunEnabled(true);
|
2010-07-06 10:57:39 +02:00
|
|
|
tokenize.setObjCEnabled(true);
|
2010-06-29 17:47:59 +02:00
|
|
|
tokenize.setSkipComments(false);
|
2010-07-06 10:57:39 +02:00
|
|
|
const QList<Token> &tokens = tokenize(tc.block().text(), BackwardsScanner::previousBlockState(tc.block()));
|
|
|
|
|
const int tokenIdx = SimpleLexer::tokenBefore(tokens, qMax(0, tc.positionInBlock() - 1)); // get the token at the left of the cursor
|
2010-07-05 11:03:52 +02:00
|
|
|
const Token tk = (tokenIdx == -1) ? Token() : tokens.at(tokenIdx);
|
2009-02-20 12:55:01 +01:00
|
|
|
|
2009-10-20 16:37:01 +02:00
|
|
|
if (completionKind == T_DOXY_COMMENT && !(tk.is(T_DOXY_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))) {
|
|
|
|
|
completionKind = T_EOF_SYMBOL;
|
2009-02-20 12:55:01 +01:00
|
|
|
start = pos;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-07-23 16:46:30 +02:00
|
|
|
// Don't complete in comments or strings, but still check for include completion
|
2009-10-20 16:37:01 +02:00
|
|
|
else if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT) ||
|
|
|
|
|
(tk.isLiteral() && (completionKind != T_STRING_LITERAL
|
|
|
|
|
&& completionKind != T_ANGLE_STRING_LITERAL
|
|
|
|
|
&& completionKind != T_SLASH))) {
|
|
|
|
|
completionKind = T_EOF_SYMBOL;
|
2009-07-23 16:46:30 +02:00
|
|
|
start = pos;
|
|
|
|
|
}
|
|
|
|
|
// Include completion: can be triggered by slash, but only in a string
|
2009-10-20 16:37:01 +02:00
|
|
|
else if (completionKind == T_SLASH && (tk.isNot(T_STRING_LITERAL) && tk.isNot(T_ANGLE_STRING_LITERAL))) {
|
|
|
|
|
completionKind = T_EOF_SYMBOL;
|
2009-02-20 12:55:01 +01:00
|
|
|
start = pos;
|
|
|
|
|
}
|
2009-10-20 16:37:01 +02:00
|
|
|
else if (completionKind == T_LPAREN) {
|
2010-07-05 11:03:52 +02:00
|
|
|
if (tokenIdx > 0) {
|
|
|
|
|
const Token &previousToken = tokens.at(tokenIdx - 1); // look at the token at the left of T_LPAREN
|
|
|
|
|
switch (previousToken.kind()) {
|
|
|
|
|
case T_IDENTIFIER:
|
|
|
|
|
case T_GREATER:
|
|
|
|
|
case T_SIGNAL:
|
|
|
|
|
case T_SLOT:
|
|
|
|
|
break; // good
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// that's a bad token :)
|
|
|
|
|
completionKind = T_EOF_SYMBOL;
|
|
|
|
|
start = pos;
|
2009-05-27 11:12:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-07-23 16:46:30 +02:00
|
|
|
// Check for include preprocessor directive
|
2009-10-20 16:37:01 +02:00
|
|
|
else if (completionKind == T_STRING_LITERAL || completionKind == T_ANGLE_STRING_LITERAL || completionKind == T_SLASH) {
|
2009-07-23 16:46:30 +02:00
|
|
|
bool include = false;
|
2009-10-08 15:59:43 +02:00
|
|
|
if (tokens.size() >= 3) {
|
|
|
|
|
if (tokens.at(0).is(T_POUND) && tokens.at(1).is(T_IDENTIFIER) && (tokens.at(2).is(T_STRING_LITERAL) ||
|
|
|
|
|
tokens.at(2).is(T_ANGLE_STRING_LITERAL))) {
|
2010-06-29 17:57:15 +02:00
|
|
|
const Token &directiveToken = tokens.at(1);
|
|
|
|
|
QString directive = tc.block().text().mid(directiveToken.begin(),
|
2010-06-29 17:47:59 +02:00
|
|
|
directiveToken.length());
|
2009-10-08 15:59:43 +02:00
|
|
|
if (directive == QLatin1String("include") ||
|
|
|
|
|
directive == QLatin1String("include_next") ||
|
|
|
|
|
directive == QLatin1String("import")) {
|
|
|
|
|
include = true;
|
2009-07-23 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!include) {
|
2009-10-20 16:37:01 +02:00
|
|
|
completionKind = T_EOF_SYMBOL;
|
2009-07-23 16:46:30 +02:00
|
|
|
start = pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-20 12:55:01 +01:00
|
|
|
if (kind)
|
2009-10-20 16:37:01 +02:00
|
|
|
*kind = completionKind;
|
2009-02-20 12:55:01 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return start;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-07 15:56:02 +02:00
|
|
|
bool CppCodeCompletion::supportsEditor(TextEditor::ITextEditable *editor)
|
2009-04-02 10:19:23 +02:00
|
|
|
{ return m_manager->isCppEditor(editor); }
|
|
|
|
|
|
2010-01-26 11:44:45 +01:00
|
|
|
TextEditor::ITextEditable *CppCodeCompletion::editor() const
|
|
|
|
|
{ return m_editor; }
|
|
|
|
|
|
|
|
|
|
int CppCodeCompletion::startPosition() const
|
|
|
|
|
{ return m_startPosition; }
|
|
|
|
|
|
2010-07-14 12:37:49 +02:00
|
|
|
bool CppCodeCompletion::shouldRestartCompletion()
|
|
|
|
|
{ return m_shouldRestartCompletion; }
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
|
|
|
|
|
{
|
2010-07-14 12:37:49 +02:00
|
|
|
m_editor = editor;
|
2010-07-19 10:25:55 +02:00
|
|
|
m_automaticCompletion = false;
|
2010-07-14 12:37:49 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
const int pos = editor->position();
|
2010-06-02 15:22:42 +02:00
|
|
|
unsigned token = T_EOF_SYMBOL;
|
|
|
|
|
|
2010-06-29 17:47:59 +02:00
|
|
|
if (startOfOperator(editor, pos, &token, /*want function call=*/ true) != pos) {
|
2010-06-02 15:22:42 +02:00
|
|
|
if (token == T_POUND) {
|
2010-07-19 10:25:55 +02:00
|
|
|
int line, column;
|
|
|
|
|
editor->convertPosition(pos, &line, &column);
|
|
|
|
|
if (column != 1)
|
|
|
|
|
return false;
|
2010-05-19 14:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
2010-07-15 16:05:43 +02:00
|
|
|
} else if (completionSettings().m_completionTrigger == TextEditor::AutomaticCompletion) {
|
2010-07-15 18:22:44 +02:00
|
|
|
// Trigger completion after three characters of a name have been typed, when not editing an existing name
|
|
|
|
|
QChar characterUnderCursor = editor->characterAt(pos);
|
|
|
|
|
if (!characterUnderCursor.isLetterOrNumber()) {
|
|
|
|
|
const int startOfName = findStartOfName(pos);
|
2010-07-19 14:06:00 +02:00
|
|
|
if (pos - startOfName >= 3) {
|
2010-07-15 18:22:44 +02:00
|
|
|
const QChar firstCharacter = editor->characterAt(startOfName);
|
|
|
|
|
if (firstCharacter.isLetter() || firstCharacter == QLatin1Char('_')) {
|
|
|
|
|
// Finally check that we're not inside a comment or string (code copied from startOfOperator)
|
|
|
|
|
TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(editor->widget());
|
|
|
|
|
QTextCursor tc(edit->textCursor());
|
|
|
|
|
tc.setPosition(pos);
|
|
|
|
|
|
|
|
|
|
SimpleLexer tokenize;
|
|
|
|
|
tokenize.setQtMocRunEnabled(true);
|
|
|
|
|
tokenize.setObjCEnabled(true);
|
|
|
|
|
tokenize.setSkipComments(false);
|
|
|
|
|
const QList<Token> &tokens = tokenize(tc.block().text(), BackwardsScanner::previousBlockState(tc.block()));
|
|
|
|
|
const int tokenIdx = SimpleLexer::tokenBefore(tokens, qMax(0, tc.positionInBlock() - 1));
|
|
|
|
|
const Token tk = (tokenIdx == -1) ? Token() : tokens.at(tokenIdx);
|
|
|
|
|
|
2010-07-19 10:25:55 +02:00
|
|
|
if (!tk.isComment() && !tk.isLiteral()) {
|
|
|
|
|
m_automaticCompletion = true;
|
2010-07-15 18:22:44 +02:00
|
|
|
return true;
|
2010-07-19 10:25:55 +02:00
|
|
|
}
|
2010-07-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-07-14 12:37:49 +02:00
|
|
|
}
|
2010-01-11 13:47:15 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
|
|
|
|
{
|
2010-05-26 14:25:01 +02:00
|
|
|
int index = startCompletionHelper(editor);
|
2010-05-27 19:00:44 +02:00
|
|
|
if (index != -1) {
|
2010-12-14 11:50:49 +01:00
|
|
|
if (m_automaticCompletion) {
|
|
|
|
|
const int pos = editor->position();
|
|
|
|
|
const QChar ch = editor->characterAt(pos);
|
|
|
|
|
if (! (ch.isLetterOrNumber() || ch == QLatin1Char('_'))) {
|
|
|
|
|
for (int i = pos - 1;; --i) {
|
|
|
|
|
const QChar ch = editor->characterAt(i);
|
|
|
|
|
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
|
|
|
|
|
const QString wordUnderCursor = editor->textAt(i, pos - i);
|
|
|
|
|
if (wordUnderCursor.at(0).isLetter() || wordUnderCursor.at(0) == QLatin1Char('_')) {
|
|
|
|
|
foreach (const TextEditor::CompletionItem &i, m_completions) {
|
|
|
|
|
if (i.text == wordUnderCursor) {
|
|
|
|
|
cleanup();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
cleanup();
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-27 19:00:44 +02:00
|
|
|
if (m_completionOperator != T_EOF_SYMBOL)
|
2010-05-28 12:51:12 +02:00
|
|
|
qSort(m_completions.begin(), m_completions.end(), completionItemLessThan);
|
|
|
|
|
|
|
|
|
|
// always remove duplicates
|
|
|
|
|
m_completions = removeDuplicates(m_completions);
|
2010-05-27 19:00:44 +02:00
|
|
|
}
|
2010-08-03 16:13:00 +02:00
|
|
|
|
|
|
|
|
for (int i = 0; i < m_completions.size(); ++i)
|
|
|
|
|
m_completions[i].originalIndex = i;
|
|
|
|
|
|
2010-05-26 14:25:01 +02:00
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-18 12:34:32 +02:00
|
|
|
void CppCodeCompletion::completeObjCMsgSend(ClassOrNamespace *binding,
|
|
|
|
|
bool staticClassAccess)
|
|
|
|
|
{
|
|
|
|
|
QList<Scope*> memberScopes;
|
|
|
|
|
foreach (Symbol *s, binding->symbols()) {
|
|
|
|
|
if (ObjCClass *c = s->asObjCClass())
|
2010-08-11 12:26:02 +02:00
|
|
|
memberScopes.append(c);
|
2010-06-18 12:34:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Scope *scope, memberScopes) {
|
2010-08-11 12:26:02 +02:00
|
|
|
for (unsigned i = 0; i < scope->memberCount(); ++i) {
|
|
|
|
|
Symbol *symbol = scope->memberAt(i);
|
2010-06-18 12:34:32 +02:00
|
|
|
|
|
|
|
|
if (ObjCMethod *method = symbol->type()->asObjCMethodType()) {
|
|
|
|
|
if (method->isStatic() == staticClassAccess) {
|
|
|
|
|
Overview oo;
|
|
|
|
|
const SelectorNameId *selectorName =
|
|
|
|
|
method->name()->asSelectorNameId();
|
|
|
|
|
QString text;
|
|
|
|
|
QString data;
|
|
|
|
|
if (selectorName->hasArguments()) {
|
|
|
|
|
for (unsigned i = 0; i < selectorName->nameCount(); ++i) {
|
|
|
|
|
if (i > 0)
|
|
|
|
|
text += QLatin1Char(' ');
|
|
|
|
|
Symbol *arg = method->argumentAt(i);
|
|
|
|
|
text += selectorName->nameAt(i)->identifier()->chars();
|
|
|
|
|
text += QLatin1Char(':');
|
2010-10-27 17:38:22 +02:00
|
|
|
text += TextEditor::Snippet::kVariableDelimiter;
|
2010-06-18 12:34:32 +02:00
|
|
|
text += QLatin1Char('(');
|
|
|
|
|
text += oo(arg->type());
|
|
|
|
|
text += QLatin1Char(')');
|
|
|
|
|
text += oo(arg->name());
|
2010-10-27 17:38:22 +02:00
|
|
|
text += TextEditor::Snippet::kVariableDelimiter;
|
2010-06-18 12:34:32 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
text = selectorName->identifier()->chars();
|
|
|
|
|
}
|
|
|
|
|
data = text;
|
|
|
|
|
|
|
|
|
|
if (!text.isEmpty()) {
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
item.text = text;
|
|
|
|
|
item.data = QVariant::fromValue(data);
|
|
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppCodeCompletion::tryObjCCompletion(TextEditor::BaseTextEditor *edit)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(edit);
|
|
|
|
|
|
|
|
|
|
int end = m_editor->position();
|
|
|
|
|
while (m_editor->characterAt(end).isSpace())
|
|
|
|
|
++end;
|
|
|
|
|
if (m_editor->characterAt(end) != QLatin1Char(']'))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QTextCursor tc(edit->document());
|
|
|
|
|
tc.setPosition(end);
|
|
|
|
|
BackwardsScanner tokens(tc);
|
|
|
|
|
if (tokens[tokens.startToken() - 1].isNot(T_RBRACKET))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const int start = tokens.startOfMatchingBrace(tokens.startToken());
|
|
|
|
|
if (start == tokens.startToken())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const int startPos = tokens[start].begin() + tokens.startPosition();
|
|
|
|
|
const QString expr = m_editor->textAt(startPos, m_editor->position() - startPos);
|
|
|
|
|
|
|
|
|
|
const Snapshot snapshot = m_manager->snapshot();
|
|
|
|
|
Document::Ptr thisDocument = snapshot.document(m_editor->file()->fileName());
|
|
|
|
|
if (! thisDocument)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
typeOfExpression.init(thisDocument, snapshot);
|
|
|
|
|
int line = 0, column = 0;
|
|
|
|
|
edit->convertPosition(m_editor->position(), &line, &column);
|
|
|
|
|
Scope *scope = thisDocument->scopeAt(line, column);
|
|
|
|
|
if (!scope)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const QList<LookupItem> items = typeOfExpression(expr, scope);
|
|
|
|
|
LookupContext lookupContext(thisDocument, snapshot);
|
|
|
|
|
|
|
|
|
|
foreach (const LookupItem &item, items) {
|
|
|
|
|
FullySpecifiedType ty = item.type().simplified();
|
|
|
|
|
if (ty->isPointerType()) {
|
|
|
|
|
ty = ty->asPointerType()->elementType().simplified();
|
|
|
|
|
|
|
|
|
|
if (NamedType *namedTy = ty->asNamedType()) {
|
|
|
|
|
ClassOrNamespace *binding = lookupContext.lookupType(namedTy->name(), item.scope());
|
|
|
|
|
completeObjCMsgSend(binding, false);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (ObjCClass *clazz = ty->asObjCClassType()) {
|
|
|
|
|
ClassOrNamespace *binding = lookupContext.lookupType(clazz->name(), item.scope());
|
|
|
|
|
completeObjCMsgSend(binding, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_completions.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_startPosition = m_editor->position();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 14:25:01 +02:00
|
|
|
int CppCodeCompletion::startCompletionHelper(TextEditor::ITextEditable *editor)
|
|
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(editor->widget());
|
|
|
|
|
if (! edit)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
m_editor = editor;
|
2010-01-11 13:47:15 +01:00
|
|
|
|
2010-06-18 12:34:32 +02:00
|
|
|
if (m_objcEnabled) {
|
|
|
|
|
if (tryObjCCompletion(edit))
|
|
|
|
|
return m_startPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-26 15:12:12 +01:00
|
|
|
const int startOfName = findStartOfName();
|
|
|
|
|
m_startPosition = startOfName;
|
2008-12-02 12:01:29 +01:00
|
|
|
m_completionOperator = T_EOF_SYMBOL;
|
|
|
|
|
|
2009-01-26 15:51:14 +01:00
|
|
|
int endOfOperator = m_startPosition;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Skip whitespace preceding this position
|
2009-01-26 15:51:14 +01:00
|
|
|
while (editor->characterAt(endOfOperator - 1).isSpace())
|
|
|
|
|
--endOfOperator;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-29 17:47:59 +02:00
|
|
|
int endOfExpression = startOfOperator(editor, endOfOperator,
|
2009-01-26 15:51:14 +01:00
|
|
|
&m_completionOperator,
|
|
|
|
|
/*want function call =*/ true);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
Core::IFile *file = editor->file();
|
|
|
|
|
QString fileName = file->fileName();
|
|
|
|
|
|
2009-02-20 12:55:01 +01:00
|
|
|
if (m_completionOperator == T_DOXY_COMMENT) {
|
|
|
|
|
for (int i = 1; i < T_DOXY_LAST_TAG; ++i) {
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
2009-10-05 18:32:08 +02:00
|
|
|
item.text.append(QString::fromLatin1(doxygenTagSpell(i)));
|
|
|
|
|
item.icon = m_icons.keywordIcon();
|
2009-02-20 12:55:01 +01:00
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_startPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-18 17:57:49 +02:00
|
|
|
// Pre-processor completion
|
|
|
|
|
if (m_completionOperator == T_POUND) {
|
|
|
|
|
completePreprocessor();
|
|
|
|
|
m_startPosition = startOfName;
|
|
|
|
|
return m_startPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-23 16:46:30 +02:00
|
|
|
// Include completion
|
|
|
|
|
if (m_completionOperator == T_STRING_LITERAL
|
|
|
|
|
|| m_completionOperator == T_ANGLE_STRING_LITERAL
|
|
|
|
|
|| m_completionOperator == T_SLASH) {
|
|
|
|
|
|
|
|
|
|
QTextCursor c = edit->textCursor();
|
|
|
|
|
c.setPosition(endOfExpression);
|
|
|
|
|
if (completeInclude(c))
|
|
|
|
|
m_startPosition = startOfName;
|
|
|
|
|
return m_startPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-29 17:47:59 +02:00
|
|
|
ExpressionUnderCursor expressionUnderCursor;
|
2009-03-26 12:37:40 +01:00
|
|
|
QTextCursor tc(edit->document());
|
2009-02-20 12:55:01 +01:00
|
|
|
|
2009-03-26 12:37:40 +01:00
|
|
|
if (m_completionOperator == T_COMMA) {
|
2008-12-02 12:01:29 +01:00
|
|
|
tc.setPosition(endOfExpression);
|
2009-03-26 12:37:40 +01:00
|
|
|
const int start = expressionUnderCursor.startOfFunctionCall(tc);
|
2009-10-08 15:32:12 +02:00
|
|
|
if (start == -1) {
|
|
|
|
|
m_completionOperator = T_EOF_SYMBOL;
|
|
|
|
|
return -1;
|
2009-03-26 12:37:40 +01:00
|
|
|
}
|
2009-10-08 15:32:12 +02:00
|
|
|
|
|
|
|
|
endOfExpression = start;
|
|
|
|
|
m_startPosition = start + 1;
|
|
|
|
|
m_completionOperator = T_LPAREN;
|
2009-03-26 12:37:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString expression;
|
2010-07-03 18:41:31 +02:00
|
|
|
int startOfExpression = editor->position();
|
2009-03-26 12:37:40 +01:00
|
|
|
tc.setPosition(endOfExpression);
|
2009-02-20 12:55:01 +01:00
|
|
|
|
2009-03-26 12:37:40 +01:00
|
|
|
if (m_completionOperator) {
|
2008-12-02 12:01:29 +01:00
|
|
|
expression = expressionUnderCursor(tc);
|
2010-07-03 18:41:31 +02:00
|
|
|
startOfExpression = endOfExpression - expression.length();
|
2009-02-20 12:55:01 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (m_completionOperator == T_LPAREN) {
|
|
|
|
|
if (expression.endsWith(QLatin1String("SIGNAL")))
|
|
|
|
|
m_completionOperator = T_SIGNAL;
|
2009-02-20 12:55:01 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
else if (expression.endsWith(QLatin1String("SLOT")))
|
|
|
|
|
m_completionOperator = T_SLOT;
|
2009-02-20 12:55:01 +01:00
|
|
|
|
2009-01-26 15:51:14 +01:00
|
|
|
else if (editor->position() != endOfOperator) {
|
|
|
|
|
// We don't want a function completion when the cursor isn't at the opening brace
|
|
|
|
|
expression.clear();
|
|
|
|
|
m_completionOperator = T_EOF_SYMBOL;
|
2009-03-26 15:12:12 +01:00
|
|
|
m_startPosition = startOfName;
|
2010-07-03 18:41:31 +02:00
|
|
|
startOfExpression = editor->position();
|
2009-01-26 15:51:14 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-07-03 18:41:31 +02:00
|
|
|
} else if (expression.isEmpty()) {
|
|
|
|
|
while (startOfExpression > 0 && editor->characterAt(startOfExpression).isSpace())
|
|
|
|
|
--startOfExpression;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-03 18:41:31 +02:00
|
|
|
int line = 0, column = 0;
|
|
|
|
|
edit->convertPosition(startOfExpression, &line, &column);
|
|
|
|
|
// qDebug() << "***** line:" << line << "column:" << column;
|
|
|
|
|
// qDebug() << "***** expression:" << expression;
|
2010-01-11 13:47:15 +01:00
|
|
|
return startCompletionInternal(edit, fileName, line, column, expression, endOfExpression);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
int CppCodeCompletion::startCompletionInternal(TextEditor::BaseTextEditor *edit,
|
|
|
|
|
const QString fileName,
|
|
|
|
|
unsigned line, unsigned column,
|
|
|
|
|
const QString &expr,
|
|
|
|
|
int endOfExpression)
|
|
|
|
|
{
|
|
|
|
|
QString expression = expr.trimmed();
|
2008-12-12 10:07:58 +01:00
|
|
|
const Snapshot snapshot = m_manager->snapshot();
|
|
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
Document::Ptr thisDocument = snapshot.document(fileName);
|
|
|
|
|
if (! thisDocument)
|
|
|
|
|
return -1;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-05 12:06:38 +02:00
|
|
|
typeOfExpression.init(thisDocument, snapshot);
|
2010-05-26 12:35:20 +02:00
|
|
|
|
|
|
|
|
Scope *scope = thisDocument->scopeAt(line, column);
|
|
|
|
|
Q_ASSERT(scope != 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
if (expression.isEmpty()) {
|
2010-05-26 12:35:20 +02:00
|
|
|
if (m_completionOperator == T_EOF_SYMBOL || m_completionOperator == T_COLON_COLON) {
|
|
|
|
|
(void) typeOfExpression(expression, scope);
|
|
|
|
|
globalCompletion(scope);
|
|
|
|
|
if (m_completions.isEmpty())
|
|
|
|
|
return -1;
|
|
|
|
|
return m_startPosition;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
else if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
|
2008-12-02 12:01:29 +01:00
|
|
|
// Apply signal/slot completion on 'this'
|
|
|
|
|
expression = QLatin1String("this");
|
|
|
|
|
}
|
2010-01-11 13:47:15 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-12 12:53:16 +02:00
|
|
|
QList<LookupItem> results = typeOfExpression(expression, scope, TypeOfExpression::Preprocess);
|
2010-05-12 14:52:24 +02:00
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
if (results.isEmpty()) {
|
|
|
|
|
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
|
|
|
|
|
if (! (expression.isEmpty() || expression == QLatin1String("this"))) {
|
|
|
|
|
expression = QLatin1String("this");
|
2010-05-12 12:53:16 +02:00
|
|
|
results = typeOfExpression(expression, scope);
|
2010-01-11 13:47:15 +01:00
|
|
|
}
|
2009-02-17 12:12:14 +01:00
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
if (results.isEmpty())
|
|
|
|
|
return -1;
|
2009-02-17 12:12:14 +01:00
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
} else if (m_completionOperator == T_LPAREN) {
|
2009-02-04 16:40:42 +01:00
|
|
|
// Find the expression that precedes the current name
|
|
|
|
|
int index = endOfExpression;
|
|
|
|
|
while (m_editor->characterAt(index - 1).isSpace())
|
|
|
|
|
--index;
|
|
|
|
|
index = findStartOfName(index);
|
|
|
|
|
|
|
|
|
|
QTextCursor tc(edit->document());
|
|
|
|
|
tc.setPosition(index);
|
2009-10-16 12:22:33 +02:00
|
|
|
|
2010-06-29 17:47:59 +02:00
|
|
|
ExpressionUnderCursor expressionUnderCursor;
|
2009-10-16 12:22:33 +02:00
|
|
|
const QString baseExpression = expressionUnderCursor(tc);
|
2009-02-04 16:40:42 +01:00
|
|
|
|
|
|
|
|
// Resolve the type of this expression
|
2009-11-17 14:21:46 +01:00
|
|
|
const QList<LookupItem> results =
|
2010-05-12 12:53:16 +02:00
|
|
|
typeOfExpression(baseExpression, scope,
|
2009-10-16 12:22:33 +02:00
|
|
|
TypeOfExpression::Preprocess);
|
2009-02-04 16:40:42 +01:00
|
|
|
|
|
|
|
|
// If it's a class, add completions for the constructors
|
2009-11-17 14:21:46 +01:00
|
|
|
foreach (const LookupItem &result, results) {
|
|
|
|
|
if (result.type()->isClassType()) {
|
2010-05-17 16:27:54 +02:00
|
|
|
if (completeConstructorOrFunction(results, endOfExpression, true))
|
2009-02-04 16:40:42 +01:00
|
|
|
return m_startPosition;
|
2010-01-11 13:47:15 +01:00
|
|
|
|
2009-02-04 16:40:42 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-11 13:47:15 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// nothing to do.
|
|
|
|
|
return -1;
|
|
|
|
|
|
2009-02-04 16:40:42 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
switch (m_completionOperator) {
|
|
|
|
|
case T_LPAREN:
|
2010-05-17 16:27:54 +02:00
|
|
|
if (completeConstructorOrFunction(results, endOfExpression, false))
|
2010-01-11 13:47:15 +01:00
|
|
|
return m_startPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_DOT:
|
|
|
|
|
case T_ARROW:
|
2010-05-17 16:27:54 +02:00
|
|
|
if (completeMember(results))
|
2010-01-11 13:47:15 +01:00
|
|
|
return m_startPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_COLON_COLON:
|
2010-05-17 16:27:54 +02:00
|
|
|
if (completeScope(results))
|
2010-01-11 13:47:15 +01:00
|
|
|
return m_startPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_SIGNAL:
|
2010-05-17 16:27:54 +02:00
|
|
|
if (completeSignal(results))
|
2010-01-11 13:47:15 +01:00
|
|
|
return m_startPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_SLOT:
|
2010-05-17 16:27:54 +02:00
|
|
|
if (completeSlot(results))
|
2010-01-11 13:47:15 +01:00
|
|
|
return m_startPosition;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
} // end of switch
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// nothing to do.
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-26 12:35:20 +02:00
|
|
|
void CppCodeCompletion::globalCompletion(Scope *currentScope)
|
2010-01-11 13:47:15 +01:00
|
|
|
{
|
2010-05-26 12:35:20 +02:00
|
|
|
const LookupContext &context = typeOfExpression.context();
|
|
|
|
|
|
|
|
|
|
if (m_completionOperator == T_COLON_COLON) {
|
|
|
|
|
completeNamespace(context.globalNamespace());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<ClassOrNamespace *> usingBindings;
|
|
|
|
|
ClassOrNamespace *currentBinding = 0;
|
|
|
|
|
|
2010-08-26 16:16:22 +02:00
|
|
|
for (Scope *scope = currentScope; scope; scope = scope->enclosingScope()) {
|
2010-08-11 12:26:02 +02:00
|
|
|
if (scope->isBlock()) {
|
|
|
|
|
if (ClassOrNamespace *binding = context.lookupType(scope)) {
|
|
|
|
|
for (unsigned i = 0; i < scope->memberCount(); ++i) {
|
|
|
|
|
Symbol *member = scope->memberAt(i);
|
2010-05-26 12:35:20 +02:00
|
|
|
if (! member->name())
|
|
|
|
|
continue;
|
|
|
|
|
else if (UsingNamespaceDirective *u = member->asUsingNamespaceDirective()) {
|
|
|
|
|
if (ClassOrNamespace *b = binding->lookupType(u->name()))
|
|
|
|
|
usingBindings.append(b);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-08-11 12:26:02 +02:00
|
|
|
} else if (scope->isFunction() || scope->isClass() || scope->isNamespace()) {
|
|
|
|
|
currentBinding = context.lookupType(scope);
|
2010-05-26 12:35:20 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2010-01-11 13:47:15 +01:00
|
|
|
}
|
|
|
|
|
|
2010-08-26 16:16:22 +02:00
|
|
|
for (Scope *scope = currentScope; scope; scope = scope->enclosingScope()) {
|
2010-08-11 12:26:02 +02:00
|
|
|
if (scope->isBlock()) {
|
|
|
|
|
for (unsigned i = 0; i < scope->memberCount(); ++i) {
|
|
|
|
|
addCompletionItem(scope->memberAt(i));
|
2010-05-26 12:35:20 +02:00
|
|
|
}
|
2010-08-11 12:26:02 +02:00
|
|
|
} else if (scope->isFunction()) {
|
|
|
|
|
Function *fun = scope->asFunction();
|
2010-08-05 17:02:25 +02:00
|
|
|
for (unsigned i = 0; i < fun->argumentCount(); ++i) {
|
|
|
|
|
addCompletionItem(fun->argumentAt(i));
|
2010-05-26 12:35:20 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-27 19:00:44 +02:00
|
|
|
|
|
|
|
|
for (; currentBinding; currentBinding = currentBinding->parent()) {
|
|
|
|
|
const QList<Symbol *> symbols = currentBinding->symbols();
|
|
|
|
|
|
|
|
|
|
if (! symbols.isEmpty()) {
|
|
|
|
|
if (symbols.first()->isNamespace())
|
|
|
|
|
completeNamespace(currentBinding);
|
|
|
|
|
else
|
2010-06-02 13:59:01 +02:00
|
|
|
completeClass(currentBinding);
|
2010-05-27 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (ClassOrNamespace *b, usingBindings)
|
|
|
|
|
completeNamespace(b);
|
|
|
|
|
|
|
|
|
|
addKeywords();
|
2010-08-04 13:10:41 +02:00
|
|
|
addMacros(QLatin1String("<configuration>"), context.snapshot());
|
2010-05-27 19:00:44 +02:00
|
|
|
addMacros(context.thisDocument()->fileName(), context.snapshot());
|
2010-12-03 13:43:13 +01:00
|
|
|
addSnippets();
|
2010-01-11 13:47:15 +01:00
|
|
|
}
|
|
|
|
|
|
2010-11-08 11:29:44 +01:00
|
|
|
static Scope *enclosingNonTemplateScope(Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
if (symbol) {
|
|
|
|
|
if (Scope *scope = symbol->enclosingScope()) {
|
|
|
|
|
if (Template *templ = scope->asTemplate())
|
|
|
|
|
return templ->enclosingScope();
|
|
|
|
|
return scope;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Function *asFunctionOrTemplateFunctionType(FullySpecifiedType ty)
|
|
|
|
|
{
|
|
|
|
|
if (Function *funTy = ty->asFunctionType())
|
|
|
|
|
return funTy;
|
|
|
|
|
else if (Template *templ = ty->asTemplateType()) {
|
|
|
|
|
if (Symbol *decl = templ->declaration())
|
|
|
|
|
return decl->asFunction();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Class *asClassOrTemplateClassType(FullySpecifiedType ty)
|
|
|
|
|
{
|
|
|
|
|
if (Class *classTy = ty->asClassType())
|
|
|
|
|
return classTy;
|
|
|
|
|
else if (Template *templ = ty->asTemplateType()) {
|
|
|
|
|
if (Symbol *decl = templ->declaration())
|
|
|
|
|
return decl->asClass();
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-17 14:21:46 +01:00
|
|
|
bool CppCodeCompletion::completeConstructorOrFunction(const QList<LookupItem> &results,
|
2009-11-09 10:10:31 +01:00
|
|
|
int endOfExpression, bool toolTipOnly)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-05-17 16:27:54 +02:00
|
|
|
const LookupContext &context = typeOfExpression.context();
|
2009-03-26 11:14:05 +01:00
|
|
|
QList<Function *> functions;
|
|
|
|
|
|
2009-11-17 14:21:46 +01:00
|
|
|
foreach (const LookupItem &result, results) {
|
|
|
|
|
FullySpecifiedType exprTy = result.type().simplified();
|
2009-02-17 11:50:56 +01:00
|
|
|
|
2010-11-08 11:29:44 +01:00
|
|
|
if (Class *klass = asClassOrTemplateClassType(exprTy)) {
|
2009-12-01 12:46:15 +01:00
|
|
|
const Name *className = klass->name();
|
2009-05-19 12:15:30 +02:00
|
|
|
if (! className)
|
2010-11-08 11:29:44 +01:00
|
|
|
continue; // nothing to do for anonymous classes.
|
2009-05-19 12:15:30 +02:00
|
|
|
|
2009-02-17 11:50:56 +01:00
|
|
|
for (unsigned i = 0; i < klass->memberCount(); ++i) {
|
|
|
|
|
Symbol *member = klass->memberAt(i);
|
2009-12-01 12:46:15 +01:00
|
|
|
const Name *memberName = member->name();
|
2009-05-19 12:15:30 +02:00
|
|
|
|
|
|
|
|
if (! memberName)
|
|
|
|
|
continue; // skip anonymous member.
|
|
|
|
|
|
|
|
|
|
else if (memberName->isQualifiedNameId())
|
|
|
|
|
continue; // skip
|
|
|
|
|
|
|
|
|
|
if (Function *funTy = member->type()->asFunctionType()) {
|
|
|
|
|
if (memberName->isEqualTo(className)) {
|
|
|
|
|
// it's a ctor.
|
|
|
|
|
functions.append(funTy);
|
|
|
|
|
}
|
2009-02-17 11:50:56 +01:00
|
|
|
}
|
2009-02-12 11:41:52 +01:00
|
|
|
}
|
2009-02-17 11:50:56 +01:00
|
|
|
|
|
|
|
|
break;
|
2009-02-12 11:41:52 +01:00
|
|
|
}
|
2009-02-17 11:50:56 +01:00
|
|
|
}
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
if (functions.isEmpty()) {
|
2009-11-17 14:21:46 +01:00
|
|
|
foreach (const LookupItem &result, results) {
|
|
|
|
|
FullySpecifiedType ty = result.type().simplified();
|
2009-02-17 11:50:56 +01:00
|
|
|
|
2010-11-08 11:29:44 +01:00
|
|
|
if (Function *fun = asFunctionOrTemplateFunctionType(ty)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-05-19 12:15:30 +02:00
|
|
|
if (! fun->name())
|
|
|
|
|
continue;
|
2010-11-08 11:29:44 +01:00
|
|
|
else if (! functions.isEmpty() && enclosingNonTemplateScope(functions.first()) != enclosingNonTemplateScope(fun))
|
2009-05-19 12:15:30 +02:00
|
|
|
continue; // skip fun, it's an hidden declaration.
|
|
|
|
|
|
|
|
|
|
bool newOverload = true;
|
|
|
|
|
|
|
|
|
|
foreach (Function *f, functions) {
|
|
|
|
|
if (fun->isEqualTo(f)) {
|
|
|
|
|
newOverload = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-05-19 12:15:30 +02:00
|
|
|
|
|
|
|
|
if (newOverload)
|
|
|
|
|
functions.append(fun);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-16 12:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (functions.isEmpty()) {
|
2009-12-01 12:46:15 +01:00
|
|
|
const Name *functionCallOp = context.control()->operatorNameId(OperatorNameId::FunctionCallOp);
|
2009-05-19 12:15:30 +02:00
|
|
|
|
2009-11-17 14:21:46 +01:00
|
|
|
foreach (const LookupItem &result, results) {
|
|
|
|
|
FullySpecifiedType ty = result.type().simplified();
|
2010-05-12 12:53:16 +02:00
|
|
|
Scope *scope = result.scope();
|
2009-10-16 12:22:33 +02:00
|
|
|
|
|
|
|
|
if (NamedType *namedTy = ty->asNamedType()) {
|
2010-05-14 13:44:54 +02:00
|
|
|
if (ClassOrNamespace *b = context.lookupType(namedTy->name(), scope)) {
|
2010-07-16 11:03:39 +02:00
|
|
|
foreach (const LookupItem &r, b->lookup(functionCallOp)) {
|
|
|
|
|
Symbol *overload = r.declaration();
|
2010-05-05 12:06:38 +02:00
|
|
|
FullySpecifiedType overloadTy = overload->type().simplified();
|
2009-05-19 12:15:30 +02:00
|
|
|
|
2010-07-16 11:03:39 +02:00
|
|
|
if (Function *funTy = overloadTy->asFunctionType()) {
|
2010-05-05 12:06:38 +02:00
|
|
|
functions.append(funTy);
|
2010-07-16 11:03:39 +02:00
|
|
|
}
|
2009-10-16 12:22:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-09 10:10:31 +01:00
|
|
|
// There are two different kinds of completion we want to provide:
|
|
|
|
|
// 1. If this is a function call, we want to pop up a tooltip that shows the user
|
|
|
|
|
// the possible overloads with their argument types and names.
|
|
|
|
|
// 2. If this is a function definition, we want to offer autocompletion of
|
|
|
|
|
// the function signature.
|
2009-10-19 14:14:28 +02:00
|
|
|
|
2009-11-09 10:10:31 +01:00
|
|
|
// check if function signature autocompletion is appropriate
|
|
|
|
|
if (! functions.isEmpty() && ! toolTipOnly) {
|
|
|
|
|
|
|
|
|
|
// function definitions will only happen in class or namespace scope,
|
|
|
|
|
// so get the current location's enclosing scope.
|
2009-10-19 14:14:28 +02:00
|
|
|
|
|
|
|
|
// get current line and column
|
|
|
|
|
TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget());
|
|
|
|
|
int lineSigned = 0, columnSigned = 0;
|
|
|
|
|
edit->convertPosition(m_editor->position(), &lineSigned, &columnSigned);
|
|
|
|
|
unsigned line = lineSigned, column = columnSigned;
|
|
|
|
|
|
|
|
|
|
// find a scope that encloses the current location, starting from the lastVisibileSymbol
|
|
|
|
|
// and moving outwards
|
|
|
|
|
|
2010-07-03 18:41:31 +02:00
|
|
|
Scope *sc = context.thisDocument()->scopeAt(line, column);
|
2009-10-19 14:14:28 +02:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
if (sc && (sc->isClass() || sc->isNamespace())) {
|
2009-10-19 14:14:28 +02:00
|
|
|
// It may still be a function call. If the whole line parses as a function
|
|
|
|
|
// declaration, we should be certain that it isn't.
|
|
|
|
|
bool autocompleteSignature = false;
|
|
|
|
|
|
|
|
|
|
QTextCursor tc(edit->document());
|
|
|
|
|
tc.setPosition(endOfExpression);
|
2010-06-29 17:47:59 +02:00
|
|
|
BackwardsScanner bs(tc);
|
2010-03-11 10:57:46 +01:00
|
|
|
const int startToken = bs.startToken();
|
|
|
|
|
const int lineStartToken = bs.startOfLine(startToken);
|
|
|
|
|
// make sure the required tokens are actually available
|
|
|
|
|
bs.LA(startToken - lineStartToken);
|
|
|
|
|
QString possibleDecl = bs.mid(lineStartToken).trimmed().append("();");
|
2009-10-19 14:14:28 +02:00
|
|
|
|
|
|
|
|
Document::Ptr doc = Document::create(QLatin1String("<completion>"));
|
|
|
|
|
doc->setSource(possibleDecl.toLatin1());
|
|
|
|
|
if (doc->parse(Document::ParseDeclaration)) {
|
|
|
|
|
doc->check();
|
|
|
|
|
if (SimpleDeclarationAST *sd = doc->translationUnit()->ast()->asSimpleDeclaration()) {
|
2009-11-20 17:25:39 +01:00
|
|
|
if (sd->declarator_list &&
|
|
|
|
|
sd->declarator_list && sd->declarator_list->value->postfix_declarator_list
|
2009-11-10 16:47:16 +01:00
|
|
|
&& sd->declarator_list->value->postfix_declarator_list->value->asFunctionDeclarator()) {
|
2009-10-19 14:14:28 +02:00
|
|
|
autocompleteSignature = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (autocompleteSignature) {
|
|
|
|
|
// set up signature autocompletion
|
|
|
|
|
foreach (Function *f, functions) {
|
|
|
|
|
Overview overview;
|
|
|
|
|
overview.setShowArgumentNames(true);
|
2010-03-05 15:43:31 +01:00
|
|
|
overview.setShowDefaultArguments(false);
|
2009-10-19 14:14:28 +02:00
|
|
|
|
2009-11-02 10:31:44 +01:00
|
|
|
// gets: "parameter list) cv-spec",
|
|
|
|
|
QString completion = overview(f->type()).mid(1);
|
|
|
|
|
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
item.text = completion;
|
|
|
|
|
item.data = QVariant::fromValue(CompleteFunctionDeclaration(f));
|
|
|
|
|
m_completions.append(item);
|
2009-10-19 14:14:28 +02:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-09 10:10:31 +01:00
|
|
|
}
|
2009-10-19 14:14:28 +02:00
|
|
|
|
2009-11-09 10:10:31 +01:00
|
|
|
if (! functions.empty()) {
|
2009-10-19 14:14:28 +02:00
|
|
|
// set up function call tooltip
|
2009-05-19 12:15:30 +02:00
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
// Recreate if necessary
|
|
|
|
|
if (!m_functionArgumentWidget)
|
|
|
|
|
m_functionArgumentWidget = new FunctionArgumentWidget;
|
2009-02-12 11:41:52 +01:00
|
|
|
|
2009-03-26 12:37:40 +01:00
|
|
|
m_functionArgumentWidget->showFunctionHint(functions,
|
2010-05-17 16:27:54 +02:00
|
|
|
typeOfExpression.context(),
|
2009-03-26 12:37:40 +01:00
|
|
|
m_startPosition);
|
2009-03-26 11:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-05-17 16:27:54 +02:00
|
|
|
bool CppCodeCompletion::completeMember(const QList<LookupItem> &baseResults)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-05-17 16:27:54 +02:00
|
|
|
const LookupContext &context = typeOfExpression.context();
|
|
|
|
|
|
2010-06-07 13:06:21 +02:00
|
|
|
// if (debug)
|
|
|
|
|
// qDebug() << Q_FUNC_INFO << __LINE__;
|
2010-05-12 14:52:24 +02:00
|
|
|
|
2009-10-16 11:08:06 +02:00
|
|
|
if (baseResults.isEmpty())
|
2008-12-29 11:53:40 +01:00
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-12 12:53:16 +02:00
|
|
|
ResolveExpression resolveExpression(context);
|
2009-10-16 11:08:06 +02:00
|
|
|
|
|
|
|
|
bool replacedDotOperator = false;
|
2009-01-05 15:23:44 +01:00
|
|
|
|
2010-05-12 14:52:24 +02:00
|
|
|
if (ClassOrNamespace *binding = resolveExpression.baseExpression(baseResults,
|
|
|
|
|
m_completionOperator,
|
|
|
|
|
&replacedDotOperator)) {
|
2010-06-07 13:06:21 +02:00
|
|
|
// if (debug)
|
|
|
|
|
// qDebug() << "cool we got a binding for the base expression";
|
2010-05-12 14:52:24 +02:00
|
|
|
|
|
|
|
|
if (replacedDotOperator && binding) {
|
|
|
|
|
// Replace . with ->
|
|
|
|
|
int length = m_editor->position() - m_startPosition + 1;
|
|
|
|
|
m_editor->setCurPos(m_startPosition - 1);
|
|
|
|
|
m_editor->replace(length, QLatin1String("->"));
|
|
|
|
|
++m_startPosition;
|
2010-05-12 12:53:16 +02:00
|
|
|
}
|
2009-01-05 15:23:44 +01:00
|
|
|
|
2010-05-12 14:52:24 +02:00
|
|
|
if (binding)
|
2010-05-17 16:27:54 +02:00
|
|
|
completeClass(binding, /*static lookup = */ false);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-12 14:52:24 +02:00
|
|
|
return ! m_completions.isEmpty();
|
2009-11-04 18:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-07 13:06:21 +02:00
|
|
|
// if (debug) {
|
|
|
|
|
// Overview oo;
|
|
|
|
|
// qDebug() << "hmm, got:" << oo(baseResults.first().type());
|
|
|
|
|
// }
|
2010-05-12 16:04:43 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-17 16:27:54 +02:00
|
|
|
bool CppCodeCompletion::completeScope(const QList<LookupItem> &results)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-05-17 16:27:54 +02:00
|
|
|
const LookupContext &context = typeOfExpression.context();
|
2010-05-05 10:59:46 +02:00
|
|
|
if (results.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-02-01 14:00:07 +01:00
|
|
|
foreach (const LookupItem &result, results) {
|
2009-11-17 14:21:46 +01:00
|
|
|
FullySpecifiedType ty = result.type();
|
2010-05-12 12:53:16 +02:00
|
|
|
Scope *scope = result.scope();
|
2008-12-30 11:35:30 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
if (NamedType *namedTy = ty->asNamedType()) {
|
2010-05-14 13:44:54 +02:00
|
|
|
if (ClassOrNamespace *b = context.lookupType(namedTy->name(), scope)) {
|
2010-05-17 16:27:54 +02:00
|
|
|
completeClass(b);
|
2010-05-05 10:59:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
} else if (Class *classTy = ty->asClassType()) {
|
2010-05-14 13:44:54 +02:00
|
|
|
if (ClassOrNamespace *b = context.lookupType(classTy)) {
|
2010-05-17 16:27:54 +02:00
|
|
|
completeClass(b);
|
2010-05-05 10:59:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
} else if (Namespace *nsTy = ty->asNamespaceType()) {
|
2010-05-14 13:44:54 +02:00
|
|
|
if (ClassOrNamespace *b = context.lookupType(nsTy)) {
|
2010-05-17 16:27:54 +02:00
|
|
|
completeNamespace(b);
|
2010-05-05 10:59:46 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2009-02-18 15:56:59 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2009-02-18 15:56:59 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return ! m_completions.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-14 14:41:51 +01:00
|
|
|
void CppCodeCompletion::addKeywords()
|
|
|
|
|
{
|
2010-02-15 12:23:48 +01:00
|
|
|
int keywordLimit = T_FIRST_OBJC_AT_KEYWORD;
|
|
|
|
|
if (objcKeywordsWanted())
|
|
|
|
|
keywordLimit = T_LAST_OBJC_AT_KEYWORD + 1;
|
2010-02-14 14:41:51 +01:00
|
|
|
|
2010-02-15 12:23:48 +01:00
|
|
|
// keyword completion items.
|
|
|
|
|
for (int i = T_FIRST_KEYWORD; i < keywordLimit; ++i) {
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
item.text = QLatin1String(Token::name(i));
|
|
|
|
|
item.icon = m_icons.keywordIcon();
|
|
|
|
|
m_completions.append(item);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
void CppCodeCompletion::addMacros(const QString &fileName, const Snapshot &snapshot)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
QSet<QString> processed;
|
2009-02-17 11:09:07 +01:00
|
|
|
QSet<QString> definedMacros;
|
|
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
addMacros_helper(snapshot, fileName, &processed, &definedMacros);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-02-17 11:09:07 +01:00
|
|
|
foreach (const QString ¯oName, definedMacros) {
|
2008-12-02 12:01:29 +01:00
|
|
|
TextEditor::CompletionItem item(this);
|
2009-10-05 18:32:08 +02:00
|
|
|
item.text = macroName;
|
|
|
|
|
item.icon = m_icons.macroIcon();
|
2008-12-02 12:01:29 +01:00
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-11 13:47:15 +01:00
|
|
|
void CppCodeCompletion::addMacros_helper(const Snapshot &snapshot,
|
2009-02-17 11:09:07 +01:00
|
|
|
const QString &fileName,
|
|
|
|
|
QSet<QString> *processed,
|
|
|
|
|
QSet<QString> *definedMacros)
|
|
|
|
|
{
|
2010-01-11 13:47:15 +01:00
|
|
|
Document::Ptr doc = snapshot.document(fileName);
|
2009-02-17 11:09:07 +01:00
|
|
|
|
|
|
|
|
if (! doc || processed->contains(doc->fileName()))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
processed->insert(doc->fileName());
|
|
|
|
|
|
|
|
|
|
foreach (const Document::Include &i, doc->includes()) {
|
2010-01-11 13:47:15 +01:00
|
|
|
addMacros_helper(snapshot, i.fileName(), processed, definedMacros);
|
2009-02-17 11:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (const Macro ¯o, doc->definedMacros()) {
|
|
|
|
|
const QString macroName = QString::fromUtf8(macro.name().constData(), macro.name().length());
|
|
|
|
|
if (! macro.isHidden())
|
|
|
|
|
definedMacros->insert(macroName);
|
|
|
|
|
else
|
|
|
|
|
definedMacros->remove(macroName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void CppCodeCompletion::addCompletionItem(Symbol *symbol)
|
|
|
|
|
{
|
|
|
|
|
ConvertToCompletionItem toCompletionItem(this);
|
2010-08-03 14:45:11 +02:00
|
|
|
TextEditor::CompletionItem item = toCompletionItem(symbol);
|
|
|
|
|
if (item.isValid())
|
2008-12-02 12:01:29 +01:00
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-23 16:46:30 +02:00
|
|
|
bool CppCodeCompletion::completeInclude(const QTextCursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
QString directoryPrefix;
|
|
|
|
|
if (m_completionOperator == T_SLASH) {
|
|
|
|
|
QTextCursor c = cursor;
|
|
|
|
|
c.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
|
|
|
|
QString sel = c.selectedText();
|
|
|
|
|
int startCharPos = sel.indexOf(QLatin1Char('"'));
|
|
|
|
|
if (startCharPos == -1) {
|
|
|
|
|
startCharPos = sel.indexOf(QLatin1Char('<'));
|
|
|
|
|
m_completionOperator = T_ANGLE_STRING_LITERAL;
|
|
|
|
|
} else {
|
|
|
|
|
m_completionOperator = T_STRING_LITERAL;
|
|
|
|
|
}
|
|
|
|
|
if (startCharPos != -1)
|
|
|
|
|
directoryPrefix = sel.mid(startCharPos + 1, sel.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make completion for all relevant includes
|
|
|
|
|
if (ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::instance()->currentProject()) {
|
|
|
|
|
QStringList includePaths = m_manager->projectInfo(project).includePaths;
|
|
|
|
|
const QString currentFilePath = QFileInfo(m_editor->file()->fileName()).path();
|
|
|
|
|
if (!includePaths.contains(currentFilePath))
|
|
|
|
|
includePaths.append(currentFilePath);
|
|
|
|
|
|
|
|
|
|
foreach (const QString &includePath, includePaths) {
|
|
|
|
|
QString realPath = includePath;
|
|
|
|
|
if (!directoryPrefix.isEmpty()) {
|
|
|
|
|
realPath += QLatin1Char('/');
|
|
|
|
|
realPath += directoryPrefix;
|
|
|
|
|
}
|
2009-07-24 13:48:21 +02:00
|
|
|
foreach (const QString &itemText, m_manager->includesInPath(realPath)) {
|
2009-07-23 16:46:30 +02:00
|
|
|
TextEditor::CompletionItem item(this);
|
2009-10-05 18:32:08 +02:00
|
|
|
item.text += itemText;
|
2009-07-23 16:46:30 +02:00
|
|
|
// TODO: Icon for include files
|
2009-10-05 18:32:08 +02:00
|
|
|
item.icon = m_icons.keywordIcon();
|
2009-07-23 16:46:30 +02:00
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-01 17:01:13 +02:00
|
|
|
|
|
|
|
|
QStringList frameworkPaths = m_manager->projectInfo(project).frameworkPaths;
|
|
|
|
|
foreach (const QString &frameworkPath, frameworkPaths) {
|
|
|
|
|
QString realPath = frameworkPath;
|
|
|
|
|
if (!directoryPrefix.isEmpty()) {
|
|
|
|
|
realPath += QLatin1Char('/');
|
|
|
|
|
realPath += directoryPrefix;
|
|
|
|
|
realPath += QLatin1String(".framework/Headers");
|
|
|
|
|
}
|
|
|
|
|
foreach (const QString &itemText, m_manager->includesInPath(realPath)) {
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
2009-10-05 18:32:08 +02:00
|
|
|
item.text += itemText;
|
2009-10-01 17:01:13 +02:00
|
|
|
// TODO: Icon for include files
|
2009-10-05 18:32:08 +02:00
|
|
|
item.icon = m_icons.keywordIcon();
|
2009-10-01 17:01:13 +02:00
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-07-23 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-24 13:48:21 +02:00
|
|
|
return !m_completions.isEmpty();
|
2009-07-23 16:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
2010-05-18 17:57:49 +02:00
|
|
|
QStringList CppCodeCompletion::preprocessorCompletions
|
|
|
|
|
= QStringList()
|
|
|
|
|
<< QLatin1String("define")
|
|
|
|
|
<< QLatin1String("error")
|
|
|
|
|
<< QLatin1String("include")
|
|
|
|
|
<< QLatin1String("line")
|
|
|
|
|
<< QLatin1String("pragma")
|
|
|
|
|
<< QLatin1String("undef")
|
|
|
|
|
<< QLatin1String("if")
|
|
|
|
|
<< QLatin1String("ifdef")
|
|
|
|
|
<< QLatin1String("ifndef")
|
|
|
|
|
<< QLatin1String("elif")
|
|
|
|
|
<< QLatin1String("else")
|
|
|
|
|
<< QLatin1String("endif")
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
void CppCodeCompletion::completePreprocessor()
|
|
|
|
|
{
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
|
|
|
|
|
foreach (const QString &preprocessorCompletion, preprocessorCompletions) {
|
|
|
|
|
item.text = preprocessorCompletion;
|
|
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (objcKeywordsWanted()) {
|
|
|
|
|
item.text = QLatin1String("import");
|
|
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-17 16:27:54 +02:00
|
|
|
void CppCodeCompletion::completeNamespace(ClassOrNamespace *b)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-05-05 10:59:46 +02:00
|
|
|
QSet<ClassOrNamespace *> bindingsVisited;
|
|
|
|
|
QList<ClassOrNamespace *> bindingsToVisit;
|
|
|
|
|
bindingsToVisit.append(b);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
while (! bindingsToVisit.isEmpty()) {
|
|
|
|
|
ClassOrNamespace *binding = bindingsToVisit.takeFirst();
|
|
|
|
|
if (! binding || bindingsVisited.contains(binding))
|
2009-12-08 12:43:40 +01:00
|
|
|
continue;
|
|
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
bindingsVisited.insert(binding);
|
|
|
|
|
bindingsToVisit += binding->usings();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
QList<Scope *> scopesToVisit;
|
|
|
|
|
QSet<Scope *> scopesVisited;
|
|
|
|
|
|
|
|
|
|
foreach (Symbol *bb, binding->symbols()) {
|
|
|
|
|
if (Namespace *ns = bb->asNamespace())
|
2010-08-11 12:26:02 +02:00
|
|
|
scopesToVisit.append(ns);
|
2010-05-05 10:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Enum *e, binding->enums()) {
|
2010-08-11 12:26:02 +02:00
|
|
|
scopesToVisit.append(e);
|
2010-05-05 10:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (! scopesToVisit.isEmpty()) {
|
|
|
|
|
Scope *scope = scopesToVisit.takeFirst();
|
|
|
|
|
if (! scope || scopesVisited.contains(scope))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
scopesVisited.insert(scope);
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
for (Scope::iterator it = scope->firstMember(); it != scope->lastMember(); ++it) {
|
2010-05-05 10:59:46 +02:00
|
|
|
Symbol *member = *it;
|
|
|
|
|
addCompletionItem(member);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-17 16:27:54 +02:00
|
|
|
void CppCodeCompletion::completeClass(ClassOrNamespace *b, bool staticLookup)
|
2010-05-05 10:59:46 +02:00
|
|
|
{
|
|
|
|
|
QSet<ClassOrNamespace *> bindingsVisited;
|
|
|
|
|
QList<ClassOrNamespace *> bindingsToVisit;
|
|
|
|
|
bindingsToVisit.append(b);
|
|
|
|
|
|
|
|
|
|
while (! bindingsToVisit.isEmpty()) {
|
|
|
|
|
ClassOrNamespace *binding = bindingsToVisit.takeFirst();
|
|
|
|
|
if (! binding || bindingsVisited.contains(binding))
|
2009-12-08 12:43:40 +01:00
|
|
|
continue;
|
|
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
bindingsVisited.insert(binding);
|
|
|
|
|
bindingsToVisit += binding->usings();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
QList<Scope *> scopesToVisit;
|
|
|
|
|
QSet<Scope *> scopesVisited;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
foreach (Symbol *bb, binding->symbols()) {
|
|
|
|
|
if (Class *k = bb->asClass())
|
2010-08-11 12:26:02 +02:00
|
|
|
scopesToVisit.append(k);
|
2010-05-05 10:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (Enum *e, binding->enums())
|
2010-08-11 12:26:02 +02:00
|
|
|
scopesToVisit.append(e);
|
2010-05-05 10:59:46 +02:00
|
|
|
|
|
|
|
|
while (! scopesToVisit.isEmpty()) {
|
|
|
|
|
Scope *scope = scopesToVisit.takeFirst();
|
|
|
|
|
if (! scope || scopesVisited.contains(scope))
|
2008-12-02 12:01:29 +01:00
|
|
|
continue;
|
|
|
|
|
|
2010-05-05 10:59:46 +02:00
|
|
|
scopesVisited.insert(scope);
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
addCompletionItem(scope); // add a completion item for the injected class name.
|
2010-06-02 13:59:01 +02:00
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
for (Scope::iterator it = scope->firstMember(); it != scope->lastMember(); ++it) {
|
2010-05-05 10:59:46 +02:00
|
|
|
Symbol *member = *it;
|
|
|
|
|
if (member->isFriend())
|
|
|
|
|
continue;
|
|
|
|
|
else if (! staticLookup && (member->isTypedef() ||
|
|
|
|
|
member->isEnum() ||
|
|
|
|
|
member->isClass()))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
addCompletionItem(member);
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-17 14:21:46 +01:00
|
|
|
bool CppCodeCompletion::completeQtMethod(const QList<LookupItem> &results,
|
2008-12-02 12:01:29 +01:00
|
|
|
bool wantSignals)
|
|
|
|
|
{
|
|
|
|
|
if (results.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-05-17 16:27:54 +02:00
|
|
|
const LookupContext &context = typeOfExpression.context();
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
ConvertToCompletionItem toCompletionItem(this);
|
|
|
|
|
Overview o;
|
|
|
|
|
o.setShowReturnTypes(false);
|
|
|
|
|
o.setShowArgumentNames(false);
|
|
|
|
|
o.setShowFunctionSignatures(true);
|
|
|
|
|
|
|
|
|
|
QSet<QString> signatures;
|
2009-11-17 14:21:46 +01:00
|
|
|
foreach (const LookupItem &p, results) {
|
|
|
|
|
FullySpecifiedType ty = p.type().simplified();
|
2009-09-30 11:24:00 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (PointerType *ptrTy = ty->asPointerType())
|
2009-10-05 11:38:54 +02:00
|
|
|
ty = ptrTy->elementType().simplified();
|
2008-12-02 12:01:29 +01:00
|
|
|
else
|
|
|
|
|
continue; // not a pointer or a reference to a pointer.
|
|
|
|
|
|
|
|
|
|
NamedType *namedTy = ty->asNamedType();
|
|
|
|
|
if (! namedTy) // not a class name.
|
|
|
|
|
continue;
|
|
|
|
|
|
2010-05-17 16:27:54 +02:00
|
|
|
ClassOrNamespace *b = context.lookupType(namedTy->name(), p.scope());
|
2010-05-05 12:06:38 +02:00
|
|
|
if (! b)
|
|
|
|
|
continue;
|
|
|
|
|
|
2010-05-26 15:44:01 +02:00
|
|
|
QList<ClassOrNamespace *>todo;
|
|
|
|
|
QSet<ClassOrNamespace *> processed;
|
|
|
|
|
QList<Scope *> scopes;
|
|
|
|
|
todo.append(b);
|
|
|
|
|
while (!todo.isEmpty()) {
|
|
|
|
|
ClassOrNamespace *binding = todo.takeLast();
|
|
|
|
|
if (!processed.contains(binding)) {
|
|
|
|
|
processed.insert(binding);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-26 15:44:01 +02:00
|
|
|
foreach (Symbol *s, binding->symbols())
|
|
|
|
|
if (Class *clazz = s->asClass())
|
2010-08-11 12:26:02 +02:00
|
|
|
scopes.append(clazz);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-26 15:44:01 +02:00
|
|
|
todo.append(binding->usings());
|
|
|
|
|
}
|
2010-05-06 10:36:11 +02:00
|
|
|
}
|
|
|
|
|
|
2010-05-26 15:44:01 +02:00
|
|
|
foreach (Scope *scope, scopes) {
|
2010-08-11 12:26:02 +02:00
|
|
|
if (! scope->isClass())
|
2008-12-02 12:01:29 +01:00
|
|
|
continue;
|
|
|
|
|
|
2010-08-11 12:26:02 +02:00
|
|
|
for (unsigned i = 0; i < scope->memberCount(); ++i) {
|
|
|
|
|
Symbol *member = scope->memberAt(i);
|
2009-02-09 17:44:06 +01:00
|
|
|
Function *fun = member->type()->asFunctionType();
|
2008-12-02 12:01:29 +01:00
|
|
|
if (! fun)
|
|
|
|
|
continue;
|
|
|
|
|
if (wantSignals && ! fun->isSignal())
|
|
|
|
|
continue;
|
|
|
|
|
else if (! wantSignals && ! fun->isSlot())
|
|
|
|
|
continue;
|
2010-08-03 14:45:11 +02:00
|
|
|
TextEditor::CompletionItem item = toCompletionItem(fun);
|
|
|
|
|
if (item.isValid()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
unsigned count = fun->argumentCount();
|
|
|
|
|
while (true) {
|
2009-01-13 17:55:02 +01:00
|
|
|
TextEditor::CompletionItem ci = item;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
QString signature;
|
|
|
|
|
signature += overview.prettyName(fun->name());
|
|
|
|
|
signature += QLatin1Char('(');
|
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
|
|
|
Symbol *arg = fun->argumentAt(i);
|
|
|
|
|
if (i != 0)
|
|
|
|
|
signature += QLatin1Char(',');
|
|
|
|
|
signature += o.prettyType(arg->type());
|
|
|
|
|
}
|
|
|
|
|
signature += QLatin1Char(')');
|
|
|
|
|
|
|
|
|
|
const QByteArray normalized =
|
|
|
|
|
QMetaObject::normalizedSignature(signature.toLatin1());
|
|
|
|
|
|
|
|
|
|
signature = QString::fromLatin1(normalized, normalized.size());
|
|
|
|
|
|
|
|
|
|
if (! signatures.contains(signature)) {
|
|
|
|
|
signatures.insert(signature);
|
|
|
|
|
|
2009-10-05 18:32:08 +02:00
|
|
|
ci.text = signature; // fix the completion item.
|
2009-01-13 17:55:02 +01:00
|
|
|
m_completions.append(ci);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count && fun->argumentAt(count - 1)->asArgument()->hasInitializer())
|
|
|
|
|
--count;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ! m_completions.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppCodeCompletion::completions(QList<TextEditor::CompletionItem> *completions)
|
|
|
|
|
{
|
|
|
|
|
const int length = m_editor->position() - m_startPosition;
|
2010-05-26 12:35:20 +02:00
|
|
|
if (length < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-01-26 11:44:45 +01:00
|
|
|
const QString key = m_editor->textAt(m_startPosition, length);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
if (length == 0)
|
2010-05-26 15:14:50 +02:00
|
|
|
*completions = m_completions;
|
2008-12-02 12:01:29 +01:00
|
|
|
else if (length > 0) {
|
2009-07-24 14:32:39 +02:00
|
|
|
/* Close on the trailing slash for include completion, to enable the slash to
|
|
|
|
|
* trigger a new completion list. */
|
|
|
|
|
if ((m_completionOperator == T_STRING_LITERAL ||
|
|
|
|
|
m_completionOperator == T_ANGLE_STRING_LITERAL) && key.endsWith(QLatin1Char('/')))
|
|
|
|
|
return;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (m_completionOperator != T_LPAREN) {
|
2010-05-26 15:14:50 +02:00
|
|
|
filter(m_completions, completions, key);
|
2009-12-09 19:08:14 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
} else if (m_completionOperator == T_LPAREN ||
|
|
|
|
|
m_completionOperator == T_SIGNAL ||
|
|
|
|
|
m_completionOperator == T_SLOT) {
|
2010-05-26 15:14:50 +02:00
|
|
|
foreach (const TextEditor::CompletionItem &item, m_completions) {
|
2009-10-05 18:32:08 +02:00
|
|
|
if (item.text.startsWith(key, Qt::CaseInsensitive)) {
|
2010-01-26 11:44:45 +01:00
|
|
|
completions->append(item);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-19 14:06:00 +02:00
|
|
|
|
2010-08-02 11:41:05 +02:00
|
|
|
if (m_automaticCompletion && completions->size() == 1 && key == completions->first().text) {
|
|
|
|
|
completions->clear();
|
2010-07-19 14:06:00 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-05-28 12:51:12 +02:00
|
|
|
QList<TextEditor::CompletionItem> CppCodeCompletion::removeDuplicates(const QList<TextEditor::CompletionItem> &items)
|
2009-12-02 12:11:49 +01:00
|
|
|
{
|
2010-09-24 20:16:34 +02:00
|
|
|
// Duplicates are kept only if they are snippets.
|
2009-12-02 12:11:49 +01:00
|
|
|
QList<TextEditor::CompletionItem> uniquelist;
|
2010-05-28 12:51:12 +02:00
|
|
|
QSet<QString> processed;
|
|
|
|
|
foreach (const TextEditor::CompletionItem &item, items) {
|
2010-09-24 20:16:34 +02:00
|
|
|
if (!processed.contains(item.text) || item.isSnippet) {
|
2009-12-02 12:11:49 +01:00
|
|
|
uniquelist.append(item);
|
2010-09-24 20:16:34 +02:00
|
|
|
if (!item.isSnippet) {
|
|
|
|
|
processed.insert(item.text);
|
|
|
|
|
if (Symbol *symbol = qvariant_cast<Symbol *>(item.data)) {
|
|
|
|
|
if (Function *funTy = symbol->type()->asFunctionType()) {
|
|
|
|
|
if (funTy->hasArguments())
|
|
|
|
|
++uniquelist.back().duplicateCount;
|
|
|
|
|
}
|
2009-12-02 12:11:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uniquelist;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-28 12:51:12 +02:00
|
|
|
QList<TextEditor::CompletionItem> CppCodeCompletion::getCompletions()
|
|
|
|
|
{
|
|
|
|
|
QList<TextEditor::CompletionItem> completionItems;
|
|
|
|
|
completions(&completionItems);
|
|
|
|
|
|
|
|
|
|
return completionItems;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-09 15:12:44 +02:00
|
|
|
bool CppCodeCompletion::typedCharCompletes(const TextEditor::CompletionItem &item, QChar typedChar)
|
|
|
|
|
{
|
2010-07-19 10:25:55 +02:00
|
|
|
if (m_automaticCompletion)
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-07-09 15:12:44 +02:00
|
|
|
if (item.data.canConvert<QString>()) // snippet
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT)
|
|
|
|
|
return typedChar == QLatin1Char('(')
|
|
|
|
|
|| typedChar == QLatin1Char(',');
|
|
|
|
|
|
|
|
|
|
if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL)
|
|
|
|
|
return typedChar == QLatin1Char('/')
|
|
|
|
|
&& item.text.endsWith(QLatin1Char('/'));
|
|
|
|
|
|
|
|
|
|
if (item.data.value<Symbol *>())
|
|
|
|
|
return typedChar == QLatin1Char(':')
|
|
|
|
|
|| typedChar == QLatin1Char(';')
|
|
|
|
|
|| typedChar == QLatin1Char('.')
|
|
|
|
|
|| typedChar == QLatin1Char(',')
|
|
|
|
|
|| typedChar == QLatin1Char('(');
|
|
|
|
|
|
|
|
|
|
if (item.data.canConvert<CompleteFunctionDeclaration>())
|
|
|
|
|
return typedChar == QLatin1Char('(');
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar typedChar)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-07-14 12:37:49 +02:00
|
|
|
m_shouldRestartCompletion = false; // Enabled for specific cases
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Symbol *symbol = 0;
|
|
|
|
|
|
2010-06-18 10:58:11 +02:00
|
|
|
if (item.data.isValid()) {
|
|
|
|
|
if (item.data.canConvert<QString>()) {
|
|
|
|
|
TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget());
|
|
|
|
|
QTextCursor tc = edit->textCursor();
|
|
|
|
|
tc.setPosition(m_startPosition, QTextCursor::KeepAnchor);
|
|
|
|
|
edit->insertCodeSnippet(tc, item.data.toString());
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
symbol = item.data.value<Symbol *>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-07-23 16:46:30 +02:00
|
|
|
QString toInsert;
|
2009-07-24 13:48:21 +02:00
|
|
|
QString extraChars;
|
2009-07-23 16:46:30 +02:00
|
|
|
int extraLength = 0;
|
2009-09-16 16:48:14 +02:00
|
|
|
int cursorOffset = 0;
|
|
|
|
|
|
2009-09-17 12:36:40 +02:00
|
|
|
bool autoParenthesesEnabled = true;
|
2009-07-23 16:46:30 +02:00
|
|
|
|
2009-03-26 12:37:40 +01:00
|
|
|
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
|
2009-10-05 18:32:08 +02:00
|
|
|
toInsert = item.text;
|
2009-07-24 13:48:21 +02:00
|
|
|
extraChars += QLatin1Char(')');
|
2010-07-09 15:12:44 +02:00
|
|
|
|
|
|
|
|
if (typedChar == QLatin1Char('(')) // Eat the opening parenthesis
|
|
|
|
|
typedChar = QChar();
|
2009-07-23 16:46:30 +02:00
|
|
|
} else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL) {
|
2009-10-05 18:32:08 +02:00
|
|
|
toInsert = item.text;
|
2010-07-14 12:37:49 +02:00
|
|
|
if (!toInsert.endsWith(QLatin1Char('/'))) {
|
2009-07-24 13:48:21 +02:00
|
|
|
extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"');
|
2010-07-14 12:37:49 +02:00
|
|
|
} else {
|
|
|
|
|
m_shouldRestartCompletion = true; // Re-trigger for subdirectory
|
|
|
|
|
if (typedChar == QLatin1Char('/')) // Eat the slash
|
|
|
|
|
typedChar = QChar();
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
} else {
|
2009-10-05 18:32:08 +02:00
|
|
|
toInsert = item.text;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
//qDebug() << "current symbol:" << overview.prettyName(symbol->name())
|
|
|
|
|
//<< overview.prettyType(symbol->type());
|
|
|
|
|
|
2010-05-10 18:30:09 +02:00
|
|
|
const bool autoInsertBrackets = completionSettings().m_autoInsertBrackets;
|
|
|
|
|
|
|
|
|
|
if (autoInsertBrackets && symbol && symbol->type()) {
|
2009-02-09 17:44:06 +01:00
|
|
|
if (Function *function = symbol->type()->asFunctionType()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
// If the member is a function, automatically place the opening parenthesis,
|
|
|
|
|
// except when it might take template parameters.
|
2010-08-26 12:23:09 +02:00
|
|
|
if (! function->hasReturnType() && (function->unqualifiedName() && !function->unqualifiedName()->isDestructorNameId())) {
|
2008-12-02 12:01:29 +01:00
|
|
|
// Don't insert any magic, since the user might have just wanted to select the class
|
|
|
|
|
|
2010-08-11 15:48:21 +02:00
|
|
|
/// ### port me
|
2010-08-11 12:47:28 +02:00
|
|
|
#if 0
|
2010-07-09 15:12:44 +02:00
|
|
|
} else if (function->templateParameterCount() != 0 && typedChar != QLatin1Char('(')) {
|
2008-12-02 12:01:29 +01:00
|
|
|
// If there are no arguments, then we need the template specification
|
|
|
|
|
if (function->argumentCount() == 0) {
|
2009-02-10 12:51:42 +01:00
|
|
|
extraChars += QLatin1Char('<');
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-08-11 12:47:28 +02:00
|
|
|
#endif
|
2009-03-17 14:46:35 +01:00
|
|
|
} else if (! function->isAmbiguous()) {
|
2010-05-10 18:30:09 +02:00
|
|
|
if (completionSettings().m_spaceAfterFunctionName)
|
2010-05-17 13:01:56 +02:00
|
|
|
extraChars += QLatin1Char(' ');
|
2009-02-10 12:51:42 +01:00
|
|
|
extraChars += QLatin1Char('(');
|
2010-07-09 15:12:44 +02:00
|
|
|
if (typedChar == QLatin1Char('('))
|
|
|
|
|
typedChar = QChar();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-09-16 16:48:14 +02:00
|
|
|
// If the function doesn't return anything, automatically place the semicolon,
|
|
|
|
|
// unless we're doing a scope completion (then it might be function definition).
|
2010-07-09 15:12:44 +02:00
|
|
|
const QChar characterAtCursor = m_editor->characterAt(m_editor->position());
|
|
|
|
|
bool endWithSemicolon = typedChar == QLatin1Char(';')
|
|
|
|
|
|| (function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON);
|
|
|
|
|
const QChar semicolon = typedChar.isNull() ? QLatin1Char(';') : typedChar;
|
|
|
|
|
|
|
|
|
|
if (endWithSemicolon && characterAtCursor == semicolon) {
|
|
|
|
|
endWithSemicolon = false;
|
|
|
|
|
typedChar = QChar();
|
|
|
|
|
}
|
2009-09-16 16:48:14 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// If the function takes no arguments, automatically place the closing parenthesis
|
2009-10-05 18:32:08 +02:00
|
|
|
if (item.duplicateCount == 0 && ! function->hasArguments()) {
|
2009-02-10 12:51:42 +01:00
|
|
|
extraChars += QLatin1Char(')');
|
2010-07-09 15:12:44 +02:00
|
|
|
if (endWithSemicolon) {
|
|
|
|
|
extraChars += semicolon;
|
|
|
|
|
typedChar = QChar();
|
|
|
|
|
}
|
2009-09-16 16:48:14 +02:00
|
|
|
} else if (autoParenthesesEnabled) {
|
2009-09-22 16:52:27 +02:00
|
|
|
const QChar lookAhead = m_editor->characterAt(m_editor->position() + 1);
|
|
|
|
|
if (MatchingText::shouldInsertMatchingText(lookAhead)) {
|
|
|
|
|
extraChars += QLatin1Char(')');
|
2009-09-16 16:48:14 +02:00
|
|
|
--cursorOffset;
|
2009-09-22 16:52:27 +02:00
|
|
|
if (endWithSemicolon) {
|
2010-07-09 15:12:44 +02:00
|
|
|
extraChars += semicolon;
|
2009-09-22 16:52:27 +02:00
|
|
|
--cursorOffset;
|
2010-07-09 15:12:44 +02:00
|
|
|
typedChar = QChar();
|
2009-09-22 16:52:27 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-07-09 15:12:44 +02:00
|
|
|
// TODO: When an opening parenthesis exists, the "semicolon" should really be
|
|
|
|
|
// inserted after the matching closing parenthesis.
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-02 10:31:44 +01:00
|
|
|
|
2010-05-10 18:30:09 +02:00
|
|
|
if (autoInsertBrackets && item.data.canConvert<CompleteFunctionDeclaration>()) {
|
2010-07-09 15:12:44 +02:00
|
|
|
if (typedChar == QLatin1Char('('))
|
|
|
|
|
typedChar = QChar();
|
|
|
|
|
|
2009-11-02 10:31:44 +01:00
|
|
|
// everything from the closing parenthesis on are extra chars, to
|
|
|
|
|
// make sure an auto-inserted ")" gets replaced by ") const" if necessary
|
|
|
|
|
int closingParen = toInsert.lastIndexOf(QLatin1Char(')'));
|
|
|
|
|
extraChars = toInsert.mid(closingParen);
|
|
|
|
|
toInsert.truncate(closingParen);
|
|
|
|
|
}
|
2009-07-24 13:48:21 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-07-09 15:12:44 +02:00
|
|
|
// Append an unhandled typed character, adjusting cursor offset when it had been adjusted before
|
|
|
|
|
if (!typedChar.isNull()) {
|
|
|
|
|
extraChars += typedChar;
|
|
|
|
|
if (cursorOffset != 0)
|
|
|
|
|
--cursorOffset;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-14 12:37:49 +02:00
|
|
|
if (!extraChars.isEmpty() && extraChars.length() + cursorOffset > 0) {
|
|
|
|
|
const QChar c = extraChars.at(extraChars.length() - 1 + cursorOffset);
|
|
|
|
|
if (c == QLatin1Char('.') || c == QLatin1Char('('))
|
|
|
|
|
m_shouldRestartCompletion = true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-24 13:48:21 +02:00
|
|
|
// Avoid inserting characters that are already there
|
|
|
|
|
for (int i = 0; i < extraChars.length(); ++i) {
|
|
|
|
|
const QChar a = extraChars.at(i);
|
|
|
|
|
const QChar b = m_editor->characterAt(m_editor->position() + i);
|
|
|
|
|
if (a == b)
|
|
|
|
|
++extraLength;
|
|
|
|
|
else
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-07-23 16:46:30 +02:00
|
|
|
|
2009-07-24 13:48:21 +02:00
|
|
|
toInsert += extraChars;
|
|
|
|
|
|
2009-07-23 16:46:30 +02:00
|
|
|
// Insert the remainder of the name
|
|
|
|
|
int length = m_editor->position() - m_startPosition + extraLength;
|
|
|
|
|
m_editor->setCurPos(m_startPosition);
|
|
|
|
|
m_editor->replace(length, toInsert);
|
2009-09-16 16:48:14 +02:00
|
|
|
if (cursorOffset)
|
|
|
|
|
m_editor->setCurPos(m_editor->position() + cursorOffset);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppCodeCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems)
|
|
|
|
|
{
|
|
|
|
|
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (completionItems.count() == 1) {
|
2010-07-09 15:12:44 +02:00
|
|
|
complete(completionItems.first(), QChar());
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
2010-05-10 18:30:09 +02:00
|
|
|
} else if (m_completionOperator != T_LPAREN) {
|
2010-01-26 11:44:45 +01:00
|
|
|
return TextEditor::ICompletionCollector::partiallyComplete(completionItems);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppCodeCompletion::cleanup()
|
|
|
|
|
{
|
2010-07-19 10:25:55 +02:00
|
|
|
m_automaticCompletion = false;
|
2008-12-02 12:01:29 +01:00
|
|
|
m_completions.clear();
|
2008-12-08 13:01:54 +01:00
|
|
|
|
|
|
|
|
// Set empty map in order to avoid referencing old versions of the documents
|
|
|
|
|
// until the next completion
|
2010-05-05 12:06:38 +02:00
|
|
|
typeOfExpression.reset();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-02-04 16:40:42 +01:00
|
|
|
int CppCodeCompletion::findStartOfName(int pos) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-02-04 16:40:42 +01:00
|
|
|
if (pos == -1)
|
|
|
|
|
pos = m_editor->position();
|
2008-12-02 12:01:29 +01:00
|
|
|
QChar chr;
|
|
|
|
|
|
|
|
|
|
// Skip to the start of a name
|
|
|
|
|
do {
|
2009-02-04 16:40:42 +01:00
|
|
|
chr = m_editor->characterAt(--pos);
|
2008-12-02 12:01:29 +01:00
|
|
|
} while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
|
|
|
|
|
|
|
|
|
|
return pos + 1;
|
|
|
|
|
}
|
2009-03-26 11:14:05 +01:00
|
|
|
|
2009-10-09 15:22:57 +02:00
|
|
|
bool CppCodeCompletion::objcKeywordsWanted() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_objcEnabled)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Core::IFile *file = m_editor->file();
|
|
|
|
|
QString fileName = file->fileName();
|
|
|
|
|
|
|
|
|
|
const Core::MimeDatabase *mdb = Core::ICore::instance()->mimeDatabase();
|
|
|
|
|
return mdb->findByFile(fileName).type() == CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 20:16:34 +02:00
|
|
|
void CppCodeCompletion::addSnippets()
|
|
|
|
|
{
|
2010-10-27 17:38:22 +02:00
|
|
|
m_completions.append(m_snippetProvider.getSnippets(this));
|
2010-09-24 20:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
2009-03-26 11:14:05 +01:00
|
|
|
#include "cppcodecompletion.moc"
|