Commit 72e0ded3 authored by Andrey M. Tokarev's avatar Andrey M. Tokarev Committed by Nikolai Kosjar
Browse files

CppEditor: add more sections (protected, ...) for declaration (refactoring)



You can write definition of function, type Alt+Enter, as usual,
and select not only public but also other possible sections
like private, public slots and so on.

Change-Id: I2faefc3833c6f05c9e2e5a2a41328bcdbe17ba14
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent f0f406ba
......@@ -126,6 +126,8 @@ private slots:
void test_quickfix_InsertDefFromDecl_headerSource_namespace2();
void test_quickfix_InsertDefFromDecl_freeFunction();
void test_quickfix_InsertDeclFromDef();
void test_quickfix_AddIncludeForUndefinedIdentifier_normal();
void test_quickfix_AddIncludeForUndefinedIdentifier_noinclude();
void test_quickfix_AddIncludeForUndefinedIdentifier_noincludeComment01();
......
......@@ -129,10 +129,11 @@ struct TestCase
TestCase(const QList<TestDocumentPtr> theTestFiles);
~TestCase();
QuickFixOperation::Ptr getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget);
QuickFixOperation::Ptr getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget,
int resultIndex = 0);
TestDocumentPtr testFileWithCursorMarker() const;
void run(CppQuickFixFactory *factory);
void run(CppQuickFixFactory *factory, int resultIndex = 0);
private:
TestCase(const TestCase &);
......@@ -141,13 +142,14 @@ private:
void init();
};
/// Apply the factory on the source and get back the first result or a null pointer.
QuickFixOperation::Ptr TestCase::getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget)
/// Apply the factory on the source and get back the resultIndex'th result or a null pointer.
QuickFixOperation::Ptr TestCase::getFix(CppQuickFixFactory *factory, CPPEditorWidget *editorWidget,
int resultIndex)
{
CppQuickFixInterface qfi(new CppQuickFixAssistInterface(editorWidget, ExplicitlyInvoked));
TextEditor::QuickFixOperations results;
factory->match(qfi, results);
return results.isEmpty() ? QuickFixOperation::Ptr() : results.first();
return results.isEmpty() ? QuickFixOperation::Ptr() : results.at(resultIndex);
}
/// The '@' in the originalSource is the position from where the quick-fix discovery is triggered.
......@@ -271,7 +273,7 @@ QByteArray &removeTrailingWhitespace(QByteArray &input)
return input;
}
void TestCase::run(CppQuickFixFactory *factory)
void TestCase::run(CppQuickFixFactory *factory, int resultIndex)
{
// Run the fix in the file having the cursor marker
TestDocumentPtr testFile;
......@@ -281,7 +283,7 @@ void TestCase::run(CppQuickFixFactory *factory)
}
QVERIFY2(testFile, "No test file with cursor marker found");
if (QuickFixOperation::Ptr fix = getFix(factory, testFile->editorWidget))
if (QuickFixOperation::Ptr fix = getFix(factory, testFile->editorWidget, resultIndex))
fix->perform();
else
qDebug() << "Quickfix was not triggered";
......@@ -881,6 +883,55 @@ void CppPlugin::test_quickfix_InsertDefFromDecl_freeFunction()
data.run(&factory);
}
// Function for one of InsertDeclDef section cases
void insertToSectionDeclFromDef(const QByteArray &section, int sectionIndex)
{
QList<TestDocumentPtr> testFiles;
QByteArray original;
QByteArray expected;
// Header File
original =
"class Foo\n"
"{\n"
"};\n";
expected =
"class Foo\n"
"{\n"
+ section + ":\n" +
" Foo();\n"
"@};\n\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.h"));
// Source File
original =
"#include \"file.h\"\n"
"\n"
"Foo::Foo@()\n"
"{\n"
"}\n"
"\n"
;
expected = original + "\n";
testFiles << TestDocument::create(original, expected, QLatin1String("file.cpp"));
InsertDeclFromDef factory;
TestCase data(testFiles);
data.run(&factory, sectionIndex);
}
/// Check from source file: Insert in header file.
void CppPlugin::test_quickfix_InsertDeclFromDef()
{
insertToSectionDeclFromDef("public", 0);
insertToSectionDeclFromDef("public slots", 1);
insertToSectionDeclFromDef("protected", 2);
insertToSectionDeclFromDef("protected slots", 3);
insertToSectionDeclFromDef("private", 4);
insertToSectionDeclFromDef("private slots", 5);
}
/// Check normal add include if there is already a include
void CppPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_normal()
{
......
......@@ -2339,6 +2339,30 @@ private:
QString m_decl;
};
class DeclOperationFactory
{
public:
DeclOperationFactory(const CppQuickFixInterface &interface, const QString &fileName,
const Class *matchingClass, const QString &decl)
: m_interface(interface)
, m_fileName(fileName)
, m_matchingClass(matchingClass)
, m_decl(decl)
{}
TextEditor::QuickFixOperation::Ptr
operator()(InsertionPointLocator::AccessSpec xsSpec)
{
return TextEditor::QuickFixOperation::Ptr(
new InsertDeclOperation(m_interface, m_fileName, m_matchingClass, xsSpec, m_decl));
}
private:
const CppQuickFixInterface &m_interface;
const QString &m_fileName;
const Class *m_matchingClass;
const QString &m_decl;
};
} // anonymous namespace
void InsertDeclFromDef::match(const CppQuickFixInterface &interface, QuickFixOperations &result)
......@@ -2387,8 +2411,16 @@ void InsertDeclFromDef::match(const CppQuickFixInterface &interface, QuickFixOpe
QString fileName = QString::fromUtf8(matchingClass->fileName(),
matchingClass->fileNameLength());
const QString decl = InsertDeclOperation::generateDeclaration(fun);
result.append(QuickFixOperation::Ptr(new InsertDeclOperation(interface, fileName, matchingClass,
InsertionPointLocator::Public, decl)));
// Add several possible insertion locations for declaration
DeclOperationFactory operation(interface, fileName, matchingClass, decl);
result.append(operation(InsertionPointLocator::Public));
result.append(operation(InsertionPointLocator::PublicSlot));
result.append(operation(InsertionPointLocator::Protected));
result.append(operation(InsertionPointLocator::ProtectedSlot));
result.append(operation(InsertionPointLocator::Private));
result.append(operation(InsertionPointLocator::PrivateSlot));
}
}
......
......@@ -6210,7 +6210,7 @@ int BaseTextEditorWidget::lineNumberDigits() const
bool BaseTextEditorWidget::selectionVisible(int blockNumber) const
{
Q_UNUSED(blockNumber)
Q_UNUSED(blockNumber);
return true;
}
......
......@@ -118,11 +118,13 @@ struct ContentLessThan
} // Anonymous
BasicProposalItemListModel::BasicProposalItemListModel()
: m_isSortingAllowed(false)
{}
BasicProposalItemListModel::BasicProposalItemListModel(const QList<BasicProposalItem *> &items)
: m_currentItems(items)
, m_originalItems(items)
, m_isSortingAllowed(true)
{
mapPersistentIds();
}
......@@ -139,6 +141,16 @@ void BasicProposalItemListModel::loadContent(const QList<BasicProposalItem *> &i
mapPersistentIds();
}
void BasicProposalItemListModel::setSortingAllowed(bool isAllowed)
{
m_isSortingAllowed = isAllowed;
}
bool BasicProposalItemListModel::isSortingAllowed() const
{
return m_isSortingAllowed;
}
void BasicProposalItemListModel::mapPersistentIds()
{
for (int i = 0; i < m_originalItems.size(); ++i)
......@@ -258,6 +270,8 @@ bool BasicProposalItemListModel::isSortable(const QString &prefix) const
{
Q_UNUSED(prefix);
if (!isSortingAllowed())
return false;
if (m_currentItems.size() < kMaxSort)
return true;
return false;
......
......@@ -65,6 +65,8 @@ public:
virtual IAssistProposalItem *proposalItem(int index) const;
void loadContent(const QList<BasicProposalItem *> &items);
void setSortingAllowed(bool isAllowed);
bool isSortingAllowed() const;
protected:
typedef QList<BasicProposalItem *>::iterator ItemIterator;
......@@ -76,6 +78,7 @@ private:
QHash<QString, int> m_idByText;
QList<BasicProposalItem *> m_originalItems;
bool m_isSortingAllowed;
};
} // TextEditor
......
......@@ -40,6 +40,7 @@
#include <texteditor/basetexteditor.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/completionsettings.h>
#include <texteditor/codeassist/basicproposalitemlistmodel.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
......@@ -265,6 +266,12 @@ void CodeAssistantPrivate::requestProposal(AssistReason reason,
}
IAssistProposal *newProposal = processor->perform(assistInterface);
if (kind == QuickFix) {
TextEditor::BasicProposalItemListModel *proposalModel =
static_cast<TextEditor::BasicProposalItemListModel *>(newProposal->model());
proposalModel->setSortingAllowed(false);
}
displayProposal(newProposal, reason);
delete processor;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment