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
|
/****************************************************************************
**
** Copyright (C) 2016 Dmitry Savchenko
** Copyright (C) 2016 Vasiliy Sorokin
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "todoitemsprovider.h"
#include "constants.h"
#include "cpptodoitemsscanner.h"
#include "qmljstodoitemsscanner.h"
#include "todoitemsmodel.h"
#include "todoitemsscanner.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/idocument.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/session.h>
#include <utils/algorithm.h>
#include <QRegExp>
#include <QTimer>
using namespace ProjectExplorer;
using namespace Utils;
namespace Todo {
namespace Internal {
TodoItemsProvider::TodoItemsProvider(Settings settings, QObject *parent) :
QObject(parent),
m_settings(settings)
{
setupItemsModel();
setupStartupProjectBinding();
setupCurrentEditorBinding();
setupUpdateListTimer();
createScanners();
}
TodoItemsModel *TodoItemsProvider::todoItemsModel()
{
return m_itemsModel;
}
void TodoItemsProvider::settingsChanged(const Settings &newSettings)
{
if (newSettings.keywords != m_settings.keywords) {
foreach (TodoItemsScanner *scanner, m_scanners)
scanner->setParams(newSettings.keywords);
}
m_settings = newSettings;
updateList();
}
void TodoItemsProvider::projectSettingsChanged(Project *project)
{
Q_UNUSED(project)
updateList();
}
void TodoItemsProvider::updateList()
{
m_itemsList.clear();
// Show only items of the current file if any
if (m_settings.scanningScope == ScanningScopeCurrentFile) {
if (m_currentEditor)
m_itemsList = m_itemsHash.value(m_currentEditor->document()->filePath());
// Show only items of the current sub-project
} else if (m_settings.scanningScope == ScanningScopeSubProject) {
if (m_startupProject)
setItemsListWithinSubproject();
// Show only items of the startup project if any
} else if (m_startupProject) {
setItemsListWithinStartupProject();
}
m_itemsModel->todoItemsListUpdated();
}
void TodoItemsProvider::createScanners()
{
qRegisterMetaType<QList<TodoItem> >("QList<TodoItem>");
if (CppTools::CppModelManager::instance())
m_scanners << new CppTodoItemsScanner(m_settings.keywords, this);
if (QmlJS::ModelManagerInterface::instance())
m_scanners << new QmlJsTodoItemsScanner(m_settings.keywords, this);
foreach (TodoItemsScanner *scanner, m_scanners) {
connect(scanner, &TodoItemsScanner::itemsFetched, this,
&TodoItemsProvider::itemsFetched, Qt::QueuedConnection);
}
}
void TodoItemsProvider::setItemsListWithinStartupProject()
{
const auto filePaths = Utils::toSet(m_startupProject->files(Project::SourceFiles));
QVariantMap settings = m_startupProject->namedSettings(Constants::SETTINGS_NAME_KEY).toMap();
for (auto it = m_itemsHash.cbegin(), end = m_itemsHash.cend(); it != end; ++it) {
const FilePath filePath = it.key();
if (filePaths.contains(filePath)) {
bool skip = false;
for (const QVariant &pattern : settings[Constants::EXCLUDES_LIST_KEY].toList()) {
QRegExp re(pattern.toString());
if (re.indexIn(filePath.toString()) != -1) {
skip = true;
break;
}
}
if (!skip)
m_itemsList << it.value();
}
}
}
void TodoItemsProvider::setItemsListWithinSubproject()
{
// TODO prefer current editor as source of sub-project
const Node *node = ProjectTree::currentNode();
if (node) {
ProjectNode *projectNode = node->parentProjectNode();
if (projectNode) {
// FIXME: The name doesn't match the implementation that lists all files.
QSet<FilePath> subprojectFileNames;
projectNode->forEachGenericNode([&](Node *node) {
subprojectFileNames.insert(node->filePath());
});
// files must be both in the current subproject and the startup-project.
const auto fileNames = Utils::toSet(m_startupProject->files(Project::SourceFiles));
for (auto it = m_itemsHash.cbegin(), end = m_itemsHash.cend(); it != end; ++it) {
if (subprojectFileNames.contains(it.key()) && fileNames.contains(it.key()))
m_itemsList << it.value();
}
}
}
}
void TodoItemsProvider::itemsFetched(const QString &fileName, const QList<TodoItem> &items)
{
// Replace old items with new ones
m_itemsHash.insert(FilePath::fromString(fileName), items);
m_shouldUpdateList = true;
}
void TodoItemsProvider::startupProjectChanged(Project *project)
{
m_startupProject = project;
updateList();
}
void TodoItemsProvider::projectsFilesChanged()
{
updateList();
}
void TodoItemsProvider::currentEditorChanged(Core::IEditor *editor)
{
m_currentEditor = editor;
if (m_settings.scanningScope == ScanningScopeCurrentFile
|| m_settings.scanningScope == ScanningScopeSubProject) {
updateList();
}
}
void TodoItemsProvider::updateListTimeoutElapsed()
{
if (m_shouldUpdateList) {
m_shouldUpdateList = false;
updateList();
}
}
void TodoItemsProvider::setupStartupProjectBinding()
{
m_startupProject = SessionManager::startupProject();
connect(SessionManager::instance(), &SessionManager::startupProjectChanged,
this, &TodoItemsProvider::startupProjectChanged);
connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::fileListChanged,
this, &TodoItemsProvider::projectsFilesChanged);
}
void TodoItemsProvider::setupCurrentEditorBinding()
{
m_currentEditor = Core::EditorManager::currentEditor();
connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
this, &TodoItemsProvider::currentEditorChanged);
}
void TodoItemsProvider::setupUpdateListTimer()
{
m_shouldUpdateList = false;
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &TodoItemsProvider::updateListTimeoutElapsed);
timer->start(Constants::OUTPUT_PANE_UPDATE_INTERVAL);
}
void TodoItemsProvider::setupItemsModel()
{
m_itemsModel = new TodoItemsModel(this);
m_itemsModel->setTodoItemsList(&m_itemsList);
}
}
}
|