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-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>
|
2009-11-13 16:43:26 +01:00
|
|
|
|
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>
|
2010-01-08 15:19:54 +01:00
|
|
|
#include <Type.h>
|
|
|
|
|
#include <CoreTypes.h>
|
|
|
|
|
#include <Symbol.h>
|
|
|
|
|
#include <Symbols.h>
|
|
|
|
|
#include <Name.h>
|
|
|
|
|
#include <Literals.h>
|
2009-11-13 16:14:26 +01:00
|
|
|
|
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-04-26 14:02:09 +02:00
|
|
|
|
2010-05-17 15:50:33 +02:00
|
|
|
#include <QtGui/QApplication>
|
2010-04-26 14:02:09 +02:00
|
|
|
#include <QtGui/QTextBlock>
|
2009-11-13 16:14:26 +01:00
|
|
|
|
|
|
|
|
using namespace CppEditor::Internal;
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
2009-11-13 16:43:26 +01:00
|
|
|
namespace {
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
class CppQuickFixState: public TextEditor::QuickFixState
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QList<CPlusPlus::AST *> path;
|
2010-06-03 14:45:55 +02:00
|
|
|
SemanticInfo info;
|
2010-06-03 14:15:56 +02:00
|
|
|
};
|
|
|
|
|
|
2009-11-26 16:03:01 +01:00
|
|
|
/*
|
|
|
|
|
Rewrite
|
2009-11-27 10:58:24 +01:00
|
|
|
a op b -> !(a invop b)
|
|
|
|
|
(a op b) -> !(a invop b)
|
|
|
|
|
!(a op b) -> (a invob b)
|
2009-11-26 16:03:01 +01:00
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class UseInverseOp: public CppQuickFixOperation
|
2009-11-26 16:03:01 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
UseInverseOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), binary(0), nested(0), negation(0)
|
2009-11-26 16:03:01 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Rewrite Using %1").arg(replacement);
|
2009-11-26 16:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual int match(const QList<AST *> &path)
|
|
|
|
|
{
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
binary = path.at(index)->asBinaryExpression();
|
|
|
|
|
if (! binary)
|
|
|
|
|
return -1;
|
|
|
|
|
if (! isCursorOn(binary->binary_op_token))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
CPlusPlus::Kind invertToken;
|
|
|
|
|
switch (tokenAt(binary->binary_op_token).kind()) {
|
|
|
|
|
case T_LESS_EQUAL:
|
|
|
|
|
invertToken = T_GREATER;
|
|
|
|
|
break;
|
|
|
|
|
case T_LESS:
|
|
|
|
|
invertToken = T_GREATER_EQUAL;
|
|
|
|
|
break;
|
|
|
|
|
case T_GREATER:
|
|
|
|
|
invertToken = T_LESS_EQUAL;
|
|
|
|
|
break;
|
|
|
|
|
case T_GREATER_EQUAL:
|
|
|
|
|
invertToken = T_LESS;
|
|
|
|
|
break;
|
|
|
|
|
case T_EQUAL_EQUAL:
|
|
|
|
|
invertToken = T_EXCLAIM_EQUAL;
|
|
|
|
|
break;
|
|
|
|
|
case T_EXCLAIM_EQUAL:
|
|
|
|
|
invertToken = T_EQUAL_EQUAL;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CPlusPlus::Token tok;
|
|
|
|
|
tok.f.kind = invertToken;
|
|
|
|
|
replacement = QLatin1String(tok.spell());
|
2009-11-27 10:58:24 +01:00
|
|
|
|
|
|
|
|
// check for enclosing nested expression
|
|
|
|
|
if (index - 1 >= 0)
|
|
|
|
|
nested = path[index - 1]->asNestedExpression();
|
|
|
|
|
|
|
|
|
|
// check for ! before parentheses
|
|
|
|
|
if (nested && index - 2 >= 0) {
|
|
|
|
|
negation = path[index - 2]->asUnaryExpression();
|
|
|
|
|
if (negation && ! tokenAt(negation->unary_op_token).is(T_EXCLAIM))
|
|
|
|
|
negation = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-26 16:03:01 +01:00
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void createChangeSet()
|
|
|
|
|
{
|
2009-11-27 10:58:24 +01:00
|
|
|
if (negation) {
|
|
|
|
|
// can't remove parentheses since that might break precedence
|
|
|
|
|
remove(negation->unary_op_token);
|
|
|
|
|
} else if (nested) {
|
|
|
|
|
insert(startOf(nested), "!");
|
|
|
|
|
} else {
|
|
|
|
|
insert(startOf(binary), "!(");
|
|
|
|
|
insert(endOf(binary), ")");
|
|
|
|
|
}
|
2009-11-26 16:03:01 +01:00
|
|
|
replace(binary->binary_op_token, replacement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
BinaryExpressionAST *binary;
|
2009-11-27 10:58:24 +01:00
|
|
|
NestedExpressionAST *nested;
|
|
|
|
|
UnaryExpressionAST *negation;
|
|
|
|
|
|
2009-11-26 16:03:01 +01:00
|
|
|
QString replacement;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Rewrite
|
|
|
|
|
a op b
|
|
|
|
|
|
|
|
|
|
As
|
|
|
|
|
b flipop a
|
|
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class FlipBinaryOp: public CppQuickFixOperation
|
2009-11-26 16:03:01 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
FlipBinaryOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), binary(0)
|
2009-11-26 16:03:01 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
|
|
|
|
if (replacement.isEmpty())
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Swap Operands");
|
2009-11-26 16:03:01 +01:00
|
|
|
else
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Rewrite Using %1").arg(replacement);
|
2009-11-26 16:03:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual int match(const QList<AST *> &path)
|
|
|
|
|
{
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
binary = path.at(index)->asBinaryExpression();
|
|
|
|
|
if (! binary)
|
|
|
|
|
return -1;
|
|
|
|
|
if (! isCursorOn(binary->binary_op_token))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
CPlusPlus::Kind flipToken;
|
|
|
|
|
switch (tokenAt(binary->binary_op_token).kind()) {
|
|
|
|
|
case T_LESS_EQUAL:
|
|
|
|
|
flipToken = T_GREATER_EQUAL;
|
|
|
|
|
break;
|
|
|
|
|
case T_LESS:
|
|
|
|
|
flipToken = T_GREATER;
|
|
|
|
|
break;
|
|
|
|
|
case T_GREATER:
|
|
|
|
|
flipToken = T_LESS;
|
|
|
|
|
break;
|
|
|
|
|
case T_GREATER_EQUAL:
|
|
|
|
|
flipToken = T_LESS_EQUAL;
|
|
|
|
|
break;
|
|
|
|
|
case T_EQUAL_EQUAL:
|
|
|
|
|
case T_EXCLAIM_EQUAL:
|
|
|
|
|
case T_AMPER_AMPER:
|
|
|
|
|
case T_PIPE_PIPE:
|
|
|
|
|
flipToken = T_EOF_SYMBOL;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flipToken != T_EOF_SYMBOL) {
|
|
|
|
|
CPlusPlus::Token tok;
|
|
|
|
|
tok.f.kind = flipToken;
|
|
|
|
|
replacement = QLatin1String(tok.spell());
|
|
|
|
|
}
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void createChangeSet()
|
|
|
|
|
{
|
|
|
|
|
flip(binary->left_expression, binary->right_expression);
|
|
|
|
|
if (! replacement.isEmpty())
|
|
|
|
|
replace(binary->binary_op_token, replacement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
BinaryExpressionAST *binary;
|
|
|
|
|
QString replacement;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Rewrite
|
|
|
|
|
!a && !b
|
|
|
|
|
|
|
|
|
|
As
|
|
|
|
|
!(a || b)
|
|
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class RewriteLogicalAndOp: public CppQuickFixOperation
|
2009-11-18 12:54:39 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
RewriteLogicalAndOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), left(0), right(0), pattern(0)
|
2009-11-18 12:54:39 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Rewrite Condition Using ||");
|
2009-11-18 12:54:39 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-24 11:49:56 +01:00
|
|
|
virtual int match(const QList<AST *> &path)
|
2009-11-18 12:54:39 +01:00
|
|
|
{
|
2009-11-19 17:06:44 +01:00
|
|
|
BinaryExpressionAST *expression = 0;
|
|
|
|
|
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
for (; index != -1; --index) {
|
|
|
|
|
expression = path.at(index)->asBinaryExpression();
|
|
|
|
|
if (expression)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! expression)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2009-11-23 16:18:34 +01:00
|
|
|
if (! isCursorOn(expression->binary_op_token))
|
2009-11-19 17:06:44 +01:00
|
|
|
return -1;
|
|
|
|
|
|
2009-11-18 12:54:39 +01:00
|
|
|
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)) {
|
2009-11-19 17:06:44 +01:00
|
|
|
return index;
|
2009-11-18 12:54:39 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-19 17:06:44 +01:00
|
|
|
return -1;
|
2009-11-18 12:54:39 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-24 12:48:36 +01:00
|
|
|
virtual void createChangeSet()
|
2009-11-18 12:54:39 +01:00
|
|
|
{
|
2009-11-24 12:48:36 +01:00
|
|
|
setTopLevelNode(pattern);
|
2009-11-18 14:53:35 +01:00
|
|
|
replace(pattern->binary_op_token, QLatin1String("||"));
|
2009-11-26 13:59:58 +01:00
|
|
|
remove(left->unary_op_token);
|
|
|
|
|
remove(right->unary_op_token);
|
|
|
|
|
insert(startOf(pattern), QLatin1String("!("));
|
2009-11-18 14:53:35 +01:00
|
|
|
insert(endOf(pattern), QLatin1String(")"));
|
2009-11-18 12:54:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ASTMatcher matcher;
|
|
|
|
|
ASTPatternBuilder mk;
|
|
|
|
|
UnaryExpressionAST *left;
|
|
|
|
|
UnaryExpressionAST *right;
|
|
|
|
|
BinaryExpressionAST *pattern;
|
|
|
|
|
};
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
class SplitSimpleDeclarationOp: public CppQuickFixOperation
|
2009-11-20 10:47:19 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
SplitSimpleDeclarationOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), declaration(0)
|
2009-11-20 10:47:19 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Split Declaration");
|
2009-11-20 10:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkDeclaration(SimpleDeclarationAST *declaration) const
|
|
|
|
|
{
|
2009-11-20 10:50:19 +01:00
|
|
|
if (! declaration->semicolon_token)
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-11-20 10:47:19 +01:00
|
|
|
if (! declaration->decl_specifier_list)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (SpecifierListAST *it = declaration->decl_specifier_list; it; it = it->next) {
|
|
|
|
|
SpecifierAST *specifier = it->value;
|
|
|
|
|
|
|
|
|
|
if (specifier->asEnumSpecifier() != 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
else if (specifier->asClassSpecifier() != 0)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! declaration->declarator_list)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
else if (! declaration->declarator_list->next)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-24 11:49:56 +01:00
|
|
|
virtual int match(const QList<AST *> &path)
|
2009-11-20 10:47:19 +01:00
|
|
|
{
|
|
|
|
|
CoreDeclaratorAST *core_declarator = 0;
|
|
|
|
|
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
for (; index != -1; --index) {
|
|
|
|
|
AST *node = path.at(index);
|
|
|
|
|
|
|
|
|
|
if (CoreDeclaratorAST *coreDecl = node->asCoreDeclarator())
|
|
|
|
|
core_declarator = coreDecl;
|
|
|
|
|
|
|
|
|
|
else if (SimpleDeclarationAST *simpleDecl = node->asSimpleDeclaration()) {
|
|
|
|
|
if (checkDeclaration(simpleDecl)) {
|
|
|
|
|
declaration = simpleDecl;
|
|
|
|
|
|
2009-11-24 11:49:56 +01:00
|
|
|
const int cursorPosition = selectionStart();
|
2009-11-20 10:47:19 +01:00
|
|
|
|
|
|
|
|
const int startOfDeclSpecifier = startOf(declaration->decl_specifier_list->firstToken());
|
|
|
|
|
const int endOfDeclSpecifier = endOf(declaration->decl_specifier_list->lastToken() - 1);
|
|
|
|
|
|
|
|
|
|
if (cursorPosition >= startOfDeclSpecifier && cursorPosition <= endOfDeclSpecifier)
|
|
|
|
|
return index; // the AST node under cursor is a specifier.
|
|
|
|
|
|
2009-11-23 16:18:34 +01:00
|
|
|
if (core_declarator && isCursorOn(core_declarator))
|
2009-11-20 15:24:13 +01:00
|
|
|
return index; // got a core-declarator under the text cursor.
|
2009-11-20 10:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-24 12:48:36 +01:00
|
|
|
virtual void createChangeSet()
|
2009-11-20 10:47:19 +01:00
|
|
|
{
|
2009-11-24 12:48:36 +01:00
|
|
|
setTopLevelNode(declaration);
|
2009-11-20 10:47:19 +01:00
|
|
|
SpecifierListAST *specifiers = declaration->decl_specifier_list;
|
2009-11-26 13:59:58 +01:00
|
|
|
int declSpecifiersStart = startOf(specifiers->firstToken());
|
|
|
|
|
int declSpecifiersEnd = endOf(specifiers->lastToken() - 1);
|
|
|
|
|
int insertPos = endOf(declaration->semicolon_token);
|
2009-11-20 10:47:19 +01:00
|
|
|
|
2009-11-26 13:59:58 +01:00
|
|
|
DeclaratorAST *prevDeclarator = declaration->declarator_list->value;
|
2009-11-20 10:47:19 +01:00
|
|
|
|
|
|
|
|
for (DeclaratorListAST *it = declaration->declarator_list->next; it; it = it->next) {
|
|
|
|
|
DeclaratorAST *declarator = it->value;
|
|
|
|
|
|
2009-11-26 13:59:58 +01:00
|
|
|
insert(insertPos, QLatin1String("\n"));
|
|
|
|
|
copy(declSpecifiersStart, declSpecifiersEnd, insertPos);
|
|
|
|
|
insert(insertPos, QLatin1String(" "));
|
|
|
|
|
move(declarator, insertPos);
|
|
|
|
|
insert(insertPos, QLatin1String(";"));
|
|
|
|
|
|
|
|
|
|
remove(endOf(prevDeclarator), startOf(declarator));
|
2009-11-20 10:47:19 +01:00
|
|
|
|
2009-11-26 13:59:58 +01:00
|
|
|
prevDeclarator = declarator;
|
|
|
|
|
}
|
2009-11-20 10:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
SimpleDeclarationAST *declaration;
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-20 15:24:13 +01:00
|
|
|
/*
|
|
|
|
|
Add curly braces to a if statement that doesn't already contain a
|
|
|
|
|
compound statement.
|
|
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class AddBracesToIfOp: public CppQuickFixOperation
|
2009-11-20 15:24:13 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
AddBracesToIfOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), _statement(0)
|
2009-11-20 15:24:13 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Add Curly Braces");
|
2009-11-20 15:24:13 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-24 11:49:56 +01:00
|
|
|
virtual int match(const QList<AST *> &path)
|
2009-11-20 15:24:13 +01:00
|
|
|
{
|
|
|
|
|
// show when we're on the 'if' of an if statement
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
IfStatementAST *ifStatement = path.at(index)->asIfStatement();
|
2009-11-23 16:18:34 +01:00
|
|
|
if (ifStatement && isCursorOn(ifStatement->if_token)
|
2009-11-20 15:24:13 +01:00
|
|
|
&& ! ifStatement->statement->asCompoundStatement()) {
|
|
|
|
|
_statement = ifStatement->statement;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// or if we're on the statement contained in the if
|
|
|
|
|
// ### This may not be such a good idea, consider nested ifs...
|
|
|
|
|
for (; index != -1; --index) {
|
|
|
|
|
IfStatementAST *ifStatement = path.at(index)->asIfStatement();
|
2009-12-15 10:07:52 +01:00
|
|
|
if (ifStatement && ifStatement->statement
|
|
|
|
|
&& isCursorOn(ifStatement->statement)
|
2009-11-20 15:24:13 +01:00
|
|
|
&& ! ifStatement->statement->asCompoundStatement()) {
|
|
|
|
|
_statement = ifStatement->statement;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ### This could very well be extended to the else branch
|
|
|
|
|
// and other nodes entirely.
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-24 12:48:36 +01:00
|
|
|
virtual void createChangeSet()
|
2009-11-20 15:24:13 +01:00
|
|
|
{
|
2009-11-24 12:48:36 +01:00
|
|
|
setTopLevelNode(_statement);
|
2009-11-20 15:24:13 +01:00
|
|
|
insert(endOf(_statement->firstToken() - 1), QLatin1String(" {"));
|
|
|
|
|
insert(endOf(_statement->lastToken() - 1), "\n}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
StatementAST *_statement;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Replace
|
|
|
|
|
if (Type name = foo()) {...}
|
|
|
|
|
|
|
|
|
|
With
|
|
|
|
|
Type name = foo;
|
|
|
|
|
if (name) {...}
|
|
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class MoveDeclarationOutOfIfOp: public CppQuickFixOperation
|
2009-11-19 17:35:49 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
MoveDeclarationOutOfIfOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), condition(0), pattern(0), core(0)
|
2009-11-19 17:35:49 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Move Declaration out of Condition");
|
2009-11-19 17:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-24 11:49:56 +01:00
|
|
|
virtual int match(const QList<AST *> &path)
|
2009-11-19 17:35:49 +01:00
|
|
|
{
|
|
|
|
|
condition = mk.Condition();
|
|
|
|
|
pattern = mk.IfStatement(condition);
|
|
|
|
|
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
for (; index != -1; --index) {
|
|
|
|
|
if (IfStatementAST *statement = path.at(index)->asIfStatement()) {
|
|
|
|
|
if (statement->match(pattern, &matcher) && condition->declarator) {
|
|
|
|
|
DeclaratorAST *declarator = condition->declarator;
|
|
|
|
|
core = declarator->core_declarator;
|
|
|
|
|
if (! core)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2009-11-23 16:18:34 +01:00
|
|
|
if (isCursorOn(core))
|
2009-11-19 17:35:49 +01:00
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-24 12:48:36 +01:00
|
|
|
virtual void createChangeSet()
|
2009-11-19 17:35:49 +01:00
|
|
|
{
|
2009-11-24 12:48:36 +01:00
|
|
|
setTopLevelNode(pattern);
|
2009-11-19 17:35:49 +01:00
|
|
|
|
2009-11-26 13:59:58 +01:00
|
|
|
copy(core, startOf(condition));
|
|
|
|
|
|
|
|
|
|
int insertPos = startOf(pattern);
|
|
|
|
|
move(condition, insertPos);
|
|
|
|
|
insert(insertPos, QLatin1String(";\n"));
|
2009-11-19 17:35:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ASTMatcher matcher;
|
|
|
|
|
ASTPatternBuilder mk;
|
|
|
|
|
CPPEditor *editor;
|
|
|
|
|
ConditionAST *condition;
|
|
|
|
|
IfStatementAST *pattern;
|
|
|
|
|
CoreDeclaratorAST *core;
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-23 15:54:24 +01:00
|
|
|
/*
|
|
|
|
|
Replace
|
|
|
|
|
while (Type name = foo()) {...}
|
|
|
|
|
|
|
|
|
|
With
|
|
|
|
|
Type name;
|
|
|
|
|
while ((name = foo()) != 0) {...}
|
|
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class MoveDeclarationOutOfWhileOp: public CppQuickFixOperation
|
2009-11-23 15:54:24 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
MoveDeclarationOutOfWhileOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), condition(0), pattern(0), core(0)
|
2009-11-23 15:54:24 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Move Declaration out of Condition");
|
2009-11-23 15:54:24 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-24 11:49:56 +01:00
|
|
|
virtual int match(const QList<AST *> &path)
|
2009-11-23 15:54:24 +01:00
|
|
|
{
|
|
|
|
|
condition = mk.Condition();
|
|
|
|
|
pattern = mk.WhileStatement(condition);
|
|
|
|
|
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
for (; index != -1; --index) {
|
|
|
|
|
if (WhileStatementAST *statement = path.at(index)->asWhileStatement()) {
|
|
|
|
|
if (statement->match(pattern, &matcher) && condition->declarator) {
|
|
|
|
|
DeclaratorAST *declarator = condition->declarator;
|
|
|
|
|
core = declarator->core_declarator;
|
|
|
|
|
|
|
|
|
|
if (! core)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
else if (! declarator->equals_token)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
else if (! declarator->initializer)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2009-11-23 16:18:34 +01:00
|
|
|
if (isCursorOn(core))
|
2009-11-23 15:54:24 +01:00
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-24 12:48:36 +01:00
|
|
|
virtual void createChangeSet()
|
2009-11-23 15:54:24 +01:00
|
|
|
{
|
2009-11-24 12:48:36 +01:00
|
|
|
setTopLevelNode(pattern);
|
2009-11-26 13:59:58 +01:00
|
|
|
|
|
|
|
|
insert(startOf(condition), QLatin1String("("));
|
|
|
|
|
insert(endOf(condition), QLatin1String(") != 0"));
|
|
|
|
|
|
|
|
|
|
int insertPos = startOf(pattern);
|
|
|
|
|
move(startOf(condition), startOf(core), insertPos);
|
|
|
|
|
copy(core, insertPos);
|
|
|
|
|
insert(insertPos, QLatin1String(";\n"));
|
2009-11-23 15:54:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ASTMatcher matcher;
|
|
|
|
|
ASTPatternBuilder mk;
|
|
|
|
|
CPPEditor *editor;
|
|
|
|
|
ConditionAST *condition;
|
|
|
|
|
WhileStatementAST *pattern;
|
|
|
|
|
CoreDeclaratorAST *core;
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-18 16:12:23 +01:00
|
|
|
/*
|
|
|
|
|
Replace
|
|
|
|
|
if (something && something_else) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
with
|
|
|
|
|
if (something) {
|
|
|
|
|
if (something_else) {
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-11-18 17:31:10 +01:00
|
|
|
|
|
|
|
|
and
|
|
|
|
|
if (something || something_else)
|
|
|
|
|
x;
|
|
|
|
|
|
|
|
|
|
with
|
|
|
|
|
if (something)
|
|
|
|
|
x;
|
|
|
|
|
else if (something_else)
|
|
|
|
|
x;
|
2009-11-18 16:12:23 +01:00
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class SplitIfStatementOp: public CppQuickFixOperation
|
2009-11-18 16:12:23 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
SplitIfStatementOp(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), condition(0), pattern(0)
|
2009-11-18 16:12:23 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Split if Statement");
|
2009-11-18 16:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-24 11:49:56 +01:00
|
|
|
virtual int match(const QList<AST *> &path)
|
2009-11-18 16:12:23 +01:00
|
|
|
{
|
2009-11-19 17:06:44 +01:00
|
|
|
pattern = 0;
|
2009-11-18 16:12:23 +01:00
|
|
|
|
2009-11-19 17:06:44 +01:00
|
|
|
int index = path.size() - 1;
|
|
|
|
|
for (; index != -1; --index) {
|
|
|
|
|
AST *node = path.at(index);
|
|
|
|
|
if (IfStatementAST *stmt = node->asIfStatement()) {
|
|
|
|
|
pattern = stmt;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-15 10:07:52 +01:00
|
|
|
if (! pattern || ! pattern->statement)
|
2009-11-19 17:06:44 +01:00
|
|
|
return -1;
|
|
|
|
|
|
2009-11-27 14:43:50 +01:00
|
|
|
unsigned splitKind = 0;
|
2009-11-19 17:06:44 +01:00
|
|
|
for (++index; index < path.size(); ++index) {
|
|
|
|
|
AST *node = path.at(index);
|
|
|
|
|
condition = node->asBinaryExpression();
|
|
|
|
|
if (! condition)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
Token binaryToken = tokenAt(condition->binary_op_token);
|
2009-11-27 14:43:50 +01:00
|
|
|
|
|
|
|
|
// only accept a chain of ||s or &&s - no mixing
|
|
|
|
|
if (! splitKind) {
|
|
|
|
|
splitKind = binaryToken.kind();
|
|
|
|
|
if (splitKind != T_AMPER_AMPER && splitKind != T_PIPE_PIPE)
|
|
|
|
|
return -1;
|
|
|
|
|
// we can't reliably split &&s in ifs with an else branch
|
|
|
|
|
if (splitKind == T_AMPER_AMPER && pattern->else_statement)
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (splitKind != binaryToken.kind()) {
|
2009-11-19 17:06:44 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
2009-11-27 14:43:50 +01:00
|
|
|
|
|
|
|
|
if (isCursorOn(condition->binary_op_token))
|
|
|
|
|
return index;
|
2009-11-19 17:06:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
2009-11-18 16:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-24 12:48:36 +01:00
|
|
|
virtual void createChangeSet()
|
2009-11-18 16:12:23 +01:00
|
|
|
{
|
2009-11-19 17:06:44 +01:00
|
|
|
Token binaryToken = tokenAt(condition->binary_op_token);
|
2009-11-18 16:12:23 +01:00
|
|
|
|
2009-11-19 17:06:44 +01:00
|
|
|
if (binaryToken.is(T_AMPER_AMPER))
|
2009-11-18 17:31:10 +01:00
|
|
|
splitAndCondition();
|
|
|
|
|
else
|
|
|
|
|
splitOrCondition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void splitAndCondition()
|
|
|
|
|
{
|
2009-11-24 12:48:36 +01:00
|
|
|
setTopLevelNode(pattern);
|
2009-11-18 17:31:10 +01:00
|
|
|
|
2009-11-26 13:59:58 +01:00
|
|
|
int startPos = startOf(pattern);
|
|
|
|
|
insert(startPos, QLatin1String("if ("));
|
|
|
|
|
move(condition->left_expression, startPos);
|
|
|
|
|
insert(startPos, QLatin1String(") {\n"));
|
|
|
|
|
|
|
|
|
|
remove(endOf(condition->left_expression), startOf(condition->right_expression));
|
|
|
|
|
insert(endOf(pattern), QLatin1String("\n}"));
|
2009-11-18 16:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-18 17:31:10 +01:00
|
|
|
void splitOrCondition()
|
|
|
|
|
{
|
|
|
|
|
StatementAST *ifTrueStatement = pattern->statement;
|
|
|
|
|
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
|
|
|
|
|
|
2009-11-26 13:59:58 +01:00
|
|
|
setTopLevelNode(pattern);
|
2009-11-18 17:31:10 +01:00
|
|
|
|
2009-11-27 14:43:50 +01:00
|
|
|
int insertPos = endOf(ifTrueStatement);
|
2009-11-18 17:31:10 +01:00
|
|
|
if (compoundStatement)
|
2009-11-26 13:59:58 +01:00
|
|
|
insert(insertPos, QLatin1String(" "));
|
2009-11-18 17:31:10 +01:00
|
|
|
else
|
2009-11-26 13:59:58 +01:00
|
|
|
insert(insertPos, QLatin1String("\n"));
|
|
|
|
|
insert(insertPos, QLatin1String("else if ("));
|
|
|
|
|
move(startOf(condition->right_expression), startOf(pattern->rparen_token), insertPos);
|
|
|
|
|
insert(insertPos, QLatin1String(")"));
|
|
|
|
|
copy(endOf(pattern->rparen_token), endOf(pattern->statement), insertPos);
|
2010-01-29 22:49:55 +01:00
|
|
|
|
2009-11-26 13:59:58 +01:00
|
|
|
remove(endOf(condition->left_expression), startOf(condition->right_expression));
|
2009-11-18 17:31:10 +01:00
|
|
|
}
|
|
|
|
|
|
2009-11-18 16:12:23 +01:00
|
|
|
private:
|
|
|
|
|
BinaryExpressionAST *condition;
|
|
|
|
|
IfStatementAST *pattern;
|
|
|
|
|
};
|
2009-11-18 12:54:39 +01:00
|
|
|
|
2009-12-22 12:24:06 +01:00
|
|
|
/*
|
|
|
|
|
Replace
|
|
|
|
|
"abcd"
|
|
|
|
|
With
|
|
|
|
|
QLatin1String("abcd")
|
|
|
|
|
*/
|
2010-06-03 14:15:56 +02:00
|
|
|
class WrapStringLiteral: public CppQuickFixOperation
|
2009-12-22 12:24:06 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
WrapStringLiteral(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), stringLiteral(0), isObjCStringLiteral(false)
|
2009-12-22 12:24:06 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
|
|
|
|
{
|
2010-05-17 15:50:33 +02:00
|
|
|
return QApplication::translate("CppTools::QuickFix", "Enclose in QLatin1String(...)");
|
2009-12-22 12:24:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual int match(const QList<AST *> &path)
|
|
|
|
|
{
|
|
|
|
|
if (path.isEmpty())
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
stringLiteral = path[index]->asStringLiteral();
|
|
|
|
|
|
|
|
|
|
if (!stringLiteral)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2009-12-29 18:32:05 +01:00
|
|
|
isObjCStringLiteral = charAt(startOf(stringLiteral)) == QLatin1Char('@');
|
|
|
|
|
|
2009-12-22 12:24:06 +01:00
|
|
|
// check if it is already wrapped in QLatin1String or -Literal
|
|
|
|
|
if (index-2 < 0)
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
|
|
CallAST *call = path[index-1]->asCall();
|
|
|
|
|
PostfixExpressionAST *postfixExp = path[index-2]->asPostfixExpression();
|
|
|
|
|
if (call && postfixExp
|
|
|
|
|
&& postfixExp->base_expression
|
|
|
|
|
&& postfixExp->postfix_expression_list
|
|
|
|
|
&& postfixExp->postfix_expression_list->value == call)
|
|
|
|
|
{
|
|
|
|
|
NameAST *callName = postfixExp->base_expression->asName();
|
|
|
|
|
if (!callName)
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
|
|
QByteArray callNameString(tokenAt(callName->firstToken()).spell());
|
|
|
|
|
if (callNameString == "QLatin1String"
|
|
|
|
|
|| callNameString == "QLatin1Literal"
|
|
|
|
|
)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void createChangeSet()
|
|
|
|
|
{
|
2009-12-29 18:32:05 +01:00
|
|
|
const int startPos = startOf(stringLiteral);
|
|
|
|
|
const QLatin1String replacement("QLatin1String(");
|
|
|
|
|
|
|
|
|
|
if (isObjCStringLiteral)
|
|
|
|
|
replace(startPos, startPos + 1, replacement);
|
|
|
|
|
else
|
|
|
|
|
insert(startPos, replacement);
|
|
|
|
|
|
2009-12-22 12:24:06 +01:00
|
|
|
insert(endOf(stringLiteral), ")");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
StringLiteralAST *stringLiteral;
|
2009-12-29 18:32:05 +01:00
|
|
|
bool isObjCStringLiteral;
|
2009-12-22 12:24:06 +01:00
|
|
|
};
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
class CStringToNSString: public CppQuickFixOperation
|
2009-12-29 18:41:41 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2010-06-03 14:15:56 +02:00
|
|
|
CStringToNSString(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: CppQuickFixOperation(editor), stringLiteral(0), qlatin1Call(0)
|
2009-12-29 18:41:41 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
virtual QString description() const
|
2010-05-17 15:50:33 +02:00
|
|
|
{
|
|
|
|
|
return QApplication::translate("CppTools::QuickFix", "Convert to Objective-C String Literal");
|
|
|
|
|
}
|
2009-12-29 18:41:41 +01:00
|
|
|
|
|
|
|
|
virtual int match(const QList<AST *> &path)
|
|
|
|
|
{
|
|
|
|
|
if (path.isEmpty())
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
int index = path.size() - 1;
|
|
|
|
|
stringLiteral = path[index]->asStringLiteral();
|
|
|
|
|
|
|
|
|
|
if (!stringLiteral)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (charAt(startOf(stringLiteral)) == QLatin1Char('@'))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
// check if it is already wrapped in QLatin1String or -Literal
|
|
|
|
|
if (index-2 < 0)
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
|
|
CallAST *call = path[index-1]->asCall();
|
|
|
|
|
PostfixExpressionAST *postfixExp = path[index-2]->asPostfixExpression();
|
|
|
|
|
if (call && postfixExp
|
|
|
|
|
&& postfixExp->base_expression
|
|
|
|
|
&& postfixExp->postfix_expression_list
|
|
|
|
|
&& postfixExp->postfix_expression_list->value == call)
|
|
|
|
|
{
|
|
|
|
|
NameAST *callName = postfixExp->base_expression->asName();
|
|
|
|
|
if (!callName)
|
|
|
|
|
return index;
|
|
|
|
|
|
|
|
|
|
if (!(postfixExp->postfix_expression_list->next)) {
|
|
|
|
|
QByteArray callNameString(tokenAt(callName->firstToken()).spell());
|
|
|
|
|
if (callNameString == "QLatin1String"
|
|
|
|
|
|| callNameString == "QLatin1Literal"
|
|
|
|
|
)
|
|
|
|
|
qlatin1Call = postfixExp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void createChangeSet()
|
|
|
|
|
{
|
|
|
|
|
if (qlatin1Call) {
|
|
|
|
|
replace(startOf(qlatin1Call), startOf(stringLiteral), QLatin1String("@"));
|
|
|
|
|
remove(endOf(stringLiteral), endOf(qlatin1Call));
|
|
|
|
|
} else {
|
|
|
|
|
insert(startOf(stringLiteral), "@");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
StringLiteralAST *stringLiteral;
|
|
|
|
|
PostfixExpressionAST *qlatin1Call;
|
|
|
|
|
};
|
|
|
|
|
|
2009-11-13 16:43:26 +01:00
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
CppQuickFixOperation::CppQuickFixOperation(TextEditor::BaseTextEditor *editor)
|
|
|
|
|
: TextEditor::QuickFixOperation(editor), _topLevelNode(0)
|
2009-11-13 16:14:26 +01:00
|
|
|
{ }
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
CppQuickFixOperation::~CppQuickFixOperation()
|
2009-11-13 16:14:26 +01:00
|
|
|
{ }
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
CppQuickFixOperation::Range CppQuickFixOperation::topLevelRange() const
|
|
|
|
|
{
|
|
|
|
|
if (topLevelNode())
|
|
|
|
|
return createRange(topLevelNode());
|
2009-11-25 12:22:55 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
return Range();
|
|
|
|
|
}
|
2009-11-25 12:22:55 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
int CppQuickFixOperation::match(TextEditor::QuickFixState *state)
|
2009-12-23 12:55:35 +01:00
|
|
|
{
|
2010-06-03 14:15:56 +02:00
|
|
|
CppQuickFixState *s = static_cast<CppQuickFixState *>(state);
|
2010-06-03 14:45:55 +02:00
|
|
|
_document = s->info.doc;
|
|
|
|
|
_snapshot = s->info.snapshot;
|
2010-06-03 14:15:56 +02:00
|
|
|
return match(s->path);
|
2009-12-23 12:55:35 +01:00
|
|
|
}
|
2009-11-25 12:22:55 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
CPlusPlus::AST *CppQuickFixOperation::topLevelNode() const
|
|
|
|
|
{ return _topLevelNode; }
|
2009-11-25 12:22:55 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::setTopLevelNode(CPlusPlus::AST *topLevelNode)
|
|
|
|
|
{ _topLevelNode = topLevelNode; }
|
2009-11-25 12:22:55 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
Document::Ptr CppQuickFixOperation::document() const
|
|
|
|
|
{ return _document; }
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2010-06-03 14:45:55 +02:00
|
|
|
const Snapshot &CppQuickFixOperation::snapshot() const
|
2010-06-03 14:15:56 +02:00
|
|
|
{ return _snapshot; }
|
2009-11-24 11:49:56 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
const CPlusPlus::Token &CppQuickFixOperation::tokenAt(unsigned index) const
|
2009-11-25 12:22:55 +01:00
|
|
|
{ return _document->translationUnit()->tokenAt(index); }
|
2009-11-13 16:14:26 +01:00
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
int CppQuickFixOperation::startOf(unsigned index) const
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
2009-11-18 11:28:06 +01:00
|
|
|
unsigned line, column;
|
2009-11-25 12:22:55 +01: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-06-03 14:15:56 +02:00
|
|
|
int CppQuickFixOperation::startOf(const CPlusPlus::AST *ast) const
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
|
|
|
|
return startOf(ast->firstToken());
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
int CppQuickFixOperation::endOf(unsigned index) const
|
2009-11-18 11:28:06 +01:00
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
2009-11-25 12:22:55 +01: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-06-03 14:15:56 +02:00
|
|
|
int CppQuickFixOperation::endOf(const CPlusPlus::AST *ast) const
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
|
|
|
|
return endOf(ast->lastToken() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::startAndEndOf(unsigned index, int *start, int *end) const
|
2009-11-26 15:00:54 +01:00
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
CPlusPlus::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-06-03 14:15:56 +02:00
|
|
|
bool CppQuickFixOperation::isCursorOn(unsigned tokenIndex) const
|
2009-11-19 17:06:44 +01:00
|
|
|
{
|
|
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
|
int cursorBegin = tc.selectionStart();
|
|
|
|
|
|
|
|
|
|
int start = startOf(tokenIndex);
|
|
|
|
|
int end = endOf(tokenIndex);
|
|
|
|
|
|
|
|
|
|
if (cursorBegin >= start && cursorBegin <= end)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
bool CppQuickFixOperation::isCursorOn(const CPlusPlus::AST *ast) const
|
2009-11-20 15:24:13 +01:00
|
|
|
{
|
|
|
|
|
QTextCursor tc = textCursor();
|
|
|
|
|
int cursorBegin = tc.selectionStart();
|
|
|
|
|
|
|
|
|
|
int start = startOf(ast);
|
|
|
|
|
int end = endOf(ast);
|
|
|
|
|
|
|
|
|
|
if (cursorBegin >= start && cursorBegin <= end)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
CppQuickFixOperation::Range CppQuickFixOperation::createRange(AST *ast) const
|
2009-11-20 10:47:19 +01:00
|
|
|
{
|
2010-06-03 14:15:56 +02:00
|
|
|
return range(startOf(ast), endOf(ast));
|
2009-11-20 10:47:19 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::move(unsigned tokenIndex, int to)
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
2009-11-26 15:00:54 +01:00
|
|
|
int start, end;
|
|
|
|
|
startAndEndOf(tokenIndex, &start, &end);
|
|
|
|
|
move(start, end, to);
|
2009-11-18 14:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::move(const CPlusPlus::AST *ast, int to)
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
|
|
|
|
move(startOf(ast), endOf(ast), to);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::replace(unsigned tokenIndex, const QString &replacement)
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
2009-11-26 15:00:54 +01:00
|
|
|
int start, end;
|
|
|
|
|
startAndEndOf(tokenIndex, &start, &end);
|
|
|
|
|
replace(start, end, replacement);
|
2009-11-18 14:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::replace(const CPlusPlus::AST *ast, const QString &replacement)
|
2009-11-18 14:53:35 +01:00
|
|
|
{
|
|
|
|
|
replace(startOf(ast), endOf(ast), replacement);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::remove(unsigned tokenIndex)
|
2009-11-26 13:59:58 +01:00
|
|
|
{
|
2009-11-26 15:00:54 +01:00
|
|
|
int start, end;
|
|
|
|
|
startAndEndOf(tokenIndex, &start, &end);
|
|
|
|
|
remove(start, end);
|
2009-11-26 13:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::remove(const CPlusPlus::AST *ast)
|
2009-11-26 13:59:58 +01:00
|
|
|
{
|
|
|
|
|
remove(startOf(ast), endOf(ast));
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::flip(const CPlusPlus::AST *ast1, const CPlusPlus::AST *ast2)
|
2009-11-26 14:48:53 +01:00
|
|
|
{
|
|
|
|
|
flip(startOf(ast1), endOf(ast1), startOf(ast2), endOf(ast2));
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::copy(unsigned tokenIndex, int to)
|
2009-11-26 13:59:58 +01:00
|
|
|
{
|
2009-11-26 15:00:54 +01:00
|
|
|
int start, end;
|
|
|
|
|
startAndEndOf(tokenIndex, &start, &end);
|
|
|
|
|
copy(start, end, to);
|
2009-11-26 13:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
void CppQuickFixOperation::copy(const CPlusPlus::AST *ast, int to)
|
2009-11-26 13:59:58 +01:00
|
|
|
{
|
|
|
|
|
copy(startOf(ast), endOf(ast), to);
|
2009-11-18 14:53:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-03 14:15:56 +02:00
|
|
|
QString CppQuickFixOperation::textOf(const AST *ast) const
|
2009-11-20 10:47:19 +01:00
|
|
|
{
|
2009-11-23 16:18:34 +01:00
|
|
|
return textOf(startOf(ast), endOf(ast));
|
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-03 15:15:11 +02:00
|
|
|
TextEditor::QuickFixState *CppQuickFixCollector::initializeCompletion(TextEditor::ITextEditable *editable)
|
|
|
|
|
{
|
|
|
|
|
if (CPPEditor *editor = qobject_cast<CPPEditor *>(editable->widget())) {
|
|
|
|
|
const SemanticInfo info = editor->semanticInfo();
|
2009-11-16 16:18:08 +01:00
|
|
|
|
2010-06-03 15:15:11 +02:00
|
|
|
if (info.revision != editor->editorRevision()) {
|
|
|
|
|
// 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-06-03 15:15:11 +02:00
|
|
|
const QList<AST *> path = astPath(editor->textCursor());
|
|
|
|
|
if (! path.isEmpty()) {
|
|
|
|
|
CppQuickFixState *state = new CppQuickFixState;
|
|
|
|
|
state->path = path;
|
|
|
|
|
state->info = info;
|
|
|
|
|
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-06-03 15:15:11 +02:00
|
|
|
QList<TextEditor::QuickFixOperation::Ptr> CppQuickFixCollector::quickFixOperations(TextEditor::BaseTextEditor *editor) const
|
2009-11-13 16:14:26 +01:00
|
|
|
{
|
2010-06-03 15:15:11 +02:00
|
|
|
QSharedPointer<RewriteLogicalAndOp> rewriteLogicalAndOp(new RewriteLogicalAndOp(editor));
|
|
|
|
|
QSharedPointer<SplitIfStatementOp> splitIfStatementOp(new SplitIfStatementOp(editor));
|
|
|
|
|
QSharedPointer<MoveDeclarationOutOfIfOp> moveDeclarationOutOfIfOp(new MoveDeclarationOutOfIfOp(editor));
|
|
|
|
|
QSharedPointer<MoveDeclarationOutOfWhileOp> moveDeclarationOutOfWhileOp(new MoveDeclarationOutOfWhileOp(editor));
|
|
|
|
|
QSharedPointer<SplitSimpleDeclarationOp> splitSimpleDeclarationOp(new SplitSimpleDeclarationOp(editor));
|
|
|
|
|
QSharedPointer<AddBracesToIfOp> addBracesToIfOp(new AddBracesToIfOp(editor));
|
|
|
|
|
QSharedPointer<UseInverseOp> useInverseOp(new UseInverseOp(editor));
|
|
|
|
|
QSharedPointer<FlipBinaryOp> flipBinaryOp(new FlipBinaryOp(editor));
|
|
|
|
|
QSharedPointer<WrapStringLiteral> wrapStringLiteral(new WrapStringLiteral(editor));
|
|
|
|
|
QSharedPointer<CStringToNSString> wrapCString(new CStringToNSString(editor));
|
|
|
|
|
|
|
|
|
|
QList<TextEditor::QuickFixOperation::Ptr> quickFixOperations;
|
|
|
|
|
quickFixOperations.append(rewriteLogicalAndOp);
|
|
|
|
|
quickFixOperations.append(splitIfStatementOp);
|
|
|
|
|
quickFixOperations.append(moveDeclarationOutOfIfOp);
|
|
|
|
|
quickFixOperations.append(moveDeclarationOutOfWhileOp);
|
|
|
|
|
quickFixOperations.append(splitSimpleDeclarationOp);
|
|
|
|
|
quickFixOperations.append(addBracesToIfOp);
|
|
|
|
|
quickFixOperations.append(useInverseOp);
|
|
|
|
|
quickFixOperations.append(flipBinaryOp);
|
|
|
|
|
quickFixOperations.append(wrapStringLiteral);
|
|
|
|
|
|
|
|
|
|
if (editor->mimeType() == CppTools::Constants::OBJECTIVE_CPP_SOURCE_MIMETYPE)
|
|
|
|
|
quickFixOperations.append(wrapCString);
|
|
|
|
|
|
|
|
|
|
return quickFixOperations;
|
2009-11-13 16:14:26 +01:00
|
|
|
}
|