itom
Loading...
Searching...
No Matches
codeCheckerItem.h
1/* ********************************************************************
2itom software
3URL: http://www.uni-stuttgart.de/ito
4Copyright (C) 2020, Institut für Technische Optik (ITO),
5Universität Stuttgart, Germany
6
7This file is part of itom.
8
9itom is free software; you can redistribute it and/or modify it
10under the terms of the GNU Library General Public Licence as published by
11the Free Software Foundation; either version 2 of the Licence, or (at
12your option) any later version.
13
14itom is distributed in the hope that it will be useful, but
15WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17General Public Licence for more details.
18
19You should have received a copy of the GNU Library General Public License
20along with itom. If not, see <http://www.gnu.org/licenses/>.
21
22Further hints:
23------------------------
24
25This file belongs to the code editor of itom. The code editor is
26in major parts a fork / rewritten version of the python-based source
27code editor PyQode from Colin Duquesnoy and others
28(see https://github.com/pyQode). PyQode itself is licensed under
29the MIT License (MIT).
30
31Some parts of the code editor of itom are also inspired by the
32source code editor of the Spyder IDE (https://github.com/spyder-ide),
33also licensed under the MIT License and developed by the Spyder Project
34Contributors.
35
36*********************************************************************** */
37
38#ifndef CODECHECKERITEM_H
39#define CODECHECKERITEM_H
40
41#include <qstring.h>
42#include <qcolor.h>
43#include <qobject.h>
44
45namespace ito {
46
47 class CodeEditor;
48
49 //------------------------------------------------------
50 /*
51 Holds data for a message displayed by the
52 :class:`pyqode.core.modes.CheckerMode`.
53
54 One entry as a result of the code checker (linter, e.g. pyflakes, flake8...).
55 */
57 {
58
59 public:
60 enum CheckerType
61 {
62 Info = 0x001, //must be a bitmask
63 Warning = 0x002,
64 Error = 0x004,
65 };
66
67 //---------------------------------------------------------
68 /*
69 :param description: The message description (used as a tooltip)
70 :param status: The status associated with the message.
71 :param line: The message line number
72 :param col: The message start column (at the moment the message ends at
73 the end of the line).
74 :param icon: Unused, we keep it for backward compatibility.
75 :param color: Text decoration color
76 :param path: file path. Optional
77 */
78 explicit CodeCheckerItem(CheckerType messageType,
79 const QString &description,
80 const QString &messageCode = "",
81 int lineNumber = -1,
82 int col = -1,
83 const QString canonicalPath = QString()) :
84 m_description(description), //The description of the message, used as a tooltip.
85 m_code(messageCode),
86 m_type(messageType), //The status associated with the message
87 m_col(col), //: The start column (used for the text decoration). If the col is None,
88 //: the whole line is highlighted.
89 m_lineNumber(lineNumber),
90 m_filePath(canonicalPath)
91 {
92 if (m_color.isValid() == false)
93 {
94 m_color = statusToColor(m_type);
95 }
96 }
97
98 //-----------------------------------------------------------
99 CodeCheckerItem(const CodeCheckerItem &other) :
100 m_description(other.m_description), //The description of the message, used as a tooltip.
101 m_code(other.m_code),
102 m_type(other.m_type), //The status associated with the message
103 m_col(other.m_col), //: The start column (used for the text decoration). If the col is None,
104 //: the whole line is highlighted.
105 m_lineNumber(other.m_lineNumber),
106 m_filePath(other.m_filePath),
107 m_color(other.m_color)
108 {
109
110 }
111
112 //------------------------------------------------------------
113 CheckerType type() const
114 {
115 return m_type;
116 }
117
118 //------------------------------------------------------------
119 QString description() const
120 {
121 return m_description;
122 }
123
124 //------------------------------------------------------------
125 int lineNumber() const
126 {
127 return m_lineNumber;
128 }
129
130 //------------------------------------------------------------
131 /*
132 Converts a message status to a string.
133
134 :param status: Status to convert (p yqode.core.modes.CheckerMessages)
135 :return: The status string.
136 :rtype: str
137 */
138 static QString statusToString(CheckerType status)
139 {
140 switch (status)
141 {
142 case Info:
143 return QObject::tr("Info");
144 case Warning:
145 return QObject::tr("Warning");
146 case Error:
147 return QObject::tr("Error");
148 }
149 }
150
151 //------------------------------------------------------------
152 /*
153 Converts a message status to a color.
154
155 :param status: Status to convert (p yqode.core.modes.CheckerMessages)
156 :return: The status color.
157 :rtype: QColor
158 */
159 static QColor statusToColor(CheckerType status)
160 {
161 switch (status)
162 {
163 case Info:
164 return QColor("#4040DD");
165 case Warning:
166 return QColor("#DDDD40");
167 case Error:
168 default:
169 return QColor("#DD4040");
170 }
171 }
172
173 //----------------------------------------------------
174 /*
175 Returns the message status as a string.
176
177 :return: The status string.
178 */
179 QString statusString() const
180 {
181 return statusToString(m_type);
182 }
183
184 //----------------------------------------------------
185 /* return a string representation of this checker item. */
186 QString checkerItemText(bool addShortType = false, int wordWrapLength = -1) const
187 {
188 QString prefix;
189 QString text;
190
191 if (addShortType)
192 {
193 switch (m_type)
194 {
195 case Info:
196 prefix = QObject::tr("[I] ");
197 break;
198 case Warning:
199 prefix = QObject::tr("[W] ");
200 break;
201 case Error:
202 prefix = QObject::tr("[E] ");
203 break;
204 }
205
206 }
207
208 if (m_code.isEmpty())
209 {
210 if (m_col != -1)
211 {
212 text = QObject::tr("%1 (Column %2)").arg(prefix + m_description).arg(m_col + 1);
213 }
214 else
215 {
216 text = m_description;
217 }
218 }
219 else
220 {
221 if (m_col != -1)
222 {
223 text = QObject::tr("%1: %2 (Column %3)")
224 .arg(prefix + m_code)
225 .arg(m_description)
226 .arg(m_col + 1);
227 }
228 else
229 {
230 text = QString("%1: %2").arg(prefix + m_code).arg(m_description);
231 }
232 }
233
234 if (wordWrapLength > 0 && text.size() > wordWrapLength)
235 {
236 // text too long. Wrap it in multiple lines, but inherit
237 // every next line by 8 spaces.
238 QStringList words = text.split(" ");
239 int len = 0;
240 QStringList finalParts;
241 QStringList parts;
242
243 foreach (const QString& word, text.split(" "))
244 {
245 parts << word;
246 len += (word.size() + 1);
247
248 if (len >= wordWrapLength)
249 {
250 finalParts.append(parts.join(" "));
251 len = 0;
252 parts.clear();
253 }
254 }
255
256 if (len > 0)
257 {
258 finalParts.append(parts.join(" "));
259 }
260
261 text = finalParts.join("\n ");
262 }
263
264 return text;
265 }
266
267 private:
268 CheckerType m_type;
269 QString m_code; //code of the message
270 QString m_description;
271 QString m_filePath;
272 int m_lineNumber;
273 int m_col;
274 QColor m_color;
275 };
276
277} //end namespace ito
278
279#endif
Definition codeCheckerItem.h:57
Definition apiFunctionsGraph.cpp:40