diff --git a/src/core/core/settings.json b/src/core/core/settings.json index 9fc1a0b1..d448136b 100644 --- a/src/core/core/settings.json +++ b/src/core/core/settings.json @@ -30,6 +30,13 @@ "LANG_NEUTRAL": "[default]" } }, + "cpp": { + "excluded_macros": [ + "AFX_EXT_CLASS", + "[A-Z_]*EXPORT[A-Z_]*", + "Q_OBJECT" + ] + }, "mime_types": { "c": "cpp_type", "cpp": "cpp_type", diff --git a/src/core/cppdocument.cpp b/src/core/cppdocument.cpp index 9500ac77..a42b0687 100644 --- a/src/core/cppdocument.cpp +++ b/src/core/cppdocument.cpp @@ -1548,14 +1548,24 @@ QStringList CppDocument::primitiveTypes() const QList CppDocument::includedRanges() const { - QList macros {"AFX_EXT_CLASS"}; + auto macros = Settings::instance()->value(Settings::CppExcludedMacros); + if (macros.isEmpty()) { + return {}; + } + + QRegularExpression regex(macros.join("|")); + if (!regex.isValid()) { + spdlog::error("CppDocument::includedRanges: Failed to create regex for excluded macros: {}", + regex.errorString()); + return {}; + } + + auto document = textEdit()->document(); + QList ranges; treesitter::Point lastPoint {0, 0}; uint32_t lastByte = 0; - auto document = textEdit()->document(); - - QRegularExpression regex(macros.join("|")); for (auto block = document->firstBlock(); block.isValid(); block = block.next()) { QRegularExpressionMatch match; auto searchFrom = 0; diff --git a/src/core/settings.h b/src/core/settings.h index 463db5d3..0a332311 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -49,6 +49,7 @@ class Settings : public QObject static inline constexpr char RcAssetFlags[] = "/rc/asset_flags"; static inline constexpr char RcAssetColors[] = "/rc/asset_transparent_colors"; static inline constexpr char RcLanguageMap[] = "/rc/language_map"; + static inline constexpr char CppExcludedMacros[] = "/cpp/excluded_macros"; static inline constexpr char SaveLogsToFile[] = "/logs/saveToFile"; static inline constexpr char ScriptPaths[] = "/script_paths"; static inline constexpr char Tab[] = "/text_editor/tab"; diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index b7a57bb2..e4eb0ed2 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -19,7 +19,9 @@ #include #include #include +#include #include +#include #include #include @@ -45,6 +47,7 @@ OptionsDialog::OptionsDialog(QWidget *parent) initializeRcSettings(); initializeSaveToLogFileSetting(); initializeEnableLSPSetting(); + initializeCppSettings(); updateScriptPaths(); } @@ -217,6 +220,42 @@ void OptionsDialog::initializeRcSettings() }); } +void OptionsDialog::initializeCppSettings() +{ + auto *model = new QStringListModel(this); + model->setStringList(DEFAULT_VALUE(QStringList, CppExcludedMacros)); + ui->cppExcludedMacros->setModel(model); + ui->cppExcludedMacros->setSelectionMode(QAbstractItemView::SingleSelection); + + connect(ui->cppAddExcludedMacro, &QPushButton::clicked, model, [this, model]() { + auto ok = false; + auto excludeMacro = QInputDialog::getText(this, tr("Exclude a new macro"), tr("Macro name (supports Regex)"), + QLineEdit::Normal, "", &ok); + if (ok) { + if (model->insertRows(0, 1)) { + model->setData(model->index(0), excludeMacro); + } else { + spdlog::warn("OptionsDialog::excludeMacro: Failed to create new row!"); + } + } + }); + + connect(ui->cppRemoveExcludedMacro, &QPushButton::clicked, model, [this, model]() { + auto selection = ui->cppExcludedMacros->selectionModel()->selectedRows(); + // single selection mode, so should be at most one. + if (!selection.isEmpty()) { + model->removeRows(selection.front().row(), 1); + } + }); + + connect(model, &QStringListModel::dataChanged, model, [model]() { + SET_DEFAULT_VALUE(CppExcludedMacros, model->stringList()); + }); + connect(model, &QStringListModel::rowsRemoved, model, [model]() { + SET_DEFAULT_VALUE(CppExcludedMacros, model->stringList()); + }); +} + void OptionsDialog::openUserSettings() { QDesktopServices::openUrl(QUrl::fromLocalFile(ui->userPath->text())); diff --git a/src/gui/optionsdialog.h b/src/gui/optionsdialog.h index c05f7020..d7108b5e 100644 --- a/src/gui/optionsdialog.h +++ b/src/gui/optionsdialog.h @@ -35,6 +35,7 @@ class OptionsDialog : public QDialog void initializeScriptPathSettings(); void initializeScriptBehaviorSettings(); void initializeRcSettings(); + void initializeCppSettings(); void openUserSettings(); void openProjectSettings(); diff --git a/src/gui/optionsdialog.ui b/src/gui/optionsdialog.ui index 32561bba..2d2f4f7c 100644 --- a/src/gui/optionsdialog.ui +++ b/src/gui/optionsdialog.ui @@ -626,6 +626,49 @@ + + + + + + C++ Files + + + + + + The Macros listed here will be excluded from parsing (supports Regex). + + + Excluded Macros + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + Add + + + + + + + Remove + + + + + + + + + + + @@ -684,6 +727,11 @@ Rc Files + + + C++ Files + +