2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-06-14 14:52:43 +02:00
|
|
|
|
|
|
|
|
#include "cpprefactoringchanges.h"
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
#include "cppeditorconstants.h"
|
2014-07-30 16:29:02 +02:00
|
|
|
|
2011-02-01 14:13:54 +01:00
|
|
|
#include <projectexplorer/editorconfiguration.h>
|
2013-03-27 18:54:03 +01:00
|
|
|
|
2012-01-23 17:44:49 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2020-06-22 21:33:45 +02:00
|
|
|
#include <texteditor/icodestylepreferencesfactory.h>
|
2022-11-23 17:21:15 +01:00
|
|
|
#include <texteditor/tabsettings.h>
|
2020-06-22 21:33:45 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
|
2015-03-05 08:22:48 +01:00
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
2010-07-19 18:27:11 +02:00
|
|
|
using namespace CPlusPlus;
|
2022-11-23 17:21:15 +01:00
|
|
|
using namespace Utils;
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
namespace CppEditor {
|
2015-02-04 17:01:07 +02:00
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
static std::unique_ptr<TextEditor::Indenter> createIndenter(const FilePath &filePath,
|
2022-02-03 10:36:40 +01:00
|
|
|
QTextDocument *textDocument)
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
2022-02-03 10:36:40 +01:00
|
|
|
TextEditor::ICodeStylePreferencesFactory *factory
|
|
|
|
|
= TextEditor::TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID);
|
|
|
|
|
std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(textDocument));
|
|
|
|
|
indenter->setFileName(filePath);
|
|
|
|
|
return indenter;
|
|
|
|
|
}
|
2011-08-17 11:35:57 +02:00
|
|
|
|
2010-08-13 15:38:45 +02:00
|
|
|
CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot)
|
2011-08-17 11:35:57 +02:00
|
|
|
: RefactoringChanges(new CppRefactoringChangesData(snapshot))
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
CppRefactoringChangesData *CppRefactoringChanges::data() const
|
2010-06-22 10:45:08 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
return static_cast<CppRefactoringChangesData *>(m_data.data());
|
2010-06-22 10:45:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
CppRefactoringFilePtr CppRefactoringChanges::file(TextEditor::TextEditorWidget *editor, const Document::Ptr &document)
|
2010-06-14 14:52:43 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
CppRefactoringFilePtr result(new CppRefactoringFile(editor));
|
|
|
|
|
result->setCppDocument(document);
|
|
|
|
|
return result;
|
2010-06-14 14:52:43 +02:00
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
CppRefactoringFilePtr CppRefactoringChanges::file(const FilePath &filePath) const
|
2010-08-12 11:34:48 +02:00
|
|
|
{
|
2021-05-28 12:02:36 +02:00
|
|
|
CppRefactoringFilePtr result(new CppRefactoringFile(filePath, m_data));
|
2011-08-17 11:35:57 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const FilePath &filePath) const
|
2011-08-17 11:35:57 +02:00
|
|
|
{
|
2019-01-14 01:40:53 +01:00
|
|
|
QTextDocument *document = nullptr;
|
2023-05-02 17:55:44 +02:00
|
|
|
if (const auto source = data()->m_workingCopy.source(filePath))
|
|
|
|
|
document = new QTextDocument(QString::fromUtf8(*source));
|
2021-05-28 12:02:36 +02:00
|
|
|
CppRefactoringFilePtr result(new CppRefactoringFile(document, filePath));
|
2011-08-17 11:35:57 +02:00
|
|
|
result->m_data = m_data;
|
2010-08-12 11:34:48 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
return result;
|
2010-08-12 11:34:48 +02:00
|
|
|
}
|
2010-08-12 13:46:18 +02:00
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
const Snapshot &CppRefactoringChanges::snapshot() const
|
2010-08-12 13:46:18 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
return data()->m_snapshot;
|
2010-08-12 13:46:18 +02:00
|
|
|
}
|
2010-08-13 11:48:29 +02:00
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
CppRefactoringFile::CppRefactoringFile(const FilePath &filePath, const QSharedPointer<TextEditor::RefactoringChangesData> &data)
|
2021-05-28 12:02:36 +02:00
|
|
|
: RefactoringFile(filePath, data)
|
2011-02-07 11:25:29 +01:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
const Snapshot &snapshot = this->data()->m_snapshot;
|
2022-11-24 15:21:15 +01:00
|
|
|
m_cppDocument = snapshot.document(filePath);
|
2023-06-26 16:01:57 +02:00
|
|
|
m_formattingEnabled = true;
|
2011-02-07 11:25:29 +01:00
|
|
|
}
|
2010-08-13 11:48:29 +02:00
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
CppRefactoringFile::CppRefactoringFile(QTextDocument *document, const FilePath &filePath)
|
2021-05-28 12:02:36 +02:00
|
|
|
: RefactoringFile(document, filePath)
|
2023-06-26 16:01:57 +02:00
|
|
|
{
|
|
|
|
|
m_formattingEnabled = true;
|
|
|
|
|
}
|
2011-08-10 09:50:04 +02:00
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
CppRefactoringFile::CppRefactoringFile(TextEditor::TextEditorWidget *editor)
|
2011-08-10 09:50:04 +02:00
|
|
|
: RefactoringFile(editor)
|
2023-06-26 16:01:57 +02:00
|
|
|
{
|
|
|
|
|
m_formattingEnabled = true;
|
|
|
|
|
}
|
2010-08-13 12:49:11 +02:00
|
|
|
|
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()) {
|
2013-08-19 15:47:51 +02:00
|
|
|
const QByteArray source = document()->toPlainText().toUtf8();
|
2011-08-17 11:35:57 +02:00
|
|
|
const Snapshot &snapshot = data()->m_snapshot;
|
2010-08-13 11:48:29 +02:00
|
|
|
|
2021-08-10 16:19:02 +02:00
|
|
|
m_cppDocument = snapshot.preprocessedDocument(source, filePath());
|
2010-08-13 11:48:29 +02:00
|
|
|
m_cppDocument->check();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_cppDocument;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-10 09:50:04 +02:00
|
|
|
void CppRefactoringFile::setCppDocument(Document::Ptr document)
|
|
|
|
|
{
|
|
|
|
|
m_cppDocument = document;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 11:48:29 +02:00
|
|
|
Scope *CppRefactoringFile::scopeAt(unsigned index) const
|
|
|
|
|
{
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2023-07-12 13:03:20 +02:00
|
|
|
cppDocument()->translationUnit()->getTokenPosition(index, &line, &column);
|
2010-08-13 11:48:29 +02:00
|
|
|
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);
|
|
|
|
|
|
2019-10-31 16:15:03 +01:00
|
|
|
return cursorBegin >= start && cursorBegin <= end;
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CppRefactoringFile::isCursorOn(const AST *ast) const
|
|
|
|
|
{
|
|
|
|
|
QTextCursor tc = cursor();
|
|
|
|
|
int cursorBegin = tc.selectionStart();
|
|
|
|
|
|
|
|
|
|
int start = startOf(ast);
|
|
|
|
|
int end = endOf(ast);
|
|
|
|
|
|
2019-10-31 16:15:03 +01:00
|
|
|
return cursorBegin >= start && cursorBegin <= end;
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
ChangeSet::Range CppRefactoringFile::range(unsigned tokenIndex) const
|
2010-08-13 11:48:29 +02:00
|
|
|
{
|
|
|
|
|
const Token &token = tokenAt(tokenIndex);
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2014-05-06 14:48:24 -04:00
|
|
|
cppDocument()->translationUnit()->getPosition(token.utf16charsBegin(), &line, &column);
|
2010-08-13 11:48:29 +02:00
|
|
|
const int start = document()->findBlockByNumber(line - 1).position() + column - 1;
|
2019-07-24 18:40:10 +02:00
|
|
|
return {start, start + token.utf16chars()};
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
ChangeSet::Range CppRefactoringFile::range(const AST *ast) const
|
2010-08-13 11:48:29 +02:00
|
|
|
{
|
2019-02-07 10:09:21 +01:00
|
|
|
return {startOf(ast), endOf(ast)};
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::startOf(unsigned index) const
|
|
|
|
|
{
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2014-05-06 14:48:24 -04:00
|
|
|
cppDocument()->translationUnit()->getPosition(tokenAt(index).utf16charsBegin(), &line, &column);
|
2010-08-13 11:48:29 +02:00
|
|
|
return document()->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::startOf(const AST *ast) const
|
|
|
|
|
{
|
2020-10-28 11:10:34 +01:00
|
|
|
int firstToken = ast->firstToken();
|
|
|
|
|
const int lastToken = ast->lastToken();
|
|
|
|
|
while (tokenAt(firstToken).generated() && firstToken < lastToken)
|
|
|
|
|
++firstToken;
|
|
|
|
|
return startOf(firstToken);
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::endOf(unsigned index) const
|
|
|
|
|
{
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2014-05-06 14:48:24 -04:00
|
|
|
cppDocument()->translationUnit()->getPosition(tokenAt(index).utf16charsEnd(), &line, &column);
|
2010-08-13 11:48:29 +02:00
|
|
|
return document()->findBlockByNumber(line - 1).position() + column - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CppRefactoringFile::endOf(const AST *ast) const
|
|
|
|
|
{
|
2020-10-28 11:10:34 +01:00
|
|
|
int lastToken = ast->lastToken() - 1;
|
|
|
|
|
QTC_ASSERT(lastToken >= 0, return -1);
|
|
|
|
|
const int firstToken = ast->firstToken();
|
|
|
|
|
while (tokenAt(lastToken).generated() && lastToken > firstToken)
|
|
|
|
|
--lastToken;
|
|
|
|
|
return endOf(lastToken);
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppRefactoringFile::startAndEndOf(unsigned index, int *start, int *end) const
|
|
|
|
|
{
|
2019-07-24 18:40:10 +02:00
|
|
|
int line, column;
|
2010-08-13 11:48:29 +02:00
|
|
|
Token token(tokenAt(index));
|
2014-05-06 14:48:24 -04:00
|
|
|
cppDocument()->translationUnit()->getPosition(token.utf16charsBegin(), &line, &column);
|
2010-08-13 11:48:29 +02:00
|
|
|
*start = document()->findBlockByNumber(line - 1).position() + column - 1;
|
2013-12-12 21:37:46 +01:00
|
|
|
*end = *start + token.utf16chars();
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 11:35:57 +02:00
|
|
|
CppRefactoringChangesData *CppRefactoringFile::data() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<CppRefactoringChangesData *>(m_data.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppRefactoringFile::fileChanged()
|
2010-08-13 11:48:29 +02:00
|
|
|
{
|
2011-08-17 11:35:57 +02:00
|
|
|
m_cppDocument.clear();
|
|
|
|
|
RefactoringFile::fileChanged();
|
2010-08-13 11:48:29 +02:00
|
|
|
}
|
2015-02-04 17:01:07 +02:00
|
|
|
|
2022-02-03 10:36:40 +01:00
|
|
|
CppRefactoringChangesData::CppRefactoringChangesData(const Snapshot &snapshot)
|
|
|
|
|
: m_snapshot(snapshot)
|
2023-07-12 09:47:29 +02:00
|
|
|
, m_workingCopy(CppModelManager::workingCopy())
|
2022-02-03 10:36:40 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void CppRefactoringChangesData::indentSelection(const QTextCursor &selection,
|
2022-11-23 17:21:15 +01:00
|
|
|
const FilePath &filePath,
|
2022-02-03 10:36:40 +01:00
|
|
|
const TextEditor::TextDocument *textDocument) const
|
|
|
|
|
{
|
|
|
|
|
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
|
|
|
|
|
textDocument->indenter()->indent(selection, QChar::Null, textDocument->tabSettings());
|
|
|
|
|
} else {
|
2022-02-03 15:14:24 +01:00
|
|
|
const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath, textDocument);
|
2022-02-03 10:36:40 +01:00
|
|
|
auto indenter = createIndenter(filePath, selection.document());
|
|
|
|
|
indenter->indent(selection, QChar::Null, tabSettings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppRefactoringChangesData::reindentSelection(const QTextCursor &selection,
|
2022-11-23 17:21:15 +01:00
|
|
|
const FilePath &filePath,
|
2022-02-03 10:36:40 +01:00
|
|
|
const TextEditor::TextDocument *textDocument) const
|
|
|
|
|
{
|
|
|
|
|
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
|
|
|
|
|
textDocument->indenter()->reindent(selection, textDocument->tabSettings());
|
|
|
|
|
} else {
|
2022-02-03 15:14:24 +01:00
|
|
|
const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath, textDocument);
|
2022-02-03 10:36:40 +01:00
|
|
|
auto indenter = createIndenter(filePath, selection.document());
|
|
|
|
|
indenter->reindent(selection, tabSettings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
void CppRefactoringChangesData::fileChanged(const FilePath &filePath)
|
2022-02-03 10:36:40 +01:00
|
|
|
{
|
2023-07-12 09:47:29 +02:00
|
|
|
CppModelManager::updateSourceFiles({filePath});
|
2022-02-03 10:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-23 17:21:15 +01:00
|
|
|
} // CppEditor
|