Changeset View
Changeset View
Standalone View
Standalone View
plugins/clazy/checks_db.cpp
- This file was added.
1 | /* This file is part of KDevelop | ||||
---|---|---|---|---|---|
2 | | ||||
3 | Copyright 2018 Anton Anikin <anton@anikin.xyz> | ||||
4 | | ||||
5 | This program is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2 of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This program is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU General Public License | ||||
16 | along with this program; see the file COPYING. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "checks_db.h" | ||||
22 | | ||||
23 | #include "globalsettings.h" | ||||
24 | #include "utils.h" | ||||
25 | | ||||
26 | #include <klocalizedstring.h> | ||||
27 | | ||||
28 | #include <QDir> | ||||
29 | #include <QRegularExpression> | ||||
30 | | ||||
31 | namespace Clazy | ||||
32 | { | ||||
33 | | ||||
34 | ChecksDB::ChecksDB(const QUrl& docsPath) | ||||
35 | { | ||||
36 | static const QHash<QString, QString> levelName = { | ||||
37 | { QStringLiteral("manuallevel"), QStringLiteral("manual") }, | ||||
38 | { QStringLiteral("hiddenlevel"), QStringLiteral("manual") } | ||||
39 | }; | ||||
40 | | ||||
41 | static const QHash<QString, QString> levelDisplayName = { | ||||
42 | { QStringLiteral("level0"), i18n("Level 0") }, | ||||
43 | { QStringLiteral("level1"), i18n("Level 1") }, | ||||
44 | { QStringLiteral("level2"), i18n("Level 2") }, | ||||
45 | { QStringLiteral("level3"), i18n("Level 3") }, | ||||
46 | { QStringLiteral("manual"), i18n("Manual Level") } | ||||
47 | }; | ||||
48 | | ||||
49 | static const QHash<QString, QString> levelDescription = { | ||||
50 | { QStringLiteral("level0"), | ||||
51 | i18n("Very stable checks, 99.99% safe, mostly no false-positives, very desirable.") }, | ||||
52 | | ||||
53 | { QStringLiteral("level1"), | ||||
54 | i18n("The default level. Very similar to level 0, slightly more false-positives but very few.") }, | ||||
55 | | ||||
56 | { QStringLiteral("level2"), | ||||
57 | i18n("Also very few false-positives, but contains noisy checks which not everyone agree should be default.") }, | ||||
58 | | ||||
59 | { QStringLiteral("level3"), | ||||
60 | i18n("Contains checks with high rate of false-positives.") }, | ||||
61 | | ||||
62 | { QStringLiteral("manual"), | ||||
63 | i18n("Checks here need to be enabled explicitly, as they don't belong to any level. " | ||||
64 | "Checks here are very stable and have very few false-positives.") } | ||||
65 | }; | ||||
66 | | ||||
67 | const QString defaultError = i18n( | ||||
68 | "Unable to load clazy checks information from '%1'. Please check your settings.", | ||||
69 | docsPath.toLocalFile()); | ||||
70 | | ||||
71 | QDir docsDir(docsPath.toLocalFile()); | ||||
72 | if (!docsDir.exists()) { | ||||
73 | m_error = defaultError; | ||||
74 | return; | ||||
75 | } | ||||
76 | | ||||
77 | auto levelsDirs = docsDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); | ||||
78 | for (const auto& levelDir : levelsDirs) { | ||||
79 | static const QRegularExpression levelRE(QStringLiteral(".*level.*")); | ||||
80 | | ||||
81 | auto match = levelRE.match(levelDir); | ||||
82 | if (!match.hasMatch()) { | ||||
83 | continue; | ||||
84 | } | ||||
85 | | ||||
86 | if (!docsDir.cd(levelDir)) { | ||||
87 | continue; | ||||
88 | } | ||||
89 | | ||||
90 | auto level = new Level; | ||||
91 | level->name = levelName.value(levelDir, levelDir); | ||||
92 | level->displayName = levelDisplayName.value(level->name, levelDir); | ||||
93 | level->description = levelDescription.value(level->name, {}); | ||||
94 | | ||||
95 | auto checksFiles = docsDir.entryList(QDir::Files | QDir::Readable); | ||||
96 | for (const auto& checkFile : checksFiles) { | ||||
97 | static const QRegularExpression checkRE(QStringLiteral("^README-(.+)\\.md$")); | ||||
98 | | ||||
99 | auto match = checkRE.match(checkFile); | ||||
100 | if (!match.hasMatch()) { | ||||
101 | continue; | ||||
102 | } | ||||
103 | | ||||
104 | QFile mdFile(docsDir.absoluteFilePath(checkFile)); | ||||
105 | if (!mdFile.open(QIODevice::ReadOnly)) { | ||||
106 | continue; | ||||
107 | } | ||||
108 | | ||||
109 | auto check = new Check; | ||||
110 | check->level = level; | ||||
111 | check->name = match.captured(1); | ||||
112 | check->description = markdown2html(mdFile.readAll()); | ||||
113 | check->url = QUrl(QStringLiteral( | ||||
114 | "https://github.com/KDE/clazy/blob/master/docs/checks/README-%1.md").arg(check->name)); | ||||
115 | level->checks[check->name] = check; | ||||
116 | | ||||
117 | m_checks[check->name] = check; | ||||
118 | } | ||||
119 | | ||||
120 | if (level->checks.isEmpty()) { | ||||
121 | delete level; | ||||
122 | } else { | ||||
123 | m_levels[level->name] = level; | ||||
124 | } | ||||
125 | | ||||
126 | docsDir.cdUp(); | ||||
127 | } | ||||
128 | | ||||
129 | if (m_levels.isEmpty()) { | ||||
130 | m_error = defaultError; | ||||
131 | } | ||||
132 | } | ||||
133 | | ||||
134 | ChecksDB::~ChecksDB() | ||||
135 | { | ||||
136 | qDeleteAll(m_levels); | ||||
137 | qDeleteAll(m_checks); | ||||
138 | } | ||||
139 | | ||||
140 | bool ChecksDB::isValid() const | ||||
141 | { | ||||
142 | return m_error.isEmpty(); | ||||
143 | } | ||||
144 | | ||||
145 | QString ChecksDB::error() const | ||||
146 | { | ||||
147 | return m_error; | ||||
148 | } | ||||
149 | | ||||
150 | const QMap<QString, Level*>& ChecksDB::levels() const | ||||
151 | { | ||||
152 | return m_levels; | ||||
153 | } | ||||
154 | | ||||
155 | const QMap<QString, Check*>& ChecksDB::checks() const | ||||
156 | { | ||||
157 | return m_checks; | ||||
158 | } | ||||
159 | | ||||
160 | } |