2010-06-14 14:52:43 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "refactoringchanges.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
#include <QtCore/QSet>
|
|
|
|
|
#include <QtGui/QTextBlock>
|
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()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
BaseTextEditor *RefactoringChanges::editorForFile(const QString &fileName,
|
|
|
|
|
bool openIfClosed)
|
|
|
|
|
{
|
|
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
|
|
|
|
|
const QList<Core::IEditor *> editors = editorManager->editorsForFileName(fileName);
|
|
|
|
|
foreach (Core::IEditor *editor, editors) {
|
|
|
|
|
BaseTextEditor *textEditor = qobject_cast<BaseTextEditor *>(editor->widget());
|
|
|
|
|
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(),
|
|
|
|
|
Core::EditorManager::NoActivate | Core::EditorManager::IgnoreNavigationHistory | Core::EditorManager::NoModeSwitch);
|
|
|
|
|
return qobject_cast<BaseTextEditor *>(editor->widget());
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
BaseTextEditor *editor = editorForFile(fileName, openEditor);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
indentSelection(cursor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor.endEditBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!editor) {
|
|
|
|
|
QFile file(fileName);
|
|
|
|
|
file.open(QFile::WriteOnly);
|
|
|
|
|
file.write(document->toPlainText().toUtf8());
|
|
|
|
|
delete document;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// ### delete!
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefactoringFile RefactoringChanges::file(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
if (QFile::exists(fileName))
|
|
|
|
|
return RefactoringFile(fileName, this);
|
|
|
|
|
else
|
|
|
|
|
return RefactoringFile();
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
BaseTextEditor *RefactoringChanges::openEditor(const QString &fileName, int pos)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2010-08-12 13:46:18 +02:00
|
|
|
BaseTextEditor *editor = editorForFile(fileName, true);
|
|
|
|
|
if (pos != -1) {
|
|
|
|
|
QTextCursor cursor = editor->textCursor();
|
|
|
|
|
cursor.setPosition(pos);
|
|
|
|
|
editor->setTextCursor(cursor);
|
|
|
|
|
}
|
|
|
|
|
return editor;
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
BaseTextEditor *RefactoringChanges::activateEditor(const QString &fileName, int pos)
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2010-08-12 13:46:18 +02:00
|
|
|
BaseTextEditor *editor = openEditor(fileName, pos);
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
editorManager->activateEditor(editor->editableInterface());
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
return editor;
|
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-08-12 13:46:18 +02:00
|
|
|
if (m_openEditor)
|
|
|
|
|
m_editor = m_refactoringChanges->openEditor(m_fileName, -1);
|
|
|
|
|
|
|
|
|
|
// apply changes, if any
|
|
|
|
|
if (!m_indentRanges.isEmpty() || !m_changes.isEmpty()) {
|
|
|
|
|
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) {
|
|
|
|
|
m_refactoringChanges->indentSelection(selection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.endEditBlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if this document doesn't have an editor, write the result to a file
|
|
|
|
|
if (!m_editor) {
|
|
|
|
|
const QByteArray &newContents = doc->toPlainText().toUtf8();
|
|
|
|
|
QFile file(m_fileName);
|
|
|
|
|
file.open(QFile::WriteOnly);
|
|
|
|
|
//file->resize(newContents.size()); // ### necessary?
|
|
|
|
|
file.write(newContents);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_refactoringChanges->fileChanged(m_fileName);
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
else if (!m_document && !m_fileName.isEmpty()) {
|
2010-08-12 11:34:48 +02:00
|
|
|
QString fileContents;
|
|
|
|
|
{
|
|
|
|
|
QFile file(m_fileName);
|
|
|
|
|
if (file.open(QIODevice::ReadOnly))
|
|
|
|
|
fileContents = file.readAll();
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString RefactoringFile::textAt(int start, int end) const
|
|
|
|
|
{
|
|
|
|
|
QTextCursor c = cursor();
|
|
|
|
|
c.setPosition(start);
|
|
|
|
|
c.setPosition(end, QTextCursor::KeepAnchor);
|
|
|
|
|
return c.selectedText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString RefactoringFile::textAt(const Range &range) const
|
|
|
|
|
{
|
|
|
|
|
return textAt(range.start, range.end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|