Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmldomlinewriter.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtCore/QCoreApplication>
6#include <QtCore/QRegularExpression>
7
9namespace QQmlJS {
10namespace Dom {
11
13{
14 return value.offset;
15}
16
18{
19 return value.offset + value.length;
20}
21
23 qint32 lineChange)
24{
25 if (offset < utf16Start()) {
26 if (change < 0 && offset - change >= utf16Start()) {
27 int c1 = offset - utf16Start();
28 int c2 = offset - change - utf16Start();
29 change = c1;
30 if (value.length < quint32(c2))
31 value.length = 0;
32 else
33 value.length -= c2;
34 }
35 value.offset += change;
36 value.startColumn += colChange;
37 value.startLine += lineChange;
38 } else if (offset < utf16End()) {
39 if (change < 0 && offset - change > utf16End())
40 change = offset - utf16End();
41 value.length += change;
42 }
43}
44
46{
47 if (toUpdate)
48 *toUpdate = value;
49 if (updater)
51}
52
54 int lineNr, int columnNr, int utf16Offset, QString currentLine)
55 : m_innerSinks({ innerSink }),
56 m_fileName(fileName),
57 m_lineNr(lineNr),
58 m_columnNr(columnNr),
59 m_currentColumnNr(columnNr),
60 m_utf16Offset(utf16Offset),
61 m_currentLine(currentLine),
62 m_options(options)
63{
64}
65
67{
68 int nToAdd = nNewline;
69 if (nToAdd <= 0)
70 return *this;
72 --nToAdd;
73 if (m_committedEmptyLines >= unsigned(nToAdd))
74 return *this;
75 nToAdd -= m_committedEmptyLines;
76 }
77 for (int i = 0; i < nToAdd; ++i)
78 write(u"\n", t);
79 return *this;
80}
81
83{
85 write(u" ", t);
86 return *this;
87}
88
90{
91 int tabSize = m_options.formatOptions.tabSize;
92 IndentInfo ind(space, tabSize);
93 auto cc = counter();
94 if (ind.nNewlines > 0)
96 if (cc != counter() || m_currentLine.isEmpty()
99 else {
100 int len = m_currentLine.size();
101 int i = len;
102 while (i != 0 && m_currentLine.at(i - 1).isSpace())
103 --i;
104 QStringView trailingSpace = QStringView(m_currentLine).mid(i, len - i);
105 int trailingSpaceStartColumn =
107 IndentInfo indExisting(trailingSpace, tabSize, trailingSpaceStartColumn);
108 if (trailingSpaceStartColumn != 0)
109 ind = IndentInfo(space, tabSize, trailingSpaceStartColumn);
110 if (i == 0) {
111 if (indExisting.column < ind.column) {
112 qint32 utf16Change = ind.trailingString.size() - trailingSpace.size();
113 m_currentColumnNr += ind.trailingString.size() - trailingSpace.size();
115 i, len - i, ind.trailingString.toString()); // invalidates most QStringViews
116 changeAtOffset(i, utf16Change, utf16Change, 0);
117 lineChanged();
118 }
119 } else if (indExisting.column < ind.column) { // use just spaces if not at start of a line
120 write(QStringLiteral(u" ").repeated(ind.column - indExisting.column), t);
121 }
122 }
123 return *this;
124}
125
127{
128 switch (m_options.lineEndings) {
130 return QStringLiteral(u"\n");
132 return QStringLiteral(u"\r\n");
134 return QStringLiteral(u"\r");
135 }
136 Q_ASSERT(false);
137 return QStringLiteral(u"\n");
138}
139
140template<typename String, typename ...Args>
142{
143 return re.matchView(s, args...);
144}
145
147{
148 QString eol;
149 // split multiple lines
151 "(\r?\n|\r)")); // does not support split of \r and \n for windows style line endings
153 if (m.hasMatch()) {
154 // add line by line
155 auto i = m.capturedStart(1);
156 auto iEnd = m.capturedEnd(1);
157 eol = eolToWrite();
158 // offset change (eol used vs input) cannot affect things,
159 // because we cannot have already opened or closed a PendingSourceLocation
160 if (iEnd < v.size()) {
161 write(v.mid(0, iEnd));
162 m = matchHelper(eolRe, v, iEnd);
163 while (m.hasMatch()) {
164 write(v.mid(iEnd, m.capturedEnd(1) - iEnd));
165 iEnd = m.capturedEnd(1);
166 m = matchHelper(eolRe, v, iEnd);
167 }
168 if (iEnd < v.size())
169 write(v.mid(iEnd, v.size() - iEnd));
170 return *this;
171 }
172 QStringView toAdd = v.mid(0, i);
173 if (!toAdd.trimmed().isEmpty())
174 textAddCallback(tAdd);
175 m_counter += i;
176 m_currentLine.append(toAdd);
179 lineChanged();
180 } else {
181 if (!v.trimmed().isEmpty())
182 textAddCallback(tAdd);
183 m_counter += v.size();
187 lineChanged();
188 }
189 if (!eol.isEmpty()
191 reindentAndSplit(eol);
192 }
193 return *this;
194}
195
197{
198 if (m_currentLine.size() > 0)
200}
201
202void LineWriter::eof(bool shouldEnsureNewline)
203{
204 if (shouldEnsureNewline)
206 reindentAndSplit(QString(), true);
207}
208
210{
212}
213
215{
218 res.value = currentSourceLocation();
219 res.toUpdate = toUpdate;
220 m_pendingSourceLocations.insert(res.id, res);
221 return res.id;
222}
223
225{
228 res.value = currentSourceLocation();
229 res.updater = updater;
230 m_pendingSourceLocations.insert(res.id, res);
231 return res.id;
232}
233
235{
236 if (m_pendingSourceLocations.contains(slId)) {
237 auto &pLoc = m_pendingSourceLocations[slId];
238 if (!pLoc.open) {
239 qWarning() << "Trying to close already closed PendingSourceLocation" << int(slId);
240 }
241 pLoc.open = false;
242 pLoc.value.length = m_utf16Offset + m_currentLine.size() - pLoc.value.offset;
243 } else {
244 qWarning() << "Trying to close non existing PendingSourceLocation" << int(slId);
245 }
246}
247
248int LineWriter::addTextAddCallback(std::function<bool(LineWriter &, TextAddType)> callback)
249{
250 int nextId = ++m_lastCallbackId;
251 Q_ASSERT(nextId != 0);
252 if (callback)
253 m_textAddCallbacks.insert(nextId, callback);
254 return nextId;
255}
256
258{
259 return addTextAddCallback([nLines](LineWriter &self, TextAddType t) {
260 if (t == TextAddType::Normal) {
261 quint32 c = self.counter();
262 QString spacesToPreserve;
263 bool spaceOnly = QStringView(self.m_currentLine).trimmed().isEmpty();
264 if (spaceOnly && !self.m_currentLine.isEmpty())
265 spacesToPreserve = self.m_currentLine;
266 self.ensureNewline(nLines, LineWriter::TextAddType::Extra);
267 if (self.counter() != c && !spacesToPreserve.isEmpty())
268 self.write(spacesToPreserve, TextAddType::Extra);
269 return false;
270 } else {
271 return true;
272 }
273 });
274}
275
276void LineWriter::setLineIndent(int indentAmount)
277{
278 int startNonSpace = 0;
279 while (startNonSpace < m_currentLine.size() && m_currentLine.at(startNonSpace).isSpace())
280 ++startNonSpace;
281 int oldColumn = column(startNonSpace);
282 if (indentAmount >= 0) {
283 QString indent;
285 indent = QStringLiteral(u"\t").repeated(indentAmount / m_options.formatOptions.tabSize)
286 + QStringLiteral(u" ").repeated(indentAmount % m_options.formatOptions.tabSize);
287 } else {
288 indent = QStringLiteral(u" ").repeated(indentAmount);
289 }
290 if (indent != m_currentLine.mid(0, startNonSpace)) {
291 quint32 colChange = indentAmount - oldColumn;
292 m_currentColumnNr += colChange;
293 qint32 oChange = indent.size() - startNonSpace;
294 m_currentLine = indent + m_currentLine.mid(startNonSpace);
296 lineChanged();
297 changeAtOffset(m_utf16Offset, oChange, oChange, 0);
298 }
299 }
300}
301
303{
304 switch (trailingSpace) {
306 break;
308 int lastNonSpace = m_currentLine.size();
309 while (lastNonSpace > 0 && m_currentLine.at(lastNonSpace - 1).isSpace())
310 --lastNonSpace;
311 if (lastNonSpace != m_currentLine.size()) {
312 qint32 oChange = lastNonSpace - m_currentLine.size();
313 m_currentLine = m_currentLine.mid(0, lastNonSpace);
314 changeAtOffset(m_utf16Offset + lastNonSpace, oChange, oChange, 0);
316 column(m_currentLine.size()); // to be extra accurate in the potential split
317 lineChanged();
318 }
319 } break;
320 }
321}
322
324{
325 // maybe write out
326 if (!eol.isEmpty() || eof) {
328 commitLine(eol);
329 }
330}
331
333{
336}
337
338void LineWriter::changeAtOffset(quint32 offset, qint32 change, qint32 colChange, qint32 lineChange)
339{
340 auto iEnd = m_pendingSourceLocations.end();
341 auto i = m_pendingSourceLocations.begin();
342 while (i != iEnd) {
343 i.value().changeAtOffset(offset, change, colChange, lineChange);
344 ++i;
345 }
346}
347
349{
350 if (index > m_currentLine.size())
353 m_columnNr);
354 return iInfo.column;
355}
356
358{
359 if (m_textAddCallbacks.isEmpty())
360 return;
361 int iNow = (--m_textAddCallbacks.end()).key() + 1;
362 while (true) {
363 auto it = m_textAddCallbacks.lowerBound(iNow);
364 if (it == m_textAddCallbacks.begin())
365 break;
366 --it;
367 iNow = it.key();
368 if (!it.value()(*this, t))
369 m_textAddCallbacks.erase(it);
370 }
371}
372
373void LineWriter::commitLine(QString eol, TextAddType tType, int untilChar)
374{
375 if (untilChar == -1)
376 untilChar = m_currentLine.size();
377 bool isSpaceOnly = QStringView(m_currentLine).mid(0, untilChar).trimmed().isEmpty();
378 bool isEmptyNewline = !eol.isEmpty() && isSpaceOnly;
379 quint32 endCommit = m_utf16Offset + untilChar;
380 // update position, lineNr,...
381 // write out
382 for (SinkF &sink : m_innerSinks)
383 sink(m_currentLine.mid(0, untilChar));
384 m_utf16Offset += untilChar;
385 if (!eol.isEmpty()) {
386 m_utf16Offset += eol.size();
387 for (SinkF &sink : m_innerSinks)
388 sink(eol);
389 ++m_lineNr;
390 int oldCol = column(untilChar);
391 m_columnNr = 0;
393 changeAtOffset(m_utf16Offset, 0, -oldCol, 1);
394 } else {
395 m_columnNr = column(untilChar);
396 m_lineUtf16Offset += untilChar;
397 }
398 if (untilChar == m_currentLine.size()) {
399 willCommit();
401 } else {
402 QString nextLine = m_currentLine.mid(untilChar);
403 m_currentLine = m_currentLine.mid(0, untilChar);
404 lineChanged();
405 willCommit();
406 m_currentLine = nextLine;
407 }
408 lineChanged();
410 TextAddType notifyType = tType;
411 switch (tType) {
413 if (eol.isEmpty())
414 notifyType = TextAddType::PartialCommit;
415 else
416 notifyType = TextAddType::Newline;
417 break;
419 if (eol.isEmpty())
420 notifyType = TextAddType::NewlineExtra;
421 else
422 notifyType = TextAddType::PartialCommit;
423 break;
428 case TextAddType::Eof:
429 break;
430 }
431 if (isEmptyNewline)
433 else if (!isSpaceOnly)
435 // commit finished pending
436 auto iEnd = m_pendingSourceLocations.end();
437 auto i = m_pendingSourceLocations.begin();
438 while (i != iEnd) {
439 auto &pLoc = i.value();
440 if (!pLoc.open && pLoc.utf16End() <= endCommit) {
441 pLoc.commit();
443 } else {
444 ++i;
445 }
446 }
447 // notify
448 textAddCallback(notifyType);
449}
450
451} // namespace Dom
452} // namespace QQmlJS
454
455#include "moc_qqmldomlinewriter_p.cpp"
\inmodule QtCore
Definition qatomic.h:112
constexpr bool isSpace() const noexcept
Returns true if the character is a separator character (Separator_* categories or certain code points...
Definition qchar.h:466
void commitLine(QString eol, TextAddType t=TextAddType::Normal, int untilChar=-1)
LineWriter & write(QStringView v, TextAddType tType=TextAddType::Normal)
void endSourceLocation(PendingSourceLocationId)
void handleTrailingSpace(LineWriterOptions::TrailingSpace s)
LineWriter & ensureNewline(int nNewlines=1, TextAddType t=TextAddType::Extra)
PendingSourceLocationId startSourceLocation(SourceLocation *)
QMap< PendingSourceLocationId, PendingSourceLocation > m_pendingSourceLocations
QMap< int, std::function< bool(LineWriter &, TextAddType)> > m_textAddCallbacks
PendingSourceLocationId m_lastSourceLocationId
LineWriter(SinkF innerSink, QString fileName, const LineWriterOptions &options=LineWriterOptions(), int lineNr=0, int columnNr=0, int utf16Offset=0, QString currentLine=QString())
virtual void reindentAndSplit(QString eol, bool eof=false)
LineWriter & ensureSpace(TextAddType t=TextAddType::Extra)
void eof(bool ensureNewline=true)
std::function< void(QStringView)> sink()
int addNewlinesAutospacerCallback(int nLines)
void changeAtOffset(quint32 offset, qint32 change, qint32 colChange, qint32 lineChange)
SourceLocation currentSourceLocation() const
SourceLocation committedLocation() const
void setLineIndent(int indentAmount)
int addTextAddCallback(std::function< bool(LineWriter &, TextAddType)> callback)
void textAddCallback(TextAddType t)
std::function< void(SourceLocation)> updater
void changeAtOffset(quint32 offset, qint32 change, qint32 colChange, qint32 lineChange)
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
QRegularExpressionMatch matchView(QStringView subjectView, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
\inmodule QtCore
Definition qstringview.h:76
constexpr bool isEmpty() const noexcept
Returns whether this string view is empty - that is, whether {size() == 0}.
constexpr qsizetype size() const noexcept
Returns the size of this string view, in UTF-16 code units (that is, surrogate pairs count as two for...
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1014
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
QStringView trimmed() const noexcept
Strips leading and trailing whitespace and returns the result.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString & replace(qsizetype i, qsizetype len, QChar after)
Definition qstring.cpp:3794
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1107
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
Definition qstring.cpp:5204
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
Definition qstring.h:1079
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QString & append(QChar c)
Definition qstring.cpp:3227
QString trimmed() const &
Definition qstring.h:380
QSet< QString >::iterator it
std::function< void(QStringView)> SinkF
static QRegularExpressionMatch matchHelper(QRegularExpression &re, String &&s, Args &&...args)
Combined button and popup list for selecting options.
SharedPointerFileDialogOptions m_options
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qWarning
Definition qlogging.h:162
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLuint64 key
GLuint index
[2]
GLenum GLuint GLintptr offset
GLenum GLenum GLsizei void GLsizei void * column
GLuint res
const GLubyte * c
GLenum GLsizei len
GLdouble GLdouble t
Definition qopenglext.h:243
GLsizei GLenum GLboolean sink
GLdouble s
[6]
Definition qopenglext.h:235
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
unsigned int quint32
Definition qtypes.h:45
int qint32
Definition qtypes.h:44
static uint nextId
MyCustomStruct c2
QJSValueList args