forked from qt-creator/qt-creator
Cleanup
This commit is contained in:
@@ -98,8 +98,8 @@ protected:
|
|||||||
class RewriteLogicalAndOp: public QuickFixOperation
|
class RewriteLogicalAndOp: public QuickFixOperation
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RewriteLogicalAndOp(Document::Ptr doc, const Snapshot &snapshot)
|
RewriteLogicalAndOp(Document::Ptr doc, const Snapshot &snapshot, CPPEditor *editor)
|
||||||
: QuickFixOperation(doc, snapshot), matcher(doc->translationUnit()),
|
: QuickFixOperation(doc, snapshot, editor), matcher(doc->translationUnit()),
|
||||||
left(0), right(0), pattern(0)
|
left(0), right(0), pattern(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -148,7 +148,7 @@ public:
|
|||||||
replace(right->unary_op_token, QLatin1String(""));
|
replace(right->unary_op_token, QLatin1String(""));
|
||||||
insert(endOf(pattern), QLatin1String(")"));
|
insert(endOf(pattern), QLatin1String(")"));
|
||||||
|
|
||||||
execute();
|
applyChanges(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -163,7 +163,7 @@ class SplitSimpleDeclarationOp: public QuickFixOperation
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SplitSimpleDeclarationOp(Document::Ptr doc, const Snapshot &snapshot, CPPEditor *editor)
|
SplitSimpleDeclarationOp(Document::Ptr doc, const Snapshot &snapshot, CPPEditor *editor)
|
||||||
: QuickFixOperation(doc, snapshot), declaration(0), editor(editor)
|
: QuickFixOperation(doc, snapshot, editor), declaration(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
virtual QString description() const
|
virtual QString description() const
|
||||||
@@ -241,8 +241,6 @@ public:
|
|||||||
|
|
||||||
virtual void apply()
|
virtual void apply()
|
||||||
{
|
{
|
||||||
QTextCursor completeDeclaration = createCursor(declaration);
|
|
||||||
|
|
||||||
SpecifierListAST *specifiers = declaration->decl_specifier_list;
|
SpecifierListAST *specifiers = declaration->decl_specifier_list;
|
||||||
const QString declSpecifiers = textOf(startOf(specifiers->firstToken()), endOf(specifiers->lastToken() - 1));
|
const QString declSpecifiers = textOf(startOf(specifiers->firstToken()), endOf(specifiers->lastToken() - 1));
|
||||||
|
|
||||||
@@ -262,22 +260,18 @@ public:
|
|||||||
|
|
||||||
insert(endOf(declaration->semicolon_token), text);
|
insert(endOf(declaration->semicolon_token), text);
|
||||||
|
|
||||||
completeDeclaration.beginEditBlock();
|
applyChanges(declaration);
|
||||||
execute();
|
|
||||||
editor->indentInsertedText(completeDeclaration);
|
|
||||||
completeDeclaration.endEditBlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SimpleDeclarationAST *declaration;
|
SimpleDeclarationAST *declaration;
|
||||||
CPPEditor *editor;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class TakeDeclarationOp: public QuickFixOperation
|
class TakeDeclarationOp: public QuickFixOperation
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TakeDeclarationOp(Document::Ptr doc, const Snapshot &snapshot, CPPEditor *editor)
|
TakeDeclarationOp(Document::Ptr doc, const Snapshot &snapshot, CPPEditor *editor)
|
||||||
: QuickFixOperation(doc, snapshot), matcher(doc->translationUnit()), editor(editor),
|
: QuickFixOperation(doc, snapshot, editor), matcher(doc->translationUnit()),
|
||||||
condition(0), pattern(0), core(0)
|
condition(0), pattern(0), core(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -319,9 +313,7 @@ public:
|
|||||||
|
|
||||||
virtual void apply()
|
virtual void apply()
|
||||||
{
|
{
|
||||||
QTextCursor completeIfStatement = createCursor(pattern);
|
const QString name = selectNode(core).selectedText();
|
||||||
|
|
||||||
QString name = selectNode(core).selectedText();
|
|
||||||
QString declaration = selectNode(condition).selectedText();
|
QString declaration = selectNode(condition).selectedText();
|
||||||
declaration += QLatin1String(";\n");
|
declaration += QLatin1String(";\n");
|
||||||
|
|
||||||
@@ -329,10 +321,7 @@ public:
|
|||||||
insert(endOf(pattern->lparen_token), name);
|
insert(endOf(pattern->lparen_token), name);
|
||||||
replace(condition, name);
|
replace(condition, name);
|
||||||
|
|
||||||
completeIfStatement.beginEditBlock();
|
applyChanges(pattern);
|
||||||
execute();
|
|
||||||
editor->indentInsertedText(completeIfStatement);
|
|
||||||
completeIfStatement.endEditBlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -369,8 +358,8 @@ class SplitIfStatementOp: public QuickFixOperation
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SplitIfStatementOp(Document::Ptr doc, const Snapshot &snapshot, CPPEditor *editor)
|
SplitIfStatementOp(Document::Ptr doc, const Snapshot &snapshot, CPPEditor *editor)
|
||||||
: QuickFixOperation(doc, snapshot),
|
: QuickFixOperation(doc, snapshot, editor),
|
||||||
condition(0), pattern(0), editor(editor)
|
condition(0), pattern(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
virtual QString description() const
|
virtual QString description() const
|
||||||
@@ -426,8 +415,6 @@ public:
|
|||||||
|
|
||||||
void splitAndCondition()
|
void splitAndCondition()
|
||||||
{
|
{
|
||||||
QTextCursor completeIfStatement = createCursor(pattern);
|
|
||||||
|
|
||||||
StatementAST *ifTrueStatement = pattern->statement;
|
StatementAST *ifTrueStatement = pattern->statement;
|
||||||
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
|
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
|
||||||
|
|
||||||
@@ -460,17 +447,11 @@ public:
|
|||||||
if (! compoundStatement)
|
if (! compoundStatement)
|
||||||
insert(endOf(ifTrueStatement), "\n}"); // finish the compound statement
|
insert(endOf(ifTrueStatement), "\n}"); // finish the compound statement
|
||||||
|
|
||||||
QTextCursor tc = textCursor();
|
applyChanges(pattern);
|
||||||
tc.beginEditBlock();
|
|
||||||
execute();
|
|
||||||
editor->indentInsertedText(completeIfStatement);
|
|
||||||
tc.endEditBlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void splitOrCondition()
|
void splitOrCondition()
|
||||||
{
|
{
|
||||||
QTextCursor completeIfStatement = createCursor(pattern);
|
|
||||||
|
|
||||||
StatementAST *ifTrueStatement = pattern->statement;
|
StatementAST *ifTrueStatement = pattern->statement;
|
||||||
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
|
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
|
||||||
|
|
||||||
@@ -500,30 +481,29 @@ public:
|
|||||||
|
|
||||||
insert(endOf(pattern), elseIfStatement);
|
insert(endOf(pattern), elseIfStatement);
|
||||||
|
|
||||||
QTextCursor tc = textCursor();
|
applyChanges(pattern);
|
||||||
tc.beginEditBlock();
|
|
||||||
execute();
|
|
||||||
editor->indentInsertedText(completeIfStatement);
|
|
||||||
tc.endEditBlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BinaryExpressionAST *condition;
|
BinaryExpressionAST *condition;
|
||||||
IfStatementAST *pattern;
|
IfStatementAST *pattern;
|
||||||
QPointer<CPPEditor> editor;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end of anonymous namespace
|
} // end of anonymous namespace
|
||||||
|
|
||||||
|
|
||||||
QuickFixOperation::QuickFixOperation(CPlusPlus::Document::Ptr doc,
|
QuickFixOperation::QuickFixOperation(CPlusPlus::Document::Ptr doc,
|
||||||
const CPlusPlus::Snapshot &snapshot)
|
const CPlusPlus::Snapshot &snapshot,
|
||||||
: _doc(doc), _snapshot(snapshot)
|
CPPEditor *editor)
|
||||||
|
: _doc(doc), _snapshot(snapshot), _editor(editor)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
QuickFixOperation::~QuickFixOperation()
|
QuickFixOperation::~QuickFixOperation()
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
CPPEditor *QuickFixOperation::editor() const
|
||||||
|
{ return _editor; }
|
||||||
|
|
||||||
QTextCursor QuickFixOperation::textCursor() const
|
QTextCursor QuickFixOperation::textCursor() const
|
||||||
{ return _textCursor; }
|
{ return _textCursor; }
|
||||||
|
|
||||||
@@ -587,15 +567,20 @@ QTextCursor QuickFixOperation::selectNode(AST *ast) const
|
|||||||
return tc;
|
return tc;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextCursor QuickFixOperation::createCursor(AST *ast) const
|
QuickFixOperation::Range QuickFixOperation::createRange(AST *ast) const
|
||||||
{
|
{
|
||||||
QTextCursor cursor = selectNode(ast);
|
QTextCursor tc = _textCursor;
|
||||||
// ### HACK
|
Range r(tc);
|
||||||
const int anchor = cursor.anchor();
|
r.begin.setPosition(startOf(ast));
|
||||||
const int position = cursor.position();
|
r.end.setPosition(endOf(ast));
|
||||||
cursor.setPosition(position);
|
return r;
|
||||||
cursor.setPosition(anchor, QTextCursor::KeepAnchor);
|
}
|
||||||
return cursor;
|
|
||||||
|
void QuickFixOperation::reindent(const Range &range)
|
||||||
|
{
|
||||||
|
QTextCursor tc = range.begin;
|
||||||
|
tc.setPosition(range.end.position(), QTextCursor::KeepAnchor);
|
||||||
|
_editor->indentInsertedText(tc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QuickFixOperation::move(int start, int end, int to)
|
void QuickFixOperation::move(int start, int end, int to)
|
||||||
@@ -648,9 +633,17 @@ QString QuickFixOperation::textOf(AST *ast) const
|
|||||||
return selectNode(ast).selectedText();
|
return selectNode(ast).selectedText();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QuickFixOperation::execute()
|
void QuickFixOperation::applyChanges(AST *ast)
|
||||||
{
|
{
|
||||||
|
Range range;
|
||||||
|
if (ast)
|
||||||
|
range = createRange(ast);
|
||||||
|
|
||||||
|
_textCursor.beginEditBlock();
|
||||||
_textWriter.write(&_textCursor);
|
_textWriter.write(&_textCursor);
|
||||||
|
if (ast)
|
||||||
|
reindent(range);
|
||||||
|
_textCursor.endEditBlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
CPPQuickFixCollector::CPPQuickFixCollector()
|
CPPQuickFixCollector::CPPQuickFixCollector()
|
||||||
@@ -687,7 +680,7 @@ int CPPQuickFixCollector::startCompletion(TextEditor::ITextEditable *editable)
|
|||||||
const QList<AST *> path = astPath(_editor->textCursor());
|
const QList<AST *> path = astPath(_editor->textCursor());
|
||||||
// ### build the list of the quick fix ops by scanning path.
|
// ### build the list of the quick fix ops by scanning path.
|
||||||
|
|
||||||
QSharedPointer<RewriteLogicalAndOp> rewriteLogicalAndOp(new RewriteLogicalAndOp(info.doc, info.snapshot));
|
QSharedPointer<RewriteLogicalAndOp> rewriteLogicalAndOp(new RewriteLogicalAndOp(info.doc, info.snapshot, _editor));
|
||||||
QSharedPointer<SplitIfStatementOp> splitIfStatementOp(new SplitIfStatementOp(info.doc, info.snapshot, _editor));
|
QSharedPointer<SplitIfStatementOp> splitIfStatementOp(new SplitIfStatementOp(info.doc, info.snapshot, _editor));
|
||||||
QSharedPointer<TakeDeclarationOp> takeDeclarationOp(new TakeDeclarationOp(info.doc, info.snapshot, _editor));
|
QSharedPointer<TakeDeclarationOp> takeDeclarationOp(new TakeDeclarationOp(info.doc, info.snapshot, _editor));
|
||||||
QSharedPointer<SplitSimpleDeclarationOp> splitSimpleDeclarationOp(new SplitSimpleDeclarationOp(info.doc, info.snapshot, _editor));
|
QSharedPointer<SplitSimpleDeclarationOp> splitSimpleDeclarationOp(new SplitSimpleDeclarationOp(info.doc, info.snapshot, _editor));
|
||||||
|
|||||||
@@ -57,12 +57,12 @@ class QuickFixOperation
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
QuickFixOperation(CPlusPlus::Document::Ptr doc,
|
QuickFixOperation(CPlusPlus::Document::Ptr doc,
|
||||||
const CPlusPlus::Snapshot &snapshot);
|
const CPlusPlus::Snapshot &snapshot,
|
||||||
|
CPPEditor *editor);
|
||||||
|
|
||||||
virtual ~QuickFixOperation();
|
virtual ~QuickFixOperation();
|
||||||
|
|
||||||
virtual QString description() const = 0;
|
virtual QString description() const = 0;
|
||||||
virtual void apply() = 0;
|
|
||||||
virtual int match(const QList<CPlusPlus::AST *> &path, QTextCursor tc) = 0;
|
virtual int match(const QList<CPlusPlus::AST *> &path, QTextCursor tc) = 0;
|
||||||
|
|
||||||
CPlusPlus::Document::Ptr document() const { return _doc; }
|
CPlusPlus::Document::Ptr document() const { return _doc; }
|
||||||
@@ -71,6 +71,10 @@ public:
|
|||||||
QTextCursor textCursor() const;
|
QTextCursor textCursor() const;
|
||||||
void setTextCursor(const QTextCursor &cursor);
|
void setTextCursor(const QTextCursor &cursor);
|
||||||
|
|
||||||
|
CPPEditor *editor() const;
|
||||||
|
|
||||||
|
virtual void apply() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const CPlusPlus::Token &tokenAt(unsigned index) const;
|
const CPlusPlus::Token &tokenAt(unsigned index) const;
|
||||||
QTextCursor selectToken(unsigned index) const;
|
QTextCursor selectToken(unsigned index) const;
|
||||||
@@ -94,15 +98,25 @@ protected:
|
|||||||
QString textOf(int firstOffset, int lastOffset) const;
|
QString textOf(int firstOffset, int lastOffset) const;
|
||||||
QString textOf(CPlusPlus::AST *ast) const;
|
QString textOf(CPlusPlus::AST *ast) const;
|
||||||
|
|
||||||
QTextCursor createCursor(CPlusPlus::AST *ast) const; // ### rename me
|
struct Range {
|
||||||
|
Range() {}
|
||||||
|
Range(const QTextCursor &tc): begin(tc), end(tc) {}
|
||||||
|
|
||||||
void execute();
|
QTextCursor begin;
|
||||||
|
QTextCursor end;
|
||||||
|
};
|
||||||
|
|
||||||
|
Range createRange(CPlusPlus::AST *ast) const; // ### rename me
|
||||||
|
void reindent(const Range &range);
|
||||||
|
|
||||||
|
void applyChanges(CPlusPlus::AST *ast = 0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CPlusPlus::Document::Ptr _doc;
|
CPlusPlus::Document::Ptr _doc;
|
||||||
CPlusPlus::Snapshot _snapshot;
|
CPlusPlus::Snapshot _snapshot;
|
||||||
QTextCursor _textCursor;
|
QTextCursor _textCursor;
|
||||||
Utils::TextWriter _textWriter;
|
Utils::TextWriter _textWriter;
|
||||||
|
CPPEditor *_editor;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CPPQuickFixCollector: public TextEditor::IQuickFixCollector
|
class CPPQuickFixCollector: public TextEditor::IQuickFixCollector
|
||||||
|
|||||||
Reference in New Issue
Block a user