Commit 8180f695 authored by Nikolai Kosjar's avatar Nikolai Kosjar
Browse files

CppEditor: Check <include path>/QSomething



For the quick fix AddIncludeForUndefinedIdentifier, if no class is found
via the locator, check the "Qt include paths" for a header file with the
same name as the class name.

Task-number: QTCREATORBUG-9538
Change-Id: I13c86844c2ff653fa479dc91eb109af2a6d76fae
Reviewed-by: default avatarLorenz Haas <lykurg@gmail.com>
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent 18dcac7a
......@@ -184,6 +184,7 @@ private slots:
void test_quickfix_AddIncludeForUndefinedIdentifier_noinclude();
void test_quickfix_AddIncludeForUndefinedIdentifier_veryFirstIncludeCppStyleCommentOnTop();
void test_quickfix_AddIncludeForUndefinedIdentifier_veryFirstIncludeCStyleCommentOnTop();
void test_quickfix_AddIncludeForUndefinedIdentifier_checkQSomethingInQtIncludePaths();
void test_quickfix_MoveFuncDefOutside_MemberFuncToCpp();
void test_quickfix_MoveFuncDefOutside_MemberFuncOutside1();
......
......@@ -344,18 +344,25 @@ private:
const QString m_include;
};
QString includeBaseDirectory()
{
return QLatin1String(SRCDIR)
+ QLatin1String("/../../../tests/auto/cplusplus/preprocessor/data/include-data");
}
QString globalQtCoreIncludePath()
{
return QDir::cleanPath(includeBaseDirectory() + QLatin1String("/QtCore"));
}
QString globalIncludePath()
{
const QString path = QLatin1String(SRCDIR)
+ QLatin1String("/../../../tests/auto/cplusplus/preprocessor/data/include-data/global");
return QDir::cleanPath(path);
return QDir::cleanPath(includeBaseDirectory() + QLatin1String("/global"));
}
QString directoryOfTestFile()
{
const QString path = QLatin1String(SRCDIR)
+ QLatin1String("/../../../tests/auto/cplusplus/preprocessor/data/include-data/local");
return QDir::cleanPath(path);
return QDir::cleanPath(includeBaseDirectory() + QLatin1String("/local"));
}
} // anonymous namespace
......@@ -1741,6 +1748,32 @@ void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_veryFirstIn
data.run(&factory);
}
/// Check: If a "Qt Class" was not found by the locator, check the header files in the Qt
/// include paths
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_checkQSomethingInQtIncludePaths()
{
QList<TestDocumentPtr> testFiles;
QByteArray original;
QByteArray expected;
original =
"@QDir dir;\n"
;
expected =
"#include <QDir>\n"
"\n"
"QDir dir;\n"
"\n"
;
testFiles << TestDocument::create(original, expected, directoryOfTestFile() + QLatin1Char('/')
+ QLatin1String("file.cpp"));
AddIncludeForUndefinedIdentifier factory;
TestCase data(testFiles, QStringList(globalQtCoreIncludePath()));
data.run(&factory);
}
/// Check: Move definition from header to cpp.
void CppEditorPlugin::test_quickfix_MoveFuncDefOutside_MemberFuncToCpp()
{
......
......@@ -1856,10 +1856,7 @@ void AddIncludeForUndefinedIdentifier::match(const CppQuickFixInterface &interfa
}
if (!inProject) {
// better use all include paths than none
foreach (const CppModelManagerInterface::ProjectInfo &info, projectInfos) {
foreach (ProjectPart::Ptr part, info.projectParts())
includePaths += part->includePaths;
}
includePaths = modelManager->includePaths();
}
// find a include file through the locator
......@@ -1897,14 +1894,34 @@ void AddIncludeForUndefinedIdentifier::match(const CppQuickFixInterface &interfa
}
}
// for QSomething, propose a <QSomething> include -- if such a class was in the locator
if (classExists
&& className.size() > 2
const bool isProbablyAQtClass = className.size() > 2
&& className.at(0) == QLatin1Char('Q')
&& className.at(1).isUpper()) {
&& className.at(1).isUpper();
if (!isProbablyAQtClass)
return;
// for QSomething, propose a <QSomething> include -- if such a class was in the locator
if (classExists) {
const QString include = QLatin1Char('<') + className + QLatin1Char('>');
result += CppQuickFixOperation::Ptr(
new AddIncludeForUndefinedIdentifierOp(interface, 1, include));
new AddIncludeForUndefinedIdentifierOp(interface, 1, include));
// otherwise, check for a header file with the same name in the Qt include paths
} else {
foreach (const QString &includePath, includePaths) {
if (!includePath.contains(QLatin1String("/Qt"))) // "QtCore", "QtGui" etc...
continue;
const QString headerPathCandidate = includePath + QLatin1Char('/') + className;
const QFileInfo fileInfo(headerPathCandidate);
if (fileInfo.exists() && fileInfo.isFile()) {
const QString include = QLatin1Char('<') + className + QLatin1Char('>');
result += CppQuickFixOperation::Ptr(
new AddIncludeForUndefinedIdentifierOp(interface, 1, include));
break;
}
}
}
}
......
......@@ -212,6 +212,8 @@ public:
virtual void updateProjectInfo(const ProjectInfo &pinfo) = 0;
virtual QList<ProjectPart::Ptr> projectPart(const QString &fileName) const = 0;
virtual QStringList includePaths() = 0;
virtual void addEditorSupport(CppTools::AbstractEditorSupport *editorSupport) = 0;
virtual void removeEditorSupport(CppTools::AbstractEditorSupport *editorSupport) = 0;
virtual CppEditorSupport *cppEditorSupport(TextEditor::BaseTextEditor *editor) = 0;
......
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