2009-11-13 16:14:26 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
|
|
|
#include <cplusplus/CppDocument.h>
|
|
|
|
|
|
2009-11-13 16:14:26 +01:00
|
|
|
#include <TranslationUnit.h>
|
2009-11-16 16:18:08 +01:00
|
|
|
#include <ASTVisitor.h>
|
|
|
|
|
#include <AST.h>
|
2009-11-18 12:54:39 +01:00
|
|
|
#include <ASTPatternBuilder.h>
|
|
|
|
|
#include <ASTMatcher.h>
|
2009-11-13 16:14:26 +01:00
|
|
|
#include <Token.h>
|
|
|
|
|
|
|
|
|
|
#include <cpptools/cppmodelmanagerinterface.h>
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
|
|
using namespace CppEditor::Internal;
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
2009-11-13 16:43:26 +01:00
|
|
|
namespace {
|
|
|
|
|
|
2009-11-16 16:18:08 +01:00
|
|
|
class ASTPath: public ASTVisitor
|
|
|
|
|
{
|
|
|
|
|
Document::Ptr _doc;
|
|
|
|
|
unsigned _line;
|
|
|
|
|
unsigned _column;
|
|
|
|
|
QList<AST *> _nodes;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ASTPath(Document::Ptr doc)
|
2009-11-17 13:28:20 +01:00
|
|
|
: ASTVisitor(doc->translationUnit()),
|
|
|
|
|
_doc(doc), _line(0), _column(0)
|
|
|
|
|
{}
|
2009-11-16 16:18:08 +01:00
|
|
|
|
|
|
|
|
QList<AST *> operator()(const QTextCursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
_nodes.clear();
|
|
|
|
|
_line = cursor.blockNumber() + 1;
|
|
|
|
|
_column = cursor.columnNumber() + 1;
|
|
|
|
|
accept(_doc->translationUnit()->ast());
|
|
|
|
|
return _nodes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual bool preVisit(AST *ast)
|
|
|
|
|
{
|
|
|
|
|
unsigned firstToken = ast->firstToken();
|
|
|
|
|
unsigned lastToken = ast->lastToken();
|
|
|
|
|
|
|
|
|
|
if (firstToken > 0 && lastToken > firstToken) {
|
|
|
|
|
unsigned startLine, startColumn;
|
|
|
|
|
getTokenStartPosition(firstToken, &startLine, &startColumn);
|
|
|
|
|
|
|
|
|
|
if (_line > startLine || (_line == startLine && _column >= startColumn)) {
|
|
|
|
|
|
|
|
|
|
unsigned endLine, endColumn;
|
|
|
|
|
getTokenEndPosition(lastToken - 1, &endLine, &endColumn);
|
|
|
|
|
|
|
|
|
|
if (_line < endLine || (_line == endLine && _column < endColumn)) {
|
|
|
|
|
_nodes.append(ast);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-18 12:54:39 +01:00
|
|
|
class RewriteLogicalAndOp: public QuickFixOperation
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
RewriteLogicalAndOp(Document::Ptr doc, const Snapshot &snapshot)
|
|
|
|
|
: QuickFixOperation(doc, snapshot), matcher(doc->translationUnit()),
|
|
|
|
|
left(0), right(0), pattern(0)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String("Rewrite condition using ||"); // ### tr?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool match(BinaryExpressionAST *expression)
|
|
|
|
|
{
|
|
|
|
|
left = mk.UnaryExpression();
|
|
|
|
|
right = mk.UnaryExpression();
|
|
|
|
|
pattern = mk.BinaryExpression(left, right);
|
|
|
|
|
|
|
|
|
|
if (expression->match(pattern, &matcher) &&
|
|
|
|
|
tokenAt(pattern->binary_op_token).is(T_AMPER_AMPER) &&
|
|
|
|
|
tokenAt(left->unary_op_token).is(T_EXCLAIM) &&
|
|
|
|
|
tokenAt(right->unary_op_token).is(T_EXCLAIM)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void apply()
|
|
|
|
|
{
|
2009-11-18 14:53:35 +01:00
|
|
|
replace(pattern->binary_op_token, QLatin1String("||"));
|
|
|
|
|
replace(left->unary_op_token, QLatin1String("!("));
|
|
|
|
|
replace(right->unary_op_token, QLatin1String(""));
|
|
|
|
|
insert(endOf(pattern), QLatin1String(")"));
|
2009-11-18 12:54:39 +01:00
|
|
|
|
2009-11-18 14:53:35 +01:00
|
|
|
execute();
|
2009-11-18 12:54:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ASTMatcher matcher;
|
|
|
|
|
ASTPatternBuilder mk;
|
|
|
|
|
UnaryExpressionAST *left;
|
|
|
|
|
UnaryExpressionAST *right;
|
|
|
|
|
BinaryExpressionAST *pattern;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-11-13 16:43:26 +01:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
2009-11-16 16:18:08 +01:00
|
|
|
QuickFixOperation::QuickFixOperation(CPlusPlus::Document::Ptr doc,
|
2009-11-18 11:10:50 +01:00
|
|
|
const CPlusPlus::Snapshot &snapshot)
|
|
|
|
|
: _doc(doc), _snapshot(snapshot)
|
2009-11-13 16:14:26 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
QuickFixOperation::~QuickFixOperation()
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
QTextCursor QuickFixOperation::textCursor() const
|
|
|
|
|
{ return _textCursor; }
|
|
|
|
|
|
2009-11-18 11:10:50 +01:00
|
|
|
void QuickFixOperation::setTextCursor(const QTextCursor &cursor)
|
|
|
|
|
{ _textCursor = cursor; }
|
|
|
|
|
|
2009-11-13 16:14:26 +01:00
|
|
|
const CPlusPlus::Token &QuickFixOperation::tokenAt(unsigned index) const
|
|
|
|
|
{ return _doc->translationUnit()->tokenAt(index); }
|
|
|
|
|
|
2009-11-18 14:53:35 +01:00
|
|
|
int QuickFixOperation::startOf(unsigned index) const
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
2009-11-18 11:28:06 +01:00
|
|
|
unsigned line, column;
|
|
|
|
|
_doc->translationUnit()->getPosition(tokenAt(index).begin(), &line, &column);
|
|
|
|
|
return _textCursor.document()->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
}
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2009-11-18 14:53:35 +01:00
|
|
|
int QuickFixOperation::startOf(const CPlusPlus::AST *ast) const
|
|
|
|
|
{
|
|
|
|
|
return startOf(ast->firstToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int QuickFixOperation::endOf(unsigned index) const
|
2009-11-18 11:28:06 +01:00
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
_doc->translationUnit()->getPosition(tokenAt(index).end(), &line, &column);
|
|
|
|
|
return _textCursor.document()->findBlockByNumber(line - 1).position() + column - 1;
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-18 14:53:35 +01:00
|
|
|
int QuickFixOperation::endOf(const CPlusPlus::AST *ast) const
|
|
|
|
|
{
|
|
|
|
|
return endOf(ast->lastToken() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-18 11:28:06 +01:00
|
|
|
QTextCursor QuickFixOperation::selectToken(unsigned index) const
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
|
|
|
|
QTextCursor tc = _textCursor;
|
2009-11-18 14:53:35 +01:00
|
|
|
tc.setPosition(startOf(index));
|
|
|
|
|
tc.setPosition(endOf(index), QTextCursor::KeepAnchor);
|
2009-11-13 16:14:26 +01:00
|
|
|
return tc;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-18 11:28:06 +01:00
|
|
|
QTextCursor QuickFixOperation::selectNode(AST *ast) const
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
|
|
|
|
QTextCursor tc = _textCursor;
|
2009-11-18 14:53:35 +01:00
|
|
|
tc.setPosition(startOf(ast->firstToken()));
|
|
|
|
|
tc.setPosition(endOf(ast->lastToken() - 1), QTextCursor::KeepAnchor);
|
2009-11-13 16:14:26 +01:00
|
|
|
return tc;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-18 14:53:35 +01:00
|
|
|
void QuickFixOperation::move(int start, int end, int to)
|
|
|
|
|
{
|
|
|
|
|
if (end > start)
|
|
|
|
|
_textWriter.move(start, end-start, to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::move(unsigned tokenIndex, int to)
|
|
|
|
|
{
|
|
|
|
|
move(startOf(tokenIndex), endOf(tokenIndex), to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::move(const CPlusPlus::AST *ast, int to)
|
|
|
|
|
{
|
|
|
|
|
move(startOf(ast), endOf(ast), to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::replace(int start, int end, const QString &replacement)
|
|
|
|
|
{
|
|
|
|
|
if (end >= start)
|
|
|
|
|
_textWriter.replace(start, end-start, replacement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::replace(unsigned tokenIndex, const QString &replacement)
|
|
|
|
|
{
|
|
|
|
|
replace(startOf(tokenIndex), endOf(tokenIndex), replacement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::replace(const CPlusPlus::AST *ast, const QString &replacement)
|
|
|
|
|
{
|
|
|
|
|
replace(startOf(ast), endOf(ast), replacement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::insert(int at, const QString &text)
|
|
|
|
|
{
|
|
|
|
|
replace(at, at, text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::execute()
|
|
|
|
|
{
|
|
|
|
|
_textWriter.write(&_textCursor);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 16:14:26 +01:00
|
|
|
CPPQuickFixCollector::CPPQuickFixCollector()
|
|
|
|
|
: _modelManager(CppTools::CppModelManagerInterface::instance()), _editor(0)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
CPPQuickFixCollector::~CPPQuickFixCollector()
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
bool CPPQuickFixCollector::supportsEditor(TextEditor::ITextEditable *editor)
|
|
|
|
|
{ return qobject_cast<CPPEditorEditable *>(editor) != 0; }
|
|
|
|
|
|
|
|
|
|
bool CPPQuickFixCollector::triggersCompletion(TextEditor::ITextEditable *)
|
2009-11-16 16:18:08 +01:00
|
|
|
{ return false; }
|
2009-11-13 16:14:26 +01:00
|
|
|
|
|
|
|
|
int CPPQuickFixCollector::startCompletion(TextEditor::ITextEditable *editable)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(editable != 0);
|
2009-11-13 16:43:26 +01:00
|
|
|
|
2009-11-13 16:14:26 +01:00
|
|
|
_editor = qobject_cast<CPPEditor *>(editable->widget());
|
2009-11-13 16:43:26 +01:00
|
|
|
Q_ASSERT(_editor != 0);
|
|
|
|
|
|
|
|
|
|
const SemanticInfo info = _editor->semanticInfo();
|
|
|
|
|
|
2009-11-16 16:18:08 +01:00
|
|
|
if (info.revision != _editor->document()->revision()) {
|
|
|
|
|
// outdated
|
|
|
|
|
qWarning() << "TODO: outdated semantic info, force a reparse.";
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 16:43:26 +01:00
|
|
|
if (info.doc) {
|
2009-11-16 16:18:08 +01:00
|
|
|
ASTPath astPath(info.doc);
|
|
|
|
|
|
|
|
|
|
const QList<AST *> path = astPath(_editor->textCursor());
|
|
|
|
|
// ### build the list of the quick fix ops by scanning path.
|
|
|
|
|
|
2009-11-18 12:54:39 +01:00
|
|
|
RewriteLogicalAndOp *op = new RewriteLogicalAndOp(info.doc, info.snapshot);
|
|
|
|
|
QuickFixOperationPtr quickFix(op);
|
|
|
|
|
|
|
|
|
|
for (int i = path.size() - 1; i != -1; --i) {
|
|
|
|
|
AST *node = path.at(i);
|
|
|
|
|
if (BinaryExpressionAST *binary = node->asBinaryExpression()) {
|
|
|
|
|
if (op->match(binary)) {
|
|
|
|
|
_quickFixes.append(quickFix);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-16 16:18:08 +01:00
|
|
|
if (! _quickFixes.isEmpty())
|
|
|
|
|
return editable->position();
|
2009-11-13 16:43:26 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-13 16:14:26 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-13 16:43:26 +01:00
|
|
|
void CPPQuickFixCollector::completions(QList<TextEditor::CompletionItem> *quickFixItems)
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
2009-11-13 16:43:26 +01:00
|
|
|
for (int i = 0; i < _quickFixes.size(); ++i) {
|
|
|
|
|
QuickFixOperationPtr op = _quickFixes.at(i);
|
|
|
|
|
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
item.text = op->description();
|
|
|
|
|
item.data = QVariant::fromValue(i);
|
|
|
|
|
quickFixItems->append(item);
|
|
|
|
|
}
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CPPQuickFixCollector::complete(const TextEditor::CompletionItem &item)
|
|
|
|
|
{
|
|
|
|
|
const int index = item.data.toInt();
|
|
|
|
|
|
2009-11-13 16:43:26 +01:00
|
|
|
if (index < _quickFixes.size()) {
|
|
|
|
|
QuickFixOperationPtr quickFix = _quickFixes.at(index);
|
2009-11-18 11:10:50 +01:00
|
|
|
quickFix->setTextCursor(_editor->textCursor());
|
|
|
|
|
quickFix->apply();
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CPPQuickFixCollector::cleanup()
|
|
|
|
|
{
|
2009-11-13 16:43:26 +01:00
|
|
|
_quickFixes.clear();
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|