aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coreplugin/locator/commandlocator.cpp
blob: a9e9ab7aa5c14814c9781f2554e0005113381fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "commandlocator.h"

#include <coreplugin/actionmanager/command.h>

#include <utils/stringutils.h>

#include <QAction>
#include <QPointer>

using namespace Utils;

namespace Core {

CommandLocator::CommandLocator(Id id, const QString &displayName, const QString &shortCutString,
                               QObject *parent)
    : ILocatorFilter(parent)
{
    setId(id);
    setDisplayName(displayName);
    setDefaultShortcutString(shortCutString);
}

LocatorMatcherTasks CommandLocator::matchers()
{
    using namespace Tasking;

    TreeStorage<LocatorStorage> storage;

    const auto onSetup = [storage, commands = m_commands] {
        const QString input = storage->input();
        const Qt::CaseSensitivity inputCaseSensitivity = caseSensitivity(input);
        LocatorFilterEntries goodEntries;
        LocatorFilterEntries betterEntries;
        for (Command *command : commands) {
            if (!command->isActive())
                continue;

            QAction *action = command->action();
            if (!action || !action->isEnabled())
                continue;

            const QString text = Utils::stripAccelerator(action->text());
            const int index = text.indexOf(input, 0, inputCaseSensitivity);
            if (index >= 0) {
                LocatorFilterEntry entry;
                entry.displayName = text;
                entry.acceptor = [actionPointer = QPointer(action)] {
                    if (actionPointer) {
                        QMetaObject::invokeMethod(actionPointer, [actionPointer] {
                            if (actionPointer && actionPointer->isEnabled())
                                actionPointer->trigger();
                        }, Qt::QueuedConnection);
                    }
                    return AcceptResult();
                };
                entry.highlightInfo = {index, int(input.length())};
                if (index == 0)
                    betterEntries.append(entry);
                else
                    goodEntries.append(entry);
            }
        }
        storage->reportOutput(betterEntries + goodEntries);
    };
    return {{Sync(onSetup), storage}};
}

}  // namespace Core