Changeset View
Changeset View
Standalone View
Standalone View
plugins/clang/duchain/headerguardassistant.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Amish K. Naidu <amhndu@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | * | ||||
20 | */ | ||||
21 | #include "headerguardassistant.h" | ||||
22 | #include "util/clangutils.h" | ||||
23 | #include "util/clangtypes.h" | ||||
24 | | ||||
25 | #include <language/codegen/documentchangeset.h> | ||||
26 | #include <language/codegen/coderepresentation.h> | ||||
27 | #include <language/duchain/duchainlock.h> | ||||
28 | | ||||
29 | #include <QDir> | ||||
30 | #include <QRegularExpression> | ||||
31 | | ||||
32 | #include <algorithm> | ||||
33 | | ||||
34 | | ||||
35 | enum class GuardType | ||||
36 | { | ||||
37 | Pragma, | ||||
38 | Macro | ||||
39 | }; | ||||
40 | | ||||
41 | class AddHeaderGuardAction | ||||
42 | : public KDevelop::IAssistantAction | ||||
43 | { | ||||
44 | public: | ||||
45 | AddHeaderGuardAction(const GuardType type, const int startLine, const KDevelop::IndexedString& path) | ||||
46 | : m_type(type) | ||||
47 | , m_startLine(startLine) | ||||
48 | , m_path(path) | ||||
49 | { | ||||
50 | } | ||||
51 | | ||||
52 | virtual ~AddHeaderGuardAction() override = default; | ||||
53 | | ||||
54 | QString description() const override | ||||
55 | { | ||||
56 | switch (m_type) { | ||||
57 | case GuardType::Pragma: return i18n("Add #pragma once"); | ||||
58 | case GuardType::Macro: return i18n("Add macro-based #ifndef/#define/#endif heard guard"); | ||||
59 | } | ||||
60 | return {}; | ||||
61 | } | ||||
62 | | ||||
63 | void execute() override | ||||
64 | { | ||||
65 | KDevelop::DocumentChangeSet changes; | ||||
66 | switch (m_type) { | ||||
67 | case GuardType::Pragma: | ||||
68 | { | ||||
69 | KDevelop::DocumentChange change(m_path, KTextEditor::Range(m_startLine, 0, m_startLine, 0), QString(), | ||||
70 | QStringLiteral("#pragma once\n\n")); | ||||
71 | changes.addChange(change); | ||||
72 | break; | ||||
73 | } | ||||
74 | case GuardType::Macro: | ||||
75 | { | ||||
76 | const QString macro = m_path.toUrl() | ||||
77 | .fileName(QUrl::PrettyDecoded) | ||||
78 | .replace(QRegularExpression(QStringLiteral("[^a-zA-Z0-9]")), QLatin1String(" ")) | ||||
79 | .simplified() | ||||
80 | .toUpper() | ||||
81 | .replace(QLatin1Char(' '), QLatin1Char('_')) | ||||
82 | .append(QLatin1String("_INCLUDED")); | ||||
83 | | ||||
84 | changes.addChange(KDevelop::DocumentChange(m_path, KTextEditor::Range(m_startLine, 0, m_startLine, 0), QString(), | ||||
85 | QStringLiteral("#ifndef %1\n#define %1\n\n").arg(macro))); | ||||
86 | | ||||
87 | const auto representation = KDevelop::createCodeRepresentation(m_path); | ||||
88 | const int lastLine = representation->lines() - 1; | ||||
89 | changes.addChange(KDevelop::DocumentChange(m_path, KTextEditor::Range(lastLine, 0, lastLine, 0), QString(), | ||||
90 | QStringLiteral("#endif // %1").arg(macro))); | ||||
91 | break; | ||||
92 | } | ||||
93 | } | ||||
94 | | ||||
95 | KDevelop::DUChainReadLocker lock; | ||||
96 | changes.setReplacementPolicy(KDevelop::DocumentChangeSet::WarnOnFailedChange); | ||||
97 | changes.applyAllChanges(); | ||||
98 | emit executed(this); | ||||
99 | } | ||||
100 | | ||||
101 | private: | ||||
102 | const GuardType m_type; | ||||
103 | const int m_startLine; | ||||
104 | const KDevelop::IndexedString m_path; | ||||
105 | }; | ||||
106 | | ||||
107 | HeaderGuardAssistant::HeaderGuardAssistant(const CXTranslationUnit unit, const CXFile file) | ||||
108 | : m_line(std::max(ClangUtils::skipTopCommentBlock(unit, file), 1u) - 1) // skip license etc | ||||
109 | , m_path(QDir(ClangString(clang_getFileName(file)).toString()).canonicalPath()) | ||||
110 | { | ||||
111 | } | ||||
112 | | ||||
113 | QString HeaderGuardAssistant::title() const | ||||
114 | { | ||||
115 | return QStringLiteral("Fix-Header"); | ||||
116 | } | ||||
117 | | ||||
118 | void HeaderGuardAssistant::createActions() | ||||
119 | { | ||||
120 | addAction(KDevelop::IAssistantAction::Ptr{new AddHeaderGuardAction(GuardType::Pragma, m_line, m_path)}); | ||||
121 | addAction(KDevelop::IAssistantAction::Ptr{new AddHeaderGuardAction(GuardType::Macro, m_line, m_path)}); | ||||
122 | } |