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
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-06-14 14:52:43 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cpprefactoringchanges.h"
|
|
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
#include <TranslationUnit.h>
|
|
|
|
|
#include <AST.h>
|
2010-08-12 11:34:48 +02:00
|
|
|
#include <cpptools/cppcodeformatter.h>
|
2010-12-03 13:49:35 +01:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2010-08-12 11:34:48 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
#include <texteditor/tabsettings.h>
|
|
|
|
|
|
|
|
|
|
#include <QtGui/QTextBlock>
|
|
|
|
|
|
2010-07-19 18:27:11 +02:00
|
|
|
using namespace CPlusPlus;
|
2010-09-30 12:09:38 +02:00
|
|
|
using namespace CppTools;
|
2010-08-13 11:48:29 +02:00
|
|
|
using namespace Utils;
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2010-08-13 15:38:45 +02:00
|
|
|
CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot)
|
|
|
|
|
: m_snapshot(snapshot)
|
2010-12-03 13:49:35 +01:00
|
|
|
, m_modelManager(Internal::CppModelManager::instance())
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
2010-06-22 10:36:28 +02:00
|
|
|
Q_ASSERT(m_modelManager);
|
|
|
|
|
m_workingCopy = m_modelManager->workingCopy();
|
2010-06-14 14:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-19 18:27:11 +02:00
|
|
|
const Snapshot &CppRefactoringChanges::snapshot() const
|
2010-06-22 10:45:08 +02:00
|
|
|
{
|
|
|
|
|
return m_snapshot;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
CppRefactoringFile CppRefactoringChanges::file(const QString &fileName)
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
2010-08-13 11:48:29 +02:00
|
|
|
return CppRefactoringFile(fileName, this);
|
2010-06-14 14:52:43 +02:00
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
|
|
|
|
|
void CppRefactoringChanges::indentSelection(const QTextCursor &selection) const
|
|
|
|
|
{
|
|
|
|
|
// ### shares code with CPPEditor::indent()
|
|
|
|
|
QTextDocument *doc = selection.document();
|
|
|
|
|
|
|
|
|
|
QTextBlock block = doc->findBlock(selection.selectionStart());
|
|
|
|
|
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
|
|
|
|
|
|
|
|
|
|
const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings());
|
2010-08-12 14:00:15 +02:00
|
|
|
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings);
|
2010-08-12 11:34:48 +02:00
|
|
|
codeFormatter.updateStateUntil(block);
|
|
|
|
|
|
|
|
|
|
do {
|
2010-09-10 14:12:14 +02:00
|
|
|
int indent;
|
|
|
|
|
int padding;
|
|
|
|
|
codeFormatter.indentFor(block, &indent, &padding);
|
|
|
|
|
tabSettings.indentLine(block, indent + padding, padding);
|
2010-08-12 11:34:48 +02:00
|
|
|
codeFormatter.updateLineStateChange(block);
|
|
|
|
|
block = block.next();
|
|
|
|
|
} while (block.isValid() && block != end);
|
|
|
|
|
}
|
2010-08-12 13:46:18 +02:00
|
|
|
|
|
|
|
|
void CppRefactoringChanges::fileChanged(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
m_modelManager->updateSourceFiles(QStringList(fileName));
|
|
|
|
|
}
|
2010-08-13 11:48:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
CppRefactoringFile::CppRefactoringFile()
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
CppRefactoringFile::CppRefactoringFile(const QString &fileName, CppRefactoringChanges *refactoringChanges)
|
|
|
|
|
: RefactoringFile(fileName, refactoringChanges)
|
|
|
|
|
{ }
|
|
|
|
|
|
2010-08-13 12:49:11 +02:00
|
|
|
CppRefactoringFile::CppRefactoringFile(TextEditor::BaseTextEditor *editor, CPlusPlus::Document::Ptr document)
|
|
|
|
|
: RefactoringFile()
|
|
|
|
|
, m_cppDocument(document)
|
|
|
|
|
{
|
|
|
|
|
m_fileName = document->fileName();
|
|
|
|
|
m_editor = editor;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
Document::Ptr CppRefactoringFile::cppDocument() const
|
|
|
|
|
{
|
2010-10-06 15:49:59 +02:00
|
|
|
if (!m_cppDocument || !m_cppDocument->translationUnit() ||
|
|
|
|
|
!m_cppDocument->translationUnit()->ast()) {
|
2010-08-13 11:48:29 +02:00
|
|
|
const QString source = document()->toPlainText();
|
|
|
|
|
const QString name = fileName();
|
|
|
|
|
const Snapshot &snapshot = refactoringChanges()->snapshot();
|
|
|
|
|
|
|
|
|
|
const QByteArray contents = snapshot.preprocessedCode(source, name);
|
|
|
|
|
m_cppDocument = snapshot.documentFromSource(contents, name);
|
|
|
|
|
m_cppDocument->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_cppDocument;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Scope *CppRefactoringFile::scopeAt(unsigned index) const
|
|
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
cppDocument()->translationUnit()->getTokenStartPosition(index, &line, &column);
|
|
|
|
|
return cppDocument()->scopeAt(line, column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppRefactoringFile::isCursorOn(unsigned tokenIndex) const
|
|
|
|
|
{
|
|
|
|
|
QTextCursor tc = cursor();
|
|
|
|
|
int cursorBegin = tc.selectionStart();
|
|
|
|
|
|
|
|
|
|
int start = startOf(tokenIndex);
|
|
|
|
|
int end = endOf(tokenIndex);
|
|
|
|
|
|
|
|
|
|
if (cursorBegin >= start && cursorBegin <= end)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppRefactoringFile::isCursorOn(const AST *ast) const
|
|
|
|
|
{
|
|
|
|
|
QTextCursor tc = cursor();
|
|
|
|
|
int cursorBegin = tc.selectionStart();
|
|
|
|
|
|
|
|
|
|
int start = startOf(ast);
|
|
|
|
|
int end = endOf(ast);
|
|
|
|
|
|
|
|
|
|
if (cursorBegin >= start && cursorBegin <= end)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChangeSet::Range CppRefactoringFile::range(unsigned tokenIndex) const
|
|
|
|
|
{
|
|
|
|
|
const Token &token = tokenAt(tokenIndex);
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
cppDocument()->translationUnit()->getPosition(token.begin(), &line, &column);
|
|
|
|
|
const int start = document()->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
return ChangeSet::Range(start, start + token.length());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChangeSet::Range CppRefactoringFile::range(AST *ast) const
|
|
|
|
|
{
|
|
|
|
|
return ChangeSet::Range(startOf(ast), endOf(ast));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::startOf(unsigned index) const
|
|
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
cppDocument()->translationUnit()->getPosition(tokenAt(index).begin(), &line, &column);
|
|
|
|
|
return document()->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::startOf(const AST *ast) const
|
|
|
|
|
{
|
|
|
|
|
return startOf(ast->firstToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::endOf(unsigned index) const
|
|
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
cppDocument()->translationUnit()->getPosition(tokenAt(index).end(), &line, &column);
|
|
|
|
|
return document()->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::endOf(const AST *ast) const
|
|
|
|
|
{
|
2010-09-09 15:18:17 +02:00
|
|
|
unsigned end = ast->lastToken();
|
|
|
|
|
Q_ASSERT(end > 0);
|
|
|
|
|
return endOf(end - 1);
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppRefactoringFile::startAndEndOf(unsigned index, int *start, int *end) const
|
|
|
|
|
{
|
|
|
|
|
unsigned line, column;
|
|
|
|
|
Token token(tokenAt(index));
|
|
|
|
|
cppDocument()->translationUnit()->getPosition(token.begin(), &line, &column);
|
|
|
|
|
*start = document()->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
*end = *start + token.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CppRefactoringFile::textOf(const AST *ast) const
|
|
|
|
|
{
|
|
|
|
|
return textOf(startOf(ast), endOf(ast));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Token &CppRefactoringFile::tokenAt(unsigned index) const
|
|
|
|
|
{
|
|
|
|
|
return cppDocument()->translationUnit()->tokenAt(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppRefactoringChanges *CppRefactoringFile::refactoringChanges() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<CppRefactoringChanges *>(m_refactoringChanges);
|
|
|
|
|
}
|