CppEditor: quick fix to generate getters and setters

This does not take namespaces on the implementation side
into account and does not properly position/indent things.

Task-number: QTCREATORBUG-1890
Change-Id: I779d12fefc79521bce38361729d4f66dada71147
Reviewed-by: hjk <hjk121@nokiamail.com>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
This commit is contained in:
Erik Verbruggen
2013-01-23 15:41:15 +01:00
parent 794b41581b
commit 72db7a8f52
10 changed files with 462 additions and 26 deletions

View File

@@ -63,7 +63,7 @@ using namespace CppTools::Internal;
using namespace TextEditor;
using namespace Core;
struct TestData
struct TestCase
{
QByteArray srcText;
int pos;
@@ -72,7 +72,7 @@ struct TestData
QTextDocument *doc;
};
static QStringList getCompletions(TestData &data)
static QStringList getCompletions(TestCase &data)
{
QStringList completions;
@@ -96,7 +96,7 @@ static QStringList getCompletions(TestData &data)
return completions;
}
static void setup(TestData *data)
static void setup(TestCase *data)
{
data->pos = data->srcText.indexOf('@');
QVERIFY(data->pos != -1);
@@ -120,7 +120,7 @@ static void setup(TestData *data)
void CppToolsPlugin::test_completion_forward_declarations_present()
{
TestData data;
TestCase data;
data.srcText = "\n"
"class Foo\n"
"{\n"
@@ -154,7 +154,7 @@ void CppToolsPlugin::test_completion_forward_declarations_present()
void CppToolsPlugin::test_completion_inside_parentheses_c_style_conversion()
{
TestData data;
TestCase data;
data.srcText = "\n"
"class Base\n"
"{\n"
@@ -195,7 +195,7 @@ void CppToolsPlugin::test_completion_inside_parentheses_c_style_conversion()
void CppToolsPlugin::test_completion_inside_parentheses_cast_operator_conversion()
{
TestData data;
TestCase data;
data.srcText = "\n"
"class Base\n"
"{\n"
@@ -235,7 +235,7 @@ void CppToolsPlugin::test_completion_inside_parentheses_cast_operator_conversion
void CppToolsPlugin::test_completion_basic_1()
{
TestData data;
TestCase data;
data.srcText = "\n"
"class Foo\n"
"{\n"
@@ -275,7 +275,7 @@ void CppToolsPlugin::test_completion_basic_1()
void CppToolsPlugin::test_completion_template_1()
{
TestData data;
TestCase data;
data.srcText = "\n"
"template <class T>\n"
"class Foo\n"
@@ -311,7 +311,7 @@ void CppToolsPlugin::test_completion_template_1()
void CppToolsPlugin::test_completion_template_2()
{
TestData data;
TestCase data;
data.srcText = "\n"
"template <class T>\n"
"struct List\n"
@@ -346,7 +346,7 @@ void CppToolsPlugin::test_completion_template_2()
void CppToolsPlugin::test_completion_template_3()
{
TestData data;
TestCase data;
data.srcText = "\n"
"template <class T>\n"
"struct List\n"
@@ -381,7 +381,7 @@ void CppToolsPlugin::test_completion_template_3()
void CppToolsPlugin::test_completion_template_4()
{
TestData data;
TestCase data;
data.srcText = "\n"
"template <class T>\n"
"struct List\n"
@@ -417,7 +417,7 @@ void CppToolsPlugin::test_completion_template_4()
void CppToolsPlugin::test_completion_template_5()
{
TestData data;
TestCase data;
data.srcText = "\n"
"template <class T>\n"
"struct List\n"
@@ -453,7 +453,7 @@ void CppToolsPlugin::test_completion_template_5()
void CppToolsPlugin::test_completion_template_6()
{
TestData data;
TestCase data;
data.srcText = "\n"
"class Item\n"
"{\n"
@@ -493,7 +493,7 @@ void CppToolsPlugin::test_completion_template_6()
void CppToolsPlugin::test_completion_template_7()
{
TestData data;
TestCase data;
data.srcText = "\n"
"struct Test\n"
"{\n"
@@ -535,7 +535,7 @@ void CppToolsPlugin::test_completion_template_7()
void CppToolsPlugin::test_completion_type_of_pointer_is_typedef()
{
TestData data;
TestCase data;
data.srcText = "\n"
"typedef struct Foo\n"
"{\n"
@@ -566,7 +566,7 @@ void CppToolsPlugin::test_completion()
QFETCH(QByteArray, code);
QFETCH(QStringList, expectedCompletions);
TestData data;
TestCase data;
data.srcText = code;
setup(&data);
@@ -1207,7 +1207,7 @@ void CppToolsPlugin::test_completion_enclosing_template_class_data()
void CppToolsPlugin::test_completion_instantiate_nested_class_when_enclosing_is_template()
{
TestData data;
TestCase data;
data.srcText = "\n"
"struct Foo \n"
"{\n"
@@ -1247,7 +1247,7 @@ void CppToolsPlugin::test_completion_instantiate_nested_class_when_enclosing_is_
void CppToolsPlugin::test_completion_instantiate_nested_of_nested_class_when_enclosing_is_template()
{
TestData data;
TestCase data;
data.srcText = "\n"
"struct Foo \n"
"{\n"

View File

@@ -95,18 +95,27 @@ struct AccessRange
unsigned start;
unsigned end;
InsertionPointLocator::AccessSpec xsSpec;
unsigned colonToken;
AccessRange()
: start(0)
, end(0)
, xsSpec(InsertionPointLocator::Invalid)
, colonToken(0)
{}
AccessRange(unsigned start, unsigned end, InsertionPointLocator::AccessSpec xsSpec)
AccessRange(unsigned start, unsigned end, InsertionPointLocator::AccessSpec xsSpec, unsigned colonToken)
: start(start)
, end(end)
, xsSpec(xsSpec)
, colonToken(colonToken)
{}
bool isEmpty() const
{
unsigned contentStart = 1 + (colonToken ? colonToken : start);
return contentStart == end;
}
};
class FindInClass: public ASTVisitor
@@ -146,16 +155,19 @@ protected:
ast->rbrace_token);
unsigned beforeToken = 0;
bool needsLeadingEmptyLine = false;
bool needsPrefix = false;
bool needsSuffix = false;
findMatch(ranges, _xsSpec, beforeToken, needsPrefix, needsSuffix);
findMatch(ranges, _xsSpec, beforeToken, needsLeadingEmptyLine, needsPrefix, needsSuffix);
unsigned line = 0, column = 0;
getTokenStartPosition(beforeToken, &line, &column);
QString prefix;
if (needsLeadingEmptyLine)
prefix += QLatin1String("\n");
if (needsPrefix)
prefix = generate(_xsSpec);
prefix += generate(_xsSpec);
QString suffix;
if (needsSuffix)
@@ -169,12 +181,15 @@ protected:
static void findMatch(const QList<AccessRange> &ranges,
InsertionPointLocator::AccessSpec xsSpec,
unsigned &beforeToken,
bool &needsLeadingEmptyLine,
bool &needsPrefix,
bool &needsSuffix)
{
QTC_ASSERT(!ranges.isEmpty(), return);
const int lastIndex = ranges.size() - 1;
needsLeadingEmptyLine = false;
// try an exact match, and ignore the first (default) access spec:
for (int i = lastIndex; i > 0; --i) {
const AccessRange &range = ranges.at(i);
@@ -200,6 +215,7 @@ protected:
// otherwise:
beforeToken = ranges.first().end;
needsLeadingEmptyLine = !ranges.first().isEmpty();
needsPrefix = true;
needsSuffix = (ranges.size() != 1);
}
@@ -210,14 +226,14 @@ protected:
int lastRangeEnd) const
{
QList<AccessRange> ranges;
ranges.append(AccessRange(firstRangeStart, lastRangeEnd, initialXs));
ranges.append(AccessRange(firstRangeStart, lastRangeEnd, initialXs, 0));
for (DeclarationListAST *iter = decls; iter; iter = iter->next) {
DeclarationAST *decl = iter->value;
if (AccessDeclarationAST *xsDecl = decl->asAccessDeclaration()) {
const unsigned token = xsDecl->access_specifier_token;
int newXsSpec = initialXs;
InsertionPointLocator::AccessSpec newXsSpec = initialXs;
bool isSlot = xsDecl->slots_token
&& tokenKind(xsDecl->slots_token) == T_Q_SLOTS;
@@ -242,7 +258,8 @@ protected:
break;
case T_Q_SLOTS: {
newXsSpec = ranges.last().xsSpec | InsertionPointLocator::SlotBit;
newXsSpec = (InsertionPointLocator::AccessSpec)
(ranges.last().xsSpec | InsertionPointLocator::SlotBit);
break;
}
@@ -250,9 +267,10 @@ protected:
break;
}
if (newXsSpec != ranges.last().xsSpec) {
if (newXsSpec != ranges.last().xsSpec || ranges.size() == 1) {
ranges.last().end = token;
ranges.append(AccessRange(token, lastRangeEnd, (InsertionPointLocator::AccessSpec) newXsSpec));
AccessRange r(token, lastRangeEnd, newXsSpec, xsDecl->colon_token);
ranges.append(r);
}
}
}