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
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
#include <texteditor/texteditor_global.h>
|
2010-06-14 14:52:43 +02:00
|
|
|
#include <utils/changeset.h>
|
2021-05-28 12:02:36 +02:00
|
|
|
#include <utils/fileutils.h>
|
2011-09-20 13:10:17 +02:00
|
|
|
#include <utils/textfileformat.h>
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QList>
|
|
|
|
|
#include <QPair>
|
2021-05-28 12:02:36 +02:00
|
|
|
#include <QSharedPointer>
|
|
|
|
|
#include <QString>
|
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;
|
2018-11-25 18:52:41 +01:00
|
|
|
using RefactoringFilePtr = QSharedPointer<RefactoringFile>;
|
|
|
|
|
using RefactoringSelections = QVector<QPair<QTextCursor, QTextCursor>>;
|
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:
|
2018-11-25 18:52:41 +01:00
|
|
|
using Range = Utils::ChangeSet::Range;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
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;
|
2021-05-28 12:02:36 +02:00
|
|
|
Utils::FilePath filePath() 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
|
2019-07-24 18:40:10 +02:00
|
|
|
int position(int line, int column) const;
|
2011-08-17 11:35:57 +02:00
|
|
|
// converts 0-based source offset into 1-based line and column
|
2019-07-24 18:40:10 +02:00
|
|
|
void lineAndColumn(int offset, int *line, int *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
|
|
|
|
2018-05-15 14:31:48 +02:00
|
|
|
Utils::ChangeSet changeSet() const;
|
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);
|
2018-05-16 14:49:12 +02:00
|
|
|
bool 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
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringFile(QTextDocument *document, const Utils::FilePath &filePath);
|
2011-09-20 13:10:17 +02:00
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
RefactoringFile(TextEditorWidget *editor);
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringFile(const Utils::FilePath &filePath,
|
|
|
|
|
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
|
|
|
|
2021-01-29 14:25:23 +01:00
|
|
|
enum IndentType {Indent, Reindent};
|
|
|
|
|
void indentOrReindent(const RefactoringSelections &ranges, IndentType indent);
|
2012-01-16 14:35:58 +01:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
Utils::FilePath m_filePath;
|
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;
|
2018-09-20 01:16:01 +03:00
|
|
|
mutable QTextDocument *m_document = nullptr;
|
|
|
|
|
TextEditorWidget *m_editor = nullptr;
|
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;
|
2018-09-20 01:16:01 +03:00
|
|
|
bool m_openEditor = false;
|
|
|
|
|
bool m_activateEditor = false;
|
|
|
|
|
int m_editorCursorPosition = -1;
|
|
|
|
|
bool m_appliedOnce = false;
|
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:
|
2018-11-25 18:52:41 +01:00
|
|
|
using Range = Utils::ChangeSet::Range;
|
2010-06-14 14:52:43 +02:00
|
|
|
|
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);
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringFilePtr file(const Utils::FilePath &filePath) const;
|
|
|
|
|
bool createFile(const Utils::FilePath &filePath,
|
|
|
|
|
const QString &contents,
|
|
|
|
|
bool reindent = true,
|
|
|
|
|
bool openEditor = true) const;
|
|
|
|
|
bool removeFile(const Utils::FilePath &filePath) 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
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
static TextEditorWidget *openEditor(const Utils::FilePath &filePath,
|
|
|
|
|
bool activate,
|
|
|
|
|
int line,
|
|
|
|
|
int column);
|
|
|
|
|
static RefactoringSelections rangesToSelections(QTextDocument *document,
|
|
|
|
|
const QList<Range> &ranges);
|
2011-08-17 11:35:57 +02:00
|
|
|
|
|
|
|
|
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:
|
2018-11-25 18:52:41 +01:00
|
|
|
RefactoringChangesData() = default;
|
2011-08-22 13:41:19 +02:00
|
|
|
virtual ~RefactoringChangesData();
|
2011-08-17 11:35:57 +02:00
|
|
|
|
|
|
|
|
virtual void indentSelection(const QTextCursor &selection,
|
2021-05-28 12:02:36 +02:00
|
|
|
const Utils::FilePath &filePath,
|
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,
|
2021-05-28 12:02:36 +02:00
|
|
|
const Utils::FilePath &filePath,
|
2014-09-22 18:43:31 +02:00
|
|
|
const TextDocument *textEditor) const;
|
2021-05-28 12:02:36 +02:00
|
|
|
virtual void fileChanged(const Utils::FilePath &filePath);
|
2010-06-14 14:52:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace TextEditor
|