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"
|
2010-07-08 16:40:46 +02:00
|
|
|
#include "cppdeclfromdef.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-26 13:06:33 +02:00
|
|
|
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-07-26 13:06:33 +02:00
|
|
|
Scope *CppQuickFixState::scopeAt(unsigned index) const
|
2010-07-19 17:40:06 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
unsigned line, column;
|
|
|
|
|
document()->translationUnit()->getTokenStartPosition(index, &line, &column);
|
|
|
|
|
return document()->scopeAt(line, column);
|
|
|
|
|
}
|
2010-07-19 17:40:06 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
bool CppQuickFixState::isCursorOn(unsigned tokenIndex) const
|
2010-07-19 19:12:24 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
|
int cursorBegin = tc.selectionStart();
|
2010-07-19 19:12:24 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
int start = startOf(tokenIndex);
|
|
|
|
|
int end = endOf(tokenIndex);
|
2010-07-19 19:12:24 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
if (cursorBegin >= start && cursorBegin <= end)
|
|
|
|
|
return true;
|
2010-07-19 19:12:24 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-07-19 19:12:24 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
bool CppQuickFixState::isCursorOn(const AST *ast) const
|
2010-07-20 11:02:37 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
|
int cursorBegin = tc.selectionStart();
|
2009-11-13 16:43:26 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
int start = startOf(ast);
|
|
|
|
|
int end = endOf(ast);
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
if (cursorBegin >= start && cursorBegin <= end)
|
|
|
|
|
return true;
|
2009-11-25 12:22:55 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
return false;
|
2009-12-23 12:55:35 +01:00
|
|
|
}
|
2009-11-25 12:22:55 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
ChangeSet::Range CppQuickFixState::range(unsigned tokenIndex) const
|
2010-06-22 10:14:06 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
const Token &token = tokenAt(tokenIndex);
|
2010-06-30 13:49:17 +02:00
|
|
|
unsigned line, column;
|
2010-07-26 13:06:33 +02:00
|
|
|
document()->translationUnit()->getPosition(token.begin(), &line, &column);
|
2010-06-30 13:49:17 +02:00
|
|
|
const int start = editor()->document()->findBlockByNumber(line - 1).position() + column - 1;
|
2010-07-26 13:06:33 +02:00
|
|
|
return ChangeSet::Range(start, start + token.length());
|
2010-06-22 10:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
ChangeSet::Range CppQuickFixState::range(AST *ast) const
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return ChangeSet::Range(startOf(ast), endOf(ast));
|
2010-06-14 14:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
int CppQuickFixState::startOf(unsigned index) const
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
2009-11-18 11:28:06 +01:00
|
|
|
unsigned line, column;
|
2010-07-26 13:06:33 +02:00
|
|
|
document()->translationUnit()->getPosition(tokenAt(index).begin(), &line, &column);
|
2010-06-03 14:15:56 +02:00
|
|
|
return editor()->document()->findBlockByNumber(line - 1).position() + column - 1;
|
2009-11-18 11:28:06 +01:00
|
|
|
}
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
int CppQuickFixState::startOf(const AST *ast) const
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
|
|
|
|
return startOf(ast->firstToken());
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
int CppQuickFixState::endOf(unsigned index) const
|
2009-11-18 11:28:06 +01:00
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
2010-07-26 13:06:33 +02:00
|
|
|
document()->translationUnit()->getPosition(tokenAt(index).end(), &line, &column);
|
2010-06-03 14:15:56 +02:00
|
|
|
return editor()->document()->findBlockByNumber(line - 1).position() + column - 1;
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
int CppQuickFixState::endOf(const AST *ast) const
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
2010-06-04 10:17:45 +02:00
|
|
|
if (unsigned end = ast->lastToken())
|
|
|
|
|
return endOf(end - 1);
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
2009-11-18 14:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
void CppQuickFixState::startAndEndOf(unsigned index, int *start, int *end) const
|
2009-11-26 15:00:54 +01:00
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
2010-07-26 13:06:33 +02:00
|
|
|
Token token(tokenAt(index));
|
|
|
|
|
document()->translationUnit()->getPosition(token.begin(), &line, &column);
|
2010-06-03 14:15:56 +02:00
|
|
|
*start = editor()->document()->findBlockByNumber(line - 1).position() + column - 1;
|
2009-11-26 15:00:54 +01:00
|
|
|
*end = *start + token.length();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QString CppQuickFixState::textOf(const AST *ast) const
|
2009-11-19 17:06:44 +01:00
|
|
|
{
|
2010-07-26 13:06:33 +02:00
|
|
|
return textOf(startOf(ast), endOf(ast));
|
|
|
|
|
}
|
2009-11-19 17:06:44 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
const Token &CppQuickFixState::tokenAt(unsigned index) const
|
|
|
|
|
{
|
|
|
|
|
return document()->translationUnit()->tokenAt(index);
|
|
|
|
|
}
|
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)
|
|
|
|
|
, _refactoringChanges(new CppRefactoringChanges(state.document(), state.snapshot()))
|
|
|
|
|
{}
|
2009-11-19 17:06:44 +01:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
CppQuickFixOperation::~CppQuickFixOperation()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
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(); }
|
|
|
|
|
|
|
|
|
|
CppRefactoringChanges *CppQuickFixOperation::refactoringChanges() const
|
|
|
|
|
{ return _refactoringChanges.data(); }
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|