| 49 | } |
| 50 | |
| 51 | void execute() override |
| 52 | { |
| 53 | KDevelop::DocumentChangeSet changes; |
| 54 | switch (m_type) { |
| 55 | case GuardType::Pragma: |
| 56 | { |
| 57 | KDevelop::DocumentChange change(m_path, KTextEditor::Range(m_startLine, 0, m_startLine, 0), QString(), |
| 58 | QStringLiteral("#pragma once\n\n")); |
| 59 | changes.addChange(change); |
| 60 | break; |
| 61 | } |
| 62 | case GuardType::Macro: |
| 63 | { |
| 64 | const QString macro = m_path.toUrl() |
| 65 | .fileName(QUrl::PrettyDecoded) |
| 66 | .replace(QRegularExpression(QStringLiteral("[^a-zA-Z0-9]")), QStringLiteral(" ")) |
| 67 | .simplified() |
| 68 | .toUpper() |
| 69 | .replace(QLatin1Char(' '), QLatin1Char('_')) |
| 70 | .append(QLatin1String("_INCLUDED")); |
| 71 | |
| 72 | const auto representation = KDevelop::createCodeRepresentation(m_path); |
| 73 | const auto lastLine = representation->lines() - 1; |
| 74 | const auto lastColumn = representation->line(lastLine).length(); |
| 75 | |
| 76 | // Add the #endif change before so that it applies correctly in case lastLine == m_startline |
| 77 | changes.addChange(KDevelop::DocumentChange(m_path, |
| 78 | KTextEditor::Range(lastLine, lastColumn, lastLine, lastColumn), |
| 79 | QString(), |
| 80 | QStringLiteral("\n#endif // %1").arg(macro))); |
| 81 | changes.addChange(KDevelop::DocumentChange(m_path, |
| 82 | KTextEditor::Range(m_startLine, 0, m_startLine, 0), |
| 83 | QString(), |
| 84 | QStringLiteral("#ifndef %1\n#define %1\n\n").arg(macro))); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | KDevelop::DUChainReadLocker lock; |
| 90 | changes.setReplacementPolicy(KDevelop::DocumentChangeSet::WarnOnFailedChange); |
| 91 | changes.applyAllChanges(); |
| 92 | emit executed(this); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | const GuardType m_type; |
nothing calls this directly
no test coverage detected