TextEditor: Introduce a per-project variant of the comments settings

These settings can clearly differ per project.
Note that the classes have to live in the ProjectExplorer plugin, which
has a (questionable) dependency on TextEditor.

Change-Id: I7e3816da30cecd1ccb66549a2226b44dc0a26cb4
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2023-08-04 13:20:13 +02:00
parent 468a08bb2b
commit b92ac9a941
16 changed files with 344 additions and 117 deletions

View File

@@ -8,6 +8,7 @@
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/documentmodel.h> #include <coreplugin/editormanager/documentmodel.h>
#include <texteditor/commentssettings.h>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
@@ -20,6 +21,7 @@ namespace { typedef QByteArray _; }
using CppEditor::Tests::TemporaryDir; using CppEditor::Tests::TemporaryDir;
using CppEditor::Tests::TestCase; using CppEditor::Tests::TestCase;
using CppEditor::Internal::Tests::VerifyCleanCppModelManager; using CppEditor::Internal::Tests::VerifyCleanCppModelManager;
using namespace TextEditor;
using namespace Utils; using namespace Utils;
@@ -27,6 +29,19 @@ namespace CppEditor {
namespace Internal { namespace Internal {
namespace Tests { namespace Tests {
class SettingsInjector
{
public:
SettingsInjector(const CommentsSettings::Data &tempSettings)
{
CommentsSettings::setData(tempSettings);
}
~SettingsInjector() { CommentsSettings::setData(m_oldSettings); }
private:
const CommentsSettings::Data m_oldSettings = CommentsSettings::data();
};
void DoxygenTest::initTestCase() void DoxygenTest::initTestCase()
{ {
verifyCleanState(); verifyCleanState();
@@ -39,8 +54,6 @@ void DoxygenTest::cleanTestCase()
void DoxygenTest::cleanup() void DoxygenTest::cleanup()
{ {
if (oldSettings)
CppToolsSettings::setCommentsSettings(*oldSettings);
QVERIFY(Core::EditorManager::closeAllEditors(false)); QVERIFY(Core::EditorManager::closeAllEditors(false));
QVERIFY(TestCase::garbageCollectGlobalSnapshot()); QVERIFY(TestCase::garbageCollectGlobalSnapshot());
} }
@@ -372,7 +385,7 @@ void DoxygenTest::testWithMacroFromHeaderBeforeFunction()
const CppTestDocument headerDocumentDefiningMacro("header.h", "#define API\n"); const CppTestDocument headerDocumentDefiningMacro("header.h", "#define API\n");
runTest(given, expected, /*settings=*/ 0, {headerDocumentDefiningMacro}); runTest(given, expected, {headerDocumentDefiningMacro});
} }
void DoxygenTest::testNoLeadingAsterisks_data() void DoxygenTest::testNoLeadingAsterisks_data()
@@ -396,8 +409,9 @@ void DoxygenTest::testNoLeadingAsterisks()
TextEditor::CommentsSettings::Data injection; TextEditor::CommentsSettings::Data injection;
injection.enableDoxygen = true; injection.enableDoxygen = true;
injection.leadingAsterisks = false; injection.leadingAsterisks = false;
const SettingsInjector injector(injection);
runTest(given, expected, &injection); runTest(given, expected);
} }
void DoxygenTest::verifyCleanState() const void DoxygenTest::verifyCleanState() const
@@ -410,7 +424,6 @@ void DoxygenTest::verifyCleanState() const
/// The '|' in the input denotes the cursor position. /// The '|' in the input denotes the cursor position.
void DoxygenTest::runTest(const QByteArray &original, void DoxygenTest::runTest(const QByteArray &original,
const QByteArray &expected, const QByteArray &expected,
TextEditor::CommentsSettings::Data *settings,
const TestDocuments &includedHeaderDocuments) const TestDocuments &includedHeaderDocuments)
{ {
// Write files to disk // Write files to disk
@@ -433,11 +446,6 @@ void DoxygenTest::runTest(const QByteArray &original,
QVERIFY(TestCase::openCppEditor(testDocument.filePath(), &testDocument.m_editor, QVERIFY(TestCase::openCppEditor(testDocument.filePath(), &testDocument.m_editor,
&testDocument.m_editorWidget)); &testDocument.m_editorWidget));
if (settings) {
oldSettings.reset(new TextEditor::CommentsSettings::Data(CppToolsSettings::commentsSettings()));
CppToolsSettings::setCommentsSettings(*settings);
}
// We want to test documents that start with a comment. By default, the // We want to test documents that start with a comment. By default, the
// editor will fold the very first comment it encounters, assuming // editor will fold the very first comment it encounters, assuming
// it is a license header. Currently unfoldAll() does not work as // it is a license header. Currently unfoldAll() does not work as

View File

@@ -5,8 +5,6 @@
#include "cpptoolstestcase.h" #include "cpptoolstestcase.h"
#include <texteditor/commentssettings.h>
#include <QObject> #include <QObject>
#include <QScopedPointer> #include <QScopedPointer>
@@ -36,10 +34,7 @@ private:
void verifyCleanState() const; void verifyCleanState() const;
void runTest(const QByteArray &original, void runTest(const QByteArray &original,
const QByteArray &expected, const QByteArray &expected,
TextEditor::CommentsSettings::Data *settings = 0,
const TestDocuments &includedHeaderDocuments = TestDocuments()); const TestDocuments &includedHeaderDocuments = TestDocuments());
QScopedPointer<TextEditor::CommentsSettings::Data> oldSettings;
}; };
} // namespace Tests } // namespace Tests

View File

@@ -307,7 +307,8 @@ bool handleDoxygenContinuation(QTextCursor &cursor,
static bool trySplitComment(TextEditor::TextEditorWidget *editorWidget, static bool trySplitComment(TextEditor::TextEditorWidget *editorWidget,
const CPlusPlus::Snapshot &snapshot) const CPlusPlus::Snapshot &snapshot)
{ {
const TextEditor::CommentsSettings::Data &settings = CppToolsSettings::instance()->commentsSettings(); const TextEditor::CommentsSettings::Data &settings
= TextEditorSettings::commentsSettings(editorWidget->textDocument()->filePath());
if (!settings.enableDoxygen && !settings.leadingAsterisks) if (!settings.enableDoxygen && !settings.leadingAsterisks)
return false; return false;

View File

@@ -9,7 +9,6 @@
#include "cppcodestylepreferencesfactory.h" #include "cppcodestylepreferencesfactory.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <texteditor/commentssettings.h>
#include <texteditor/completionsettingspage.h> #include <texteditor/completionsettingspage.h>
#include <texteditor/codestylepool.h> #include <texteditor/codestylepool.h>
#include <texteditor/tabsettings.h> #include <texteditor/tabsettings.h>
@@ -33,7 +32,6 @@ namespace Internal {
class CppToolsSettingsPrivate class CppToolsSettingsPrivate
{ {
public: public:
CommentsSettings::Data m_commentsSettings;
CppCodeStylePreferences *m_globalCodeStyle = nullptr; CppCodeStylePreferences *m_globalCodeStyle = nullptr;
}; };
@@ -50,10 +48,6 @@ CppToolsSettings::CppToolsSettings()
qRegisterMetaType<CppCodeStyleSettings>("CppEditor::CppCodeStyleSettings"); qRegisterMetaType<CppCodeStyleSettings>("CppEditor::CppCodeStyleSettings");
d->m_commentsSettings = TextEditorSettings::commentsSettings();
connect(TextEditorSettings::instance(), &TextEditorSettings::commentsSettingsChanged,
this, &CppToolsSettings::setCommentsSettings);
// code style factory // code style factory
ICodeStylePreferencesFactory *factory = new CppCodeStylePreferencesFactory(); ICodeStylePreferencesFactory *factory = new CppCodeStylePreferencesFactory();
TextEditorSettings::registerCodeStyleFactory(factory); TextEditorSettings::registerCodeStyleFactory(factory);
@@ -164,16 +158,6 @@ CppCodeStylePreferences *CppToolsSettings::cppCodeStyle()
return d->m_globalCodeStyle; return d->m_globalCodeStyle;
} }
const CommentsSettings::Data &CppToolsSettings::commentsSettings()
{
return d->m_commentsSettings;
}
void CppToolsSettings::setCommentsSettings(const CommentsSettings::Data &commentsSettings)
{
d->m_commentsSettings = commentsSettings;
}
static QString sortEditorDocumentOutlineKey() static QString sortEditorDocumentOutlineKey()
{ {
return QLatin1String(Constants::CPPEDITOR_SETTINGSGROUP) return QLatin1String(Constants::CPPEDITOR_SETTINGSGROUP)

View File

@@ -5,12 +5,8 @@
#include "cppeditor_global.h" #include "cppeditor_global.h"
#include <texteditor/texteditorsettings.h>
#include <QObject> #include <QObject>
namespace TextEditor { class CommentsSettings; }
namespace CppEditor { namespace CppEditor {
class CppCodeStylePreferences; class CppCodeStylePreferences;
@@ -27,9 +23,6 @@ public:
static CppCodeStylePreferences *cppCodeStyle(); static CppCodeStylePreferences *cppCodeStyle();
static const TextEditor::CommentsSettings::Data &commentsSettings();
static void setCommentsSettings(const TextEditor::CommentsSettings::Data &commentsSettings);
static bool sortedEditorDocumentOutline(); static bool sortedEditorDocumentOutline();
static void setSortedEditorDocumentOutline(bool sorted); static void setSortedEditorDocumentOutline(bool sorted);
}; };

View File

@@ -124,6 +124,7 @@ add_qtc_plugin(ProjectExplorer
processparameters.cpp processparameters.h processparameters.cpp processparameters.h
processstep.cpp processstep.h processstep.cpp processstep.h
project.cpp project.h project.cpp project.h
projectcommentssettings.cpp projectcommentssettings.h
projectconfiguration.cpp projectconfiguration.h projectconfiguration.cpp projectconfiguration.h
projectconfigurationmodel.cpp projectconfigurationmodel.h projectconfigurationmodel.cpp projectconfigurationmodel.h
projectexplorer.cpp projectexplorer.h projectexplorer.cpp projectexplorer.h

View File

@@ -0,0 +1,128 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "projectcommentssettings.h"
#include "project.h"
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <QCheckBox>
#include <QVBoxLayout>
using namespace TextEditor;
namespace ProjectExplorer {
namespace Internal {
const char kUseGlobalKey[] = "UseGlobalKey";
ProjectCommentsSettings::ProjectCommentsSettings(ProjectExplorer::Project *project)
: m_project(project)
{
loadSettings();
}
CommentsSettings::Data ProjectCommentsSettings::settings() const
{
return m_useGlobalSettings ? CommentsSettings::data() : m_customSettings;
}
void ProjectCommentsSettings::setSettings(const CommentsSettings::Data &settings)
{
if (settings == m_customSettings)
return;
m_customSettings = settings;
saveSettings();
emit TextEditorSettings::instance()->commentsSettingsChanged();
}
void ProjectCommentsSettings::setUseGlobalSettings(bool useGlobal)
{
if (useGlobal == m_useGlobalSettings)
return;
m_useGlobalSettings = useGlobal;
saveSettings();
emit TextEditorSettings::instance()->commentsSettingsChanged();
}
void ProjectCommentsSettings::loadSettings()
{
if (!m_project)
return;
const QVariant entry = m_project->namedSettings(CommentsSettings::mainSettingsKey());
if (!entry.isValid())
return;
const QVariantMap data = entry.toMap();
m_useGlobalSettings = data.value(kUseGlobalKey, true).toBool();
m_customSettings.enableDoxygen = data.value(CommentsSettings::enableDoxygenSettingsKey(),
m_customSettings.enableDoxygen).toBool();
m_customSettings.generateBrief = data.value(CommentsSettings::generateBriefSettingsKey(),
m_customSettings.generateBrief).toBool();
m_customSettings.leadingAsterisks = data.value(CommentsSettings::leadingAsterisksSettingsKey(),
m_customSettings.leadingAsterisks).toBool();
}
void ProjectCommentsSettings::saveSettings()
{
if (!m_project)
return;
// Optimization: Don't save anything if the user never switched away from the default.
if (m_useGlobalSettings && !m_project->namedSettings
(CommentsSettings::mainSettingsKey()).isValid()) {
return;
}
QVariantMap data;
data.insert(kUseGlobalKey, m_useGlobalSettings);
data.insert(CommentsSettings::enableDoxygenSettingsKey(), m_customSettings.enableDoxygen);
data.insert(CommentsSettings::generateBriefSettingsKey(), m_customSettings.generateBrief);
data.insert(CommentsSettings::leadingAsterisksSettingsKey(), m_customSettings.leadingAsterisks);
m_project->setNamedSettings(CommentsSettings::mainSettingsKey(), data);
}
class ProjectCommentsSettingsWidget::Private
{
public:
Private(ProjectExplorer::Project *project) : settings(project) {}
ProjectCommentsSettings settings;
CommentsSettingsWidget widget{settings.settings()};
QCheckBox useGlobalSettingsCheckBox;
};
ProjectCommentsSettingsWidget::ProjectCommentsSettingsWidget(ProjectExplorer::Project *project)
: d(new Private(project))
{
setGlobalSettingsId(TextEditor::Constants::TEXT_EDITOR_COMMENTS_SETTINGS);
const auto layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(&d->widget);
const auto updateGlobalSettingsCheckBox = [this] {
setUseGlobalSettingsCheckBoxEnabled(true);
setUseGlobalSettings(d->settings.useGlobalSettings());
d->widget.setEnabled(!useGlobalSettings());
};
updateGlobalSettingsCheckBox();
connect(TextEditorSettings::instance(), &TextEditorSettings::commentsSettingsChanged,
this, updateGlobalSettingsCheckBox);
connect(this, &ProjectSettingsWidget::useGlobalSettingsChanged, this,
[this](bool checked) {
d->widget.setEnabled(!checked);
d->settings.setUseGlobalSettings(checked);
if (!checked)
d->settings.setSettings(d->widget.settingsData());
});
connect(&d->widget, &CommentsSettingsWidget::settingsChanged, this, [this] {
d->settings.setSettings(d->widget.settingsData());
});
}
ProjectCommentsSettingsWidget::~ProjectCommentsSettingsWidget() { delete d; }
} // namespace Internal
} // namespace ProjectExplorer

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include "projectsettingswidget.h"
#include <texteditor/commentssettings.h>
namespace ProjectExplorer {
class Project;
namespace Internal {
class ProjectCommentsSettings
{
public:
// Passing a null ptr is allowed and yields the global settings, so you can use
// this class transparently for both cases.
ProjectCommentsSettings(Project *project);
TextEditor::CommentsSettings::Data settings() const;
void setSettings(const TextEditor::CommentsSettings::Data &settings);
bool useGlobalSettings() const { return m_useGlobalSettings; }
void setUseGlobalSettings(bool useGlobal);
private:
void loadSettings();
void saveSettings();
ProjectExplorer::Project * const m_project;
TextEditor::CommentsSettings::Data m_customSettings;
bool m_useGlobalSettings = true;
};
class ProjectCommentsSettingsWidget : public ProjectSettingsWidget
{
public:
ProjectCommentsSettingsWidget(ProjectExplorer::Project *project);
~ProjectCommentsSettingsWidget();
private:
class Private;
Private * const d;
};
} // namespace Internal
} // namespace ProjectExplorer

View File

@@ -55,6 +55,7 @@
#include "parseissuesdialog.h" #include "parseissuesdialog.h"
#include "processstep.h" #include "processstep.h"
#include "project.h" #include "project.h"
#include "projectcommentssettings.h"
#include "projectexplorericons.h" #include "projectexplorericons.h"
#include "projectexplorersettings.h" #include "projectexplorersettings.h"
#include "projectexplorertr.h" #include "projectexplorertr.h"
@@ -115,6 +116,7 @@
#include <texteditor/findinfiles.h> #include <texteditor/findinfiles.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
@@ -883,6 +885,17 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
panelFactory->setCreateWidgetFunction([](Project *project) { return new CodeStyleSettingsWidget(project); }); panelFactory->setCreateWidgetFunction([](Project *project) { return new CodeStyleSettingsWidget(project); });
ProjectPanelFactory::registerFactory(panelFactory); ProjectPanelFactory::registerFactory(panelFactory);
panelFactory = new ProjectExplorer::ProjectPanelFactory;
panelFactory->setPriority(45);
panelFactory->setDisplayName(Tr::tr("Documentation Comments"));
panelFactory->setCreateWidgetFunction([](ProjectExplorer::Project *project) {
return new ProjectCommentsSettingsWidget(project);
});
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory);
TextEditor::TextEditorSettings::setCommentsSettingsRetriever([](const FilePath &filePath) {
return ProjectCommentsSettings(ProjectManager::projectForFile(filePath)).settings();
});
panelFactory = new ProjectPanelFactory; panelFactory = new ProjectPanelFactory;
panelFactory->setPriority(50); panelFactory->setPriority(50);
panelFactory->setDisplayName(Tr::tr("Dependencies")); panelFactory->setDisplayName(Tr::tr("Dependencies"));

View File

@@ -101,6 +101,7 @@ Project {
"processparameters.cpp", "processparameters.h", "processparameters.cpp", "processparameters.h",
"processstep.cpp", "processstep.h", "processstep.cpp", "processstep.h",
"project.cpp", "project.h", "project.cpp", "project.h",
"projectcommentssettings.cpp", "projectcommentssettings.h",
"projectconfiguration.cpp", "projectconfiguration.h", "projectconfiguration.cpp", "projectconfiguration.h",
"projectconfigurationmodel.cpp", "projectconfigurationmodel.h", "projectconfigurationmodel.cpp", "projectconfigurationmodel.h",
"projectexplorer.cpp", "projectexplorer.h", "projectexplorer.cpp", "projectexplorer.h",

View File

@@ -8,6 +8,7 @@
#include "texteditortr.h" #include "texteditortr.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <projectexplorer/project.h>
#include <utils/layoutbuilder.h> #include <utils/layoutbuilder.h>
#include <QCheckBox> #include <QCheckBox>
@@ -24,22 +25,19 @@ const char kGenerateBrief[] = "GenerateBrief";
const char kAddLeadingAsterisks[] = "AddLeadingAsterisks"; const char kAddLeadingAsterisks[] = "AddLeadingAsterisks";
} }
bool operator==(const CommentsSettings::Data &a, const CommentsSettings::Data &b)
{
return a.enableDoxygen == b.enableDoxygen
&& a.generateBrief == b.generateBrief
&& a.leadingAsterisks == b.leadingAsterisks;
}
void CommentsSettings::setData(const Data &data) void CommentsSettings::setData(const Data &data)
{ {
if (data == instance().m_data) if (data == instance().m_data)
return; return;
instance().m_data = data; instance().m_data = data;
instance().save(); instance().save();
emit TextEditorSettings::instance()->commentsSettingsChanged(data);
} }
QString CommentsSettings::mainSettingsKey() { return kDocumentationCommentsGroup; }
QString CommentsSettings::enableDoxygenSettingsKey() { return kEnableDoxygenBlocks; }
QString CommentsSettings::generateBriefSettingsKey() { return kGenerateBrief; }
QString CommentsSettings::leadingAsterisksSettingsKey() { return kAddLeadingAsterisks; }
CommentsSettings::CommentsSettings() CommentsSettings::CommentsSettings()
{ {
load(); load();
@@ -71,79 +69,86 @@ void CommentsSettings::load()
s->endGroup(); s->endGroup();
} }
namespace Internal { class CommentsSettingsWidget::Private
class CommentsSettingsWidget final : public Core::IOptionsPageWidget
{ {
public: public:
explicit CommentsSettingsWidget()
{
initFromSettings();
m_enableDoxygenCheckBox.setText(Tr::tr("Enable Doxygen blocks"));
m_enableDoxygenCheckBox.setToolTip(
Tr::tr("Automatically creates a Doxygen comment upon pressing "
"enter after a '/**', '/*!', '//!' or '///'."));
m_generateBriefCheckBox.setText(Tr::tr("Generate brief description"));
m_generateBriefCheckBox.setToolTip(
Tr::tr("Generates a <i>brief</i> command with an initial "
"description for the corresponding declaration."));
m_leadingAsterisksCheckBox.setText(Tr::tr("Add leading asterisks"));
m_leadingAsterisksCheckBox.setToolTip(
Tr::tr("Adds leading asterisks when continuing C/C++ \"/*\", Qt \"/*!\" "
"and Java \"/**\" style comments on new lines."));
Column {
&m_enableDoxygenCheckBox,
Row { Space(30), &m_generateBriefCheckBox },
&m_leadingAsterisksCheckBox,
st
}.attachTo(this);
connect(&m_enableDoxygenCheckBox, &QCheckBox::toggled,
&m_generateBriefCheckBox, &QCheckBox::setEnabled);
}
private:
void initFromSettings()
{
const CommentsSettings::Data &settings = CommentsSettings::data();
m_enableDoxygenCheckBox.setChecked(settings.enableDoxygen);
m_generateBriefCheckBox.setEnabled(m_enableDoxygenCheckBox.isChecked());
m_generateBriefCheckBox.setChecked(settings.generateBrief);
m_leadingAsterisksCheckBox.setChecked(settings.leadingAsterisks);
}
CommentsSettings::Data toSettings() const
{
CommentsSettings::Data settings;
settings.enableDoxygen = m_enableDoxygenCheckBox.isChecked();
settings.generateBrief = m_generateBriefCheckBox.isChecked();
settings.leadingAsterisks = m_leadingAsterisksCheckBox.isChecked();
return settings;
}
void apply() override
{
CommentsSettings::setData(toSettings());
}
QCheckBox m_overwriteClosingChars; QCheckBox m_overwriteClosingChars;
QCheckBox m_enableDoxygenCheckBox; QCheckBox m_enableDoxygenCheckBox;
QCheckBox m_generateBriefCheckBox; QCheckBox m_generateBriefCheckBox;
QCheckBox m_leadingAsterisksCheckBox; QCheckBox m_leadingAsterisksCheckBox;
}; };
CommentsSettingsWidget::CommentsSettingsWidget(const CommentsSettings::Data &settings)
: d(new Private)
{
initFromSettings(settings);
d->m_enableDoxygenCheckBox.setText(Tr::tr("Enable Doxygen blocks"));
d->m_enableDoxygenCheckBox.setToolTip(
Tr::tr("Automatically creates a Doxygen comment upon pressing "
"enter after a '/**', '/*!', '//!' or '///'."));
d->m_generateBriefCheckBox.setText(Tr::tr("Generate brief description"));
d->m_generateBriefCheckBox.setToolTip(
Tr::tr("Generates a <i>brief</i> command with an initial "
"description for the corresponding declaration."));
d->m_leadingAsterisksCheckBox.setText(Tr::tr("Add leading asterisks"));
d->m_leadingAsterisksCheckBox.setToolTip(
Tr::tr("Adds leading asterisks when continuing C/C++ \"/*\", Qt \"/*!\" "
"and Java \"/**\" style comments on new lines."));
Column {
&d->m_enableDoxygenCheckBox,
Row { Space(30), &d->m_generateBriefCheckBox },
&d->m_leadingAsterisksCheckBox,
st
}.attachTo(this);
connect(&d->m_enableDoxygenCheckBox, &QCheckBox::toggled,
&d->m_generateBriefCheckBox, &QCheckBox::setEnabled);
for (QCheckBox *checkBox : {&d->m_enableDoxygenCheckBox, &d->m_generateBriefCheckBox,
&d->m_leadingAsterisksCheckBox}) {
connect(checkBox, &QCheckBox::clicked, this, &CommentsSettingsWidget::settingsChanged);
}
}
CommentsSettingsWidget::~CommentsSettingsWidget() { delete d; }
CommentsSettings::Data CommentsSettingsWidget::settingsData() const
{
CommentsSettings::Data settings;
settings.enableDoxygen = d->m_enableDoxygenCheckBox.isChecked();
settings.generateBrief = d->m_generateBriefCheckBox.isChecked();
settings.leadingAsterisks = d->m_leadingAsterisksCheckBox.isChecked();
return settings;
}
void CommentsSettingsWidget::apply()
{
// This function is only ever called for the global settings page.
CommentsSettings::setData(settingsData());
}
void CommentsSettingsWidget::initFromSettings(const CommentsSettings::Data &settings)
{
d->m_enableDoxygenCheckBox.setChecked(settings.enableDoxygen);
d->m_generateBriefCheckBox.setEnabled(d->m_enableDoxygenCheckBox.isChecked());
d->m_generateBriefCheckBox.setChecked(settings.generateBrief);
d->m_leadingAsterisksCheckBox.setChecked(settings.leadingAsterisks);
}
namespace Internal {
CommentsSettingsPage::CommentsSettingsPage() CommentsSettingsPage::CommentsSettingsPage()
{ {
setId("Q.Comments"); setId(Constants::TEXT_EDITOR_COMMENTS_SETTINGS);
setDisplayName(Tr::tr("Documentation Comments")); setDisplayName(Tr::tr("Documentation Comments"));
setCategory(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY); setCategory(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY);
setDisplayCategory(Tr::tr("Text Editor")); setDisplayCategory(Tr::tr("Text Editor"));
setCategoryIconPath(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY_ICON_PATH); setCategoryIconPath(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY_ICON_PATH);
setWidgetCreator([] { return new CommentsSettingsWidget; }); setWidgetCreator([] { return new CommentsSettingsWidget(CommentsSettings::data()); });
} }
} // namespace Internal } // namespace Internal

View File

@@ -7,9 +7,7 @@
#include <coreplugin/dialogs/ioptionspage.h> #include <coreplugin/dialogs/ioptionspage.h>
QT_BEGIN_NAMESPACE namespace ProjectExplorer { class Project; }
class QSettings;
QT_END_NAMESPACE
namespace TextEditor { namespace TextEditor {
@@ -18,9 +16,6 @@ class TEXTEDITOR_EXPORT CommentsSettings
public: public:
class Data { class Data {
public: public:
friend bool operator==(const Data &a, const Data &b);
friend bool operator!=(const Data &a, const Data &b) { return !(a == b); }
bool enableDoxygen = true; bool enableDoxygen = true;
bool generateBrief = true; bool generateBrief = true;
bool leadingAsterisks = true; bool leadingAsterisks = true;
@@ -29,6 +24,11 @@ public:
static Data data() { return instance().m_data; } static Data data() { return instance().m_data; }
static void setData(const Data &data); static void setData(const Data &data);
static QString mainSettingsKey();
static QString enableDoxygenSettingsKey();
static QString generateBriefSettingsKey();
static QString leadingAsterisksSettingsKey();
private: private:
CommentsSettings(); CommentsSettings();
static CommentsSettings &instance(); static CommentsSettings &instance();
@@ -37,6 +37,38 @@ private:
Data m_data; Data m_data;
}; };
inline bool operator==(const CommentsSettings::Data &a, const CommentsSettings::Data &b)
{
return a.enableDoxygen == b.enableDoxygen
&& a.generateBrief == b.generateBrief
&& a.leadingAsterisks == b.leadingAsterisks;
}
inline bool operator!=(const CommentsSettings::Data &a, const CommentsSettings::Data &b)
{
return !(a == b);
}
class TEXTEDITOR_EXPORT CommentsSettingsWidget final : public Core::IOptionsPageWidget
{
Q_OBJECT
public:
CommentsSettingsWidget(const CommentsSettings::Data &settings);
~CommentsSettingsWidget();
CommentsSettings::Data settingsData() const;
signals:
void settingsChanged();
private:
void apply() override;
void initFromSettings(const CommentsSettings::Data &settings);
class Private;
Private * const d;
};
namespace Internal { namespace Internal {
@@ -47,5 +79,4 @@ public:
}; };
} // namespace Internal } // namespace Internal
} // namespace TextEditor } // namespace TextEditor

View File

@@ -226,6 +226,7 @@ const char TEXT_EDITOR_BEHAVIOR_SETTINGS[] = "B.BehaviourSettings";
const char TEXT_EDITOR_DISPLAY_SETTINGS[] = "D.DisplaySettings"; const char TEXT_EDITOR_DISPLAY_SETTINGS[] = "D.DisplaySettings";
const char TEXT_EDITOR_HIGHLIGHTER_SETTINGS[] = "E.HighlighterSettings"; const char TEXT_EDITOR_HIGHLIGHTER_SETTINGS[] = "E.HighlighterSettings";
const char TEXT_EDITOR_SNIPPETS_SETTINGS[] = "F.SnippetsSettings"; const char TEXT_EDITOR_SNIPPETS_SETTINGS[] = "F.SnippetsSettings";
const char TEXT_EDITOR_COMMENTS_SETTINGS[] = "Q.CommentsSettings";
const char HIGHLIGHTER_SETTINGS_CATEGORY[] = "HighlighterSettings"; const char HIGHLIGHTER_SETTINGS_CATEGORY[] = "HighlighterSettings";

View File

@@ -3,6 +3,7 @@
#include "texteditorplugin.h" #include "texteditorplugin.h"
#include "commentssettings.h"
#include "findincurrentfile.h" #include "findincurrentfile.h"
#include "findinfiles.h" #include "findinfiles.h"
#include "findinopenfiles.h" #include "findinopenfiles.h"
@@ -35,6 +36,8 @@
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <projectexplorer/projectpanelfactory.h>
#include <utils/fancylineedit.h> #include <utils/fancylineedit.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/macroexpander.h> #include <utils/macroexpander.h>

View File

@@ -56,6 +56,8 @@ public:
QMap<Utils::Id, CodeStylePool *> m_languageToCodeStylePool; QMap<Utils::Id, CodeStylePool *> m_languageToCodeStylePool;
QMap<QString, Utils::Id> m_mimeTypeToLanguage; QMap<QString, Utils::Id> m_mimeTypeToLanguage;
std::function<CommentsSettings::Data(const Utils::FilePath &)> m_retrieveCommentsSettings;
private: private:
static std::vector<FormatDescription> initialFormats(); static std::vector<FormatDescription> initialFormats();
}; };
@@ -503,9 +505,16 @@ const ExtraEncodingSettings &TextEditorSettings::extraEncodingSettings()
return d->m_behaviorSettingsPage.extraEncodingSettings(); return d->m_behaviorSettingsPage.extraEncodingSettings();
} }
CommentsSettings::Data TextEditorSettings::commentsSettings() void TextEditorSettings::setCommentsSettingsRetriever(
const std::function<CommentsSettings::Data(const Utils::FilePath &)> &retrieve)
{ {
return CommentsSettings::data(); d->m_retrieveCommentsSettings = retrieve;
}
CommentsSettings::Data TextEditorSettings::commentsSettings(const Utils::FilePath &filePath)
{
QTC_ASSERT(d->m_retrieveCommentsSettings, return CommentsSettings::data());
return d->m_retrieveCommentsSettings(filePath);
} }
void TextEditorSettings::registerCodeStyleFactory(ICodeStylePreferencesFactory *factory) void TextEditorSettings::registerCodeStyleFactory(ICodeStylePreferencesFactory *factory)

View File

@@ -10,6 +10,8 @@
#include <QObject> #include <QObject>
#include <functional>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
template <typename Key, typename T> template <typename Key, typename T>
class QMap; class QMap;
@@ -55,7 +57,10 @@ public:
static const CompletionSettings &completionSettings(); static const CompletionSettings &completionSettings();
static const HighlighterSettings &highlighterSettings(); static const HighlighterSettings &highlighterSettings();
static const ExtraEncodingSettings &extraEncodingSettings(); static const ExtraEncodingSettings &extraEncodingSettings();
static CommentsSettings::Data commentsSettings();
static void setCommentsSettingsRetriever(
const std::function<CommentsSettings::Data(const Utils::FilePath &)> &);
static CommentsSettings::Data commentsSettings(const Utils::FilePath &filePath);
static ICodeStylePreferencesFactory *codeStyleFactory(Utils::Id languageId); static ICodeStylePreferencesFactory *codeStyleFactory(Utils::Id languageId);
static const QMap<Utils::Id, ICodeStylePreferencesFactory *> &codeStyleFactories(); static const QMap<Utils::Id, ICodeStylePreferencesFactory *> &codeStyleFactories();
@@ -87,7 +92,7 @@ signals:
void displaySettingsChanged(const TextEditor::DisplaySettings &); void displaySettingsChanged(const TextEditor::DisplaySettings &);
void completionSettingsChanged(const TextEditor::CompletionSettings &); void completionSettingsChanged(const TextEditor::CompletionSettings &);
void extraEncodingSettingsChanged(const TextEditor::ExtraEncodingSettings &); void extraEncodingSettingsChanged(const TextEditor::ExtraEncodingSettings &);
void commentsSettingsChanged(const TextEditor::CommentsSettings::Data &); void commentsSettingsChanged();
}; };
} // namespace TextEditor } // namespace TextEditor