Commit 27cca2f3 authored by Amish Naidu's avatar Amish Naidu Committed by Kevin Funk

Don't add 'override' specifier for non-modern project settings

Summary:
Override code completion will only add the override specifier if the parser
settings don't have either -std=c++03 or 98.

Supersedes: D4039

BUG: 372280

Test Plan: Run `test_codecompletion testOverrideExecute`

Reviewers: kfunk

Reviewed By: kfunk

Subscribers: kfunk, kdevelop-devel

Tags: #kdevelop

Differential Revision: https://phabricator.kde.org/D16773

(cherry picked from commit 0636cfdd)
parent 7898bb41
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include <interfaces/icore.h> #include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h> #include <interfaces/idocumentcontroller.h>
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <language/duchain/duchainlock.h> #include <language/duchain/duchainlock.h>
#include <language/duchain/ducontext.h> #include <language/duchain/ducontext.h>
...@@ -45,6 +47,8 @@ ...@@ -45,6 +47,8 @@
#include <language/codecompletion/normaldeclarationcompletionitem.h> #include <language/codecompletion/normaldeclarationcompletionitem.h>
#include <util/foregroundlock.h> #include <util/foregroundlock.h>
#include <qtcompat_p.h> #include <qtcompat_p.h>
#include <custom-definesandincludes/idefinesandincludesmanager.h>
#include <project/projectmodel.h>
#include "../util/clangdebug.h" #include "../util/clangdebug.h"
#include "../util/clangtypes.h" #include "../util/clangtypes.h"
...@@ -170,7 +174,29 @@ public: ...@@ -170,7 +174,29 @@ public:
void execute(KTextEditor::View* view, const KTextEditor::Range& word) override void execute(KTextEditor::View* view, const KTextEditor::Range& word) override
{ {
view->document()->replaceText(word, m_returnType + QLatin1Char(' ') + m_display.replace(QRegularExpression(QStringLiteral("\\s*=\\s*0")), QString()) + QLatin1String(" override;")); QString replacement = m_returnType + QLatin1Char(' ') + m_display.replace(QRegularExpression(QStringLiteral("\\s*=\\s*0")), QString());
bool appendSpecifer = true;
if (const auto* project =
KDevelop::ICore::self()->projectController()->findProjectForUrl(view->document()->url())) {
const auto arguments = KDevelop::IDefinesAndIncludesManager::manager()->parserArguments(
project->filesForPath(IndexedString(view->document()->url().path())).first());
const auto match = QRegularExpression(QStringLiteral(R"(-std=c\+\+(\w+))")).match(arguments);
appendSpecifer = match.hasMatch(); // assume non-modern if no standard is specified
if (appendSpecifer) {
const auto standard = match.captured(1);
appendSpecifer = (standard != QLatin1String("98") && standard != QLatin1String("03"));
}
}
if (appendSpecifer) {
replacement.append(QLatin1String(" override;"));
} else {
replacement.append(QLatin1Char(';'));
}
view->document()->replaceText(word, replacement);
} }
private: private:
......
...@@ -613,6 +613,71 @@ void TestCodeCompletion::testVirtualOverride_data() ...@@ -613,6 +613,71 @@ void TestCodeCompletion::testVirtualOverride_data()
)" << CompletionItems{{8, 14}, {}}; )" << CompletionItems{{8, 14}, {}};
} }
void TestCodeCompletion::testOverrideExecute()
{
QFETCH(QString, code);
QFETCH(CompletionItems, expectedItems);
QFETCH(QString, itemToExecute);
QFETCH(QString, cppStandard);
QFETCH(QString, expectedCode);
QTemporaryDir directory;
TestProject testProject {Path{directory.path()}};
auto t = testProject.path().toLocalFile();
auto configGroup = testProject.projectConfiguration()->group("CustomDefinesAndIncludes").group("ProjectPath0");
configGroup.writeEntry("Path", ".");
configGroup.writeEntry("parserArguments", cppStandard);
configGroup.sync();
m_projectController->addProject(&testProject);
TestFile file(code, QStringLiteral("cpp"), &testProject, directory.path());
QVERIFY(file.parseAndWait(TopDUContext::AllDeclarationsContextsUsesAndAST));
auto executeItem = [=] (const ClangCodeCompletionItemTester& tester) {
auto item = tester.findItem(itemToExecute);
QVERIFY(item);
auto view = createView(tester.completionContext->duContext()->url().toUrl(), this);
item->execute(view.get(), view->document()->wordRangeAt(expectedItems.position));
QCOMPARE(view->document()->text(), expectedCode);
};
executeCompletionTest(file.topContext(), expectedItems, NoMacroOrBuiltin, executeItem);
m_projectController->closeProject(&testProject);
}
void TestCodeCompletion::testOverrideExecute_data()
{
QTest::addColumn<QString>("code");
QTest::addColumn<CompletionItems>("expectedItems");
QTest::addColumn<QString>("itemToExecute");
QTest::addColumn<QString>("cppStandard");
QTest::addColumn<QString>("expectedCode");
QTest::newRow("override-modern")
<< "class Foo { virtual int bar(char c); };\nclass Baz : Foo\n{\n};"
<< CompletionItems{{3, 0}, {"Baz", "Foo", "bar(char c)"}}
<< "bar(char c)"
<< "-std=c++11"
<< "class Foo { virtual int bar(char c); };\n"
"class Baz : Foo\n{\nint bar(char c) override;};";
QTest::newRow("override-non-modern")
<< "class Foo { virtual int bar(char c); };\nclass Baz : Foo\n{\n};"
<< CompletionItems{{3, 0}, {"Baz", "Foo", "bar(char c)"}}
<< "bar(char c)"
<< "-std=c++98"
<< "class Foo { virtual int bar(char c); };\n"
"class Baz : Foo\n{\nint bar(char c);};";
QTest::newRow("override-unknown")
<< "class Foo { virtual int bar(char c); };\nclass Baz : Foo\n{\n};"
<< CompletionItems{{3, 0}, {"Baz", "Foo", "bar(char c)"}}
<< "bar(char c)"
<< "-std=c++1z"
<< "class Foo { virtual int bar(char c); };\n"
"class Baz : Foo\n{\nint bar(char c) override;};";
}
void TestCodeCompletion::testImplement() void TestCodeCompletion::testImplement()
{ {
QFETCH(QString, code); QFETCH(QString, code);
......
...@@ -37,6 +37,8 @@ private Q_SLOTS: ...@@ -37,6 +37,8 @@ private Q_SLOTS:
void testClangCodeCompletion_data(); void testClangCodeCompletion_data();
void testVirtualOverride(); void testVirtualOverride();
void testVirtualOverride_data(); void testVirtualOverride_data();
void testOverrideExecute();
void testOverrideExecute_data();
void testImplement(); void testImplement();
void testImplement_data(); void testImplement_data();
void testImplementOtherFile(); void testImplementOtherFile();
......
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