aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/quickfixes/converttocamelcase.cpp
blob: 5d799972971e7f60ca10b83d6045b0cdc84ae8d4 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "converttocamelcase.h"

#include "../cppeditortr.h"
#include "../cppeditorwidget.h"
#include "../cpprefactoringchanges.h"
#include "cppquickfix.h"

#ifdef WITH_TESTS
#include "cppquickfix_test.h"
#include <QtTest>
#endif

using namespace CPlusPlus;
using namespace Utils;

namespace CppEditor::Internal {
namespace {

class ConvertToCamelCaseOp: public CppQuickFixOperation
{
public:
    ConvertToCamelCaseOp(const CppQuickFixInterface &interface, const QString &name,
                         const AST *nameAst, bool test)
        : CppQuickFixOperation(interface, -1)
        , m_name(name)
        , m_nameAst(nameAst)
        , m_isAllUpper(name.isUpper())
        , m_test(test)
    {
        setDescription(Tr::tr("Convert to Camel Case"));
    }

    static bool isConvertibleUnderscore(const QString &name, int pos)
    {
        return name.at(pos) == QLatin1Char('_') && name.at(pos+1).isLetter()
               && !(pos == 1 && name.at(0) == QLatin1Char('m'));
    }

private:
    void perform() override
    {
        CppRefactoringChanges refactoring(snapshot());
        CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());

        QString newName = m_isAllUpper ? m_name.toLower() : m_name;
        for (int i = 1; i < newName.length(); ++i) {
            const QChar c = newName.at(i);
            if (c.isUpper() && m_isAllUpper) {
                newName[i] = c.toLower();
            } else if (i < newName.length() - 1 && isConvertibleUnderscore(newName, i)) {
                newName.remove(i, 1);
                newName[i] = newName.at(i).toUpper();
            }
        }
        if (m_test) {
            ChangeSet changeSet;
            changeSet.replace(currentFile->range(m_nameAst), newName);
            currentFile->setChangeSet(changeSet);
            currentFile->apply();
        } else {
            editor()->renameUsages(newName);
        }
    }

    const QString m_name;
    const AST * const m_nameAst;
    const bool m_isAllUpper;
    const bool m_test;
};

/*!
  Turns "an_example_symbol" into "anExampleSymbol" and
  "AN_EXAMPLE_SYMBOL" into "AnExampleSymbol".

  Activates on: identifiers
*/
class ConvertToCamelCase : public CppQuickFixFactory
{
public:
    ConvertToCamelCase(bool test = false) : m_test(test) {}

#ifdef WITH_TESTS
    static QObject *createTest();
#endif

private:
    void doMatch(const CppQuickFixInterface &interface, QuickFixOperations &result) override
    {
        const QList<AST *> &path = interface.path();

        if (path.isEmpty())
            return;

        AST * const ast = path.last();
        const Name *name = nullptr;
        const AST *astForName = nullptr;
        if (const NameAST * const nameAst = ast->asName()) {
            if (nameAst->name && nameAst->name->asNameId()) {
                astForName = nameAst;
                name = nameAst->name;
            }
        } else if (const NamespaceAST * const namespaceAst = ast->asNamespace()) {
            astForName = namespaceAst;
            name = namespaceAst->symbol->name();
        }

        if (!name)
            return;

        QString nameString = QString::fromUtf8(name->identifier()->chars());
        if (nameString.length() < 3)
            return;
        for (int i = 1; i < nameString.length() - 1; ++i) {
            if (ConvertToCamelCaseOp::isConvertibleUnderscore(nameString, i)) {
                result << new ConvertToCamelCaseOp(interface, nameString, astForName, m_test);
                return;
            }
        }
    }

    const bool m_test;
};

#ifdef WITH_TESTS
using namespace Tests;

class ConvertToCamelCaseTest : public QObject
{
    Q_OBJECT

private slots:
    void test_data()
    {
        QTest::addColumn<QByteArray>("original");
        QTest::addColumn<QByteArray>("expected");

        using QByteArray = QByteArray;

        QTest::newRow("convert to camel case: normal")
            << QByteArray("void @lower_case_function();\n")
            << QByteArray("void lowerCaseFunction();\n");
        QTest::newRow("convert to camel case: already camel case")
            << QByteArray("void @camelCaseFunction();\n")
            << QByteArray();
        QTest::newRow("convert to camel case: no underscores (lower case)")
            << QByteArray("void @lowercasefunction();\n")
            << QByteArray();
        QTest::newRow("convert to camel case: no underscores (upper case)")
            << QByteArray("void @UPPERCASEFUNCTION();\n")
            << QByteArray();
        QTest::newRow("convert to camel case: non-applicable underscore")
            << QByteArray("void @m_a_member;\n")
            << QByteArray("void m_aMember;\n");
        QTest::newRow("convert to camel case: upper case")
            << QByteArray("void @UPPER_CASE_FUNCTION();\n")
            << QByteArray("void upperCaseFunction();\n");
        QTest::newRow("convert to camel case: partially camel case already")
            << QByteArray("void mixed@_andCamelCase();\n")
            << QByteArray("void mixedAndCamelCase();\n");
        QTest::newRow("convert to camel case: wild mix")
            << QByteArray("void @WhAt_TODO_hErE();\n")
            << QByteArray("void WhAtTODOHErE();\n");
    }

    void test()
    {
        QFETCH(QByteArray, original);
        QFETCH(QByteArray, expected);
        ConvertToCamelCase factory(true);
        QuickFixOperationTest(singleDocument(original, expected), &factory);
    }
};

QObject *ConvertToCamelCase::createTest() { return new ConvertToCamelCaseTest; }

#endif // WITH_TESTS
} // namespace

void registerConvertToCamelCaseQuickfix()
{
    CppQuickFixFactory::registerFactory<ConvertToCamelCase>();
}

} // namespace CppEditor::Internal

#ifdef WITH_TESTS
#include <converttocamelcase.moc>
#endif