2009-11-13 16:14:26 +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).
|
2009-11-13 16:14:26 +01:00
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cppquickfix.h"
|
|
|
|
|
#include "cppeditor.h"
|
2009-11-13 16:43:26 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
#include <AST.h>
|
|
|
|
|
#include <TranslationUnit.h>
|
|
|
|
|
#include <Token.h>
|
|
|
|
|
|
2010-05-28 15:25:24 +02:00
|
|
|
#include <cplusplus/ASTPath.h>
|
2009-11-13 16:43:26 +01:00
|
|
|
#include <cplusplus/CppDocument.h>
|
2009-12-23 14:54:02 +01:00
|
|
|
#include <cplusplus/ResolveExpression.h>
|
2010-07-05 09:24:49 +02:00
|
|
|
#include <cplusplus/Overview.h>
|
2010-07-08 17:28:10 +02:00
|
|
|
#include <cplusplus/TypeOfExpression.h>
|
2010-07-19 17:40:06 +02:00
|
|
|
#include <cplusplus/DependencyTable.h>
|
2010-07-20 15:04:50 +02:00
|
|
|
#include <cplusplus/CppRewriter.h>
|
2009-11-13 16:43:26 +01:00
|
|
|
|
2010-07-20 11:02:37 +02:00
|
|
|
#include <cppeditor/cppeditor.h>
|
2010-06-22 09:27:34 +02:00
|
|
|
#include <cppeditor/cpprefactoringchanges.h>
|
2010-03-23 16:32:07 +01:00
|
|
|
#include <cpptools/cpptoolsconstants.h>
|
2009-11-13 16:14:26 +01:00
|
|
|
#include <cpptools/cppmodelmanagerinterface.h>
|
2010-06-22 14:14:22 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
#include <QtGui/QTextBlock>
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
using namespace CppEditor;
|
2009-11-13 16:14:26 +01:00
|
|
|
using namespace CppEditor::Internal;
|
2010-07-26 13:06:33 +02:00
|
|
|
using namespace TextEditor;
|
2009-11-13 16:14:26 +01:00
|
|
|
using namespace CPlusPlus;
|
2010-06-14 17:15:53 +02:00
|
|
|
using namespace Utils;
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
CppQuickFixState::CppQuickFixState(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: QuickFixState(editor)
|
|
|
|
|
{}
|
2009-11-20 15:24:13 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
const QList<AST *> &CppQuickFixState::path() const
|
2009-11-19 17:35:49 +01:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return _path;
|
|
|
|
|
}
|
2010-07-02 17:15:12 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
Snapshot CppQuickFixState::snapshot() const
|
2010-07-02 17:15:12 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return _snapshot;
|
|
|
|
|
}
|
2010-07-02 17:15:12 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
Document::Ptr CppQuickFixState::document() const
|
2010-07-02 17:15:12 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return _semanticInfo.doc;
|
|
|
|
|
}
|
2010-07-02 17:15:12 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
SemanticInfo CppQuickFixState::semanticInfo() const
|
2010-07-02 17:15:12 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return _semanticInfo;
|
|
|
|
|
}
|
2010-07-02 17:15:12 +02:00
|
|
|
|
2010-07-27 15:29:16 +02:00
|
|
|
const LookupContext &CppQuickFixState::context() const
|
2010-07-08 17:28:10 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return _context;
|
|
|
|
|
}
|
2010-07-19 17:40:06 +02:00
|
|
|
|
2010-08-13 12:49:11 +02:00
|
|
|
const CppRefactoringFile CppQuickFixState::currentFile() const
|
2010-07-19 17:40:06 +02:00
|
|
|
{
|
2010-08-13 12:49:11 +02:00
|
|
|
return CppRefactoringFile(editor(), document());
|
2010-07-26 13:06:33 +02:00
|
|
|
}
|
2010-07-19 17:40:06 +02:00
|
|
|
|
2009-11-19 17:06:44 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
CppQuickFixOperation::CppQuickFixOperation(const CppQuickFixState &state, int priority)
|
|
|
|
|
: QuickFixOperation(priority)
|
|
|
|
|
, _state(state)
|
|
|
|
|
{}
|
2009-11-19 17:06:44 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
CppQuickFixOperation::~CppQuickFixOperation()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
void CppQuickFixOperation::perform()
|
|
|
|
|
{
|
2010-08-13 15:38:45 +02:00
|
|
|
CppRefactoringChanges refactoring(_state.snapshot());
|
2010-08-13 11:48:29 +02:00
|
|
|
CppRefactoringFile current = refactoring.file(fileName());
|
2010-08-12 13:46:18 +02:00
|
|
|
|
|
|
|
|
performChanges(¤t, &refactoring);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
const CppQuickFixState &CppQuickFixOperation::state() const
|
|
|
|
|
{
|
|
|
|
|
return _state;
|
2009-11-19 17:06:44 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QString CppQuickFixOperation::fileName() const
|
|
|
|
|
{ return state().document()->fileName(); }
|
|
|
|
|
|
|
|
|
|
CppQuickFixFactory::CppQuickFixFactory()
|
2009-11-20 15:24:13 +01:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
}
|
2009-11-20 15:24:13 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
CppQuickFixFactory::~CppQuickFixFactory()
|
|
|
|
|
{
|
|
|
|
|
}
|
2009-11-20 15:24:13 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QList<QuickFixOperation::Ptr> CppQuickFixFactory::matchingOperations(QuickFixState *state)
|
|
|
|
|
{
|
|
|
|
|
if (CppQuickFixState *cppState = static_cast<CppQuickFixState *>(state))
|
|
|
|
|
return match(*cppState);
|
|
|
|
|
else
|
|
|
|
|
return QList<TextEditor::QuickFixOperation::Ptr>();
|
|
|
|
|
}
|
2009-11-20 15:24:13 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QList<CppQuickFixOperation::Ptr> CppQuickFixFactory::singleResult(CppQuickFixOperation *operation)
|
|
|
|
|
{
|
|
|
|
|
QList<CppQuickFixOperation::Ptr> result;
|
|
|
|
|
result.append(CppQuickFixOperation::Ptr(operation));
|
|
|
|
|
return result;
|
2009-11-20 15:24:13 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QList<CppQuickFixOperation::Ptr> CppQuickFixFactory::noResult()
|
2009-11-20 10:47:19 +01:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return QList<CppQuickFixOperation::Ptr>();
|
2009-11-20 10:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
CppQuickFixCollector::CppQuickFixCollector()
|
2010-06-03 15:15:11 +02:00
|
|
|
{
|
|
|
|
|
}
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
CppQuickFixCollector::~CppQuickFixCollector()
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
2010-06-03 15:15:11 +02:00
|
|
|
}
|
2009-11-16 16:18:08 +01:00
|
|
|
|
2010-06-07 12:12:07 +02:00
|
|
|
bool CppQuickFixCollector::supportsEditor(TextEditor::ITextEditable *editor)
|
|
|
|
|
{
|
|
|
|
|
return CppTools::CppModelManagerInterface::instance()->isCppEditor(editor);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
TextEditor::QuickFixState *CppQuickFixCollector::initializeCompletion(TextEditor::BaseTextEditor *editor)
|
2010-06-03 15:15:11 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
if (CPPEditor *cppEditor = qobject_cast<CPPEditor *>(editor)) {
|
|
|
|
|
const SemanticInfo info = cppEditor->semanticInfo();
|
2009-11-16 16:18:08 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
if (info.revision != cppEditor->editorRevision()) {
|
2010-06-03 15:15:11 +02:00
|
|
|
// outdated
|
|
|
|
|
qWarning() << "TODO: outdated semantic info, force a reparse.";
|
|
|
|
|
return 0;
|
2009-11-19 17:06:44 +01:00
|
|
|
}
|
2009-11-18 16:12:23 +01:00
|
|
|
|
2010-06-03 15:15:11 +02:00
|
|
|
if (info.doc) {
|
|
|
|
|
ASTPath astPath(info.doc);
|
2009-11-19 17:06:44 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
const QList<AST *> path = astPath(cppEditor->textCursor());
|
2010-06-03 15:15:11 +02:00
|
|
|
if (! path.isEmpty()) {
|
2010-07-26 13:06:33 +02:00
|
|
|
CppQuickFixState *state = new CppQuickFixState(editor);
|
|
|
|
|
state->_path = path;
|
|
|
|
|
state->_semanticInfo = info;
|
|
|
|
|
state->_snapshot = CppTools::CppModelManagerInterface::instance()->snapshot();
|
|
|
|
|
state->_context = LookupContext(info.doc, state->snapshot());
|
2010-06-03 15:15:11 +02:00
|
|
|
return state;
|
|
|
|
|
}
|
2009-11-18 12:54:39 +01:00
|
|
|
}
|
2009-11-13 16:43:26 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 15:15:11 +02:00
|
|
|
return 0;
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QList<TextEditor::QuickFixFactory *> CppQuickFixCollector::quickFixFactories() const
|
2010-06-22 14:14:22 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
QList<TextEditor::QuickFixFactory *> results;
|
|
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
|
foreach (CppQuickFixFactory *f, pm->getObjects<CppEditor::CppQuickFixFactory>())
|
|
|
|
|
results.append(f);
|
|
|
|
|
return results;
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|