Skip to content

Commit

Permalink
feat: Allow specifying custom macros to exclude
Browse files Browse the repository at this point in the history
Users can now specify a list of macros to exclude from the C++ parser
through the options dialog.
By default the list is pre-filled with:
- AFX_EXT_CLASS
- [A-Z_]*EXPORT[A-Z_]*
- Q_OBJECT
  • Loading branch information
LeonMatthesKDAB committed Jul 18, 2024
1 parent acb23f3 commit 1ded7cd
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/core/core/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 14 additions & 4 deletions src/core/cppdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,14 +1548,24 @@ QStringList CppDocument::primitiveTypes() const

QList<treesitter::Range> CppDocument::includedRanges() const
{
QList<QString> macros {"AFX_EXT_CLASS"};
auto macros = Settings::instance()->value<QStringList>(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<treesitter::Range> 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;
Expand Down
1 change: 1 addition & 0 deletions src/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
39 changes: 39 additions & 0 deletions src/gui/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include <QApplication>
#include <QDesktopServices>
#include <QFileDialog>
#include <QInputDialog>
#include <QIntValidator>
#include <QStringListModel>
#include <QUrl>
#include <algorithm>

Expand All @@ -45,6 +47,7 @@ OptionsDialog::OptionsDialog(QWidget *parent)
initializeRcSettings();
initializeSaveToLogFileSetting();
initializeEnableLSPSetting();
initializeCppSettings();

updateScriptPaths();
}
Expand Down Expand Up @@ -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()));
Expand Down
1 change: 1 addition & 0 deletions src/gui/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class OptionsDialog : public QDialog
void initializeScriptPathSettings();
void initializeScriptBehaviorSettings();
void initializeRcSettings();
void initializeCppSettings();

void openUserSettings();
void openProjectSettings();
Expand Down
48 changes: 48 additions & 0 deletions src/gui/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,49 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="stackedWidgetPage5">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QGroupBox" name="groupBox_7">
<property name="title">
<string>C++ Files</string>
</property>
<layout class="QGridLayout" name="gridLayout_11">
<item row="0" column="0">
<widget class="QLabel" name="cppLabelExcludedMacros">
<property name="toolTip">
<string>The Macros listed here will be excluded from parsing (supports Regex).</string>
</property>
<property name="text">
<string>Excluded Macros</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="cppAddExcludedMacro">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="cppRemoveExcludedMacro">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item row="0" column="1" rowspan="3">
<widget class="QListView" name="cppExcludedMacros"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="0" column="1">
Expand Down Expand Up @@ -684,6 +727,11 @@
<string>Rc Files</string>
</property>
</item>
<item>
<property name="text">
<string>C++ Files</string>
</property>
</item>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 1ded7cd

Please sign in to comment.