1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "clangfixitoperation.h"
27
28 #include <texteditor/refactoringchanges.h>
29
30 #include <utils/qtcassert.h>
31
32 #include <QTextDocument>
33
34 namespace ClangCodeModel {
35 namespace Internal {
36
37 using FileToFixits = QMap<QString, QVector<ClangBackEnd::FixItContainer>>;
38 using RefactoringFilePtr = QSharedPointer<TextEditor::RefactoringFile>;
39
ClangFixItOperation(const Utf8String & fixItText,const QVector<ClangBackEnd::FixItContainer> & fixItContainers)40 ClangFixItOperation::ClangFixItOperation(
41 const Utf8String &fixItText,
42 const QVector<ClangBackEnd::FixItContainer> &fixItContainers)
43 : fixItText(fixItText)
44 , fixItContainers(fixItContainers)
45 {
46 }
47
priority() const48 int ClangFixItOperation::priority() const
49 {
50 return 10;
51 }
52
description() const53 QString ClangFixItOperation::description() const
54 {
55 return QStringLiteral("Apply Fix: ") + fixItText.toString();
56 }
57
fixitsPerFile(const QVector<ClangBackEnd::FixItContainer> & fixItContainers)58 static FileToFixits fixitsPerFile(const QVector<ClangBackEnd::FixItContainer> &fixItContainers)
59 {
60 FileToFixits mapping;
61
62 for (const auto &fixItContainer : fixItContainers) {
63 const QString rangeStartFilePath = fixItContainer.range.start.filePath.toString();
64 const QString rangeEndFilePath = fixItContainer.range.end.filePath.toString();
65 QTC_CHECK(rangeStartFilePath == rangeEndFilePath);
66 mapping[rangeStartFilePath].append(fixItContainer);
67 }
68
69 return mapping;
70 }
71
perform()72 void ClangFixItOperation::perform()
73 {
74 const TextEditor::RefactoringChanges refactoringChanges;
75 const FileToFixits fileToFixIts = fixitsPerFile(fixItContainers);
76
77 for (auto i = fileToFixIts.cbegin(), end = fileToFixIts.cend(); i != end; ++i) {
78 const QString filePath = i.key();
79 const QVector<ClangBackEnd::FixItContainer> fixits = i.value();
80
81 RefactoringFilePtr refactoringFile = refactoringChanges.file(
82 Utils::FilePath::fromString(filePath));
83 refactoringFiles.append(refactoringFile);
84
85 applyFixitsToFile(*refactoringFile, fixits);
86 }
87 }
88
firstRefactoringFileContent_forTestOnly() const89 QString ClangFixItOperation::firstRefactoringFileContent_forTestOnly() const
90 {
91 return refactoringFiles.first()->document()->toPlainText();
92 }
93
applyFixitsToFile(TextEditor::RefactoringFile & refactoringFile,const QVector<ClangBackEnd::FixItContainer> fixItContainers)94 void ClangFixItOperation::applyFixitsToFile(
95 TextEditor::RefactoringFile &refactoringFile,
96 const QVector<ClangBackEnd::FixItContainer> fixItContainers)
97 {
98 const Utils::ChangeSet changeSet = toChangeSet(refactoringFile, fixItContainers);
99
100 refactoringFile.setChangeSet(changeSet);
101 refactoringFile.apply();
102 }
103
toChangeSet(TextEditor::RefactoringFile & refactoringFile,const QVector<ClangBackEnd::FixItContainer> fixItContainers) const104 Utils::ChangeSet ClangFixItOperation::toChangeSet(
105 TextEditor::RefactoringFile &refactoringFile,
106 const QVector<ClangBackEnd::FixItContainer> fixItContainers) const
107 {
108 Utils::ChangeSet changeSet;
109
110 for (const auto &fixItContainer : fixItContainers) {
111 const auto &range = fixItContainer.range;
112 const auto &start = range.start;
113 const auto &end = range.end;
114 changeSet.replace(refactoringFile.position(start.line, start.column),
115 refactoringFile.position(end.line, end.column),
116 fixItContainer.text);
117 }
118
119 return changeSet;
120 }
121
122 } // namespace Internal
123 } // namespace ClangCodeModel
124