2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2015-08-24 18:26:09 +02:00
|
|
|
|
|
|
|
|
#include "clangfixitoperation.h"
|
|
|
|
|
|
|
|
|
|
#include <texteditor/refactoringchanges.h>
|
|
|
|
|
|
2016-06-27 10:45:51 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2015-12-02 20:07:29 +01:00
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
2015-08-24 18:26:09 +02:00
|
|
|
namespace ClangCodeModel {
|
2019-02-06 16:17:03 +01:00
|
|
|
namespace Internal {
|
2015-08-24 18:26:09 +02:00
|
|
|
|
2022-05-02 12:29:57 +02:00
|
|
|
using FileToFixits = QMap<QString, QList<ClangFixIt>>;
|
2016-06-27 10:45:51 +02:00
|
|
|
using RefactoringFilePtr = QSharedPointer<TextEditor::RefactoringFile>;
|
|
|
|
|
|
2022-05-02 12:29:57 +02:00
|
|
|
ClangFixItOperation::ClangFixItOperation(const QString &fixItText, const QList<ClangFixIt> &fixIts)
|
|
|
|
|
: fixItText(fixItText), fixIts(fixIts)
|
2015-08-24 18:26:09 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ClangFixItOperation::priority() const
|
|
|
|
|
{
|
|
|
|
|
return 10;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 16:17:03 +01:00
|
|
|
QString ClangFixItOperation::description() const
|
2015-08-24 18:26:09 +02:00
|
|
|
{
|
2022-05-02 12:29:57 +02:00
|
|
|
return QStringLiteral("Apply Fix: ") + fixItText;
|
2015-08-24 18:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 12:29:57 +02:00
|
|
|
static FileToFixits fixitsPerFile(const QList<ClangFixIt> &fixIts)
|
2016-06-27 10:45:51 +02:00
|
|
|
{
|
|
|
|
|
FileToFixits mapping;
|
|
|
|
|
|
2022-05-02 12:29:57 +02:00
|
|
|
for (const auto &fixItContainer : fixIts) {
|
|
|
|
|
const QString rangeStartFilePath = fixItContainer.range.start.targetFilePath.toString();
|
|
|
|
|
const QString rangeEndFilePath = fixItContainer.range.end.targetFilePath.toString();
|
2016-06-27 10:45:51 +02:00
|
|
|
QTC_CHECK(rangeStartFilePath == rangeEndFilePath);
|
|
|
|
|
mapping[rangeStartFilePath].append(fixItContainer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mapping;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-24 18:26:09 +02:00
|
|
|
void ClangFixItOperation::perform()
|
|
|
|
|
{
|
|
|
|
|
const TextEditor::RefactoringChanges refactoringChanges;
|
2022-05-02 12:29:57 +02:00
|
|
|
const FileToFixits fileToFixIts = fixitsPerFile(fixIts);
|
2016-06-27 10:45:51 +02:00
|
|
|
|
2019-07-24 13:43:54 +02:00
|
|
|
for (auto i = fileToFixIts.cbegin(), end = fileToFixIts.cend(); i != end; ++i) {
|
2016-06-27 10:45:51 +02:00
|
|
|
const QString filePath = i.key();
|
2022-05-02 12:29:57 +02:00
|
|
|
const QList<ClangFixIt> fixits = i.value();
|
2016-06-27 10:45:51 +02:00
|
|
|
|
2021-05-28 12:02:36 +02:00
|
|
|
RefactoringFilePtr refactoringFile = refactoringChanges.file(
|
|
|
|
|
Utils::FilePath::fromString(filePath));
|
2016-06-27 10:45:51 +02:00
|
|
|
refactoringFiles.append(refactoringFile);
|
|
|
|
|
|
|
|
|
|
applyFixitsToFile(*refactoringFile, fixits);
|
|
|
|
|
}
|
2015-08-24 18:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-27 10:45:51 +02:00
|
|
|
QString ClangFixItOperation::firstRefactoringFileContent_forTestOnly() const
|
2015-12-02 20:07:29 +01:00
|
|
|
{
|
2016-06-27 10:45:51 +02:00
|
|
|
return refactoringFiles.first()->document()->toPlainText();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 12:29:57 +02:00
|
|
|
void ClangFixItOperation::applyFixitsToFile(TextEditor::RefactoringFile &refactoringFile,
|
|
|
|
|
const QList<ClangFixIt> fixIts)
|
2016-06-27 10:45:51 +02:00
|
|
|
{
|
2022-05-02 12:29:57 +02:00
|
|
|
const Utils::ChangeSet changeSet = toChangeSet(refactoringFile, fixIts);
|
2016-06-27 10:45:51 +02:00
|
|
|
|
|
|
|
|
refactoringFile.setChangeSet(changeSet);
|
|
|
|
|
refactoringFile.apply();
|
2015-12-02 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-27 10:45:51 +02:00
|
|
|
Utils::ChangeSet ClangFixItOperation::toChangeSet(
|
|
|
|
|
TextEditor::RefactoringFile &refactoringFile,
|
2022-05-02 12:29:57 +02:00
|
|
|
const QList<ClangFixIt> fixIts) const
|
2015-08-24 18:26:09 +02:00
|
|
|
{
|
|
|
|
|
Utils::ChangeSet changeSet;
|
|
|
|
|
|
2022-05-02 12:29:57 +02:00
|
|
|
for (const auto &fixItContainer : fixIts) {
|
2018-04-04 18:25:23 +02:00
|
|
|
const auto &range = fixItContainer.range;
|
|
|
|
|
const auto &start = range.start;
|
|
|
|
|
const auto &end = range.end;
|
2022-05-02 12:29:57 +02:00
|
|
|
changeSet.replace(refactoringFile.position(start.targetLine, start.targetColumn + 1),
|
|
|
|
|
refactoringFile.position(end.targetLine, end.targetColumn + 1),
|
2018-04-04 18:25:23 +02:00
|
|
|
fixItContainer.text);
|
2015-08-24 18:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return changeSet;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 16:17:03 +01:00
|
|
|
} // namespace Internal
|
2015-08-24 18:26:09 +02:00
|
|
|
} // namespace ClangCodeModel
|