2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
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 {
|
2014-09-22 18:43:31 +02:00
|
|
|
class TextDocument;
|
2014-09-26 11:37:54 +02:00
|
|
|
class TextEditorWidget;
|
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;
|
2014-09-20 21:54:00 +02:00
|
|
|
typedef QVector<QPair<QTextCursor, QTextCursor> > RefactoringSelections;
|
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;
|
2014-09-26 11:37:54 +02:00
|
|
|
TextEditorWidget *editor() 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
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
RefactoringFile(TextEditorWidget *editor);
|
2011-08-17 11:35:57 +02:00
|
|
|
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 &,
|
2014-09-22 18:43:31 +02:00
|
|
|
const TextDocument *) const,
|
2014-09-20 21:54:00 +02:00
|
|
|
const RefactoringSelections &ranges);
|
2012-01-16 14:35:58 +01:00
|
|
|
|
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;
|
2014-09-26 11:37:54 +02:00
|
|
|
TextEditorWidget *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();
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
static RefactoringFilePtr file(TextEditorWidget *editor);
|
2011-08-17 11:35:57 +02:00
|
|
|
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
|
|
|
protected:
|
|
|
|
|
explicit RefactoringChanges(RefactoringChangesData *data);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
static TextEditorWidget *openEditor(const QString &fileName, bool activate, int line, int column);
|
2014-09-20 21:54:00 +02:00
|
|
|
static RefactoringSelections 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,
|
2014-09-22 18:43:31 +02:00
|
|
|
const TextDocument *textEditor) const;
|
2012-01-12 13:23:48 +01:00
|
|
|
virtual void reindentSelection(const QTextCursor &selection,
|
|
|
|
|
const QString &fileName,
|
2014-09-22 18:43:31 +02:00
|
|
|
const TextDocument *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
|