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
|
|
|
|
|
|
|
|
#include "refactoringchanges.h"
|
2014-09-26 09:14:03 +02:00
|
|
|
#include "texteditor.h"
|
2015-02-26 13:22:35 +01:00
|
|
|
#include "textdocument.h"
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2012-10-15 11:53:22 +02:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/dialogs/readonlyfilesdialog.h>
|
2018-08-20 14:26:51 +02:00
|
|
|
#include <coreplugin/documentmanager.h>
|
2015-02-26 13:22:35 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2020-06-12 13:41:00 +02:00
|
|
|
#include <utils/algorithm.h>
|
2012-10-15 11:53:22 +02:00
|
|
|
#include <utils/fileutils.h>
|
2020-06-12 13:41:00 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2011-03-30 15:15:15 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFile>
|
2012-10-15 11:53:22 +02:00
|
|
|
#include <QFileInfo>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextBlock>
|
|
|
|
|
#include <QTextCursor>
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
#include <QDebug>
|
2012-10-15 11:53:22 +02:00
|
|
|
#include <QApplication>
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2014-09-20 21:52:56 +02:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
2014-09-20 21:54:00 +02:00
|
|
|
namespace TextEditor {
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2010-07-08 16:40:46 +02:00
|
|
|
RefactoringChanges::RefactoringChanges()
|
2011-08-17 11:35:57 +02:00
|
|
|
: m_data(new RefactoringChangesData)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
RefactoringChanges::RefactoringChanges(RefactoringChangesData *data)
|
|
|
|
|
: m_data(data)
|
2010-07-08 16:40:46 +02:00
|
|
|
{}
|
|
|
|
|
|
2018-11-25 18:52:41 +01:00
|
|
|
RefactoringChanges::~RefactoringChanges() = default;
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2014-09-20 21:54:00 +02:00
|
|
|
RefactoringSelections RefactoringChanges::rangesToSelections(QTextDocument *document,
|
|
|
|
|
const QList<Range> &ranges)
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
2014-09-20 21:54:00 +02:00
|
|
|
RefactoringSelections selections;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
foreach (const Range &range, ranges) {
|
2012-01-16 14:35:58 +01:00
|
|
|
QTextCursor start(document);
|
|
|
|
|
start.setPosition(range.start);
|
|
|
|
|
start.setKeepPositionOnInsert(true);
|
|
|
|
|
QTextCursor end(document);
|
|
|
|
|
end.setPosition(qMin(range.end, document->characterCount() - 1));
|
|
|
|
|
|
|
|
|
|
selections.append(qMakePair(start, end));
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return selections;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
bool RefactoringChanges::createFile(const FilePath &filePath,
|
|
|
|
|
const QString &contents,
|
|
|
|
|
bool reindent,
|
|
|
|
|
bool openEditor) const
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
if (filePath.exists())
|
2010-08-12 11:34:48 +02:00
|
|
|
return false;
|
|
|
|
|
|
2012-01-18 16:31:16 +01:00
|
|
|
// Create a text document for the new file:
|
2018-09-20 01:16:01 +03:00
|
|
|
auto document = new QTextDocument;
|
2012-01-18 16:31:16 +01:00
|
|
|
QTextCursor cursor(document);
|
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
|
cursor.insertText(contents);
|
|
|
|
|
|
|
|
|
|
// Reindent the contents:
|
|
|
|
|
if (reindent) {
|
|
|
|
|
cursor.select(QTextCursor::Document);
|
2021-05-28 12:02:36 +02:00
|
|
|
m_data->indentSelection(cursor, filePath, nullptr);
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
2012-01-18 16:31:16 +01:00
|
|
|
cursor.endEditBlock();
|
|
|
|
|
|
|
|
|
|
// Write the file to disk:
|
2014-09-20 21:52:56 +02:00
|
|
|
TextFileFormat format;
|
|
|
|
|
format.codec = EditorManager::defaultTextCodec();
|
2012-01-18 16:31:16 +01:00
|
|
|
QString error;
|
2021-05-28 12:02:36 +02:00
|
|
|
bool saveOk = format.writeFile(filePath, document->toPlainText(), &error);
|
2012-01-18 16:31:16 +01:00
|
|
|
delete document;
|
|
|
|
|
if (!saveOk)
|
|
|
|
|
return false;
|
2010-08-12 13:46:18 +02:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
m_data->fileChanged(filePath);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2012-01-18 16:31:16 +01:00
|
|
|
if (openEditor)
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringChanges::openEditor(filePath, /*bool activate =*/ false, -1, -1);
|
2012-01-18 16:31:16 +01:00
|
|
|
|
2010-08-12 11:34:48 +02:00
|
|
|
return true;
|
2010-06-14 14:52:43 +02:00
|
|
|
}
|
2010-07-08 16:40:46 +02:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
bool RefactoringChanges::removeFile(const FilePath &filePath) const
|
2010-07-08 16:40:46 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
if (!filePath.exists())
|
2010-08-12 11:34:48 +02:00
|
|
|
return false;
|
|
|
|
|
|
2010-08-12 14:26:30 +02:00
|
|
|
// ### implement!
|
|
|
|
|
qWarning() << "RefactoringChanges::removeFile is not implemented";
|
2010-08-12 11:34:48 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
TextEditorWidget *RefactoringChanges::openEditor(const FilePath &filePath,
|
|
|
|
|
bool activate,
|
|
|
|
|
int line,
|
|
|
|
|
int column)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2014-09-20 21:52:56 +02:00
|
|
|
EditorManager::OpenEditorFlags flags = EditorManager::IgnoreNavigationHistory;
|
2020-08-12 12:39:27 +02:00
|
|
|
if (activate)
|
|
|
|
|
flags |= EditorManager::SwitchSplitIfAlreadyVisible;
|
|
|
|
|
else
|
2014-09-20 21:52:56 +02:00
|
|
|
flags |= EditorManager::DoNotChangeCurrentEditor;
|
2011-08-17 11:35:57 +02:00
|
|
|
if (line != -1) {
|
|
|
|
|
// openEditorAt uses a 1-based line and a 0-based column!
|
|
|
|
|
column -= 1;
|
|
|
|
|
}
|
2021-05-28 12:02:36 +02:00
|
|
|
IEditor *editor = EditorManager::openEditorAt(Link{filePath, line, column}, Id(), flags);
|
2012-01-18 16:31:16 +01:00
|
|
|
|
|
|
|
|
if (editor)
|
2020-02-11 14:00:09 +01:00
|
|
|
return TextEditorWidget::fromEditor(editor);
|
2012-01-18 16:31:16 +01:00
|
|
|
else
|
2018-09-20 01:16:01 +03:00
|
|
|
return nullptr;
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
RefactoringFilePtr RefactoringChanges::file(TextEditorWidget *editor)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
return RefactoringFilePtr(new RefactoringFile(editor));
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringFilePtr RefactoringChanges::file(const FilePath &filePath) const
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
return RefactoringFilePtr(new RefactoringFile(filePath, m_data));
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringFile::RefactoringFile(QTextDocument *document, const FilePath &filePath)
|
|
|
|
|
: m_filePath(filePath)
|
2011-08-10 09:50:04 +02:00
|
|
|
, m_document(document)
|
|
|
|
|
{ }
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
RefactoringFile::RefactoringFile(TextEditorWidget *editor)
|
2021-05-28 12:02:36 +02:00
|
|
|
: m_filePath(editor->textDocument()->filePath())
|
2011-08-10 09:50:04 +02:00
|
|
|
, m_editor(editor)
|
|
|
|
|
{ }
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringFile::RefactoringFile(const FilePath &filePath,
|
|
|
|
|
const QSharedPointer<RefactoringChangesData> &data)
|
|
|
|
|
: m_filePath(filePath)
|
2011-08-17 11:35:57 +02:00
|
|
|
, m_data(data)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
QList<IEditor *> editors = DocumentModel::editorsForFilePath(filePath);
|
2018-08-20 14:26:51 +02:00
|
|
|
if (!editors.isEmpty()) {
|
2020-02-11 14:00:09 +01:00
|
|
|
auto editorWidget = TextEditorWidget::fromEditor(editors.first());
|
2018-08-20 14:26:51 +02:00
|
|
|
if (editorWidget && !editorWidget->isReadOnly())
|
|
|
|
|
m_editor = editorWidget;
|
|
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefactoringFile::~RefactoringFile()
|
|
|
|
|
{
|
|
|
|
|
delete m_document;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RefactoringFile::isValid() const
|
|
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
if (m_filePath.isEmpty())
|
2011-08-17 11:35:57 +02:00
|
|
|
return false;
|
|
|
|
|
return document();
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QTextDocument *RefactoringFile::document() const
|
|
|
|
|
{
|
|
|
|
|
return mutableDocument();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextDocument *RefactoringFile::mutableDocument() const
|
|
|
|
|
{
|
2010-08-12 13:46:18 +02:00
|
|
|
if (m_editor)
|
|
|
|
|
return m_editor->document();
|
2013-07-17 00:01:45 +03:00
|
|
|
if (!m_document) {
|
2010-08-12 11:34:48 +02:00
|
|
|
QString fileContents;
|
2021-05-28 12:02:36 +02:00
|
|
|
if (!m_filePath.isEmpty()) {
|
2011-09-20 13:10:17 +02:00
|
|
|
QString error;
|
2014-09-20 21:52:56 +02:00
|
|
|
QTextCodec *defaultCodec = EditorManager::defaultTextCodec();
|
2021-05-28 12:02:36 +02:00
|
|
|
TextFileFormat::ReadResult result = TextFileFormat::readFile(m_filePath,
|
2021-05-18 07:57:14 +02:00
|
|
|
defaultCodec,
|
|
|
|
|
&fileContents,
|
|
|
|
|
&m_textFileFormat,
|
|
|
|
|
&error);
|
2014-09-20 21:52:56 +02:00
|
|
|
if (result != TextFileFormat::ReadSuccess) {
|
2021-05-28 12:02:36 +02:00
|
|
|
qWarning() << "Could not read " << m_filePath << ". Error: " << error;
|
2018-09-20 01:16:01 +03:00
|
|
|
m_textFileFormat.codec = nullptr;
|
2011-09-20 13:10:17 +02:00
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
2011-09-20 13:10:17 +02:00
|
|
|
// always make a QTextDocument to avoid excessive null checks
|
2010-08-12 11:34:48 +02:00
|
|
|
m_document = new QTextDocument(fileContents);
|
|
|
|
|
}
|
|
|
|
|
return m_document;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QTextCursor RefactoringFile::cursor() const
|
|
|
|
|
{
|
|
|
|
|
if (m_editor)
|
|
|
|
|
return m_editor->textCursor();
|
2021-05-28 12:02:36 +02:00
|
|
|
if (!m_filePath.isEmpty()) {
|
2011-08-17 11:35:57 +02:00
|
|
|
if (QTextDocument *doc = mutableDocument())
|
|
|
|
|
return QTextCursor(doc);
|
|
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
return QTextCursor();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
FilePath RefactoringFile::filePath() const
|
2010-08-12 14:18:36 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
return m_filePath;
|
2010-08-12 14:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
TextEditorWidget *RefactoringFile::editor() const
|
2013-07-18 09:40:50 +02:00
|
|
|
{
|
|
|
|
|
return m_editor;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
int RefactoringFile::position(int line, int column) const
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2013-02-14 15:40:11 +01:00
|
|
|
QTC_ASSERT(line != 0, return -1);
|
|
|
|
|
QTC_ASSERT(column != 0, return -1);
|
2010-08-12 11:34:48 +02:00
|
|
|
if (const QTextDocument *doc = document())
|
|
|
|
|
return doc->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 18:40:10 +02:00
|
|
|
void RefactoringFile::lineAndColumn(int offset, int *line, int *column) const
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
2013-02-14 15:40:11 +01:00
|
|
|
QTC_ASSERT(line, return);
|
|
|
|
|
QTC_ASSERT(column, return);
|
|
|
|
|
QTC_ASSERT(offset >= 0, return);
|
2011-08-17 11:35:57 +02:00
|
|
|
QTextCursor c(cursor());
|
|
|
|
|
c.setPosition(offset);
|
|
|
|
|
*line = c.blockNumber() + 1;
|
|
|
|
|
*column = c.positionInBlock() + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 11:34:48 +02:00
|
|
|
QChar RefactoringFile::charAt(int pos) const
|
|
|
|
|
{
|
|
|
|
|
if (const QTextDocument *doc = document())
|
|
|
|
|
return doc->characterAt(pos);
|
|
|
|
|
return QChar();
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
QString RefactoringFile::textOf(int start, int end) const
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
|
|
|
|
QTextCursor c = cursor();
|
|
|
|
|
c.setPosition(start);
|
|
|
|
|
c.setPosition(end, QTextCursor::KeepAnchor);
|
|
|
|
|
return c.selectedText();
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
QString RefactoringFile::textOf(const Range &range) const
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2010-08-13 11:48:29 +02:00
|
|
|
return textOf(range.start, range.end);
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-15 14:31:48 +02:00
|
|
|
ChangeSet RefactoringFile::changeSet() const
|
|
|
|
|
{
|
|
|
|
|
return m_changes;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-20 21:52:56 +02:00
|
|
|
void RefactoringFile::setChangeSet(const ChangeSet &changeSet)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
if (m_filePath.isEmpty())
|
2011-08-17 11:35:57 +02:00
|
|
|
return;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
m_changes = changeSet;
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
void RefactoringFile::appendIndentRange(const Range &range)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
if (m_filePath.isEmpty())
|
2011-08-17 11:35:57 +02:00
|
|
|
return;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
m_indentRanges.append(range);
|
2011-08-17 11:35:57 +02:00
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2012-01-12 13:23:48 +01:00
|
|
|
void RefactoringFile::appendReindentRange(const Range &range)
|
|
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
if (m_filePath.isEmpty())
|
2012-01-12 13:23:48 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_reindentRanges.append(range);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
void RefactoringFile::setOpenEditor(bool activate, int pos)
|
|
|
|
|
{
|
|
|
|
|
m_openEditor = true;
|
|
|
|
|
m_activateEditor = activate;
|
|
|
|
|
m_editorCursorPosition = pos;
|
|
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2018-05-16 14:49:12 +02:00
|
|
|
bool RefactoringFile::apply()
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
2012-10-15 11:53:22 +02:00
|
|
|
// test file permissions
|
2021-05-28 12:02:36 +02:00
|
|
|
if (!m_filePath.toFileInfo().isWritable()) {
|
|
|
|
|
ReadOnlyFilesDialog roDialog(m_filePath, ICore::dialogParent());
|
2012-10-15 11:53:22 +02:00
|
|
|
const QString &failDetailText = QApplication::translate("RefactoringFile::apply",
|
|
|
|
|
"Refactoring cannot be applied.");
|
|
|
|
|
roDialog.setShowFailWarning(true, failDetailText);
|
2014-09-20 21:52:56 +02:00
|
|
|
if (roDialog.exec() == ReadOnlyFilesDialog::RO_Cancel)
|
2018-05-16 14:49:12 +02:00
|
|
|
return false;
|
2012-10-15 11:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
// open / activate / goto position
|
2021-05-28 12:02:36 +02:00
|
|
|
if (m_openEditor && !m_filePath.isEmpty()) {
|
2019-07-24 18:40:10 +02:00
|
|
|
int line = -1, column = -1;
|
2011-08-17 11:35:57 +02:00
|
|
|
if (m_editorCursorPosition != -1)
|
|
|
|
|
lineAndColumn(m_editorCursorPosition, &line, &column);
|
2021-05-28 12:02:36 +02:00
|
|
|
m_editor = RefactoringChanges::openEditor(m_filePath, m_activateEditor, line, column);
|
2011-08-17 11:35:57 +02:00
|
|
|
m_openEditor = false;
|
|
|
|
|
m_activateEditor = false;
|
|
|
|
|
m_editorCursorPosition = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 17:59:51 +02:00
|
|
|
const bool withUnmodifiedEditor = m_editor && !m_editor->textDocument()->isModified();
|
2018-05-16 14:49:12 +02:00
|
|
|
bool result = true;
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
// apply changes, if any
|
|
|
|
|
if (m_data && !(m_indentRanges.isEmpty() && m_changes.isEmpty())) {
|
|
|
|
|
QTextDocument *doc = mutableDocument();
|
2012-01-12 13:23:48 +01:00
|
|
|
if (doc) {
|
2011-10-20 15:41:44 +02:00
|
|
|
QTextCursor c = cursor();
|
2012-01-12 13:23:48 +01:00
|
|
|
if (m_appliedOnce)
|
|
|
|
|
c.joinPreviousEditBlock();
|
|
|
|
|
else
|
|
|
|
|
c.beginEditBlock();
|
2011-08-17 11:35:57 +02:00
|
|
|
|
2020-06-12 13:41:00 +02:00
|
|
|
sort(m_indentRanges);
|
|
|
|
|
sort(m_reindentRanges);
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
// build indent selections now, applying the changeset will change locations
|
2014-09-20 21:54:00 +02:00
|
|
|
const RefactoringSelections &indentSelections =
|
2012-01-12 13:23:48 +01:00
|
|
|
RefactoringChanges::rangesToSelections(doc, m_indentRanges);
|
2011-08-17 11:35:57 +02:00
|
|
|
m_indentRanges.clear();
|
2014-09-20 21:54:00 +02:00
|
|
|
const RefactoringSelections &reindentSelections =
|
2012-01-12 13:23:48 +01:00
|
|
|
RefactoringChanges::rangesToSelections(doc, m_reindentRanges);
|
|
|
|
|
m_reindentRanges.clear();
|
2011-08-17 11:35:57 +02:00
|
|
|
|
|
|
|
|
// apply changes and reindent
|
|
|
|
|
m_changes.apply(&c);
|
|
|
|
|
m_changes.clear();
|
2012-01-12 13:23:48 +01:00
|
|
|
|
2021-01-29 14:25:23 +01:00
|
|
|
indentOrReindent(indentSelections, Indent);
|
|
|
|
|
indentOrReindent(reindentSelections, Reindent);
|
2011-08-17 11:35:57 +02:00
|
|
|
|
|
|
|
|
c.endEditBlock();
|
|
|
|
|
|
2012-01-12 13:23:48 +01:00
|
|
|
// if this document doesn't have an editor, write the result to a file
|
|
|
|
|
if (!m_editor && m_textFileFormat.codec) {
|
2021-05-28 12:02:36 +02:00
|
|
|
QTC_ASSERT(!m_filePath.isEmpty(), return false);
|
2012-01-12 13:23:48 +01:00
|
|
|
QString error;
|
2018-08-20 14:26:51 +02:00
|
|
|
// suppress "file has changed" warnings if the file is open in a read-only editor
|
2021-06-09 09:59:37 +02:00
|
|
|
Core::FileChangeBlocker block(m_filePath);
|
2021-05-28 12:02:36 +02:00
|
|
|
if (!m_textFileFormat.writeFile(m_filePath,
|
2021-05-18 07:57:14 +02:00
|
|
|
doc->toPlainText(),
|
|
|
|
|
&error)) {
|
2021-05-28 12:02:36 +02:00
|
|
|
qWarning() << "Could not apply changes to" << m_filePath
|
|
|
|
|
<< ". Error: " << error;
|
2018-05-16 14:49:12 +02:00
|
|
|
result = false;
|
|
|
|
|
}
|
2012-01-12 13:23:48 +01:00
|
|
|
}
|
2011-08-17 11:35:57 +02:00
|
|
|
|
2012-01-12 13:23:48 +01:00
|
|
|
fileChanged();
|
2021-08-17 17:59:51 +02:00
|
|
|
if (withUnmodifiedEditor && EditorManager::autoSaveAfterRefactoring())
|
|
|
|
|
m_editor->textDocument()->save(nullptr, m_filePath, false);
|
2012-01-12 13:23:48 +01:00
|
|
|
}
|
2011-08-17 11:35:57 +02:00
|
|
|
}
|
2012-01-12 13:23:48 +01:00
|
|
|
|
|
|
|
|
m_appliedOnce = true;
|
2018-05-16 14:49:12 +02:00
|
|
|
return result;
|
2011-08-17 11:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-29 14:25:23 +01:00
|
|
|
void RefactoringFile::indentOrReindent(const RefactoringSelections &ranges,
|
|
|
|
|
RefactoringFile::IndentType indent)
|
2012-01-16 14:35:58 +01:00
|
|
|
{
|
2021-01-29 14:25:23 +01:00
|
|
|
TextDocument * document = m_editor ? m_editor->textDocument() : nullptr;
|
|
|
|
|
for (const auto &[position, anchor]: ranges) {
|
|
|
|
|
QTextCursor selection(anchor);
|
|
|
|
|
selection.setPosition(position.position(), QTextCursor::KeepAnchor);
|
|
|
|
|
if (indent == Indent)
|
2021-05-28 12:02:36 +02:00
|
|
|
m_data->indentSelection(selection, m_filePath, document);
|
2021-01-29 14:25:23 +01:00
|
|
|
else
|
2021-05-28 12:02:36 +02:00
|
|
|
m_data->reindentSelection(selection, m_filePath, document);
|
2012-01-16 14:35:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
void RefactoringFile::fileChanged()
|
|
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
if (!m_filePath.isEmpty())
|
|
|
|
|
m_data->fileChanged(m_filePath);
|
2011-08-17 11:35:57 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-25 18:52:41 +01:00
|
|
|
RefactoringChangesData::~RefactoringChangesData() = default;
|
2011-08-22 13:41:19 +02:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
void RefactoringChangesData::indentSelection(const QTextCursor &,
|
|
|
|
|
const FilePath &,
|
|
|
|
|
const TextDocument *) const
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "not implemented";
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
void RefactoringChangesData::reindentSelection(const QTextCursor &,
|
|
|
|
|
const FilePath &,
|
|
|
|
|
const TextDocument *) const
|
2012-01-12 13:23:48 +01:00
|
|
|
{
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "not implemented";
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
void RefactoringChangesData::fileChanged(const FilePath &)
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
2010-07-08 16:40:46 +02:00
|
|
|
}
|
2014-09-20 21:54:00 +02:00
|
|
|
|
|
|
|
|
} // namespace TextEditor
|