Commit 581dca0a authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer

Rewrote PrettyPrinter to output the document exactly as it was before

This means at the moment the PrettyPrinter isn't doing anything useful,
but the idea is that from here we can improve it to adapt the code to
a certain style.

Reviewed-by: Roberto Raggi
parent ecc2b25d
......@@ -33,6 +33,7 @@
#include "cpphighlighter.h"
#include <AST.h>
#include <Control.h>
#include <Token.h>
#include <Scope.h>
#include <Symbols.h>
......@@ -40,8 +41,10 @@
#include <Control.h>
#include <CoreTypes.h>
#include <Literals.h>
#include <PrettyPrinter.h>
#include <Semantic.h>
#include <SymbolVisitor.h>
#include <TranslationUnit.h>
#include <cplusplus/ExpressionUnderCursor.h>
#include <cplusplus/LookupContext.h>
#include <cplusplus/Overview.h>
......@@ -77,6 +80,8 @@
#include <QtGui/QTreeView>
#include <QtGui/QSortFilterProxyModel>
#include <sstream>
using namespace CPlusPlus;
using namespace CppEditor::Internal;
......@@ -377,6 +382,34 @@ void CPPEditor::onDocumentUpdated(Document::Ptr doc)
updateMethodBoxIndex();
}
void CPPEditor::reformatDocument()
{
using namespace CPlusPlus;
QByteArray source = toPlainText().toUtf8();
Control control;
StringLiteral *fileId = control.findOrInsertFileName("<file>");
TranslationUnit unit(&control, fileId);
unit.setQtMocRunEnabled(true);
unit.setSource(source.constData(), source.length());
unit.parse();
if (! unit.ast())
return;
std::ostringstream s;
TranslationUnitAST *ast = unit.ast()->asTranslationUnit();
PrettyPrinter pp(&control, s);
pp(ast, source);
const std::string str = s.str();
QTextCursor c = textCursor();
c.setPosition(0);
c.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
c.insertText(QString::fromUtf8(str.c_str(), str.length()));
}
void CPPEditor::updateFileName()
{ }
......@@ -856,6 +889,10 @@ void CPPEditor::contextMenuEvent(QContextMenuEvent *e)
foreach (QAction *action, contextMenu->actions())
menu->addAction(action);
QAction *reformatDocument = new QAction(tr("Reformat Document"), menu);
connect(reformatDocument, SIGNAL(triggered()), this, SLOT(reformatDocument()));
menu->addAction(reformatDocument);
menu->exec(e->globalPos());
delete menu;
}
......
......@@ -96,6 +96,7 @@ public slots:
void deleteStartOfToken();
void deleteEndOfToken();
protected:
void contextMenuEvent(QContextMenuEvent *);
void mouseMoveEvent(QMouseEvent *);
......@@ -114,6 +115,7 @@ private slots:
void updateMethodBoxIndex();
void updateMethodBoxToolTip();
void onDocumentUpdated(CPlusPlus::Document::Ptr doc);
void reformatDocument();
private:
bool sortedMethodOverview() const;
......
......@@ -69,6 +69,9 @@ Control *ASTVisitor::control() const
TranslationUnit *ASTVisitor::translationUnit() const
{ return _control->translationUnit(); }
unsigned ASTVisitor::tokenCount() const
{ return translationUnit()->tokenCount(); }
const Token &ASTVisitor::tokenAt(unsigned index) const
{ return translationUnit()->tokenAt(index); }
......
......@@ -67,6 +67,7 @@ public:
Control *control() const;
TranslationUnit *translationUnit() const;
unsigned tokenCount() const;
const Token &tokenAt(unsigned index) const;
int tokenKind(unsigned index) const;
const char *spell(unsigned index) const;
......
This diff is collapsed.
......@@ -34,6 +34,7 @@
#include "ASTVisitor.h"
#include <iosfwd>
#include <QByteArray>
CPLUSPLUS_BEGIN_HEADER
CPLUSPLUS_BEGIN_NAMESPACE
......@@ -43,7 +44,7 @@ class CPLUSPLUS_EXPORT PrettyPrinter: protected ASTVisitor
public:
PrettyPrinter(Control *control, std::ostream &out);
void operator()(AST *ast);
void operator()(AST *ast, const QByteArray &contents);
protected:
virtual bool visit(AccessDeclarationAST *ast);
......@@ -148,10 +149,14 @@ protected:
void indent();
void deindent();
void newline();
void outToken(unsigned token);
private:
std::ostream &out;
unsigned depth;
std::ostream &_out;
unsigned _depth;
unsigned _lastToken;
QByteArray _contents;
};
CPLUSPLUS_END_NAMESPACE
......
......@@ -73,8 +73,9 @@ public:
void rewrite(const TranslationUnit *unit,
const QByteArray &contents,
QByteArray *out) const
QByteArray *out)
{
_source = contents;
const char *source = contents.constData();
unsigned previousTokenEndPosition = 0;
for (unsigned i = 0; i < unit->tokenCount(); ++i) {
......@@ -104,6 +105,9 @@ public:
previousTokenEndPosition = tk.end();
}
}
protected:
QByteArray _source;
};
class SimpleRefactor: protected ASTVisitor, Rewrite {
......@@ -192,12 +196,13 @@ protected:
return false;
}
virtual bool visit(CppCastExpressionAST *ast) {
virtual bool visit(CppCastExpressionAST *ast)
{
// Replace the C++ cast expression (e.g. static_cast<foo>(a)) with
// the one generated by the pretty printer.
std::ostringstream o;
PrettyPrinter pp(control(), o);
pp(ast);
pp(ast, _source);
remove(ast->firstToken(), ast->lastToken());
const std::string str = o.str();
insertTextBefore(ast->firstToken(), str.c_str());
......@@ -267,7 +272,7 @@ int main(int argc, char *argv[])
printf("%s\n", out.constData());
} else if (test_pretty_printer) {
PrettyPrinter pp(&control, std::cout);
pp(unit.ast());
pp(unit.ast(), source);
}
return EXIT_SUCCESS;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment