| 31 | } |
| 32 | |
| 33 | QVariant CompletionItem::data(const QModelIndex& index, int role, const CodeCompletionModel* model) const |
| 34 | { |
| 35 | DUChainReadLocker lock; |
| 36 | Declaration* decl = declaration().data(); |
| 37 | |
| 38 | if (!decl) { |
| 39 | return QVariant(); |
| 40 | } |
| 41 | |
| 42 | auto* classDecl = dynamic_cast<ClassDeclaration *>(decl); |
| 43 | auto declType = decl->abstractType().dynamicCast<StructureType>(); |
| 44 | auto funcType = decl->abstractType().dynamicCast<QmlJS::FunctionType>(); |
| 45 | |
| 46 | if (role == CodeCompletionModel::BestMatchesCount) { |
| 47 | return 5; |
| 48 | } else if (role == CodeCompletionModel::MatchQuality) { |
| 49 | AbstractType::Ptr referenceType = |
| 50 | static_cast<QmlJS::CodeCompletionContext*>(model->completionContext().data())->typeToMatch(); |
| 51 | |
| 52 | if (!referenceType) { |
| 53 | return QVariant(); |
| 54 | } |
| 55 | |
| 56 | AbstractType::Ptr declType = decl->abstractType(); |
| 57 | if (!declType) { |
| 58 | return QVariant(); |
| 59 | } |
| 60 | |
| 61 | auto declFunc = declType.dynamicCast<QmlJS::FunctionType>(); |
| 62 | |
| 63 | if (declType->equals(referenceType.constData())) { |
| 64 | // Perfect type match |
| 65 | return QVariant(10); |
| 66 | } else if (declFunc && declFunc->returnType() && |
| 67 | declFunc->returnType()->equals(referenceType.constData())) { |
| 68 | // Also very nice: a function returning the proper type |
| 69 | return QVariant(9); |
| 70 | } else { |
| 71 | // Completely different types, no luck |
| 72 | return QVariant(); |
| 73 | } |
| 74 | } else if (role == Qt::DisplayRole && funcType) { |
| 75 | // Functions are displayed using the "type funcName(arg, arg, arg...)" format |
| 76 | Declaration* funcDecl = funcType->declaration(decl->topContext()); |
| 77 | |
| 78 | if (funcDecl) { |
| 79 | switch (index.column()) { |
| 80 | case CodeCompletionModel::Prefix: |
| 81 | return funcType->returnType()->toString(); |
| 82 | case CodeCompletionModel::Name: |
| 83 | // Return the identifier of the declaration being listed, not of its |
| 84 | // function declaration (because the function may have been declared |
| 85 | // anonymously, even if it has been assigned to a variable) |
| 86 | return decl->identifier().toString(); |
| 87 | case CodeCompletionModel::Arguments: |
| 88 | { |
| 89 | QStringList args; |
| 90 | const auto localDeclarations = funcDecl->internalContext()->localDeclarations(); |