diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index fd90338e5c7ce374defafd2330ee9ff9dc2edc05..bd81f847dda9174d36b2b579e0bd4acbf5c53084 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1429,12 +1429,6 @@ void CPPEditor::jumpToDefinition() openLink(findLinkAt(textCursor())); } -struct DefinitionScore -{ - Function *f; - int score; -}; - Symbol *CPPEditor::findDefinition(Symbol *symbol) { if (symbol->isFunction()) @@ -1473,6 +1467,9 @@ Symbol *CPPEditor::findDefinition(Symbol *symbol) // a dummy document. Document::Ptr expressionDocument = Document::create("<empty>"); + Function *bestMatch = 0; + int bestScore = -1; + QMapIterator<QString, QList<Function *> > it(functionDefinitions); while (it.hasNext()) { it.next(); @@ -1480,52 +1477,43 @@ Symbol *CPPEditor::findDefinition(Symbol *symbol) // get the instance of the document. Document::Ptr thisDocument = snapshot.document(it.key()); - QList<DefinitionScore> definitionScores; foreach (Function *f, it.value()) { - DefinitionScore score; - score.score = 0; // current function's score - score.f = f; // current function + int score = 0; // current function's score - int funTyArgsCount = funTy->argumentCount(); - int fArgsCount = f->argumentCount(); + const int funTyArgsCount = funTy->argumentCount(); + const int fArgsCount = f->argumentCount(); // max score if arguments count equals if (funTyArgsCount == fArgsCount) - score.score += funTyArgsCount + 1; + score += funTyArgsCount + 1; else - score.score += (funTyArgsCount < fArgsCount) ? funTyArgsCount : fArgsCount; + score += (funTyArgsCount < fArgsCount) ? funTyArgsCount : fArgsCount; // +1 to score for every equal parameter unsigned minCount = (funTyArgsCount < fArgsCount) ? funTyArgsCount : fArgsCount; for (unsigned i = 0; i < minCount; ++i) if (Symbol *funTyArg = funTy->argumentAt(i)) if (Symbol *fArg = f->argumentAt(i)) - if (funTyArg->type().isEqualTo(fArg->type())) { - score.score++; + if (funTyArg->type().isEqualTo(fArg->type())) + score++; + + if (score > bestScore) { + // create a lookup context + const LookupContext context(f, expressionDocument, + thisDocument, snapshot); + + // search the matching definition for the function declaration `symbol'. + foreach (Symbol *s, context.resolve(f->name())) { + if (s == symbol) { + bestMatch = f; + bestScore = score; } - definitionScores.append(score); - } - - // looking for max score - if (!definitionScores.isEmpty()) { - DefinitionScore maxScore = definitionScores.first(); - foreach (const DefinitionScore& score, definitionScores) { - if (maxScore.score < score.score) - maxScore = score; + } } - - // create a lookup context - const LookupContext context(maxScore.f, expressionDocument, - thisDocument, snapshot); - - // search the matching definition for the function declaration `symbol'. - foreach (Symbol *s, context.resolve(maxScore.f->name())) - if (s == symbol) - return maxScore.f; } } - return 0; + return bestMatch; } unsigned CPPEditor::editorRevision() const