CppTools: Allow prefering getter names with "get" prefix

We default to "foo()" for e.g. a member variable "m_foo", but other
coding styles require "getFoo()".

Task-number: QTCREATORBUG-16452
Change-Id: I9ccfdf88e4c469bc1c06fde855ad754faf2bd238
Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
Nikolai Kosjar
2016-06-21 11:16:39 +02:00
parent b928a1ce46
commit 1ea6404337
7 changed files with 156 additions and 27 deletions

View File

@@ -117,6 +117,7 @@ private slots:
void test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAndNamespaceToCpp(); void test_quickfix_GenerateGetterSetter_basicGetterWithPrefixAndNamespaceToCpp();
void test_quickfix_GenerateGetterSetter_onlyGetter(); void test_quickfix_GenerateGetterSetter_onlyGetter();
void test_quickfix_GenerateGetterSetter_onlyGetter_DontPreferGetterWithGet();
void test_quickfix_GenerateGetterSetter_onlySetter(); void test_quickfix_GenerateGetterSetter_onlySetter();
void test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent(); void test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent();
void test_quickfix_GenerateGetterSetter_offerSetterWhenGetterPresent(); void test_quickfix_GenerateGetterSetter_offerSetterWhenGetterPresent();

View File

@@ -1890,6 +1890,85 @@ void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlySetter()
QuickFixOperationTest(testDocuments, &factory, ProjectPartHeaderPaths(), 2); QuickFixOperationTest(testDocuments, &factory, ProjectPartHeaderPaths(), 2);
} }
class CppCodeStyleSettingsChanger {
public:
CppCodeStyleSettingsChanger(const CppCodeStyleSettings &settings);
~CppCodeStyleSettingsChanger(); // Restore original
static CppCodeStyleSettings currentSettings();
private:
void setSettings(const CppCodeStyleSettings &settings);
CppCodeStyleSettings m_originalSettings;
};
CppCodeStyleSettingsChanger::CppCodeStyleSettingsChanger(const CppCodeStyleSettings &settings)
{
m_originalSettings = currentSettings();
setSettings(settings);
}
CppCodeStyleSettingsChanger::~CppCodeStyleSettingsChanger()
{
setSettings(m_originalSettings);
}
void CppCodeStyleSettingsChanger::setSettings(const CppCodeStyleSettings &settings)
{
QVariant variant;
variant.setValue(settings);
CppCodeStylePreferences *preferences = CppToolsSettings::instance()->cppCodeStyle();
preferences->currentDelegate()->setValue(variant);
}
CppCodeStyleSettings CppCodeStyleSettingsChanger::currentSettings()
{
CppCodeStylePreferences *preferences = CppToolsSettings::instance()->cppCodeStyle();
return preferences->currentDelegate()->value().value<CppCodeStyleSettings>();
}
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_onlyGetter_DontPreferGetterWithGet()
{
CppCodeStyleSettings modifiedSettings = CppCodeStyleSettingsChanger::currentSettings();
modifiedSettings.preferGetterNameWithoutGetPrefix = false;
CppCodeStyleSettingsChanger changer(modifiedSettings);
QList<QuickFixTestDocument::Ptr> testDocuments;
QByteArray original;
QByteArray expected;
// Header File
original =
"class Foo\n"
"{\n"
"public:\n"
" int m_bar@;\n"
"};\n";
expected =
"class Foo\n"
"{\n"
"public:\n"
" int m_bar@;\n"
" int getBar() const;\n"
"};\n";
testDocuments << QuickFixTestDocument::create("file.h", original, expected);
// Source File
original.resize(0);
expected =
"\n"
"int Foo::getBar() const\n"
"{\n"
" return m_bar;\n"
"}\n";
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
GenerateGetterSetter factory;
QuickFixOperationTest(testDocuments, &factory, ProjectPartHeaderPaths(), 1);
}
/// Checks: Offer a "generate getter" quick fix if there is a setter /// Checks: Offer a "generate getter" quick fix if there is a setter
void CppEditorPlugin::test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent() void CppEditorPlugin::test_quickfix_GenerateGetterSetter_offerGetterWhenSetterPresent()
{ {

View File

@@ -5967,9 +5967,10 @@ void GenerateGetterSetterOperation::determineGetterSetterNames()
m_baseName = QLatin1String("value"); m_baseName = QLatin1String("value");
// Getter Name // Getter Name
const CppCodeStyleSettings settings = CppCodeStyleSettings::currentProjectCodeStyle();
const bool hasValidBaseName = m_baseName != m_variableString; const bool hasValidBaseName = m_baseName != m_variableString;
const bool getPrefixIsAlreadyUsed = hasClassMemberWithGetPrefix(m_classSpecifier->symbol); const bool getPrefixIsAlreadyUsed = hasClassMemberWithGetPrefix(m_classSpecifier->symbol);
if (hasValidBaseName && !getPrefixIsAlreadyUsed) { if (settings.preferGetterNameWithoutGetPrefix && hasValidBaseName && !getPrefixIsAlreadyUsed) {
m_getterName = m_baseName; m_getterName = m_baseName;
} else { } else {
const QString baseNameWithCapital = m_baseName.left(1).toUpper() + m_baseName.mid(1); const QString baseNameWithCapital = m_baseName.left(1).toUpper() + m_baseName.mid(1);

View File

@@ -59,6 +59,7 @@ static const char bindStarToLeftSpecifierKey[] = "BindStarToLeftSpecifier";
static const char bindStarToRightSpecifierKey[] = "BindStarToRightSpecifier"; static const char bindStarToRightSpecifierKey[] = "BindStarToRightSpecifier";
static const char extraPaddingForConditionsIfConfusingAlignKey[] = "ExtraPaddingForConditionsIfConfusingAlign"; static const char extraPaddingForConditionsIfConfusingAlignKey[] = "ExtraPaddingForConditionsIfConfusingAlign";
static const char alignAssignmentsKey[] = "AlignAssignments"; static const char alignAssignmentsKey[] = "AlignAssignments";
static const char shortGetterNameKey[] = "ShortGetterName";
using namespace CppTools; using namespace CppTools;
@@ -85,6 +86,7 @@ CppCodeStyleSettings::CppCodeStyleSettings() :
, bindStarToRightSpecifier(false) , bindStarToRightSpecifier(false)
, extraPaddingForConditionsIfConfusingAlign(true) , extraPaddingForConditionsIfConfusingAlign(true)
, alignAssignments(false) , alignAssignments(false)
, preferGetterNameWithoutGetPrefix(true)
{ {
} }
@@ -121,6 +123,7 @@ void CppCodeStyleSettings::toMap(const QString &prefix, QVariantMap *map) const
map->insert(prefix + QLatin1String(bindStarToRightSpecifierKey), bindStarToRightSpecifier); map->insert(prefix + QLatin1String(bindStarToRightSpecifierKey), bindStarToRightSpecifier);
map->insert(prefix + QLatin1String(extraPaddingForConditionsIfConfusingAlignKey), extraPaddingForConditionsIfConfusingAlign); map->insert(prefix + QLatin1String(extraPaddingForConditionsIfConfusingAlignKey), extraPaddingForConditionsIfConfusingAlign);
map->insert(prefix + QLatin1String(alignAssignmentsKey), alignAssignments); map->insert(prefix + QLatin1String(alignAssignmentsKey), alignAssignments);
map->insert(prefix + QLatin1String(shortGetterNameKey), preferGetterNameWithoutGetPrefix);
} }
void CppCodeStyleSettings::fromMap(const QString &prefix, const QVariantMap &map) void CppCodeStyleSettings::fromMap(const QString &prefix, const QVariantMap &map)
@@ -165,6 +168,8 @@ void CppCodeStyleSettings::fromMap(const QString &prefix, const QVariantMap &map
extraPaddingForConditionsIfConfusingAlign).toBool(); extraPaddingForConditionsIfConfusingAlign).toBool();
alignAssignments = map.value(prefix + QLatin1String(alignAssignmentsKey), alignAssignments = map.value(prefix + QLatin1String(alignAssignmentsKey),
alignAssignments).toBool(); alignAssignments).toBool();
preferGetterNameWithoutGetPrefix = map.value(prefix + QLatin1String(shortGetterNameKey),
preferGetterNameWithoutGetPrefix).toBool();
} }
bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
@@ -188,7 +193,37 @@ bool CppCodeStyleSettings::equals(const CppCodeStyleSettings &rhs) const
&& bindStarToLeftSpecifier == rhs.bindStarToLeftSpecifier && bindStarToLeftSpecifier == rhs.bindStarToLeftSpecifier
&& bindStarToRightSpecifier == rhs.bindStarToRightSpecifier && bindStarToRightSpecifier == rhs.bindStarToRightSpecifier
&& extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign && extraPaddingForConditionsIfConfusingAlign == rhs.extraPaddingForConditionsIfConfusingAlign
&& alignAssignments == rhs.alignAssignments; && alignAssignments == rhs.alignAssignments
&& preferGetterNameWithoutGetPrefix == rhs.preferGetterNameWithoutGetPrefix
;
}
CppCodeStyleSettings CppCodeStyleSettings::currentProjectCodeStyle()
{
ProjectExplorer::Project *project = ProjectExplorer::ProjectTree::currentProject();
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());
CppCodeStylePreferences *cppCodeStylePreferences
= dynamic_cast<CppCodeStylePreferences *>(codeStylePreferences);
QTC_ASSERT(cppCodeStylePreferences, return currentGlobalCodeStyle());
return cppCodeStylePreferences->currentCodeStyleSettings();
}
CppCodeStyleSettings CppCodeStyleSettings::currentGlobalCodeStyle()
{
CppCodeStylePreferences *cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
QTC_ASSERT(cppCodeStylePreferences, return CppCodeStyleSettings());
return cppCodeStylePreferences->currentCodeStyleSettings();
} }
static void configureOverviewWithCodeStyleSettings(CPlusPlus::Overview &overview, static void configureOverviewWithCodeStyleSettings(CPlusPlus::Overview &overview,
@@ -207,37 +242,14 @@ static void configureOverviewWithCodeStyleSettings(CPlusPlus::Overview &overview
CPlusPlus::Overview CppCodeStyleSettings::currentProjectCodeStyleOverview() CPlusPlus::Overview CppCodeStyleSettings::currentProjectCodeStyleOverview()
{ {
ProjectExplorer::Project *project = ProjectExplorer::ProjectTree::currentProject();
if (!project)
return currentGlobalCodeStyleOverview();
ProjectExplorer::EditorConfiguration *editorConfiguration = project->editorConfiguration();
QTC_ASSERT(editorConfiguration, return currentGlobalCodeStyleOverview());
TextEditor::ICodeStylePreferences *codeStylePreferences
= editorConfiguration->codeStyle(Constants::CPP_SETTINGS_ID);
QTC_ASSERT(codeStylePreferences, return currentGlobalCodeStyleOverview());
CppCodeStylePreferences *cppCodeStylePreferences
= dynamic_cast<CppCodeStylePreferences *>(codeStylePreferences);
QTC_ASSERT(cppCodeStylePreferences, return currentGlobalCodeStyleOverview());
CppCodeStyleSettings settings = cppCodeStylePreferences->currentCodeStyleSettings();
CPlusPlus::Overview overview; CPlusPlus::Overview overview;
configureOverviewWithCodeStyleSettings(overview, settings); configureOverviewWithCodeStyleSettings(overview, currentProjectCodeStyle());
return overview; return overview;
} }
CPlusPlus::Overview CppCodeStyleSettings::currentGlobalCodeStyleOverview() CPlusPlus::Overview CppCodeStyleSettings::currentGlobalCodeStyleOverview()
{ {
CPlusPlus::Overview overview; CPlusPlus::Overview overview;
configureOverviewWithCodeStyleSettings(overview, currentGlobalCodeStyle());
CppCodeStylePreferences *cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
QTC_ASSERT(cppCodeStylePreferences, return overview);
CppCodeStyleSettings settings = cppCodeStylePreferences->currentCodeStyleSettings();
configureOverviewWithCodeStyleSettings(overview, settings);
return overview; return overview;
} }

View File

@@ -80,6 +80,8 @@ public:
// b // b
bool alignAssignments; bool alignAssignments;
bool preferGetterNameWithoutGetPrefix;
void toSettings(const QString &category, QSettings *s) const; void toSettings(const QString &category, QSettings *s) const;
void fromSettings(const QString &category, const QSettings *s); void fromSettings(const QString &category, const QSettings *s);
@@ -90,6 +92,9 @@ public:
bool operator==(const CppCodeStyleSettings &s) const { return equals(s); } bool operator==(const CppCodeStyleSettings &s) const { return equals(s); }
bool operator!=(const CppCodeStyleSettings &s) const { return !equals(s); } bool operator!=(const CppCodeStyleSettings &s) const { return !equals(s); }
static CppCodeStyleSettings currentProjectCodeStyle();
static CppCodeStyleSettings currentGlobalCodeStyle();
/*! Returns an Overview configured by the current project's code style. /*! Returns an Overview configured by the current project's code style.
If no current project is available or an error occurs when getting the If no current project is available or an error occurs when getting the

View File

@@ -321,6 +321,8 @@ CppCodeStylePreferencesWidget::CppCodeStylePreferencesWidget(QWidget *parent)
this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
connect(m_ui->bindStarToRightSpecifier, &QCheckBox::toggled, connect(m_ui->bindStarToRightSpecifier, &QCheckBox::toggled,
this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged); this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
connect(m_ui->preferGetterNamesWithoutGet, &QCheckBox::toggled,
this, &CppCodeStylePreferencesWidget::slotCodeStyleSettingsChanged);
m_ui->categoryTab->setCurrentIndex(0); m_ui->categoryTab->setCurrentIndex(0);
@@ -380,6 +382,7 @@ CppCodeStyleSettings CppCodeStylePreferencesWidget::cppCodeStyleSettings() const
set.bindStarToRightSpecifier = m_ui->bindStarToRightSpecifier->isChecked(); set.bindStarToRightSpecifier = m_ui->bindStarToRightSpecifier->isChecked();
set.extraPaddingForConditionsIfConfusingAlign = m_ui->extraPaddingConditions->isChecked(); set.extraPaddingForConditionsIfConfusingAlign = m_ui->extraPaddingConditions->isChecked();
set.alignAssignments = m_ui->alignAssignments->isChecked(); set.alignAssignments = m_ui->alignAssignments->isChecked();
set.preferGetterNameWithoutGetPrefix = m_ui->preferGetterNamesWithoutGet->isChecked();
return set; return set;
} }
@@ -413,6 +416,7 @@ void CppCodeStylePreferencesWidget::setCodeStyleSettings(const CppCodeStyleSetti
m_ui->bindStarToRightSpecifier->setChecked(s.bindStarToRightSpecifier); m_ui->bindStarToRightSpecifier->setChecked(s.bindStarToRightSpecifier);
m_ui->extraPaddingConditions->setChecked(s.extraPaddingForConditionsIfConfusingAlign); m_ui->extraPaddingConditions->setChecked(s.extraPaddingForConditionsIfConfusingAlign);
m_ui->alignAssignments->setChecked(s.alignAssignments); m_ui->alignAssignments->setChecked(s.alignAssignments);
m_ui->preferGetterNamesWithoutGet->setChecked(s.preferGetterNameWithoutGetPrefix);
m_blockUpdates = wasBlocked; m_blockUpdates = wasBlocked;
if (preview) if (preview)
updatePreview(); updatePreview();

View File

@@ -484,6 +484,33 @@ if they would align to the next line</string>
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="getterSetterTab">
<attribute name="title">
<string>Getter and Setter</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_7">
<item>
<widget class="QCheckBox" name="preferGetterNamesWithoutGet">
<property name="text">
<string>Prefer getter names without &quot;get&quot;</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_7">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
</layout> </layout>