2010-06-14 14:52:43 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Contact: Nokia Corporation (info@qt.nokia.com)
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-05-06 15:05:37 +02:00
|
|
|
** Nokia at info@qt.nokia.com.
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "refactoringchanges.h"
|
2011-01-20 14:03:07 +01:00
|
|
|
#include "basetexteditor.h"
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2010-06-14 14:52:43 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
2010-06-14 14:52:43 +02:00
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
#include <QtCore/QSet>
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <QtGui/QMainWindow>
|
2010-06-14 14:52:43 +02:00
|
|
|
#include <QtGui/QTextBlock>
|
2011-01-20 14:03:07 +01:00
|
|
|
#include <QtGui/QTextCursor>
|
|
|
|
|
#include <QtGui/QTextDocument>
|
2010-06-22 15:09:20 +02:00
|
|
|
#include <QtCore/QDebug>
|
2010-06-14 14:52:43 +02:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2010-07-08 16:40:46 +02:00
|
|
|
RefactoringChanges::RefactoringChanges()
|
|
|
|
|
{}
|
|
|
|
|
|
2010-06-14 14:52:43 +02:00
|
|
|
RefactoringChanges::~RefactoringChanges()
|
2010-09-23 18:22:07 +02:00
|
|
|
{
|
|
|
|
|
if (!m_fileToOpen.isEmpty()) {
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget::openEditorAt(m_fileToOpen, m_lineToOpen, m_columnToOpen);
|
2010-09-23 18:22:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *RefactoringChanges::editorForFile(const QString &fileName,
|
2010-06-14 14:52:43 +02:00
|
|
|
bool openIfClosed)
|
|
|
|
|
{
|
|
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
|
|
|
|
|
const QList<Core::IEditor *> editors = editorManager->editorsForFileName(fileName);
|
|
|
|
|
foreach (Core::IEditor *editor, editors) {
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *textEditor = qobject_cast<BaseTextEditorWidget *>(editor->widget());
|
2010-06-14 14:52:43 +02:00
|
|
|
if (textEditor != 0)
|
|
|
|
|
return textEditor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!openIfClosed)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2010-08-12 11:34:48 +02:00
|
|
|
QFile file(fileName);
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
if (!file.open(QIODevice::Append))
|
|
|
|
|
return 0;
|
|
|
|
|
file.close();
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-14 14:52:43 +02:00
|
|
|
Core::IEditor *editor = editorManager->openEditor(fileName, QString(),
|
2010-09-14 15:15:57 +02:00
|
|
|
Core::EditorManager::NoActivate | Core::EditorManager::IgnoreNavigationHistory);
|
2011-02-21 16:02:26 +01:00
|
|
|
return qobject_cast<BaseTextEditorWidget *>(editor->widget());
|
2010-06-14 14:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-12 11:34:48 +02:00
|
|
|
QList<QTextCursor> RefactoringChanges::rangesToSelections(QTextDocument *document, const QList<Range> &ranges)
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
2010-08-12 11:34:48 +02:00
|
|
|
QList<QTextCursor> selections;
|
|
|
|
|
|
|
|
|
|
foreach (const Range &range, ranges) {
|
|
|
|
|
QTextCursor selection(document);
|
|
|
|
|
// ### workaround for moving the textcursor when inserting text at the beginning of the range.
|
|
|
|
|
selection.setPosition(qMax(0, range.start - 1));
|
|
|
|
|
selection.setPosition(qMin(range.end, document->characterCount() - 1), QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
|
|
selections.append(selection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return selections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RefactoringChanges::createFile(const QString &fileName, const QString &contents, bool reindent, bool openEditor)
|
|
|
|
|
{
|
|
|
|
|
if (QFile::exists(fileName))
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *editor = editorForFile(fileName, openEditor);
|
2010-08-12 13:46:18 +02:00
|
|
|
|
|
|
|
|
QTextDocument *document;
|
|
|
|
|
if (editor)
|
|
|
|
|
document = editor->document();
|
|
|
|
|
else
|
|
|
|
|
document = new QTextDocument;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
{
|
|
|
|
|
QTextCursor cursor(document);
|
|
|
|
|
cursor.beginEditBlock();
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
cursor.insertText(contents);
|
|
|
|
|
|
|
|
|
|
if (reindent) {
|
|
|
|
|
cursor.select(QTextCursor::Document);
|
2011-02-01 14:13:54 +01:00
|
|
|
indentSelection(cursor, fileName, editor);
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.endEditBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!editor) {
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileSaver saver(fileName);
|
|
|
|
|
saver.write(document->toPlainText().toUtf8());
|
2010-08-12 13:46:18 +02:00
|
|
|
delete document;
|
2011-03-30 15:15:15 +02:00
|
|
|
if (!saver.finalize(Core::ICore::instance()->mainWindow()))
|
|
|
|
|
return false;
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileChanged(fileName);
|
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
|
|
|
|
2010-08-12 11:34:48 +02:00
|
|
|
bool RefactoringChanges::removeFile(const QString &fileName)
|
2010-07-08 16:40:46 +02:00
|
|
|
{
|
2010-08-12 11:34:48 +02:00
|
|
|
if (!QFile::exists(fileName))
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefactoringFile RefactoringChanges::file(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
if (QFile::exists(fileName))
|
|
|
|
|
return RefactoringFile(fileName, this);
|
|
|
|
|
else
|
|
|
|
|
return RefactoringFile();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *RefactoringChanges::openEditor(const QString &fileName, int pos)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2011-02-21 16:02:26 +01:00
|
|
|
BaseTextEditorWidget *editor = editorForFile(fileName, true);
|
2010-08-12 13:46:18 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
|
QTextCursor cursor = editor->textCursor();
|
|
|
|
|
cursor.setPosition(pos);
|
|
|
|
|
editor->setTextCursor(cursor);
|
|
|
|
|
}
|
|
|
|
|
return editor;
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
2010-09-23 18:22:07 +02:00
|
|
|
void RefactoringChanges::activateEditor(const QString &fileName, int line, int column)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2010-09-23 18:22:07 +02:00
|
|
|
m_fileToOpen = fileName;
|
|
|
|
|
m_lineToOpen = line;
|
|
|
|
|
m_columnToOpen = column;
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RefactoringFile::RefactoringFile()
|
|
|
|
|
: m_refactoringChanges(0)
|
|
|
|
|
, m_document(0)
|
|
|
|
|
, m_editor(0)
|
2010-08-12 13:46:18 +02:00
|
|
|
, m_openEditor(false)
|
2010-08-12 11:34:48 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
RefactoringFile::RefactoringFile(const QString &fileName, RefactoringChanges *refactoringChanges)
|
|
|
|
|
: m_fileName(fileName)
|
|
|
|
|
, m_refactoringChanges(refactoringChanges)
|
|
|
|
|
, m_document(0)
|
|
|
|
|
, m_editor(0)
|
2010-08-12 13:46:18 +02:00
|
|
|
, m_openEditor(false)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
|
|
|
|
m_editor = RefactoringChanges::editorForFile(fileName, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefactoringFile::RefactoringFile(const RefactoringFile &other)
|
|
|
|
|
: m_fileName(other.m_fileName)
|
|
|
|
|
, m_refactoringChanges(other.m_refactoringChanges)
|
|
|
|
|
, m_document(0)
|
|
|
|
|
, m_editor(other.m_editor)
|
|
|
|
|
{
|
2010-08-12 13:46:18 +02:00
|
|
|
Q_ASSERT_X(!other.m_document && other.m_changes.isEmpty() && other.m_indentRanges.isEmpty(),
|
|
|
|
|
"RefactoringFile", "A refactoring file with changes is not copyable");
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefactoringFile::~RefactoringFile()
|
|
|
|
|
{
|
2010-10-07 14:47:25 +02:00
|
|
|
if (m_refactoringChanges && m_openEditor && !m_fileName.isEmpty())
|
2010-08-12 13:46:18 +02:00
|
|
|
m_editor = m_refactoringChanges->openEditor(m_fileName, -1);
|
|
|
|
|
|
|
|
|
|
// apply changes, if any
|
2010-08-13 11:48:29 +02:00
|
|
|
if (m_refactoringChanges && !(m_indentRanges.isEmpty() && m_changes.isEmpty())) {
|
2010-08-12 13:46:18 +02:00
|
|
|
QTextDocument *doc = mutableDocument();
|
|
|
|
|
{
|
|
|
|
|
QTextCursor c = cursor();
|
|
|
|
|
c.beginEditBlock();
|
|
|
|
|
|
|
|
|
|
// build indent selections now, applying the changeset will change locations
|
|
|
|
|
const QList<QTextCursor> &indentSelections =
|
|
|
|
|
RefactoringChanges::rangesToSelections(
|
|
|
|
|
doc, m_indentRanges);
|
|
|
|
|
|
|
|
|
|
// apply changes and reindent
|
|
|
|
|
m_changes.apply(&c);
|
|
|
|
|
foreach (const QTextCursor &selection, indentSelections) {
|
2011-02-01 14:13:54 +01:00
|
|
|
m_refactoringChanges->indentSelection(selection, m_fileName, m_editor);
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.endEditBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if this document doesn't have an editor, write the result to a file
|
2010-10-07 14:47:25 +02:00
|
|
|
if (!m_editor && !m_fileName.isEmpty()) {
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileSaver saver(m_fileName);
|
|
|
|
|
saver.write(doc->toPlainText().toUtf8());
|
|
|
|
|
saver.finalize(Core::ICore::instance()->mainWindow());
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
2010-10-07 14:47:25 +02:00
|
|
|
if (!m_fileName.isEmpty())
|
|
|
|
|
m_refactoringChanges->fileChanged(m_fileName);
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-12 11:34:48 +02:00
|
|
|
delete m_document;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RefactoringFile::isValid() const
|
|
|
|
|
{
|
|
|
|
|
return !m_fileName.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2010-10-07 14:47:25 +02:00
|
|
|
else if (!m_document) {
|
2010-08-12 11:34:48 +02:00
|
|
|
QString fileContents;
|
2010-10-07 14:47:25 +02:00
|
|
|
if (!m_fileName.isEmpty()) {
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileReader reader;
|
|
|
|
|
if (reader.fetch(m_fileName, Core::ICore::instance()->mainWindow()))
|
|
|
|
|
fileContents = QString::fromUtf8(reader.data());
|
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();
|
|
|
|
|
else if (!m_fileName.isEmpty())
|
|
|
|
|
return QTextCursor(mutableDocument());
|
|
|
|
|
|
|
|
|
|
return QTextCursor();
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 14:18:36 +02:00
|
|
|
QString RefactoringFile::fileName() const
|
|
|
|
|
{
|
|
|
|
|
return m_fileName;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 11:34:48 +02:00
|
|
|
int RefactoringFile::position(unsigned line, unsigned column) const
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(line != 0);
|
|
|
|
|
Q_ASSERT(column != 0);
|
|
|
|
|
if (const QTextDocument *doc = document())
|
|
|
|
|
return doc->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RefactoringFile::change(const Utils::ChangeSet &changeSet, bool openEditor)
|
|
|
|
|
{
|
|
|
|
|
if (m_fileName.isEmpty())
|
|
|
|
|
return false;
|
2010-08-12 13:46:18 +02:00
|
|
|
if (!m_changes.isEmpty())
|
2010-08-12 11:34:48 +02:00
|
|
|
return false;
|
|
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
m_changes = changeSet;
|
|
|
|
|
m_openEditor = openEditor;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RefactoringFile::indent(const Range &range, bool openEditor)
|
|
|
|
|
{
|
|
|
|
|
if (m_fileName.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
m_indentRanges.append(range);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
if (openEditor)
|
2010-08-12 13:46:18 +02:00
|
|
|
m_openEditor = true;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
return true;
|
2010-07-08 16:40:46 +02:00
|
|
|
}
|