Files
qt-creator/src/plugins/cppeditor/cppquickfix.cpp

282 lines
7.9 KiB
C++
Raw Normal View History

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"
#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>
#include <cplusplus/ASTPath.h>
2009-11-13 16:43:26 +01:00
#include <cplusplus/CppDocument.h>
#include <cplusplus/ResolveExpression.h>
#include <cplusplus/Overview.h>
#include <cplusplus/TypeOfExpression.h>
2010-07-19 17:40:06 +02:00
#include <cplusplus/DependencyTable.h>
#include <cplusplus/CppRewriter.h>
2009-11-13 16:43:26 +01:00
#include <cppeditor/cppeditor.h>
2010-06-22 09:27:34 +02:00
#include <cppeditor/cpprefactoringchanges.h>
#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>
#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;
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)
{}
2010-07-26 13:06:33 +02:00
const QList<AST *> &CppQuickFixState::path() const
{
2010-07-26 13:06:33 +02:00
return _path;
}
2010-07-26 13:06:33 +02:00
Snapshot CppQuickFixState::snapshot() const
{
2010-07-26 13:06:33 +02:00
return _snapshot;
}
2010-07-26 13:06:33 +02:00
Document::Ptr CppQuickFixState::document() const
{
2010-07-26 13:06:33 +02:00
return _semanticInfo.doc;
}
2010-07-26 13:06:33 +02:00
SemanticInfo CppQuickFixState::semanticInfo() const
{
2010-07-26 13:06:33 +02:00
return _semanticInfo;
}
2010-07-26 13:06:33 +02:00
LookupContext CppQuickFixState::context() const
{
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-26 13:06:33 +02:00
QTextCursor tc = textCursor();
int cursorBegin = tc.selectionStart();
2010-07-26 13:06:33 +02:00
int start = startOf(tokenIndex);
int end = endOf(tokenIndex);
2010-07-26 13:06:33 +02:00
if (cursorBegin >= start && cursorBegin <= end)
return true;
2010-07-26 13:06:33 +02:00
return false;
}
2010-07-26 13:06:33 +02:00
bool CppQuickFixState::isCursorOn(const AST *ast) const
{
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-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);
unsigned line, column;
2010-07-26 13:06:33 +02:00
document()->translationUnit()->getPosition(token.begin(), &line, &column);
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-07-26 13:06:33 +02:00
return ChangeSet::Range(startOf(ast), endOf(ast));
}
2010-07-26 13:06:33 +02:00
int CppQuickFixState::startOf(unsigned index) const
2009-11-13 16:14:26 +01:00
{
unsigned line, column;
2010-07-26 13:06:33 +02:00
document()->translationUnit()->getPosition(tokenAt(index).begin(), &line, &column);
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::startOf(const AST *ast) const
{
return startOf(ast->firstToken());
}
2010-07-26 13:06:33 +02:00
int CppQuickFixState::endOf(unsigned index) const
{
unsigned line, column;
2010-07-26 13:06:33 +02:00
document()->translationUnit()->getPosition(tokenAt(index).end(), &line, &column);
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
{
2010-06-04 10:17:45 +02:00
if (unsigned end = ast->lastToken())
return endOf(end - 1);
else
return 0;
}
2010-07-26 13:06:33 +02:00
void CppQuickFixState::startAndEndOf(unsigned index, int *start, int *end) const
{
unsigned line, column;
2010-07-26 13:06:33 +02:00
Token token(tokenAt(index));
document()->translationUnit()->getPosition(token.begin(), &line, &column);
*start = editor()->document()->findBlockByNumber(line - 1).position() + column - 1;
*end = *start + token.length();
}
2010-07-26 13:06:33 +02:00
QString CppQuickFixState::textOf(const AST *ast) const
{
2010-07-26 13:06:33 +02:00
return textOf(startOf(ast), endOf(ast));
}
2010-07-26 13:06:33 +02:00
const Token &CppQuickFixState::tokenAt(unsigned index) const
{
return document()->translationUnit()->tokenAt(index);
}
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()))
{}
2010-07-26 13:06:33 +02:00
CppQuickFixOperation::~CppQuickFixOperation()
{}
const CppQuickFixState &CppQuickFixOperation::state() const
{
return _state;
}
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()
{
2010-07-26 13:06:33 +02:00
}
2010-07-26 13:06:33 +02:00
CppQuickFixFactory::~CppQuickFixFactory()
{
}
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>();
}
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;
}
2010-07-26 13:06:33 +02:00
QList<CppQuickFixOperation::Ptr> CppQuickFixFactory::noResult()
{
2010-07-26 13:06:33 +02:00
return QList<CppQuickFixOperation::Ptr>();
}
CppQuickFixCollector::CppQuickFixCollector()
2010-06-03 15:15:11 +02:00
{
}
2009-11-13 16:14:26 +01:00
CppQuickFixCollector::~CppQuickFixCollector()
2009-11-13 16:14:26 +01:00
{
2010-06-03 15:15:11 +02: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();
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-18 16:12:23 +01:00
2010-06-03 15:15:11 +02:00
if (info.doc) {
ASTPath astPath(info.doc);
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
}