summaryrefslogtreecommitdiff
path: root/src/plugins/coreplugin/locator/ilocatorfilter.h
blob: b008c12bb5cfe7886fad245566c936ece021512a (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <coreplugin/core_global.h>

#include <solutions/tasking/tasktree.h>

#include <utils/filepath.h>
#include <utils/id.h>
#include <utils/link.h>

#include <QIcon>
#include <QKeySequence>

#include <optional>

QT_BEGIN_NAMESPACE
template <typename T>
class QFuture;
QT_END_NAMESPACE

namespace Core {

namespace Internal {
class Locator;
class LocatorWidget;
}

class ILocatorFilter;
class LocatorStoragePrivate;
class LocatorFileCachePrivate;

class AcceptResult
{
public:
    QString newText;
    int selectionStart = -1;
    int selectionLength = 0;
};

class LocatorFilterEntry
{
public:
    struct HighlightInfo {
        enum DataType {
            DisplayName,
            ExtraInfo
        };

        HighlightInfo() = default;

        HighlightInfo(QVector<int> startIndex,
                      QVector<int> length,
                      DataType type = DataType::DisplayName)
        {
            if (type == DataType::DisplayName) {
                startsDisplay = startIndex;
                lengthsDisplay = length;
            } else {
                startsExtraInfo = startIndex;
                lengthsExtraInfo = length;
            }
        }

        HighlightInfo(int startIndex, int length, DataType type = DataType::DisplayName)
            : HighlightInfo(QVector<int>{startIndex}, QVector<int>{length}, type)
        { }

        QVector<int> starts(DataType type = DataType::DisplayName) const
        {
            return type == DataType::DisplayName ? startsDisplay : startsExtraInfo;
        };

        QVector<int> lengths(DataType type = DataType::DisplayName) const
        {
            return type == DataType::DisplayName ? lengthsDisplay : lengthsExtraInfo;
        };

        QVector<int> startsDisplay;
        QVector<int> lengthsDisplay;
        QVector<int> startsExtraInfo;
        QVector<int> lengthsExtraInfo;
    };

    LocatorFilterEntry() = default;

    using Acceptor = std::function<AcceptResult()>;
    /* displayed string */
    QString displayName;
    /* extra information displayed in parentheses and light-gray next to display name (optional)*/
    QString displayExtra;
    /* extra information displayed in light-gray in a second column (optional) */
    QString extraInfo;
    /* additional tooltip */
    QString toolTip;
    /* called by locator widget on accept. By default, when acceptor is empty,
       EditorManager::openEditor(LocatorFilterEntry) will be used instead. */
    Acceptor acceptor;
    /* icon to display along with the entry */
    std::optional<QIcon> displayIcon;
    /* file path, if the entry is related to a file, is used e.g. for resolving a file icon */
    Utils::FilePath filePath;
    /* highlighting support */
    HighlightInfo highlightInfo;
    // Should be used only when accept() calls BaseFileFilter::openEditorAt()
    std::optional<Utils::Link> linkForEditor;
    static bool compareLexigraphically(const Core::LocatorFilterEntry &lhs,
                                       const Core::LocatorFilterEntry &rhs)
    {
        const int cmp = lhs.displayName.compare(rhs.displayName);
        if (cmp < 0)
            return true;
        if (cmp > 0)
            return false;
        return lhs.extraInfo < rhs.extraInfo;
    }
};

using LocatorFilterEntries = QList<LocatorFilterEntry>;

class CORE_EXPORT LocatorStorage final
{
public:
    LocatorStorage() = default;
    QString input() const;
    void reportOutput(const LocatorFilterEntries &outputData) const;

private:
    friend class LocatorMatcher;
    LocatorStorage(const std::shared_ptr<LocatorStoragePrivate> &priv) { d = priv; }
    void finalize() const;
    std::shared_ptr<LocatorStoragePrivate> d;
};

class CORE_EXPORT LocatorMatcherTask final
{
public:
    // The main task. Initial data (searchTerm) should be taken from storage.input().
    // Results reporting is done via the storage.reportOutput().
    Tasking::TaskItem task = Tasking::Group{};

    // When constructing the task, don't place the storage inside the task above.
    Tasking::TreeStorage<LocatorStorage> storage;
};

using LocatorMatcherTasks = QList<LocatorMatcherTask>;
using LocatorMatcherTaskCreator = std::function<LocatorMatcherTasks()>;
class LocatorMatcherPrivate;

enum class MatcherType {
    AllSymbols,
    Classes,
    Functions,
    CurrentDocumentSymbols
};

class CORE_EXPORT LocatorMatcher final : public QObject
{
    Q_OBJECT

public:
    LocatorMatcher();
    ~LocatorMatcher();
    void setTasks(const LocatorMatcherTasks &tasks);
    void setInputData(const QString &inputData);
    void setParallelLimit(int limit); // by default 0 = parallel
    void start();
    void stop();

    bool isRunning() const;
    // Total data collected so far, even when running.
    LocatorFilterEntries outputData() const;

    // Note: Starts internal event loop.
    static LocatorFilterEntries runBlocking(const LocatorMatcherTasks &tasks,
                                            const QString &input, int parallelLimit = 0);

    static void addMatcherCreator(MatcherType type, const LocatorMatcherTaskCreator &creator);
    static LocatorMatcherTasks matchers(MatcherType type);

signals:
    void serialOutputDataReady(const LocatorFilterEntries &serialOutputData);
    void done(bool success);

private:
    std::unique_ptr<LocatorMatcherPrivate> d;
};

class CORE_EXPORT ILocatorFilter : public QObject
{
    Q_OBJECT

public:
    enum class MatchLevel {
        Best = 0,
        Better,
        Good,
        Normal,
        Count
    };

    enum Priority {Highest = 0, High = 1, Medium = 2, Low = 3};

    ILocatorFilter(QObject *parent = nullptr);
    ~ILocatorFilter() override;

    Utils::Id id() const;
    Utils::Id actionId() const;

    QString displayName() const;
    void setDisplayName(const QString &displayString);

    QString description() const;
    void setDescription(const QString &description);

    Priority priority() const;

    QString shortcutString() const;
    void setDefaultShortcutString(const QString &shortcut);
    void setShortcutString(const QString &shortcut);

    QKeySequence defaultKeySequence() const;
    void setDefaultKeySequence(const QKeySequence &sequence);

    std::optional<QString> defaultSearchText() const;
    void setDefaultSearchText(const QString &defaultSearchText);

    virtual QByteArray saveState() const;
    virtual void restoreState(const QByteArray &state);

    virtual bool openConfigDialog(QWidget *parent, bool &needsRefresh);
    bool isConfigurable() const;

    bool isIncludedByDefault() const;
    void setDefaultIncludedByDefault(bool includedByDefault);
    void setIncludedByDefault(bool includedByDefault);

    bool isHidden() const;

    bool isEnabled() const;

    static Qt::CaseSensitivity caseSensitivity(const QString &str);
    static QRegularExpression createRegExp(const QString &text,
                                           Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive,
                                           bool multiWord = false);
    static LocatorFilterEntry::HighlightInfo highlightInfo(const QRegularExpressionMatch &match,
        LocatorFilterEntry::HighlightInfo::DataType dataType = LocatorFilterEntry::HighlightInfo::DisplayName);

    static QString msgConfigureDialogTitle();
    static QString msgPrefixLabel();
    static QString msgPrefixToolTip();
    static QString msgIncludeByDefault();
    static QString msgIncludeByDefaultToolTip();

public slots:
    void setEnabled(bool enabled);

signals:
    void enabledChanged(bool enabled);

protected:
    void setHidden(bool hidden);
    void setId(Utils::Id id);
    void setPriority(Priority priority);
    void setConfigurable(bool configurable);
    bool openConfigDialog(QWidget *parent, QWidget *additionalWidget);

    virtual void saveState(QJsonObject &object) const;
    virtual void restoreState(const QJsonObject &object);

    void setRefreshRecipe(const std::optional<Tasking::TaskItem> &recipe);
    std::optional<Tasking::TaskItem> refreshRecipe() const;

    static bool isOldSetting(const QByteArray &state);

private:
    virtual LocatorMatcherTasks matchers() = 0;

    friend class Internal::Locator;
    friend class Internal::LocatorWidget;
    static const QList<ILocatorFilter *> allLocatorFilters();

    Utils::Id m_id;
    QString m_shortcut;
    Priority m_priority = Medium;
    QString m_displayName;
    QString m_description;
    QString m_defaultShortcut;
    std::optional<QString> m_defaultSearchText;
    std::optional<Tasking::TaskItem> m_refreshRecipe;
    QKeySequence m_defaultKeySequence;
    bool m_defaultIncludedByDefault = false;
    bool m_includedByDefault = m_defaultIncludedByDefault;
    bool m_hidden = false;
    bool m_enabled = true;
    bool m_isConfigurable = true;
};

class CORE_EXPORT LocatorFileCache final
{
    Q_DISABLE_COPY_MOVE(LocatorFileCache)

public:
    // Always called from non-main thread.
    using FilePathsGenerator = std::function<Utils::FilePaths(const QFuture<void> &)>;
    // Always called from main thread.
    using GeneratorProvider = std::function<FilePathsGenerator()>;

    LocatorFileCache();

    void invalidate();
    void setFilePathsGenerator(const FilePathsGenerator &generator);
    void setFilePaths(const Utils::FilePaths &filePaths);
    void setGeneratorProvider(const GeneratorProvider &provider);

    std::optional<Utils::FilePaths> filePaths() const;

    static FilePathsGenerator filePathsGenerator(const Utils::FilePaths &filePaths);
    LocatorMatcherTask matcher() const;

    using MatchedEntries = std::array<LocatorFilterEntries, int(ILocatorFilter::MatchLevel::Count)>;
    static Utils::FilePaths processFilePaths(const QFuture<void> &future,
                                             const Utils::FilePaths &filePaths,
                                             bool hasPathSeparator,
                                             const QRegularExpression &regExp,
                                             const Utils::Link &inputLink,
                                             LocatorFileCache::MatchedEntries *entries);
private:
    std::shared_ptr<LocatorFileCachePrivate> d;
};

} // namespace Core