Commit ace380ec authored by Roberto Raggi's avatar Roberto Raggi

Make the symbols used in the current editors.

parent 3b280a3a
......@@ -830,11 +830,11 @@ void CPPEditor::reformatDocument()
c.insertText(QString::fromUtf8(str.c_str(), str.length()));
}
void CPPEditor::findReferences()
CPlusPlus::Symbol *CPPEditor::findCanonicalSymbol(const QTextCursor &cursor,
Document::Ptr doc,
const Snapshot &snapshot) const
{
QTextCursor tc = textCursor();
m_currentRenameSelection = -1;
QTextCursor tc = cursor;
int line, col;
convertPosition(tc.position(), &line, &col);
++col;
......@@ -843,10 +843,9 @@ void CPPEditor::findReferences()
ExpressionUnderCursor expressionUnderCursor;
const QString code = expressionUnderCursor(tc);
qDebug() << "code:" << code;
// qDebug() << "code:" << code;
Snapshot snapshot = m_modelManager->snapshot();
Document::Ptr doc = snapshot.value(file()->fileName());
const QString fileName = const_cast<CPPEditor *>(this)->file()->fileName();
TypeOfExpression typeOfExpression;
typeOfExpression.setSnapshot(snapshot);
......@@ -857,8 +856,46 @@ void CPPEditor::findReferences()
lastVisibleSymbol,
TypeOfExpression::Preprocess);
if (Symbol *canonicalSymbol = LookupContext::canonicalSymbol(results)) {
m_modelManager->findReferences(canonicalSymbol);
Symbol *canonicalSymbol = LookupContext::canonicalSymbol(results);
return canonicalSymbol;
}
void CPPEditor::findReferences()
{
m_currentRenameSelection = -1;
QList<QTextEdit::ExtraSelection> selections;
SemanticInfo info = m_lastSemanticInfo;
if (info.doc) {
if (Symbol *canonicalSymbol = findCanonicalSymbol(textCursor(), info.doc, info.snapshot)) {
TranslationUnit *unit = info.doc->translationUnit();
const QList<int> references = m_modelManager->references(canonicalSymbol, info.doc, info.snapshot);
foreach (int index, references) {
unsigned line, column;
unit->getTokenPosition(index, &line, &column);
if (column)
--column; // adjust the column position.
const int len = unit->tokenAt(index).f.length;
QTextCursor cursor(document()->findBlockByNumber(line - 1));
cursor.setPosition(cursor.position() + column);
cursor.setPosition(cursor.position() + len, QTextCursor::KeepAnchor);
QTextEdit::ExtraSelection sel;
sel.format = m_occurrencesFormat;
sel.cursor = cursor;
selections.append(sel);
}
setExtraSelections(CodeSemanticsSelection, selections);
m_modelManager->findReferences(canonicalSymbol);
}
}
}
......@@ -2015,7 +2052,7 @@ SemanticInfo SemanticHighlighter::semanticInfo(const Source &source)
doc = source.snapshot.documentFromSource(preprocessedCode, source.fileName);
doc->check();
snapshot = source.snapshot.simplified(doc);
snapshot = source.snapshot;
}
Control *control = doc->control();
......
......@@ -223,6 +223,10 @@ protected:
bool isInComment(const QTextCursor &cursor) const;
CPlusPlus::Symbol *findCanonicalSymbol(const QTextCursor &cursor,
CPlusPlus::Document::Ptr doc,
const CPlusPlus::Snapshot &snapshot) const;
private Q_SLOTS:
void updateFileName();
void jumpToMethod(int index);
......
......@@ -67,8 +67,8 @@ namespace {
struct Process: protected ASTVisitor
{
public:
Process(QFutureInterface<Core::Utils::FileSearchResult> &future,
Document::Ptr doc, const Snapshot &snapshot)
Process(Document::Ptr doc, const Snapshot &snapshot,
QFutureInterface<Core::Utils::FileSearchResult> *future)
: ASTVisitor(doc->control()),
_future(future),
_doc(doc),
......@@ -79,12 +79,14 @@ public:
_snapshot.insert(_doc);
}
void operator()(Symbol *symbol, Identifier *id, AST *ast)
QList<int> operator()(Symbol *symbol, Identifier *id, AST *ast)
{
_references.clear();
_declSymbol = symbol;
_id = id;
_exprDoc = Document::create("<references>");
accept(ast);
return _references;
}
protected:
......@@ -123,10 +125,13 @@ protected:
if (col)
--col; // adjust the column position.
int len = tk.f.length;
const int len = tk.f.length;
_future.reportResult(Core::Utils::FileSearchResult(QDir::toNativeSeparators(_doc->fileName()),
line, lineText, col, len));
if (_future)
_future->reportResult(Core::Utils::FileSearchResult(QDir::toNativeSeparators(_doc->fileName()),
line, lineText, col, len));
_references.append(tokenIndex);
}
bool checkCandidates(const QList<Symbol *> &candidates) const
......@@ -351,7 +356,7 @@ protected:
}
private:
QFutureInterface<Core::Utils::FileSearchResult> &_future;
QFutureInterface<Core::Utils::FileSearchResult> *_future;
Identifier *_id; // ### remove me
Symbol *_declSymbol;
Document::Ptr _doc;
......@@ -361,6 +366,7 @@ private:
Semantic _sem;
QList<PostfixExpressionAST *> _postfixExpressionStack;
QList<QualifiedNameAST *> _qualifiedNameStack;
QList<int> _references;
};
} // end of anonymous namespace
......@@ -378,6 +384,28 @@ CppFindReferences::~CppFindReferences()
{
}
QList<int> CppFindReferences::references(Symbol *symbol,
Document::Ptr doc,
const Snapshot& snapshot) const
{
Identifier *id = 0;
if (Identifier *symbolId = symbol->identifier())
id = doc->control()->findIdentifier(symbolId->chars(), symbolId->size());
QList<int> references;
if (! id)
return references;
TranslationUnit *translationUnit = doc->translationUnit();
Q_ASSERT(translationUnit != 0);
Process process(doc, snapshot, /*future = */ 0);
references = process(symbol, id, translationUnit->ast());
return references;
}
static void find_helper(QFutureInterface<Core::Utils::FileSearchResult> &future,
const QMap<QString, QString> wl,
Snapshot snapshot,
......@@ -429,7 +457,7 @@ static void find_helper(QFutureInterface<Core::Utils::FileSearchResult> &future,
if (Identifier *id = control->findIdentifier(symbolId->chars(), symbolId->size())) {
doc->check();
TranslationUnit *unit = doc->translationUnit();
Process process(future, doc, snapshot);
Process process(doc, snapshot, &future);
process(symbol, id, unit->ast());
}
}
......
......@@ -35,16 +35,12 @@
#include <QtCore/QFuture>
#include <QtCore/QFutureWatcher>
#include <utils/filesearch.h>
#include <cplusplus/CppDocument.h>
namespace Find {
class SearchResultWindow;
} // end of namespace Find
namespace CPlusPlus {
class Snapshot;
class Symbol;
} // end of namespace CPlusPlus
namespace CppTools {
namespace Internal {
......@@ -58,6 +54,10 @@ public:
CppFindReferences(CppModelManager *modelManager);
virtual ~CppFindReferences();
QList<int> references(CPlusPlus::Symbol *symbol,
CPlusPlus::Document::Ptr doc,
const CPlusPlus::Snapshot& snapshot) const;
Q_SIGNALS:
void changed();
......
......@@ -739,6 +739,13 @@ void CppModelManager::removeEditorSupport(AbstractEditorSupport *editorSupport)
m_addtionalEditorSupport.remove(editorSupport);
}
QList<int> CppModelManager::references(CPlusPlus::Symbol *symbol,
CPlusPlus::Document::Ptr doc,
const CPlusPlus::Snapshot &snapshot)
{
return m_findReferences->references(LookupContext::canonicalSymbol(symbol), doc, snapshot);
}
void CppModelManager::findReferences(CPlusPlus::Symbol *symbol)
{
if (symbol->identifier())
......
......@@ -102,6 +102,10 @@ public:
virtual void addEditorSupport(AbstractEditorSupport *editorSupport);
virtual void removeEditorSupport(AbstractEditorSupport *editorSupport);
virtual QList<int> references(CPlusPlus::Symbol *symbol,
CPlusPlus::Document::Ptr doc,
const CPlusPlus::Snapshot &snapshot);
virtual void findReferences(CPlusPlus::Symbol *symbol);
void setHeaderSuffixes(const QStringList &suffixes)
......
......@@ -95,6 +95,10 @@ public:
virtual void addEditorSupport(AbstractEditorSupport *editorSupport) = 0;
virtual void removeEditorSupport(AbstractEditorSupport *editorSupport) = 0;
virtual QList<int> references(CPlusPlus::Symbol *symbol,
CPlusPlus::Document::Ptr doc,
const CPlusPlus::Snapshot &snapshot) = 0;
virtual void findReferences(CPlusPlus::Symbol *symbol) = 0;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment