aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/commentssettings.cpp
blob: d3171a94d68a9bc89d08260bcd451c671f3380fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "commentssettings.h"

#include "texteditorconstants.h"
#include "texteditorsettings.h"
#include "texteditortr.h"

#include <coreplugin/icore.h>
#include <projectexplorer/project.h>
#include <utils/layoutbuilder.h>

#include <QCheckBox>
#include <QComboBox>

using namespace Layouting;
using namespace Utils;

namespace TextEditor {

namespace {
const char kDocumentationCommentsGroup[] = "CppToolsDocumentationComments";
const char kEnableDoxygenBlocks[] = "EnableDoxygenBlocks";
const char kGenerateBrief[] = "GenerateBrief";
const char kAddLeadingAsterisks[] = "AddLeadingAsterisks";
const char kCommandPrefix[] = "CommandPrefix";
}

void CommentsSettings::setData(const Data &data)
{
    if (data == instance().m_data)
        return;
    instance().m_data = data;
    instance().save();
}

Key CommentsSettings::mainSettingsKey() { return kDocumentationCommentsGroup; }
Key CommentsSettings::enableDoxygenSettingsKey() { return kEnableDoxygenBlocks; }
Key CommentsSettings::generateBriefSettingsKey() { return kGenerateBrief; }
Key CommentsSettings::leadingAsterisksSettingsKey() { return kAddLeadingAsterisks; }
Key CommentsSettings::commandPrefixKey() { return kCommandPrefix; }

CommentsSettings::CommentsSettings()
{
    load();
}

CommentsSettings &CommentsSettings::instance()
{
    static CommentsSettings settings;
    return settings;
}

void CommentsSettings::save() const
{
    Utils::QtcSettings * const s = Core::ICore::settings();
    s->beginGroup(kDocumentationCommentsGroup);
    s->setValue(kEnableDoxygenBlocks, m_data.enableDoxygen);
    s->setValue(kGenerateBrief, m_data.generateBrief);
    s->setValue(kAddLeadingAsterisks, m_data.leadingAsterisks);
    s->setValueWithDefault(kCommandPrefix, int(m_data.commandPrefix));
    s->endGroup();
}

void CommentsSettings::load()
{
    Utils::QtcSettings * const s = Core::ICore::settings();
    s->beginGroup(kDocumentationCommentsGroup);
    m_data.enableDoxygen = s->value(kEnableDoxygenBlocks, true).toBool();
    m_data.generateBrief = m_data.enableDoxygen && s->value(kGenerateBrief, true).toBool();
    m_data.leadingAsterisks = s->value(kAddLeadingAsterisks, true).toBool();
    m_data.commandPrefix = static_cast<CommandPrefix>(
        s->value(kCommandPrefix, int(m_data.commandPrefix)).toInt());
    s->endGroup();
}

class CommentsSettingsWidget::Private
{
public:
    QCheckBox m_overwriteClosingChars;
    QCheckBox m_enableDoxygenCheckBox;
    QCheckBox m_generateBriefCheckBox;
    QCheckBox m_leadingAsterisksCheckBox;
    QComboBox m_commandPrefixComboBox;
};

CommentsSettingsWidget::CommentsSettingsWidget(const CommentsSettings::Data &settings)
    : d(new Private)
{
    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."));

    const auto commandPrefixLabel = new QLabel(Tr::tr("Doxygen command prefix:"));
    const QString commandPrefixToolTip = Tr::tr(R"(Doxygen allows "@" and "\" to start commands.
By default, "@" is used if the surrounding comment starts with "/**" or "///", and "\" is used
if the comment starts with "/*!" or "//!".)");
    commandPrefixLabel->setToolTip(commandPrefixToolTip);
    d->m_commandPrefixComboBox.setToolTip(commandPrefixToolTip);
    d->m_commandPrefixComboBox.addItem(Tr::tr("Automatic"));
    d->m_commandPrefixComboBox.addItem("@");
    d->m_commandPrefixComboBox.addItem("\\");

    initFromSettings(settings);

    Column {
        &d->m_enableDoxygenCheckBox,
        Row { Space(30), &d->m_generateBriefCheckBox },
        &d->m_leadingAsterisksCheckBox,
        Row { commandPrefixLabel, &d->m_commandPrefixComboBox, st },
        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);
    }
    connect(&d->m_commandPrefixComboBox, &QComboBox::currentIndexChanged,
            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();
    settings.commandPrefix = static_cast<CommentsSettings::CommandPrefix>(
        d->m_commandPrefixComboBox.currentIndex());
    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);
    d->m_commandPrefixComboBox.setCurrentIndex(int(settings.commandPrefix));
}

namespace Internal {

CommentsSettingsPage::CommentsSettingsPage()
{
    setId(Constants::TEXT_EDITOR_COMMENTS_SETTINGS);
    setDisplayName(Tr::tr("Documentation Comments"));
    setCategory(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY);
    setDisplayCategory(Tr::tr("Text Editor"));
    setCategoryIconPath(TextEditor::Constants::TEXT_EDITOR_SETTINGS_CATEGORY_ICON_PATH);
    setWidgetCreator([] { return new CommentsSettingsWidget(CommentsSettings::data()); });
}

} // namespace Internal
} // namespace TextEditor