Allow plugins to insert custom imports.

This patch allows a plugin to insert custom imports. These imports are
used by QtC for syntax highlighting and code completion. This way a
plugin can register types and objects that are available only at
runtime.

This is an example of an imports function implementation:

QList<Import> MyPlugin::imports(ValueOwner *valueOwner, const Document
*context) const
{
  // context is needed to know from which project is the opened document
  // in this example we don't care about multiple projects

  Import import;
  import.object = new QmlJS::ObjectValue(valueOwner, "<defaults>");
  import.valid = true;
  const ComponentVersion version(1, 0);
  import.info = ImportInfo::moduleImport("MyPlugin", version,
QString());
  auto myType = valueOwner->newObject(nullptr)
  myType->setMember("myProperty", valueOwner->valueOwner->intValue());
  // add more properties & methods/signals to myType

  import.object->setMember("MyType", myType);

  // in this example we return only one, but you care return more than
one
  return QList<Import>(import);
}

Change-Id: I395c273c7b15a9e4ed5a89a81d70ff92db2b7c0c
Reviewed-by: Marco Benelli <marco.benelli@theqtcompany.com>
Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com>
This commit is contained in:
BogDan Vatra
2015-04-24 14:32:43 +03:00
parent 83fc8e1fd3
commit 28d2b16165
4 changed files with 21 additions and 2 deletions

View File

@@ -2,4 +2,5 @@ QTC_LIB_NAME = QmlJS
QTC_LIB_DEPENDS += \ QTC_LIB_DEPENDS += \
utils \ utils \
languageutils \ languageutils \
cplusplus cplusplus \
extensionsystem

View File

@@ -1124,6 +1124,15 @@ public:
const LanguageUtils::FakeMetaMethod &fakeMetaMethod() const; const LanguageUtils::FakeMetaMethod &fakeMetaMethod() const;
}; };
class QMLJS_EXPORT CustomImportsProvider : public QObject
{
Q_OBJECT
public:
explicit CustomImportsProvider(QObject *parent = 0) : QObject(parent) {}
virtual ~CustomImportsProvider() {}
virtual QList<Import> imports(ValueOwner *valueOwner, const Document *context) const = 0;
};
} // namespace QmlJS } // namespace QmlJS
#endif // QMLJS_INTERPRETER_H #endif // QMLJS_INTERPRETER_H

View File

@@ -38,6 +38,8 @@
#include "qmljsqrcparser.h" #include "qmljsqrcparser.h"
#include "qmljsconstants.h" #include "qmljsconstants.h"
#include <extensionsystem/pluginmanager.h>
#include <QDir> #include <QDir>
using namespace LanguageUtils; using namespace LanguageUtils;
@@ -213,6 +215,13 @@ Context::ImportsPerDocument LinkPrivate::linkImports()
if (document) { if (document) {
// do it on document first, to make sure import errors are shown // do it on document first, to make sure import errors are shown
Imports *imports = new Imports(valueOwner); Imports *imports = new Imports(valueOwner);
// Add custom imports for the opened document
auto providers = ExtensionSystem::PluginManager::getObjects<CustomImportsProvider>();
foreach (const auto &provider, providers)
foreach (const auto &import, provider->imports(valueOwner, document.data()))
importCache.insert(ImportCacheKey(import.info), import);
populateImportedTypes(imports, document); populateImportedTypes(imports, document);
importsPerDocument.insert(document.data(), QSharedPointer<Imports>(imports)); importsPerDocument.insert(document.data(), QSharedPointer<Imports>(imports));
} }

View File

@@ -1,4 +1,4 @@
QTC_LIB_DEPENDS += cplusplus utils QTC_LIB_DEPENDS += cplusplus utils extensionsystem
include(../../../qttest.pri) include(../../../qttest.pri)
include($$IDE_SOURCE_TREE/src/rpath.pri) include($$IDE_SOURCE_TREE/src/rpath.pri)
DEFINES += QMLJS_BUILD_DIR DEFINES += QMLJS_BUILD_DIR