| 42 | } |
| 43 | |
| 44 | IndexedString fetchImplementationFileForClass(const Declaration& targetClass) |
| 45 | { |
| 46 | DUChainReadLocker lock(DUChain::lock()); |
| 47 | qCDebug(LANGUAGE) << "Looking for implementation file for class:" << targetClass.identifier().toString(); |
| 48 | |
| 49 | DUContext* context = targetClass.internalContext(); |
| 50 | |
| 51 | //If this declaration is not a user defined type, then ignore and return empty file |
| 52 | if (targetClass.kind() != Declaration::Type) |
| 53 | return IndexedString(); |
| 54 | |
| 55 | //If this is a forward declaration attempt to resolve it. |
| 56 | const Declaration* realClass = &targetClass; |
| 57 | if (const auto* forward = dynamic_cast<const ForwardDeclaration*>(realClass)) { |
| 58 | if (!(realClass = forward->resolve(context->topContext()))) |
| 59 | return IndexedString(); |
| 60 | context = realClass->internalContext(); |
| 61 | } |
| 62 | |
| 63 | const QVector<Declaration*> declarations = context->localDeclarations(); |
| 64 | |
| 65 | QMap<IndexedString, unsigned int> implementationsInFile; |
| 66 | |
| 67 | for (Declaration* decl : declarations) { |
| 68 | ///@todo check for static variable instantiation as well |
| 69 | if (auto* classFun = dynamic_cast<ClassFunctionDeclaration*>(decl)) |
| 70 | if (auto* def = FunctionDefinition::definition(classFun)) { |
| 71 | qCDebug(LANGUAGE) << "Definition For declaration in:" << def->url().toUrl(); |
| 72 | ++implementationsInFile[def->url()]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | QMultiMap<unsigned int, IndexedString> sorter; |
| 77 | for (auto it = implementationsInFile.constBegin(), end = implementationsInFile.constEnd(); it != end; ++it) { |
| 78 | const IndexedString& file = it.key(); |
| 79 | unsigned int count = it.value(); |
| 80 | sorter.insert(count, file); |
| 81 | } |
| 82 | QList<IndexedString> sortedFiles = sorter.values(); |
| 83 | |
| 84 | //If there are no methods, then just return the file the declaration is in |
| 85 | if (sortedFiles.empty()) |
| 86 | return context->url(); |
| 87 | |
| 88 | if (sortedFiles.size() == 1) |
| 89 | return sortedFiles[0]; |
| 90 | |
| 91 | const QList<IndexedString> tiedFiles = sorter.values(sorter.end().key()); |
| 92 | if (tiedFiles.size() > 1) { |
| 93 | //Return the file that has the most uses |
| 94 | const auto uses = realClass->uses(); |
| 95 | |
| 96 | IndexedString mostUsesFile; |
| 97 | unsigned int mostUses = 0; |
| 98 | for (const IndexedString& currentFile : tiedFiles) { |
| 99 | if (static_cast<unsigned int>(uses[currentFile].size()) > mostUses) { |
| 100 | mostUses = uses[currentFile].size(); |
| 101 | mostUsesFile = currentFile; |
nothing calls this directly
no test coverage detected