aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/cppcodestylesettings.cpp
blob: 5499005c62b3537e2348574f5086a1b1c7407281 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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 "cppcodestylesettings.h"

#include "cppcodestylepreferences.h"
#include "cppeditorconstants.h"
#include "cpptoolssettings.h"

#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projecttree.h>

#include <texteditor/tabsettings.h>

#include <cplusplus/Overview.h>

#include <utils/qtcassert.h>

static const char statementMacrosKey[] = "StatementMacros";
static const char indentBlockBracesKey[] = "IndentBlockBraces";
static const char indentBlockBodyKey[] = "IndentBlockBody";
static const char indentClassBracesKey[] = "IndentClassBraces";
static const char indentEnumBracesKey[] = "IndentEnumBraces";
static const char indentNamespaceBracesKey[] = "IndentNamespaceBraces";
static const char indentNamespaceBodyKey[] = "IndentNamespaceBody";
static const char indentAccessSpecifiersKey[] = "IndentAccessSpecifiers";
static const char indentDeclarationsRelativeToAccessSpecifiersKey[] = "IndentDeclarationsRelativeToAccessSpecifiers";
static const char indentFunctionBodyKey[] = "IndentFunctionBody";
static const char indentFunctionBracesKey[] = "IndentFunctionBraces";
static const char indentSwitchLabelsKey[] = "IndentSwitchLabels";
static const char indentStatementsRelativeToSwitchLabelsKey[] = "IndentStatementsRelativeToSwitchLabels";
static const char indentBlocksRelativeToSwitchLabelsKey[] = "IndentBlocksRelativeToSwitchLabels";
static const char indentControlFlowRelativeToSwitchLabelsKey[] = "IndentControlFlowRelativeToSwitchLabels";
static const char bindStarToIdentifierKey[] = "BindStarToIdentifier";
static const char bindStarToTypeNameKey[] = "BindStarToTypeName";
static const char bindStarToLeftSpecifierKey[] = "BindStarToLeftSpecifier";
static const char bindStarToRightSpecifierKey[] = "BindStarToRightSpecifier";
static const char extraPaddingForConditionsIfConfusingAlignKey[] = "ExtraPaddingForConditionsIfConfusingAlign";
static const char alignAssignmentsKey[] = "AlignAssignments";
static const char shortGetterNameKey[] = "ShortGetterName";

using namespace Utils;

namespace CppEditor {

// ------------------ CppCodeStyleSettingsWidget

CppCodeStyleSettings::CppCodeStyleSettings() = default;

Store CppCodeStyleSettings::toMap() const
{
    return {
        {statementMacrosKey, statementMacros},
        {indentBlockBracesKey, indentBlockBraces},
        {indentBlockBodyKey, indentBlockBody},
        {indentClassBracesKey, indentClassBraces},
        {indentEnumBracesKey, indentEnumBraces},
        {indentNamespaceBracesKey, indentNamespaceBraces},
        {indentNamespaceBodyKey, indentNamespaceBody},
        {indentAccessSpecifiersKey, indentAccessSpecifiers},
        {indentDeclarationsRelativeToAccessSpecifiersKey, indentDeclarationsRelativeToAccessSpecifiers},
        {indentFunctionBodyKey, indentFunctionBody},
        {indentFunctionBracesKey, indentFunctionBraces},
        {indentSwitchLabelsKey, indentSwitchLabels},
        {indentStatementsRelativeToSwitchLabelsKey, indentStatementsRelativeToSwitchLabels},
        {indentBlocksRelativeToSwitchLabelsKey, indentBlocksRelativeToSwitchLabels},
        {indentControlFlowRelativeToSwitchLabelsKey, indentControlFlowRelativeToSwitchLabels},
        {bindStarToIdentifierKey, bindStarToIdentifier},
        {bindStarToTypeNameKey, bindStarToTypeName},
        {bindStarToLeftSpecifierKey, bindStarToLeftSpecifier},
        {bindStarToRightSpecifierKey, bindStarToRightSpecifier},
        {extraPaddingForConditionsIfConfusingAlignKey, extraPaddingForConditionsIfConfusingAlign},
        {alignAssignmentsKey, alignAssignments},
        {shortGetterNameKey, preferGetterNameWithoutGetPrefix}
    };
}

void CppCodeStyleSettings::fromMap(const Store &map)
{
    statementMacros = map.value(statementMacrosKey, statementMacros).toStringList();
    indentBlockBraces = map.value(indentBlockBracesKey, indentBlockBraces).toBool();
    indentBlockBody = map.value(indentBlockBodyKey, indentBlockBody).toBool();
    indentClassBraces = map.value(indentClassBracesKey, indentClassBraces).toBool();
    indentEnumBraces = map.value(indentEnumBracesKey, indentEnumBraces).toBool();
    indentNamespaceBraces = map.value(indentNamespaceBracesKey, indentNamespaceBraces).toBool();
    indentNamespaceBody = map.value(indentNamespaceBodyKey, indentNamespaceBody).toBool();
    indentAccessSpecifiers = map.value(indentAccessSpecifiersKey, indentAccessSpecifiers).toBool();
    indentDeclarationsRelativeToAccessSpecifiers =
            map.value(indentDeclarationsRelativeToAccessSpecifiersKey,
                      indentDeclarationsRelativeToAccessSpecifiers).toBool();
    indentFunctionBody = map.value(indentFunctionBodyKey, indentFunctionBody).toBool();
    indentFunctionBraces = map.value(indentFunctionBracesKey, indentFunctionBraces).toBool();
    indentSwitchLabels = map.value(indentSwitchLabelsKey, indentSwitchLabels).toBool();
    indentStatementsRelativeToSwitchLabels = map.value(indentStatementsRelativeToSwitchLabelsKey,
                                indentStatementsRelativeToSwitchLabels).toBool();
    indentBlocksRelativeToSwitchLabels = map.value(indentBlocksRelativeToSwitchLabelsKey,
                                indentBlocksRelativeToSwitchLabels).toBool();
    indentControlFlowRelativeToSwitchLabels = map.value(indentControlFlowRelativeToSwitchLabelsKey,
                                indentControlFlowRelativeToSwitchLabels).toBool();
    bindStarToIdentifier = map.value(bindStarToIdentifierKey, bindStarToIdentifier).toBool();
    bindStarToTypeName = map.value(bindStarToTypeNameKey, bindStarToTypeName).toBool();
    bindStarToLeftSpecifier = map.value(bindStarToLeftSpecifierKey, bindStarToLeftSpecifier).toBool();
    bindStarToRightSpecifier = map.value(bindStarToRightSpecifierKey, bindStarToRightSpecifier).toBool();
    extraPaddingForConditionsIfConfusingAlign = map.value(extraPaddingForConditionsIfConfusingAlignKey,
                                extraPaddingForConditionsIfConfusingAlign).toBool();
    alignAssignments = map.value(alignAssignmentsKey, alignAssignments).toBool();
    preferGetterNameWithoutGetPrefix = map.value(shortGetterNameKey,
                                preferGetterNameWithoutGetPrefix).toBool();
}

bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
{
    return indentBlockBraces == rhs.indentBlockBraces
           && indentBlockBody == rhs.indentBlockBody
           && indentClassBraces == rhs.indentClassBraces
           && indentEnumBraces == rhs.indentEnumBraces
           && indentNamespaceBraces == rhs.indentNamespaceBraces
           && indentNamespaceBody == rhs.indentNamespaceBody
           && indentAccessSpecifiers == rhs.indentAccessSpecifiers
           && indentDeclarationsRelativeToAccessSpecifiers == rhs.indentDeclarationsRelativeToAccessSpecifiers
           && indentFunctionBody == rhs.indentFunctionBody
           && indentFunctionBraces == rhs.indentFunctionBraces
           && indentSwitchLabels == rhs.indentSwitchLabels
           && indentStatementsRelativeToSwitchLabels == rhs.indentStatementsRelativeToSwitchLabels
           && indentBlocksRelativeToSwitchLabels == rhs.indentBlocksRelativeToSwitchLabels
           && indentControlFlowRelativeToSwitchLabels == rhs.indentControlFlowRelativeToSwitchLabels
           && bindStarToIdentifier == rhs.bindStarToIdentifier
           && bindStarToTypeName == rhs.bindStarToTypeName
           && bindStarToLeftSpecifier == rhs.bindStarToLeftSpecifier
           && bindStarToRightSpecifier == rhs.bindStarToRightSpecifier
           && extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
           && alignAssignments == rhs.alignAssignments
           && statementMacros == rhs.statementMacros
           && preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix
#ifdef WITH_TESTS
           && forceFormatting == rhs.forceFormatting
#endif
           ;
}

CppCodeStyleSettings CppCodeStyleSettings::getProjectCodeStyle(ProjectExplorer::Project *project)
{
    if (!project)
        return currentGlobalCodeStyle();

    ProjectExplorer::EditorConfiguration *editorConfiguration = project->editorConfiguration();
    QTC_ASSERT(editorConfiguration, return currentGlobalCodeStyle());

    TextEditor::ICodeStylePreferences *codeStylePreferences
        = editorConfiguration->codeStyle(Constants::CPP_SETTINGS_ID);
    QTC_ASSERT(codeStylePreferences, return currentGlobalCodeStyle());

    auto cppCodeStylePreferences =
        dynamic_cast<const CppCodeStylePreferences *>(codeStylePreferences);
    if (!cppCodeStylePreferences)
        return currentGlobalCodeStyle();

    return cppCodeStylePreferences->currentCodeStyleSettings();
}

CppCodeStyleSettings CppCodeStyleSettings::currentProjectCodeStyle()
{
    return getProjectCodeStyle(ProjectExplorer::ProjectTree::currentProject());
}

CppCodeStyleSettings CppCodeStyleSettings::currentGlobalCodeStyle()
{
    CppCodeStylePreferences *cppCodeStylePreferences = CppToolsSettings::cppCodeStyle();
    QTC_ASSERT(cppCodeStylePreferences, return CppCodeStyleSettings());

    return cppCodeStylePreferences->currentCodeStyleSettings();
}

TextEditor::TabSettings CppCodeStyleSettings::getProjectTabSettings(ProjectExplorer::Project *project)
{
    if (!project)
        return currentGlobalTabSettings();

    ProjectExplorer::EditorConfiguration *editorConfiguration = project->editorConfiguration();
    QTC_ASSERT(editorConfiguration, return currentGlobalTabSettings());

    TextEditor::ICodeStylePreferences *codeStylePreferences
        = editorConfiguration->codeStyle(Constants::CPP_SETTINGS_ID);
    QTC_ASSERT(codeStylePreferences, return currentGlobalTabSettings());
    return codeStylePreferences->currentTabSettings();
}

TextEditor::TabSettings CppCodeStyleSettings::currentProjectTabSettings()
{
    return getProjectTabSettings(ProjectExplorer::ProjectTree::currentProject());
}

TextEditor::TabSettings CppCodeStyleSettings::currentGlobalTabSettings()
{
    CppCodeStylePreferences *cppCodeStylePreferences = CppToolsSettings::cppCodeStyle();
    QTC_ASSERT(cppCodeStylePreferences, return TextEditor::TabSettings());

    return cppCodeStylePreferences->currentTabSettings();
}


static void configureOverviewWithCodeStyleSettings(CPlusPlus::Overview &overview,
                                                   const CppCodeStyleSettings &settings)
{
    overview.starBindFlags = {};
    if (settings.bindStarToIdentifier)
        overview.starBindFlags |= CPlusPlus::Overview::BindToIdentifier;
    if (settings.bindStarToTypeName)
        overview.starBindFlags |= CPlusPlus::Overview::BindToTypeName;
    if (settings.bindStarToLeftSpecifier)
        overview.starBindFlags |= CPlusPlus::Overview::BindToLeftSpecifier;
    if (settings.bindStarToRightSpecifier)
        overview.starBindFlags |= CPlusPlus::Overview::BindToRightSpecifier;
}

CPlusPlus::Overview CppCodeStyleSettings::currentProjectCodeStyleOverview()
{
    CPlusPlus::Overview overview;
    const std::optional<CppCodeStyleSettings> codeStyleSettings = currentProjectCodeStyle();
    configureOverviewWithCodeStyleSettings(overview,
                                           codeStyleSettings.value_or(currentGlobalCodeStyle()));
    return overview;
}

CPlusPlus::Overview CppCodeStyleSettings::currentGlobalCodeStyleOverview()
{
    CPlusPlus::Overview overview;
    configureOverviewWithCodeStyleSettings(overview, currentGlobalCodeStyle());
    return overview;
}

} // namespace CppEditor