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 <fawzi.mohamed@theqtcompany.com>
This commit is contained in:
Thomas Hartmann
2015-02-17 12:10:04 +01:00
committed by Thomas Hartmann
parent 38609296d1
commit ed49052f6a
2 changed files with 32 additions and 1 deletions

View File

@@ -920,6 +920,33 @@ void FindReferences::renameUsages(const QString &fileName, quint32 offset,
m_watcher.setFuture(result); m_watcher.setFuture(result);
} }
QList<FindReferences::Usage> FindReferences::findUsageOfType(const QString &fileName, const QString typeName)
{
QList<Usage> 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) void FindReferences::displayResults(int first, int last)
{ {
// the first usage is always a dummy to indicate we now start searching // the first usage is always a dummy to indicate we now start searching

View File

@@ -31,6 +31,8 @@
#ifndef QMLJSFINDREFERENCES_H #ifndef QMLJSFINDREFERENCES_H
#define QMLJSFINDREFERENCES_H #define QMLJSFINDREFERENCES_H
#include "qmljseditor_global.h"
#include <QMutex> #include <QMutex>
#include <QObject> #include <QObject>
#include <QFuture> #include <QFuture>
@@ -48,7 +50,7 @@ class SearchResult;
namespace QmlJSEditor { namespace QmlJSEditor {
class FindReferences: public QObject class QMLJSEDITOR_EXPORT FindReferences: public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
@@ -81,6 +83,8 @@ public:
void renameUsages(const QString &fileName, quint32 offset, void renameUsages(const QString &fileName, quint32 offset,
const QString &replacement = QString()); const QString &replacement = QString());
static QList<Usage> findUsageOfType(const QString &fileName, const QString typeName);
private Q_SLOTS: private Q_SLOTS:
void displayResults(int first, int last); void displayResults(int first, int last);
void searchFinished(); void searchFinished();