CppEditor: Extend "Add Include" quickfix

... so it works for all kinds of symbols, not just Qt classes.

Fixes: QTCREATORBUG-21
Change-Id: I2d329c09b64cd8f7eef8cce7d9f022aca8586c0d
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2020-06-04 16:16:57 +02:00
parent d811e7773f
commit e100939ba7
3 changed files with 45 additions and 34 deletions

View File

@@ -3771,6 +3771,20 @@ void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier_data()
<< TestIncludePaths::globalQtCoreIncludePath() << TestIncludePaths::globalQtCoreIncludePath()
<< testDocuments << firstRefactoringOperation << ""; << testDocuments << firstRefactoringOperation << "";
testDocuments.clear(); testDocuments.clear();
original =
"std::s@tring s;\n"
;
expected =
"#include <string>\n"
"\n"
"std::string s;\n"
;
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
QTest::newRow("inserting_std::string")
<< TestIncludePaths::globalIncludePath()
<< testDocuments << firstRefactoringOperation << "";
testDocuments.clear();
} }
void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier() void CppEditorPlugin::test_quickfix_AddIncludeForUndefinedIdentifier()

View File

@@ -58,6 +58,7 @@
#include <projectexplorer/projectnodes.h> #include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h> #include <projectexplorer/projecttree.h>
#include <utils/algorithm.h>
#include <utils/fancylineedit.h> #include <utils/fancylineedit.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
@@ -1916,25 +1917,20 @@ QString findShortestInclude(const QString currentDocumentFilePath,
return result; return result;
} }
QString findQtIncludeWithSameName(const QString &className, QString findMatchingInclude(const QString &className,
const ProjectExplorer::HeaderPaths &headerPaths) const ProjectExplorer::HeaderPaths &headerPaths)
{ {
QString result; const QStringList candidateFileNames{className, className + ".h", className + ".hpp",
className.toLower(), className.toLower() + ".h", className.toLower() + ".hpp"};
// Check for a header file with the same name in the Qt include paths for (const QString &fileName : candidateFileNames) {
foreach (const ProjectExplorer::HeaderPath &headerPath, headerPaths) { for (const ProjectExplorer::HeaderPath &headerPath : headerPaths) {
if (!headerPath.path.contains(QLatin1String("/Qt"))) // "QtCore", "QtGui" etc... const QString headerPathCandidate = headerPath.path + QLatin1Char('/') + fileName;
continue;
const QString headerPathCandidate = headerPath.path + QLatin1Char('/') + className;
const QFileInfo fileInfo(headerPathCandidate); const QFileInfo fileInfo(headerPathCandidate);
if (fileInfo.exists() && fileInfo.isFile()) { if (fileInfo.exists() && fileInfo.isFile())
result = QLatin1Char('<') + className + QLatin1Char('>'); return '<' + fileName + '>';
break;
} }
} }
return {};
return result;
} }
ProjectExplorer::HeaderPaths relevantHeaderPaths(const QString &filePath) ProjectExplorer::HeaderPaths relevantHeaderPaths(const QString &filePath)
@@ -2030,17 +2026,11 @@ Snapshot forwardingHeaders(const CppQuickFixInterface &interface)
return result; return result;
} }
bool looksLikeAQtClass(const QString &identifier)
{
return identifier.size() > 2
&& identifier.at(0) == QLatin1Char('Q')
&& identifier.at(1).isUpper();
}
bool matchName(const Name *name, QList<Core::LocatorFilterEntry> *matches, QString *className) { bool matchName(const Name *name, QList<Core::LocatorFilterEntry> *matches, QString *className) {
if (!name) if (!name)
return false; return false;
QString simpleName;
if (Core::ILocatorFilter *classesFilter if (Core::ILocatorFilter *classesFilter
= CppTools::CppModelManager::instance()->classesFilter()) { = CppTools::CppModelManager::instance()->classesFilter()) {
QFutureInterface<Core::LocatorFilterEntry> dummy; QFutureInterface<Core::LocatorFilterEntry> dummy;
@@ -2051,7 +2041,8 @@ bool matchName(const Name *name, QList<Core::LocatorFilterEntry> *matches, QStri
if (const TemplateNameId *templateName = name->asTemplateNameId()) { if (const TemplateNameId *templateName = name->asTemplateNameId()) {
*className = templateNameAsString(templateName); *className = templateNameAsString(templateName);
} else { } else {
*className = oo.prettyName(name); simpleName = oo.prettyName(name);
*className = simpleName;
*matches = classesFilter->matchesFor(dummy, *className); *matches = classesFilter->matchesFor(dummy, *className);
if (matches->empty()) { if (matches->empty()) {
if (const Name *name = qualifiedName->base()) { if (const Name *name = qualifiedName->base()) {
@@ -2070,6 +2061,8 @@ bool matchName(const Name *name, QList<Core::LocatorFilterEntry> *matches, QStri
if (matches->empty()) if (matches->empty())
*matches = classesFilter->matchesFor(dummy, *className); *matches = classesFilter->matchesFor(dummy, *className);
if (matches->empty() && !simpleName.isEmpty())
*className = simpleName;
} }
return !matches->empty(); return !matches->empty();
@@ -2092,7 +2085,7 @@ void AddIncludeForUndefinedIdentifier::match(const CppQuickFixInterface &interfa
QList<Core::LocatorFilterEntry> matches; QList<Core::LocatorFilterEntry> matches;
const QString currentDocumentFilePath = interface.semanticInfo().doc->fileName(); const QString currentDocumentFilePath = interface.semanticInfo().doc->fileName();
const ProjectExplorer::HeaderPaths headerPaths = relevantHeaderPaths(currentDocumentFilePath); const ProjectExplorer::HeaderPaths headerPaths = relevantHeaderPaths(currentDocumentFilePath);
bool qtHeaderFileIncludeOffered = false; QList<Utils::FilePath> headers;
// Find an include file through the locator // Find an include file through the locator
if (matchName(nameAst->name, &matches, &className)) { if (matchName(nameAst->name, &matches, &className)) {
@@ -2124,11 +2117,9 @@ void AddIncludeForUndefinedIdentifier::match(const CppQuickFixInterface &interfa
else if (headerFileName.at(1).isUpper()) else if (headerFileName.at(1).isUpper())
priority = 1; priority = 1;
if (looksLikeAQtClass(include.mid(1, include.size() - 2)))
qtHeaderFileIncludeOffered = true;
result << new AddIncludeForUndefinedIdentifierOp(interface, priority, result << new AddIncludeForUndefinedIdentifierOp(interface, priority,
include); include);
headers << header;
} }
} }
} }
@@ -2158,12 +2149,16 @@ void AddIncludeForUndefinedIdentifier::match(const CppQuickFixInterface &interfa
if (className.isEmpty()) if (className.isEmpty())
return; return;
// The header file we are looking for might not be (yet) included in any file we have parsed. // Fallback: Check the include paths for files that look like candidates
// As such, it will not be findable via locator. At least for Qt classes, check also for // for the given name.
// headers with the same name. if (!Utils::contains(headers,
if (!qtHeaderFileIncludeOffered && looksLikeAQtClass(className)) { [&className](const Utils::FilePath &fp) { return fp.fileName() == className; })) {
const QString include = findQtIncludeWithSameName(className, headerPaths); const QString include = findMatchingInclude(className, headerPaths);
if (!include.isEmpty()) const auto matcher = [&include](const QuickFixOperation::Ptr &o) {
const auto includeOp = o.dynamicCast<AddIncludeForUndefinedIdentifierOp>();
return includeOp && includeOp->include() == include;
};
if (!include.isEmpty() && !Utils::contains(result, matcher))
result << new AddIncludeForUndefinedIdentifierOp(interface, 1, include); result << new AddIncludeForUndefinedIdentifierOp(interface, 1, include);
} }
} }

View File

@@ -67,6 +67,8 @@ public:
const QString &include); const QString &include);
void perform() override; void perform() override;
QString include() const { return m_include; }
private: private:
QString m_include; QString m_include;
}; };