Commit c8c72bf6 authored by Alexander Lohnau's avatar Alexander Lohnau 💬
Browse files

Add helprunner to show usage information for KRunner plugins

BUG: 433636
BUG: 369136
parent a1fdb95d
......@@ -278,7 +278,8 @@ ColumnLayout {
onUpdateQueryString: {
queryField.text = text
queryField.cursorPosition = cursorPosition
queryField.select(cursorPosition, root.query.length)
queryField.focus = true;
}
}
}
......
......@@ -3,6 +3,7 @@ if(KF5Baloo_FOUND)
endif()
add_subdirectory(bookmarks)
add_subdirectory(calculator)
add_subdirectory(helprunner)
add_subdirectory(locations)
add_subdirectory(places)
add_subdirectory(services)
......
kcoreaddons_add_plugin(helprunner SOURCES helprunner.cpp INSTALL_NAMESPACE "kf5/krunner")
target_link_libraries(helprunner Qt::Widgets KF5::I18n KF5::Runner)
/*
SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "helprunner.h"
#include <KLocalizedString>
HelpRunner::HelpRunner(QObject *parent, const KPluginMetaData &pluginMetaData, const QVariantList &args)
: AbstractRunner(parent, pluginMetaData, args)
{
setTriggerWords({i18nc("this is a runner keyword", "help"), QStringLiteral("?")});
m_manager = qobject_cast<RunnerManager *>(parent);
Q_ASSERT(m_manager);
}
void HelpRunner::match(RunnerContext &context)
{
const QString sanatizedQuery = context.query().remove(matchRegex());
auto runners = m_manager->runners();
for (auto it = runners.begin(); it != runners.end();) {
if (*it == this || (*it)->syntaxes().isEmpty()) {
it = runners.erase(it);
} else {
++it;
}
}
QList<AbstractRunner *> matchingRunners;
if (sanatizedQuery.isEmpty()) {
matchingRunners = runners;
} else {
for (AbstractRunner *runner : std::as_const(runners)) {
if (runner->id().contains(sanatizedQuery, Qt::CaseInsensitive) || runner->name().contains(sanatizedQuery, Qt::CaseInsensitive)) {
matchingRunners << runner;
}
}
}
QList<QueryMatch> matches;
bool showExtendedHelp = matchingRunners.count() == 1 && sanatizedQuery.count() >= 3;
for (AbstractRunner *runner : std::as_const(matchingRunners)) {
const QList<RunnerSyntax> syntaxes = runner->syntaxes();
if (showExtendedHelp) {
for (const RunnerSyntax &syntax : syntaxes) {
QueryMatch match(this);
QString matchText;
matchText.append(syntax.exampleQueries().join(QStringLiteral("\n")));
matchText.append(QLatin1String("\n"));
matchText.append(syntax.description());
match.setText(matchText);
match.setData(syntax.exampleQueries().constFirst());
match.setMultiLine(true);
match.setMatchCategory(runner->name());
match.setIcon(runner->icon());
match.setSubtext(runner->description());
matches << match;
}
} else {
QueryMatch match(this);
match.setText(syntaxes.constFirst().exampleQueries().constFirst());
match.setIcon(runner->icon());
match.setSubtext(runner->description());
match.setType(QueryMatch::InformationalMatch);
match.setData(QVariant(QStringLiteral("?") + runner->name()));
matches << match;
}
}
context.addMatches(matches);
}
void HelpRunner::run(const RunnerContext &context, const QueryMatch &match)
{
Q_UNUSED(context);
const QString query = match.data().toString();
const int idx = query.indexOf(i18nd("krunner", "<search term>"));
context.requestQueryStringUpdate(query, idx == -1 ? query.count() : idx);
}
K_PLUGIN_CLASS_WITH_JSON(HelpRunner, "helprunner.json")
#include "helprunner.moc"
/*
SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#pragma once
#include <KRunner/AbstractRunner>
#include <KRunner/RunnerManager>
#include <KPluginMetaData>
#include <QObject>
using namespace Plasma;
class HelpRunner : public AbstractRunner
{
public:
HelpRunner(QObject *parent, const KPluginMetaData &pluginMetaData, const QVariantList &args);
void match(RunnerContext &context) override;
void run(const RunnerContext &context, const QueryMatch &match) override;
private:
RunnerManager *m_manager;
};
{
"KPlugin": {
"Authors": [
{
"Email": "alexander.lohnau@gmx.de",
"Name": "Alexander Lohnau"
}
],
"Description": "Shows help information about runners when typing \"?\" or \"help\"",
"EnabledByDefault": true,
"Icon": "help-contextual",
"Name": "Help Runner",
"ServiceTypes": [
"Plasma/Runner"
]
}
}
Supports Markdown
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