CppEditor: Fix go to definition of macros.

- Go to macro definition only on macro name (ie not in parameters).
- Prefer macro definition over expanded code definition, as the
  preprocessor is executed first: when trying to go to definition, the
  user sees the macro, not the expanded code.

Task-number: QTCREATORBUG-2240
Task-number: QTCREATORBUG-6175
Task-number: QTCREATORBUG-6848
Task-number: QTCREATORBUG-7008
Task-number: QTCREATORBUG-7009

Change-Id: I819c763524e79b20518c26a46a99a3a3b0a131bd
Reviewed-by: Andre Hartmann <aha_1980@gmx.de>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@nokia.com>
This commit is contained in:
Francois Ferrand
2012-03-08 15:53:28 +01:00
committed by Erik Verbruggen
parent 0cf5044d5a
commit a450c13327
2 changed files with 34 additions and 11 deletions

View File

@@ -707,11 +707,14 @@ const Macro *CPPEditorWidget::findCanonicalMacro(const QTextCursor &cursor, Docu
int line, col;
convertPosition(cursor.position(), &line, &col);
if (const Macro *macro = doc->findMacroDefinitionAt(line))
return macro;
if (const Document::MacroUse *use = doc->findMacroUseAt(cursor.position()))
if (const Macro *macro = doc->findMacroDefinitionAt(line)) {
QTextCursor macroCursor = cursor;
const QByteArray name = identifierUnderCursor(&macroCursor).toLatin1();
if (macro->name() == name)
return macro;
} else if (const Document::MacroUse *use = doc->findMacroUseAt(cursor.position())) {
return &use->macro();
}
return 0;
}
@@ -722,12 +725,13 @@ void CPPEditorWidget::findUsages()
info.snapshot = CppModelManagerInterface::instance()->snapshot();
info.snapshot.insert(info.doc);
CanonicalSymbol cs(this, info);
Symbol *canonicalSymbol = cs(textCursor());
if (canonicalSymbol) {
m_modelManager->findUsages(canonicalSymbol, cs.context());
} else if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
m_modelManager->findMacroUsages(*macro);
} else {
CanonicalSymbol cs(this, info);
Symbol *canonicalSymbol = cs(textCursor());
if (canonicalSymbol)
m_modelManager->findUsages(canonicalSymbol, cs.context());
}
}
@@ -1381,6 +1385,25 @@ CPPEditorWidget::Link CPPEditorWidget::findLinkAt(const QTextCursor &cursor,
tc.setPosition(endOfToken);
}
// Handle macro uses
const Macro *macro = doc->findMacroDefinitionAt(line);
if (macro) {
QTextCursor macroCursor = cursor;
const QByteArray name = identifierUnderCursor(&macroCursor).toLatin1();
if (macro->name() == name)
return link; //already on definition!
} else {
const Document::MacroUse *use = doc->findMacroUseAt(endOfToken - 1);
if (use && use->macro().fileName() != QLatin1String("<configuration>")) {
const Macro &macro = use->macro();
link.fileName = macro.fileName();
link.line = macro.line();
link.begin = use->begin();
link.end = use->end();
return link;
}
}
// Find the last symbol up to the cursor position
Scope *scope = doc->scopeAt(line, column);
if (!scope)