Commit 297b281c authored by Erik Verbruggen's avatar Erik Verbruggen

Added infrastructure to change multiple files at once.

parent b57a1611
......@@ -733,6 +733,11 @@ TokenCache *CPPEditor::tokenCache() const
return m_modelManager->tokenCache(editableInterface());
}
CppTools::CppModelManagerInterface *CPPEditor::modelManager() const
{
return m_modelManager;
}
void CPPEditor::startRename()
{
m_inRenameChanged = false;
......
......@@ -201,6 +201,8 @@ public:
CPlusPlus::TokenCache *tokenCache() const;
CppTools::CppModelManagerInterface *modelManager() const;
public Q_SLOTS:
virtual void setFontSettings(const TextEditor::FontSettings &);
void setSortedMethodOverview(bool sort);
......
......@@ -15,6 +15,7 @@ HEADERS += cppplugin.h \
cppeditor_global.h \
cppclasswizard.h \
cppquickfix.h
SOURCES += cppplugin.cpp \
cppeditor.cpp \
cpphighlighter.cpp \
......@@ -22,6 +23,7 @@ SOURCES += cppplugin.cpp \
cppfilewizard.cpp \
cppclasswizard.cpp \
cppquickfix.cpp
RESOURCES += cppeditor.qrc
OTHER_FILES += CppEditor.pluginspec CppEditor.mimetypes.xml
......@@ -47,6 +47,7 @@
#include <Name.h>
#include <Literals.h>
#include <cpptools/cpprefactoringchanges.h>
#include <cpptools/cpptoolsconstants.h>
#include <cpptools/cppmodelmanagerinterface.h>
......@@ -864,28 +865,39 @@ private:
CppQuickFixOperation::CppQuickFixOperation(TextEditor::BaseTextEditor *editor)
: TextEditor::QuickFixOperation(editor), _topLevelNode(0)
: TextEditor::QuickFixOperation(editor)
, _refactoringChanges(0)
, _topLevelNode(0)
{ }
CppQuickFixOperation::~CppQuickFixOperation()
{ }
CppQuickFixOperation::Range CppQuickFixOperation::topLevelRange() const
{
if (topLevelNode())
return createRange(topLevelNode());
return Range();
if (_refactoringChanges)
delete _refactoringChanges;
}
int CppQuickFixOperation::match(TextEditor::QuickFixState *state)
{
CppQuickFixState *s = static_cast<CppQuickFixState *>(state);
_document = s->info.doc;
_snapshot = s->info.snapshot;
if (_refactoringChanges)
delete _refactoringChanges;
CPPEditor *cppEditor = qobject_cast<CPPEditor*>(editor());
_refactoringChanges = new CppTools::CppRefactoringChanges(s->info.snapshot, cppEditor->modelManager());
return match(s->path);
}
void CppQuickFixOperation::apply()
{
cppRefactoringChanges()->apply();
}
CppTools::CppRefactoringChanges *CppQuickFixOperation::cppRefactoringChanges() const
{ return _refactoringChanges; }
TextEditor::RefactoringChanges *CppQuickFixOperation::refactoringChanges() const
{ return cppRefactoringChanges(); }
CPlusPlus::AST *CppQuickFixOperation::topLevelNode() const
{ return _topLevelNode; }
......@@ -896,7 +908,9 @@ Document::Ptr CppQuickFixOperation::document() const
{ return _document; }
const Snapshot &CppQuickFixOperation::snapshot() const
{ return _snapshot; }
{
return _refactoringChanges->snapshot();
}
const CPlusPlus::Token &CppQuickFixOperation::tokenAt(unsigned index) const
{ return _document->translationUnit()->tokenAt(index); }
......@@ -965,11 +979,6 @@ bool CppQuickFixOperation::isCursorOn(const CPlusPlus::AST *ast) const
return false;
}
CppQuickFixOperation::Range CppQuickFixOperation::createRange(AST *ast) const
{
return range(startOf(ast), endOf(ast));
}
void CppQuickFixOperation::move(unsigned tokenIndex, int to)
{
int start, end;
......
......@@ -43,6 +43,7 @@
namespace CppTools {
class CppModelManagerInterface;
class CppRefactoringChanges;
} // end of namespace CppTools
namespace CppEditor {
......@@ -61,10 +62,13 @@ public:
CPlusPlus::Document::Ptr document() const;
const CPlusPlus::Snapshot &snapshot() const;
virtual Range topLevelRange() const;
virtual int match(TextEditor::QuickFixState *state);
protected:
virtual void apply();
virtual CppTools::CppRefactoringChanges *cppRefactoringChanges() const;
virtual TextEditor::RefactoringChanges *refactoringChanges() const;
CPlusPlus::AST *topLevelNode() const;
void setTopLevelNode(CPlusPlus::AST *topLevelNode);
......@@ -100,11 +104,10 @@ protected:
void copy(const CPlusPlus::AST *ast, int to);
QString textOf(const CPlusPlus::AST *ast) const;
Range createRange(CPlusPlus::AST *ast) const; // ### rename me
private:
CppTools::CppRefactoringChanges *_refactoringChanges;
CPlusPlus::Document::Ptr _document;
CPlusPlus::Snapshot _snapshot;
CPlusPlus::AST *_topLevelNode;
};
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 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 "cpprefactoringchanges.h"
using namespace CPlusPlus;
using namespace CppTools;
using namespace TextEditor;
CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot,
CppModelManagerInterface *modelManager)
: m_snapshot(snapshot)
, m_modelManager(modelManager)
, m_workingCopy(modelManager->workingCopy())
{
Q_ASSERT(modelManager);
}
QStringList CppRefactoringChanges::apply()
{
const QStringList changedFiles = TextEditor::RefactoringChanges::apply();
m_modelManager->updateSourceFiles(changedFiles);
return changedFiles;
}
Document::Ptr CppRefactoringChanges::parsedDocumentForFile(const QString &fileName) const
{
Document::Ptr doc = m_snapshot.document(fileName);
QString source;
if (m_workingCopy.contains(fileName)) {
QPair<QString, unsigned> workingCopy = m_workingCopy.get(fileName);
if (doc && doc->editorRevision() == workingCopy.second)
return doc;
else
source = workingCopy.first;
} else {
QFile file(fileName);
if (! file.open(QFile::ReadOnly))
return Document::Ptr();
source = QTextStream(&file).readAll(); // ### FIXME read bytes, and remove the convert below
file.close();
}
doc = m_snapshot.documentFromSource(source.toLatin1(), fileName);
doc->check();
return doc;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 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.
**
**************************************************************************/
#ifndef CPPREFACTORINGCHANGES_H
#define CPPREFACTORINGCHANGES_H
#include <cplusplus/CppDocument.h>
#include <cpptools/cppmodelmanagerinterface.h>
#include <cpptools/cpptools_global.h>
#include <texteditor/refactoringchanges.h>
namespace CppTools {
class CPPTOOLS_EXPORT CppRefactoringChanges: public TextEditor::RefactoringChanges
{
public:
CppRefactoringChanges(const CPlusPlus::Snapshot &snapshot, CppModelManagerInterface *modelManager);
virtual QStringList apply();
const CPlusPlus::Snapshot &snapshot() const
{ return m_snapshot; }
CPlusPlus::Document::Ptr parsedDocumentForFile(const QString &fileName) const;
private:
CPlusPlus::Snapshot m_snapshot;
CppModelManagerInterface *m_modelManager;
CppModelManagerInterface::WorkingCopy m_workingCopy;
};
} // namespace CppTools
#endif // CPPREFACTORINGCHANGES_H
......@@ -23,7 +23,8 @@ HEADERS += completionsettingspage.h \
searchsymbols.h \
cppdoxygen.h \
cppfilesettingspage.h \
cppfindreferences.h
cppfindreferences.h \
cpprefactoringchanges.h
SOURCES += completionsettingspage.cpp \
cppclassesfilter.cpp \
......@@ -38,7 +39,8 @@ SOURCES += completionsettingspage.cpp \
cppdoxygen.cpp \
cppfilesettingspage.cpp \
abstracteditorsupport.cpp \
cppfindreferences.cpp
cppfindreferences.cpp \
cpprefactoringchanges.cpp
FORMS += completionsettingspage.ui \
cppfilesettingspage.ui
......
......@@ -436,7 +436,7 @@ protected:
range.ast = member;
range.begin = QTextCursor(_textDocument);
range.begin.setPosition(ast->lbraceToken.begin());
range.begin.setPosition(member->firstSourceLocation().begin());
range.end = QTextCursor(_textDocument);
range.end.setPosition(ast->rbraceToken.end());
......
......@@ -21,7 +21,8 @@ HEADERS += \
qmljshoverhandler.h \
qmljsmodelmanager.h \
qmljspreviewrunner.h \
qmljsquickfix.h
qmljsquickfix.h \
qmljsrefactoringchanges.h
SOURCES += \
qmljscodecompletion.cpp \
......@@ -35,7 +36,8 @@ SOURCES += \
qmljshoverhandler.cpp \
qmljsmodelmanager.cpp \
qmljspreviewrunner.cpp \
qmljsquickfix.cpp
qmljsquickfix.cpp \
qmljsrefactoringchanges.cpp
RESOURCES += qmljseditor.qrc
OTHER_FILES += QmlJSEditor.pluginspec QmlJSEditor.mimetypes.xml
......@@ -29,11 +29,19 @@
#include "qmljsquickfix.h"
#include "qmljseditor.h"
#include "qmljsrefactoringchanges.h"
#include "qmljs/parser/qmljsast_p.h"
#include <extensionsystem/pluginmanager.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <QtGui/QApplication>
#include <QtCore/QDebug>
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
using TextEditor::RefactoringChanges;
class QmlJSQuickFixState: public TextEditor::QuickFixState
{
......@@ -54,14 +62,6 @@ public:
return QApplication::translate("QmlJSEditor::QuickFix", "Split initializer");
}
virtual Range topLevelRange() const
{
Q_ASSERT(_objectInitializer);
return range(position(_objectInitializer->lbraceToken),
position(_objectInitializer->rbraceToken));
}
virtual void createChangeSet()
{
Q_ASSERT(_objectInitializer != 0);
......@@ -77,6 +77,10 @@ public:
// insert a newline before the closing brace
insert(position(_objectInitializer->rbraceToken), QLatin1String("\n"));
reindent(RefactoringChanges::Range(position(_objectInitializer->lbraceToken),
position(_objectInitializer->rbraceToken)));
}
virtual int check()
......@@ -112,11 +116,14 @@ private:
QmlJSQuickFixOperation::QmlJSQuickFixOperation(TextEditor::BaseTextEditor *editor)
: TextEditor::QuickFixOperation(editor)
, _refactoringChanges(0)
{
}
QmlJSQuickFixOperation::~QmlJSQuickFixOperation()
{
if (_refactoringChanges)
delete _refactoringChanges;
}
QmlJS::Document::Ptr QmlJSQuickFixOperation::document() const
......@@ -136,11 +143,27 @@ const SemanticInfo &QmlJSQuickFixOperation::semanticInfo() const
int QmlJSQuickFixOperation::match(TextEditor::QuickFixState *state)
{
QmlJS::ModelManagerInterface *modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>();
QmlJSQuickFixState *s = static_cast<QmlJSQuickFixState *>(state);
_semanticInfo = s->semanticInfo;
if (_refactoringChanges) {
delete _refactoringChanges;
}
_refactoringChanges = new QmlJSRefactoringChanges(modelManager, _semanticInfo.snapshot);
return check();
}
void QmlJSQuickFixOperation::apply()
{
_refactoringChanges->apply();
}
QmlJSRefactoringChanges *QmlJSQuickFixOperation::qmljsRefactoringChanges() const
{ return _refactoringChanges; }
RefactoringChanges *QmlJSQuickFixOperation::refactoringChanges() const
{ return qmljsRefactoringChanges(); }
unsigned QmlJSQuickFixOperation::position(const QmlJS::AST::SourceLocation &loc) const
{
return position(loc.startLine, loc.startColumn);
......
......@@ -31,6 +31,7 @@
#define QMLJSQUICKFIX_H
#include "qmljseditor.h"
#include <texteditor/quickfix.h>
#include <qmljs/parser/qmljsastfwd_p.h>
#include <qmljs/qmljsdocument.h>
......@@ -40,6 +41,7 @@ namespace QmlJS {
}
namespace QmlJSEditor {
class QmlJSRefactoringChanges;
namespace Internal {
......@@ -69,6 +71,10 @@ protected:
using TextEditor::QuickFixOperation::charAt;
using TextEditor::QuickFixOperation::position;
virtual void apply();
QmlJSRefactoringChanges *qmljsRefactoringChanges() const;
virtual TextEditor::RefactoringChanges *refactoringChanges() const;
unsigned position(const QmlJS::AST::SourceLocation &loc) const;
// token based operations
......@@ -79,6 +85,7 @@ protected:
private:
SemanticInfo _semanticInfo;
QmlJSRefactoringChanges *_refactoringChanges;
};
class QmlJSQuickFixCollector: public TextEditor::QuickFixCollector
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 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 "qmljsrefactoringchanges.h"
#include <qmljs/qmljsmodelmanagerinterface.h>
using namespace QmlJS;
using namespace QmlJSEditor;
QmlJSRefactoringChanges::QmlJSRefactoringChanges(ModelManagerInterface *modelManager,
const Snapshot &snapshot)
: m_modelManager(modelManager)
, m_snapshot(snapshot)
{
Q_ASSERT(modelManager);
}
QStringList QmlJSRefactoringChanges::apply()
{
const QStringList changedFiles = TextEditor::RefactoringChanges::apply();
m_modelManager->updateSourceFiles(changedFiles, true);
return changedFiles;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 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.
**
**************************************************************************/
#ifndef QMLREFACTORINGCHANGES_H
#define QMLREFACTORINGCHANGES_H
#include <qmljs/qmljsdocument.h>
#include <texteditor/refactoringchanges.h>
namespace QmlJS {
class ModelManagerInterface;
} // namespace QmlJS
namespace QmlJSEditor {
class QmlJSRefactoringChanges: public TextEditor::RefactoringChanges
{
public:
QmlJSRefactoringChanges(QmlJS::ModelManagerInterface *modelManager,
const QmlJS::Snapshot &snapshot);
virtual QStringList apply();
private:
QmlJS::ModelManagerInterface *m_modelManager;
QmlJS::Snapshot m_snapshot;
};
} // namespace QmlJSEditor
#endif // QMLREFACTORINGCHANGES_H
......@@ -69,6 +69,7 @@ public:
/* returns the list of unique files that were passed in items */
static QStringList replaceAll(const QString &txt,
const QList<Find::SearchResultItem> &items);
protected:
virtual QStringList files() = 0;
void writeCommonSettings(QSettings *settings);
......
......@@ -30,11 +30,14 @@
#include "quickfix.h"
#include "basetexteditor.h"
#include <coreplugin/ifile.h>
#include <QtGui/QApplication>
#include <QtGui/QTextBlock>
#include <QtCore/QDebug>
using TextEditor::RefactoringChanges;
using TextEditor::QuickFixOperation;
using TextEditor::QuickFixCollector;
......@@ -72,49 +75,16 @@ int QuickFixOperation::selectionEnd() const
return _textCursor.selectionEnd();
}
const Utils::ChangeSet &QuickFixOperation::changeSet() const
{
return _changeSet;
}
void QuickFixOperation::apply()
{
const Range r = topLevelRange();
_textCursor.beginEditBlock();
_changeSet.apply(&_textCursor);
reindent(r);
_textCursor.endEditBlock();
}
QuickFixOperation::Range QuickFixOperation::range(int from, int to) const
{
QTextDocument *doc = editor()->document();
QTextCursor begin(doc);
begin.setPosition(from);
QTextCursor end(doc);
end.setPosition(to);
Range range;
range.begin = begin;
range.end = end;
return range;
}
int QuickFixOperation::position(int line, int column) const
{
QTextDocument *doc = editor()->document();
return doc->findBlockByNumber(line - 1).position() + column - 1;
}
void QuickFixOperation::reindent(const Range &range)
void QuickFixOperation::reindent(const RefactoringChanges::Range &range)
{