forked from qt-creator/qt-creator
JavaScriptFilter: Add engine reset
Change-Id: I80c0e1c9270c9a67aa2494eeab1ccd942bf1885e Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
committed by
André Hartmann
parent
ff0d67dcd2
commit
76d58e8303
@@ -32,6 +32,11 @@
|
|||||||
namespace Core {
|
namespace Core {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
|
enum JavaScriptAction
|
||||||
|
{
|
||||||
|
ResetEngine = QVariant::UserType + 1
|
||||||
|
};
|
||||||
|
|
||||||
JavaScriptFilter::JavaScriptFilter()
|
JavaScriptFilter::JavaScriptFilter()
|
||||||
{
|
{
|
||||||
setId("JavaScriptFilter");
|
setId("JavaScriptFilter");
|
||||||
@@ -48,7 +53,8 @@ void JavaScriptFilter::prepareSearch(const QString &entry)
|
|||||||
{
|
{
|
||||||
Q_UNUSED(entry);
|
Q_UNUSED(entry);
|
||||||
|
|
||||||
setupEngine();
|
if (!m_engine)
|
||||||
|
setupEngine();
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LocatorFilterEntry> JavaScriptFilter::matchesFor(
|
QList<LocatorFilterEntry> JavaScriptFilter::matchesFor(
|
||||||
@@ -56,13 +62,17 @@ QList<LocatorFilterEntry> JavaScriptFilter::matchesFor(
|
|||||||
{
|
{
|
||||||
Q_UNUSED(future);
|
Q_UNUSED(future);
|
||||||
|
|
||||||
const QString result = m_engine->evaluate(entry).toString();
|
|
||||||
const QString expression = entry + " = " + result;
|
|
||||||
|
|
||||||
QList<LocatorFilterEntry> entries;
|
QList<LocatorFilterEntry> entries;
|
||||||
entries.append({this, expression, QVariant()});
|
if (entry.trimmed().isEmpty()) {
|
||||||
entries.append({this, tr("Copy to clipboard: %1").arg(result), result});
|
entries.append({this, tr("Reset Engine"), QVariant(ResetEngine, nullptr)});
|
||||||
entries.append({this, tr("Copy to clipboard: %1").arg(expression), expression});
|
} else {
|
||||||
|
const QString result = m_engine->evaluate(entry).toString();
|
||||||
|
const QString expression = entry + " = " + result;
|
||||||
|
|
||||||
|
entries.append({this, expression, QVariant()});
|
||||||
|
entries.append({this, tr("Copy to clipboard: %1").arg(result), result});
|
||||||
|
entries.append({this, tr("Copy to clipboard: %1").arg(expression), expression});
|
||||||
|
}
|
||||||
|
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
@@ -77,6 +87,11 @@ void JavaScriptFilter::accept(Core::LocatorFilterEntry selection, QString *newTe
|
|||||||
if (selection.internalData.isNull())
|
if (selection.internalData.isNull())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (selection.internalData.userType() == ResetEngine) {
|
||||||
|
m_engine.reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QClipboard *clipboard = QGuiApplication::clipboard();
|
QClipboard *clipboard = QGuiApplication::clipboard();
|
||||||
clipboard->setText(selection.internalData.toString());
|
clipboard->setText(selection.internalData.toString());
|
||||||
}
|
}
|
||||||
@@ -89,10 +104,7 @@ void JavaScriptFilter::refresh(QFutureInterface<void> &future)
|
|||||||
|
|
||||||
void JavaScriptFilter::setupEngine()
|
void JavaScriptFilter::setupEngine()
|
||||||
{
|
{
|
||||||
if (m_engine)
|
m_engine.reset(new QJSEngine);
|
||||||
return;
|
|
||||||
|
|
||||||
m_engine = new QJSEngine(this);
|
|
||||||
m_engine->evaluate(
|
m_engine->evaluate(
|
||||||
"function abs(x) { return Math.abs(x); }\n"
|
"function abs(x) { return Math.abs(x); }\n"
|
||||||
"function acos(x) { return Math.acos(x); }\n"
|
"function acos(x) { return Math.acos(x); }\n"
|
||||||
|
@@ -27,6 +27,8 @@
|
|||||||
|
|
||||||
#include <coreplugin/locator/ilocatorfilter.h>
|
#include <coreplugin/locator/ilocatorfilter.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QJSEngine;
|
class QJSEngine;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
@@ -51,7 +53,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
void setupEngine();
|
void setupEngine();
|
||||||
|
|
||||||
QJSEngine *m_engine = nullptr;
|
mutable std::unique_ptr<QJSEngine> m_engine;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
Reference in New Issue
Block a user