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
|
/*
SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam <laszlo.kis-adam@kdemail.net>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "filteredproblemstore.h"
#include "problem.h"
#include "watcheddocumentset.h"
#include "problemstorenode.h"
#include <language/editor/documentrange.h>
#include <KLocalizedString>
using namespace KDevelop;
namespace
{
/// Adds diagnostics as sub-nodes
void addDiagnostics(ProblemStoreNode *node, const QVector<IProblem::Ptr> &diagnostics)
{
for (const IProblem::Ptr& ptr : diagnostics) {
auto *child = new ProblemNode(node, ptr);
node->addChild(child);
addDiagnostics(child, ptr->diagnostics());
}
}
/**
* @brief Base class for grouping strategy classes
*
* These classes build the problem tree based on the respective strategies
*/
class GroupingStrategy
{
public:
explicit GroupingStrategy( ProblemStoreNode *root )
: m_rootNode(root)
, m_groupedRootNode(new ProblemStoreNode())
{
}
virtual ~GroupingStrategy(){
}
/// Add a problem to the appropriate group
virtual void addProblem(const IProblem::Ptr &problem) = 0;
/// Find the specified noe
const ProblemStoreNode* findNode(int row, ProblemStoreNode *parent = nullptr) const
{
if (parent == nullptr)
return m_groupedRootNode->child(row);
else
return parent->child(row);
}
/// Returns the number of children nodes
int count(ProblemStoreNode *parent = nullptr)
{
if (parent == nullptr)
return m_groupedRootNode->count();
else
return parent->count();
}
/// Clears the problems
virtual void clear()
{
m_groupedRootNode->clear();
}
protected:
ProblemStoreNode* const m_rootNode;
QScopedPointer<ProblemStoreNode> m_groupedRootNode;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Implements no grouping strategy, that is just stores the problems without any grouping
class NoGroupingStrategy final : public GroupingStrategy
{
public:
explicit NoGroupingStrategy(ProblemStoreNode *root)
: GroupingStrategy(root)
{
}
void addProblem(const IProblem::Ptr &problem) override
{
auto *node = new ProblemNode(m_groupedRootNode.data(), problem);
addDiagnostics(node, problem->diagnostics());
m_groupedRootNode->addChild(node);
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Implements grouping based on path
class PathGroupingStrategy final : public GroupingStrategy
{
public:
explicit PathGroupingStrategy(ProblemStoreNode *root)
: GroupingStrategy(root)
{
}
void addProblem(const IProblem::Ptr &problem) override
{
QString path = problem->finalLocation().document.str();
/// See if we already have this path
const auto childrenNodes = m_groupedRootNode->children();
auto it = std::find_if(childrenNodes.begin(), childrenNodes.end(), [&](ProblemStoreNode* node) {
return (node->label() == path);
});
ProblemStoreNode* parent = (it != childrenNodes.end()) ? *it : nullptr;
/// If not add it!
if (parent == nullptr) {
parent = new LabelNode(m_groupedRootNode.data(), path);
m_groupedRootNode->addChild(parent);
}
auto *node = new ProblemNode(parent, problem);
addDiagnostics(node, problem->diagnostics());
parent->addChild(node);
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Implements grouping based on severity
class SeverityGroupingStrategy final : public GroupingStrategy
{
public:
enum SeverityGroups
{
GroupError = 0,
GroupWarning = 1,
GroupHint = 2
};
explicit SeverityGroupingStrategy(ProblemStoreNode *root)
: GroupingStrategy(root)
{
/// Create the groups on construction, so there's no need to search for them on addition
m_groupedRootNode->addChild(new LabelNode(m_groupedRootNode.data(), i18n("Error")));
m_groupedRootNode->addChild(new LabelNode(m_groupedRootNode.data(), i18n("Warning")));
m_groupedRootNode->addChild(new LabelNode(m_groupedRootNode.data(), i18n("Hint")));
}
void addProblem(const IProblem::Ptr &problem) override
{
ProblemStoreNode *parent = nullptr;
switch (problem->severity()) {
case IProblem::Error: parent = m_groupedRootNode->child(GroupError); break;
case IProblem::Warning: parent = m_groupedRootNode->child(GroupWarning); break;
case IProblem::Hint: parent = m_groupedRootNode->child(GroupHint); break;
default: break;
}
auto *node = new ProblemNode(m_groupedRootNode.data(), problem);
addDiagnostics(node, problem->diagnostics());
parent->addChild(node);
}
void clear() override
{
m_groupedRootNode->child(GroupError)->clear();
m_groupedRootNode->child(GroupWarning)->clear();
m_groupedRootNode->child(GroupHint)->clear();
}
};
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace KDevelop
{
class FilteredProblemStorePrivate
{
public:
explicit FilteredProblemStorePrivate(FilteredProblemStore* q)
: q(q)
, m_strategy(new NoGroupingStrategy(q->rootNode()))
, m_grouping(NoGrouping)
{
}
/// Tells if the problem matches the filters
bool match(const IProblem::Ptr &problem) const;
FilteredProblemStore* const q;
QScopedPointer<GroupingStrategy> m_strategy;
GroupingMethod m_grouping;
};
FilteredProblemStore::FilteredProblemStore(QObject *parent)
: ProblemStore(parent)
, d_ptr(new FilteredProblemStorePrivate(this))
{
}
FilteredProblemStore::~FilteredProblemStore()
{
}
void FilteredProblemStore::addProblem(const IProblem::Ptr &problem)
{
Q_D(FilteredProblemStore);
ProblemStore::addProblem(problem);
if (d->match(problem))
d->m_strategy->addProblem(problem);
}
const ProblemStoreNode* FilteredProblemStore::findNode(int row, ProblemStoreNode *parent) const
{
Q_D(const FilteredProblemStore);
return d->m_strategy->findNode(row, parent);
}
int FilteredProblemStore::count(ProblemStoreNode *parent) const
{
Q_D(const FilteredProblemStore);
return d->m_strategy->count(parent);
}
void FilteredProblemStore::clear()
{
Q_D(FilteredProblemStore);
d->m_strategy->clear();
ProblemStore::clear();
}
void FilteredProblemStore::rebuild()
{
Q_D(FilteredProblemStore);
emit beginRebuild();
d->m_strategy->clear();
const auto childrenNodes = rootNode()->children();
for (ProblemStoreNode* node : childrenNodes) {
IProblem::Ptr problem = node->problem();
if (d->match(problem)) {
d->m_strategy->addProblem(problem);
}
}
emit endRebuild();
}
void FilteredProblemStore::setGrouping(int grouping)
{
Q_D(FilteredProblemStore);
auto g = GroupingMethod(grouping);
if(g == d->m_grouping)
return;
d->m_grouping = g;
switch (g) {
case NoGrouping: d->m_strategy.reset(new NoGroupingStrategy(rootNode())); break;
case PathGrouping: d->m_strategy.reset(new PathGroupingStrategy(rootNode())); break;
case SeverityGrouping: d->m_strategy.reset(new SeverityGroupingStrategy(rootNode())); break;
}
rebuild();
emit changed();
}
int FilteredProblemStore::grouping() const
{
Q_D(const FilteredProblemStore);
return d->m_grouping;
}
bool FilteredProblemStorePrivate::match(const IProblem::Ptr &problem) const
{
if (q->scope() != ProblemScope::BypassScopeFilter &&
!q->documents()->get().contains(problem.data()->finalLocation().document) &&
!(q->showImports() && q->documents()->imports().contains(problem.data()->finalLocation().document)))
return false;
if(problem->severity()!=IProblem::NoSeverity)
{
/// If the problem severity isn't in the filter severities it's discarded
if(!q->severities().testFlag(problem->severity()))
return false;
}
else
{
if(!q->severities().testFlag(IProblem::Hint))//workaround for problems without correctly set severity
return false;
}
return true;
}
}
|