diff options
author | Nikolai Kosjar <nikolai.kosjar@digia.com> | 2013-06-14 14:02:17 +0200 |
---|---|---|
committer | Nikolai Kosjar <nikolai.kosjar@digia.com> | 2013-06-17 08:16:43 +0200 |
commit | 8180f695f1a9e2a5ff04d8ac764c1f58b6e97649 (patch) | |
tree | 4cd6da6b9a1bf2fc6adba314f32f236d08d85b2d /src/plugins/cppeditor | |
parent | 18dcac7ad04077bebf5be7a57b05d5dcbec54711 (diff) | |
download | qt-creator-8180f695f1a9e2a5ff04d8ac764c1f58b6e97649.tar.gz |
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: Lorenz Haas <lykurg@gmail.com>
Reviewed-by: hjk <hjk121@nokiamail.com>
Diffstat (limited to 'src/plugins/cppeditor')
-rw-r--r-- | src/plugins/cppeditor/cppeditorplugin.h | 1 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppquickfix_test.cpp | 45 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppquickfixes.cpp | 35 |
3 files changed, 66 insertions, 15 deletions
diff --git a/src/plugins/cppeditor/cppeditorplugin.h b/src/plugins/cppeditor/cppeditorplugin.h index 50b5bc141b..df975c805b 100644 --- a/src/plugins/cppeditor/cppeditorplugin.h +++ b/src/plugins/cppeditor/cppeditorplugin.h @@ -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(); diff --git a/src/plugins/cppeditor/cppquickfix_test.cpp b/src/plugins/cppeditor/cppquickfix_test.cpp index dd22a5d089..eeb2f34156 100644 --- a/src/plugins/cppeditor/cppquickfix_test.cpp +++ b/src/plugins/cppeditor/cppquickfix_test.cpp @@ -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() { diff --git a/src/plugins/cppeditor/cppquickfixes.cpp b/src/plugins/cppeditor/cppquickfixes.cpp index 14456b0eb2..e75b777665 100644 --- a/src/plugins/cppeditor/cppquickfixes.cpp +++ b/src/plugins/cppeditor/cppquickfixes.cpp @@ -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; + } + } } } |