Simplify finding the best match for switching to definition

Also make sure it searches for the best match in all files that define
potential matching definitions.
This commit is contained in:
Thorbjørn Lindeijer
2010-03-09 17:17:11 +01:00
parent a72042ab6a
commit a456164631

View File

@@ -1429,12 +1429,6 @@ void CPPEditor::jumpToDefinition()
openLink(findLinkAt(textCursor())); openLink(findLinkAt(textCursor()));
} }
struct DefinitionScore
{
Function *f;
int score;
};
Symbol *CPPEditor::findDefinition(Symbol *symbol) Symbol *CPPEditor::findDefinition(Symbol *symbol)
{ {
if (symbol->isFunction()) if (symbol->isFunction())
@@ -1473,6 +1467,9 @@ Symbol *CPPEditor::findDefinition(Symbol *symbol)
// a dummy document. // a dummy document.
Document::Ptr expressionDocument = Document::create("<empty>"); Document::Ptr expressionDocument = Document::create("<empty>");
Function *bestMatch = 0;
int bestScore = -1;
QMapIterator<QString, QList<Function *> > it(functionDefinitions); QMapIterator<QString, QList<Function *> > it(functionDefinitions);
while (it.hasNext()) { while (it.hasNext()) {
it.next(); it.next();
@@ -1480,52 +1477,43 @@ Symbol *CPPEditor::findDefinition(Symbol *symbol)
// get the instance of the document. // get the instance of the document.
Document::Ptr thisDocument = snapshot.document(it.key()); Document::Ptr thisDocument = snapshot.document(it.key());
QList<DefinitionScore> definitionScores;
foreach (Function *f, it.value()) { foreach (Function *f, it.value()) {
DefinitionScore score; int score = 0; // current function's score
score.score = 0; // current function's score
score.f = f; // current function
int funTyArgsCount = funTy->argumentCount(); const int funTyArgsCount = funTy->argumentCount();
int fArgsCount = f->argumentCount(); const int fArgsCount = f->argumentCount();
// max score if arguments count equals // max score if arguments count equals
if (funTyArgsCount == fArgsCount) if (funTyArgsCount == fArgsCount)
score.score += funTyArgsCount + 1; score += funTyArgsCount + 1;
else else
score.score += (funTyArgsCount < fArgsCount) ? funTyArgsCount : fArgsCount; score += (funTyArgsCount < fArgsCount) ? funTyArgsCount : fArgsCount;
// +1 to score for every equal parameter // +1 to score for every equal parameter
unsigned minCount = (funTyArgsCount < fArgsCount) ? funTyArgsCount : fArgsCount; unsigned minCount = (funTyArgsCount < fArgsCount) ? funTyArgsCount : fArgsCount;
for (unsigned i = 0; i < minCount; ++i) for (unsigned i = 0; i < minCount; ++i)
if (Symbol *funTyArg = funTy->argumentAt(i)) if (Symbol *funTyArg = funTy->argumentAt(i))
if (Symbol *fArg = f->argumentAt(i)) if (Symbol *fArg = f->argumentAt(i))
if (funTyArg->type().isEqualTo(fArg->type())) { if (funTyArg->type().isEqualTo(fArg->type()))
score.score++; 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 unsigned CPPEditor::editorRevision() const