aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/cppincludesfilter.cpp
blob: d955dbeb43b003e492e8e8bfe93359519c6c7698 (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
// 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 "cppincludesfilter.h"

#include "cppeditorconstants.h"
#include "cppeditortr.h"
#include "cppmodelmanager.h"

#include <coreplugin/editormanager/documentmodel.h>

#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectmanager.h>

using namespace Core;
using namespace ProjectExplorer;
using namespace Utils;

namespace CppEditor::Internal {

static FilePaths generateFilePaths(const QFuture<void> &future,
                                   const CPlusPlus::Snapshot &snapshot,
                                   const std::unordered_set<FilePath> &inputFilePaths)
{
    FilePaths results;
    std::unordered_set<FilePath> resultsCache;
    std::unordered_set<FilePath> queuedPaths = inputFilePaths;

    while (!queuedPaths.empty()) {
        if (future.isCanceled())
            return {};

        const auto iterator = queuedPaths.cbegin();
        const FilePath filePath = *iterator;
        queuedPaths.erase(iterator);
        const CPlusPlus::Document::Ptr doc = snapshot.document(filePath);
        if (!doc)
            continue;
        const FilePaths includedFiles = doc->includedFiles();
        for (const FilePath &includedFile : includedFiles) {
            if (resultsCache.emplace(includedFile).second) {
                queuedPaths.emplace(includedFile);
                results.append(includedFile);
            }
        }
    }
    return results;
}

CppIncludesFilter::CppIncludesFilter()
{
    setId(Constants::INCLUDES_FILTER_ID);
    setDisplayName(Tr::tr(Constants::INCLUDES_FILTER_DISPLAY_NAME));
    setDescription(
        Tr::tr("Locates files that are included by C++ files of any open project. Append "
               "\"+<number>\" or \":<number>\" to jump to the given line number. Append another "
               "\"+<number>\" or \":<number>\" to jump to the column number as well."));
    setDefaultShortcutString("ai");
    setDefaultIncludedByDefault(true);
    const auto invalidate = [this] { m_cache.invalidate(); };
    setRefreshRecipe(Tasking::Sync([invalidate] { invalidate(); }));
    setPriority(ILocatorFilter::Low);

    connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::fileListChanged,
            this, invalidate);
    connect(CppModelManager::instance(), &CppModelManager::documentUpdated,
            this, invalidate);
    connect(CppModelManager::instance(), &CppModelManager::aboutToRemoveFiles,
            this, invalidate);
    connect(DocumentModel::model(), &QAbstractItemModel::rowsInserted,
            this, invalidate);
    connect(DocumentModel::model(), &QAbstractItemModel::rowsRemoved,
            this, invalidate);
    connect(DocumentModel::model(), &QAbstractItemModel::dataChanged,
            this, invalidate);
    connect(DocumentModel::model(), &QAbstractItemModel::modelReset,
            this, invalidate);

    const auto generatorProvider = [] {
        // This body runs in main thread
        std::unordered_set<FilePath> inputFilePaths;
        for (Project *project : ProjectManager::projects()) {
            const FilePaths allFiles = project->files(Project::SourceFiles);
            for (const FilePath &filePath : allFiles)
                inputFilePaths.insert(filePath);
        }
        const QList<DocumentModel::Entry *> entries = DocumentModel::entries();
        for (DocumentModel::Entry *entry : entries) {
            if (entry)
                inputFilePaths.insert(entry->filePath());
        }
        const CPlusPlus::Snapshot snapshot = CppModelManager::snapshot();
        return [snapshot, inputFilePaths](const QFuture<void> &future) {
            // This body runs in non-main thread
            return generateFilePaths(future, snapshot, inputFilePaths);
        };
    };
    m_cache.setGeneratorProvider(generatorProvider);
}

} // namespace CppEditor::Internal