From ed49052f6a82ea6ce13ad82e37d77df7696c1e45 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Tue, 17 Feb 2015 12:10:04 +0100 Subject: [PATCH] QmlJSEditor: Adding and exporting helper function for usages This patch adds and exports findUsageOfType() which can be used in e.g. the Qt Quick Desigener. Change-Id: I6e87df746bdb377485da052823f86b89d638ac2f Reviewed-by: Fawzi Mohamed --- .../qmljseditor/qmljsfindreferences.cpp | 27 +++++++++++++++++++ src/plugins/qmljseditor/qmljsfindreferences.h | 6 ++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/plugins/qmljseditor/qmljsfindreferences.cpp b/src/plugins/qmljseditor/qmljsfindreferences.cpp index c3ad1ec7d13..2c518ca52ed 100644 --- a/src/plugins/qmljseditor/qmljsfindreferences.cpp +++ b/src/plugins/qmljseditor/qmljsfindreferences.cpp @@ -920,6 +920,33 @@ void FindReferences::renameUsages(const QString &fileName, quint32 offset, m_watcher.setFuture(result); } +QList FindReferences::findUsageOfType(const QString &fileName, const QString typeName) +{ + QList usages; + ModelManagerInterface *modelManager = ModelManagerInterface::instance(); + + Document::Ptr doc = modelManager->snapshot().document(fileName); + if (!doc) + return usages; + + Link link(modelManager->snapshot(), modelManager->defaultVContext(doc->language(), doc), modelManager->builtins(doc)); + ContextPtr context = link(); + ScopeChain scopeChain(doc, context); + + const ObjectValue *targetValue = scopeChain.context()->lookupType(doc.data(), QStringList(typeName)); + + QmlJS::Snapshot snapshot = modelManager->snapshot(); + + foreach (const QmlJS::Document::Ptr &doc, snapshot) { + FindTypeUsages findUsages(doc, context); + FindTypeUsages::Result results = findUsages(typeName, targetValue); + foreach (const AST::SourceLocation &loc, results) { + usages.append(Usage(doc->fileName(), matchingLine(loc.offset, doc->source()), loc.startLine, loc.startColumn - 1, loc.length)); + } + } + return usages; +} + void FindReferences::displayResults(int first, int last) { // the first usage is always a dummy to indicate we now start searching diff --git a/src/plugins/qmljseditor/qmljsfindreferences.h b/src/plugins/qmljseditor/qmljsfindreferences.h index c05a2c42777..1435c40395f 100644 --- a/src/plugins/qmljseditor/qmljsfindreferences.h +++ b/src/plugins/qmljseditor/qmljsfindreferences.h @@ -31,6 +31,8 @@ #ifndef QMLJSFINDREFERENCES_H #define QMLJSFINDREFERENCES_H +#include "qmljseditor_global.h" + #include #include #include @@ -48,7 +50,7 @@ class SearchResult; namespace QmlJSEditor { -class FindReferences: public QObject +class QMLJSEDITOR_EXPORT FindReferences: public QObject { Q_OBJECT public: @@ -81,6 +83,8 @@ public: void renameUsages(const QString &fileName, quint32 offset, const QString &replacement = QString()); + static QList findUsageOfType(const QString &fileName, const QString typeName); + private Q_SLOTS: void displayResults(int first, int last); void searchFinished();