2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-06-14 14:52:43 +02:00
|
|
|
|
|
|
|
|
#ifndef REFACTORINGCHANGES_H
|
|
|
|
|
#define REFACTORINGCHANGES_H
|
|
|
|
|
|
|
|
|
|
#include <utils/changeset.h>
|
2011-09-20 13:10:17 +02:00
|
|
|
#include <utils/textfileformat.h>
|
2010-06-14 14:52:43 +02:00
|
|
|
#include <texteditor/texteditor_global.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QList>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QSharedPointer>
|
|
|
|
|
#include <QPair>
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
class QTextDocument;
|
|
|
|
|
QT_END_NAMESPACE
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2011-01-20 14:03:07 +01:00
|
|
|
namespace TextEditor {
|
2011-02-21 16:02:26 +01:00
|
|
|
class BaseTextEditorWidget;
|
2010-08-12 11:34:48 +02:00
|
|
|
class RefactoringChanges;
|
2011-08-17 11:35:57 +02:00
|
|
|
class RefactoringFile;
|
|
|
|
|
class RefactoringChangesData;
|
|
|
|
|
typedef QSharedPointer<RefactoringFile> RefactoringFilePtr;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
// ### listen to the m_editor::destroyed signal?
|
2010-08-12 11:34:48 +02:00
|
|
|
class TEXTEDITOR_EXPORT RefactoringFile
|
|
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
Q_DISABLE_COPY(RefactoringFile)
|
2010-08-12 11:34:48 +02:00
|
|
|
public:
|
|
|
|
|
typedef Utils::ChangeSet::Range Range;
|
|
|
|
|
|
|
|
|
|
public:
|
2010-08-13 11:48:29 +02:00
|
|
|
virtual ~RefactoringFile();
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
bool isValid() const;
|
|
|
|
|
|
|
|
|
|
const QTextDocument *document() const;
|
2011-08-17 11:35:57 +02:00
|
|
|
// mustn't use the cursor to change the document
|
2010-08-12 11:34:48 +02:00
|
|
|
const QTextCursor cursor() const;
|
2010-08-12 14:18:36 +02:00
|
|
|
QString fileName() const;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
// converts 1-based line and column into 0-based source offset
|
2010-08-12 11:34:48 +02:00
|
|
|
int position(unsigned line, unsigned column) const;
|
2011-08-17 11:35:57 +02:00
|
|
|
// converts 0-based source offset into 1-based line and column
|
|
|
|
|
void lineAndColumn(int offset, unsigned *line, unsigned *column) const;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
QChar charAt(int pos) const;
|
2010-08-13 11:48:29 +02:00
|
|
|
QString textOf(int start, int end) const;
|
|
|
|
|
QString textOf(const Range &range) const;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
void setChangeSet(const Utils::ChangeSet &changeSet);
|
|
|
|
|
void appendIndentRange(const Range &range);
|
2012-01-12 13:23:48 +01:00
|
|
|
void appendReindentRange(const Range &range);
|
2011-08-17 11:35:57 +02:00
|
|
|
void setOpenEditor(bool activate = false, int pos = -1);
|
|
|
|
|
void apply();
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
protected:
|
2011-09-20 13:10:17 +02:00
|
|
|
// users may only get const access to RefactoringFiles created through
|
|
|
|
|
// this constructor, because it can't be used to apply changes
|
2011-08-17 11:35:57 +02:00
|
|
|
RefactoringFile(QTextDocument *document, const QString &fileName);
|
2011-09-20 13:10:17 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
RefactoringFile(BaseTextEditorWidget *editor);
|
|
|
|
|
RefactoringFile(const QString &fileName, const QSharedPointer<RefactoringChangesData> &data);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
QTextDocument *mutableDocument() const;
|
2011-08-17 11:35:57 +02:00
|
|
|
// derived classes may want to clear language specific extra data
|
|
|
|
|
virtual void fileChanged();
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2012-01-16 14:35:58 +01:00
|
|
|
void indentOrReindent(void (RefactoringChangesData::*mf)(const QTextCursor &,
|
|
|
|
|
const QString &,
|
|
|
|
|
const BaseTextEditorWidget *) const,
|
|
|
|
|
const QList<QPair<QTextCursor, QTextCursor> > &ranges);
|
|
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
protected:
|
2010-08-12 11:34:48 +02:00
|
|
|
QString m_fileName;
|
2011-08-17 11:35:57 +02:00
|
|
|
QSharedPointer<RefactoringChangesData> m_data;
|
2011-09-20 13:10:17 +02:00
|
|
|
mutable Utils::TextFileFormat m_textFileFormat;
|
2010-08-12 11:34:48 +02:00
|
|
|
mutable QTextDocument *m_document;
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *m_editor;
|
2010-08-12 13:46:18 +02:00
|
|
|
Utils::ChangeSet m_changes;
|
|
|
|
|
QList<Range> m_indentRanges;
|
2012-01-12 13:23:48 +01:00
|
|
|
QList<Range> m_reindentRanges;
|
2010-08-12 13:46:18 +02:00
|
|
|
bool m_openEditor;
|
2011-08-17 11:35:57 +02:00
|
|
|
bool m_activateEditor;
|
|
|
|
|
int m_editorCursorPosition;
|
2012-01-12 13:23:48 +01:00
|
|
|
bool m_appliedOnce;
|
2011-08-10 09:50:04 +02:00
|
|
|
|
|
|
|
|
friend class RefactoringChanges; // access to constructor
|
2010-08-12 11:34:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*!
|
2010-07-28 17:33:21 +02:00
|
|
|
This class batches changes to multiple file, which are applied as a single big
|
|
|
|
|
change.
|
|
|
|
|
*/
|
2010-06-14 14:52:43 +02:00
|
|
|
class TEXTEDITOR_EXPORT RefactoringChanges
|
|
|
|
|
{
|
|
|
|
|
public:
|
2010-06-22 10:14:06 +02:00
|
|
|
typedef Utils::ChangeSet::Range Range;
|
2010-06-14 14:52:43 +02:00
|
|
|
|
|
|
|
|
public:
|
2010-07-08 16:40:46 +02:00
|
|
|
RefactoringChanges();
|
2010-06-14 14:52:43 +02:00
|
|
|
virtual ~RefactoringChanges();
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
static RefactoringFilePtr file(BaseTextEditorWidget *editor);
|
|
|
|
|
RefactoringFilePtr file(const QString &fileName) const;
|
|
|
|
|
bool createFile(const QString &fileName, const QString &contents, bool reindent = true, bool openEditor = true) const;
|
|
|
|
|
bool removeFile(const QString &fileName) const;
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
static BaseTextEditorWidget *editorForFile(const QString &fileName);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
protected:
|
|
|
|
|
explicit RefactoringChanges(RefactoringChangesData *data);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
static BaseTextEditorWidget *openEditor(const QString &fileName, bool activate, int line, int column);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2012-01-16 14:35:58 +01:00
|
|
|
static QList<QPair<QTextCursor, QTextCursor> > rangesToSelections(QTextDocument *document,
|
|
|
|
|
const QList<Range> &ranges);
|
2011-08-17 11:35:57 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
QSharedPointer<RefactoringChangesData> m_data;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
friend class RefactoringFile;
|
2011-08-17 11:35:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TEXTEDITOR_EXPORT RefactoringChangesData
|
|
|
|
|
{
|
|
|
|
|
Q_DISABLE_COPY(RefactoringChangesData)
|
2010-09-23 18:22:07 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
public:
|
|
|
|
|
RefactoringChangesData() {}
|
2011-08-22 13:41:19 +02:00
|
|
|
virtual ~RefactoringChangesData();
|
2011-08-17 11:35:57 +02:00
|
|
|
|
|
|
|
|
virtual void indentSelection(const QTextCursor &selection,
|
|
|
|
|
const QString &fileName,
|
|
|
|
|
const BaseTextEditorWidget *textEditor) const;
|
2012-01-12 13:23:48 +01:00
|
|
|
virtual void reindentSelection(const QTextCursor &selection,
|
|
|
|
|
const QString &fileName,
|
|
|
|
|
const BaseTextEditorWidget *textEditor) const;
|
2011-08-17 11:35:57 +02:00
|
|
|
virtual void fileChanged(const QString &fileName);
|
2010-06-14 14:52:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace TextEditor
|
|
|
|
|
|
|
|
|
|
#endif // REFACTORINGCHANGES_H
|