Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 const SinkF &innerSink, const QString &fileName, const LineWriterOptions &options,
55 int lineNr, int columnNr, int utf16Offset, const QString &currentLine)
56 : m_innerSinks({ innerSink }),
57 m_fileName(fileName),
58 m_lineNr(lineNr),
59 m_columnNr(columnNr),
60 m_currentColumnNr(columnNr),
61 m_utf16Offset(utf16Offset),
62 m_currentLine(currentLine),
63 m_options(options)
64{
65}
66
68{
69 int nToAdd = nNewline;
70 if (nToAdd <= 0)
71 return *this;
73 --nToAdd;
74 if (m_committedEmptyLines >= unsigned(nToAdd))
75 return *this;
76 nToAdd -= m_committedEmptyLines;
77 }
78 for (int i = 0; i < nToAdd; ++i)
79 write(u"\n", t);
80 return *this;
81}
82
84{
85 if (!m_currentLine.isEmpty() && !m_currentLine.at(m_currentLine.size() - 1).isSpace())
86 write(u" ", t);
87 return *this;
88}
89
91{
92 int tabSize = m_options.formatOptions.tabSize;
93 IndentInfo ind(space, tabSize);
94 auto cc = counter();
95 if (ind.nNewlines > 0)
97 if (cc != counter() || m_currentLine.isEmpty()
98 || !m_currentLine.at(m_currentLine.size() - 1).isSpace())
100 else {
101 int len = m_currentLine.size();
102 int i = len;
103 while (i != 0 && m_currentLine.at(i - 1).isSpace())
104 --i;
105 QStringView trailingSpace = QStringView(m_currentLine).mid(i, len - i);
106 int trailingSpaceStartColumn =
108 IndentInfo indExisting(trailingSpace, tabSize, trailingSpaceStartColumn);
109 if (trailingSpaceStartColumn != 0)
110 ind = IndentInfo(space, tabSize, trailingSpaceStartColumn);
111 if (i == 0) {
112 if (indExisting.column < ind.column) {
113 qint32 utf16Change = ind.trailingString.size() - trailingSpace.size();
114 m_currentColumnNr += ind.trailingString.size() - trailingSpace.size();
116 i, len - i, ind.trailingString.toString()); // invalidates most QStringViews
117 changeAtOffset(i, utf16Change, utf16Change, 0);
118 lineChanged();
119 }
120 } else if (indExisting.column < ind.column) { // use just spaces if not at start of a line
121 write(QStringLiteral(u" ").repeated(ind.column - indExisting.column), t);
122 }
123 }
124 return *this;
125}
126
128{
129 switch (m_options.lineEndings) {
131 return QStringLiteral(u"\n");
133 return QStringLiteral(u"\r\n");
135 return QStringLiteral(u"\r");
136 }
137 Q_ASSERT(false);
138 return QStringLiteral(u"\n");
139}
140
141template<typename String, typename ...Args>
143{
144 return re.matchView(s, args...);
145}
146
148{
149 QString eol;
150 // split multiple lines
152 "(\r?\n|\r)")); // does not support split of \r and \n for windows style line endings
154 if (m.hasMatch()) {
155 // add line by line
156 auto i = m.capturedStart(1);
157 auto iEnd = m.capturedEnd(1);
158 eol = eolToWrite();
159 // offset change (eol used vs input) cannot affect things,
160 // because we cannot have already opened or closed a PendingSourceLocation
161 if (iEnd < v.size()) {
162 write(v.mid(0, iEnd));
163 m = matchHelper(eolRe, v, iEnd);
164 while (m.hasMatch()) {
165 write(v.mid(iEnd, m.capturedEnd(1) - iEnd));
166 iEnd = m.capturedEnd(1);
167 m = matchHelper(eolRe, v, iEnd);
168 }
169 if (iEnd < v.size())
170 write(v.mid(iEnd, v.size() - iEnd));
171 return *this;
172 }
173 QStringView toAdd = v.mid(0, i);
174 if (!toAdd.trimmed().isEmpty())
175 textAddCallback(tAdd);
176 m_counter += i;
177 m_currentLine.append(toAdd);
180 lineChanged();
181 } else {
182 if (!v.trimmed().isEmpty())
183 textAddCallback(tAdd);
184 m_counter += v.size();
188 lineChanged();
189 }
190 if (!eol.isEmpty()
192 reindentAndSplit(eol);
193 }
194 return *this;
195}
196
198{
199 if (m_currentLine.size() > 0)
201}
202
203void LineWriter::eof(bool shouldEnsureNewline)
204{
205 if (shouldEnsureNewline)
207 reindentAndSplit(QString(), true);
208}
209
214
216{
219 res.value = currentSourceLocation();
220 res.toUpdate = toUpdate;
221 m_pendingSourceLocations.insert(res.id, res);
222 return res.id;
223}
224
226{
229 res.value = currentSourceLocation();
230 res.updater = updater;
231 m_pendingSourceLocations.insert(res.id, res);
232 return res.id;
233}
234
236{
237 if (m_pendingSourceLocations.contains(slId)) {
238 auto &pLoc = m_pendingSourceLocations[slId];
239 if (!pLoc.open) {
240 qWarning() << "Trying to close already closed PendingSourceLocation" << int(slId);
241 }
242 pLoc.open = false;
243 pLoc.value.length = m_utf16Offset + m_currentLine.size() - pLoc.value.offset;
244 } else {
245 qWarning() << "Trying to close non existing PendingSourceLocation" << int(slId);
246 }
247}
248
249int LineWriter::addTextAddCallback(std::function<bool(LineWriter &, TextAddType)> callback)
250{
251 int nextId = ++m_lastCallbackId;
252 Q_ASSERT(nextId != 0);
253 if (callback)
254 m_textAddCallbacks.insert(nextId, callback);
255 return nextId;
256}
257
259{
260 return addTextAddCallback([nLines](LineWriter &self, TextAddType t) {
261 if (t == TextAddType::Normal) {
262 quint32 c = self.counter();
263 QString spacesToPreserve;
264 bool spaceOnly = QStringView(self.m_currentLine).trimmed().isEmpty();
265 if (spaceOnly && !self.m_currentLine.isEmpty())
266 spacesToPreserve = self.m_currentLine;
267 self.ensureNewline(nLines, LineWriter::TextAddType::Extra);
268 if (self.counter() != c && !spacesToPreserve.isEmpty())
269 self.write(spacesToPreserve, TextAddType::Extra);
270 return false;
271 } else {
272 return true;
273 }
274 });
275}
276
277void LineWriter::setLineIndent(int indentAmount)
278{
279 int startNonSpace = 0;
280 while (startNonSpace < m_currentLine.size() && m_currentLine.at(startNonSpace).isSpace())
281 ++startNonSpace;
282 int oldColumn = column(startNonSpace);
283 if (indentAmount >= 0) {
284 QString indent;
286 indent = QStringLiteral(u"\t").repeated(indentAmount / m_options.formatOptions.tabSize)
287 + QStringLiteral(u" ").repeated(indentAmount % m_options.formatOptions.tabSize);
288 } else {
289 indent = QStringLiteral(u" ").repeated(indentAmount);
290 }
291 if (indent != m_currentLine.mid(0, startNonSpace)) {
292 quint32 colChange = indentAmount - oldColumn;
293 m_currentColumnNr += colChange;
294 qint32 oChange = indent.size() - startNonSpace;
295 m_currentLine = indent + m_currentLine.mid(startNonSpace);
297 lineChanged();
298 changeAtOffset(m_utf16Offset, oChange, oChange, 0);
299 }
300 }
301}
302
304{
305 switch (trailingSpace) {
307 break;
309 int lastNonSpace = m_currentLine.size();
310 while (lastNonSpace > 0 && m_currentLine.at(lastNonSpace - 1).isSpace())
311 --lastNonSpace;
312 if (lastNonSpace != m_currentLine.size()) {
313 qint32 oChange = lastNonSpace - m_currentLine.size();
314 m_currentLine = m_currentLine.mid(0, lastNonSpace);
315 changeAtOffset(m_utf16Offset + lastNonSpace, oChange, oChange, 0);
317 column(m_currentLine.size()); // to be extra accurate in the potential split
318 lineChanged();
319 }
320 } break;
321 }
322}
323
324void LineWriter::reindentAndSplit(const QString &eol, bool eof)
325{
326 // maybe write out
327 if (!eol.isEmpty() || eof) {
329 commitLine(eol);
330 }
331}
332
338
339void LineWriter::changeAtOffset(quint32 offset, qint32 change, qint32 colChange, qint32 lineChange)
340{
341 auto iEnd = m_pendingSourceLocations.end();
342 auto i = m_pendingSourceLocations.begin();
343 while (i != iEnd) {
344 i.value().changeAtOffset(offset, change, colChange, lineChange);
345 ++i;
346 }
347}
348
350{
351 if (index > m_currentLine.size())
354 m_columnNr);
355 return iInfo.column;
356}
357
359{
360 if (m_textAddCallbacks.isEmpty())
361 return;
362 int iNow = (--m_textAddCallbacks.end()).key() + 1;
363 while (true) {
364 auto it = m_textAddCallbacks.lowerBound(iNow);
365 if (it == m_textAddCallbacks.begin())
366 break;
367 --it;
368 iNow = it.key();
369 if (!it.value()(*this, t))
370 m_textAddCallbacks.erase(it);
371 }
372}
373
374void LineWriter::commitLine(const QString &eol, TextAddType tType, int untilChar)
375{
376 if (untilChar == -1)
377 untilChar = m_currentLine.size();
378 bool isSpaceOnly = QStringView(m_currentLine).mid(0, untilChar).trimmed().isEmpty();
379 bool isEmptyNewline = !eol.isEmpty() && isSpaceOnly;
380 quint32 endCommit = m_utf16Offset + untilChar;
381 // update position, lineNr,...
382 // write out
383 for (SinkF &sink : m_innerSinks)
384 sink(m_currentLine.mid(0, untilChar));
385 m_utf16Offset += untilChar;
386 if (!eol.isEmpty()) {
387 m_utf16Offset += eol.size();
388 for (SinkF &sink : m_innerSinks)
389 sink(eol);
390 ++m_lineNr;
391 int oldCol = column(untilChar);
392 m_columnNr = 0;
394 changeAtOffset(m_utf16Offset, 0, -oldCol, 1);
395 } else {
396 m_columnNr = column(untilChar);
397 m_lineUtf16Offset += untilChar;
398 }
399 if (untilChar == m_currentLine.size()) {
400 willCommit();
402 } else {
403 QString nextLine = m_currentLine.mid(untilChar);
404 m_currentLine = m_currentLine.mid(0, untilChar);
405 lineChanged();
406 willCommit();
407 m_currentLine = nextLine;
408 }
409 lineChanged();
411 TextAddType notifyType = tType;
412 switch (tType) {
414 if (eol.isEmpty())
415 notifyType = TextAddType::PartialCommit;
416 else
417 notifyType = TextAddType::Newline;
418 break;
420 if (eol.isEmpty())
421 notifyType = TextAddType::NewlineExtra;
422 else
423 notifyType = TextAddType::PartialCommit;
424 break;
429 case TextAddType::Eof:
430 break;
431 }
432 if (isEmptyNewline)
434 else if (!isSpaceOnly)
436 // commit finished pending
437 auto iEnd = m_pendingSourceLocations.end();
438 auto i = m_pendingSourceLocations.begin();
439 while (i != iEnd) {
440 auto &pLoc = i.value();
441 if (!pLoc.open && pLoc.utf16End() <= endCommit) {
442 pLoc.commit();
444 } else {
445 ++i;
446 }
447 }
448 // notify
449 textAddCallback(notifyType);
450}
451
452} // namespace Dom
453} // namespace QQmlJS
455
456#include "moc_qqmldomlinewriter_p.cpp"
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
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)
virtual void reindentAndSplit(const QString &eol, bool eof=false)
void commitLine(const QString &eol, TextAddType t=TextAddType::Normal, int untilChar=-1)
SourceLocation currentSourceLocation() const
PendingSourceLocationIdAtomic m_lastSourceLocationId
SourceLocation committedLocation() const
QMap< int, std::function< bool(LineWriter &, TextAddType)> m_textAddCallbacks)
void setLineIndent(int indentAmount)
int addTextAddCallback(std::function< bool(LineWriter &, TextAddType)> callback)
void textAddCallback(TextAddType t)
LineWriter(const SinkF &innerSink, const QString &fileName, const LineWriterOptions &options=LineWriterOptions(), int lineNr=0, int columnNr=0, int utf16Offset=0, const QString &currentLine=QString())
std::function< void(SourceLocation)> updater
void changeAtOffset(quint32 offset, qint32 change, qint32 colChange, qint32 lineChange)
\inmodule QtCore \reentrant
\inmodule QtCore \reentrant
\inmodule QtCore
Definition qstringview.h:78
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:1121
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:129
QString & replace(qsizetype i, qsizetype len, QChar after)
Definition qstring.cpp:3824
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
const QChar at(qsizetype i) const
Returns the character at the given index position in the string.
Definition qstring.h:1226
QString & append(QChar c)
Definition qstring.cpp:3252
QString trimmed() const &
Definition qstring.h:447
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:166
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLuint64 key
GLuint index
[2]
GLenum GLuint GLintptr offset
GLenum GLenum GLsizei void GLsizei void * column
GLdouble s
[6]
Definition qopenglext.h:235
GLuint res
const GLubyte * c
GLdouble GLdouble t
Definition qopenglext.h:243
GLsizei GLenum GLboolean sink
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
unsigned int quint32
Definition qtypes.h:50
int qint32
Definition qtypes.h:49
static uint nextId
MyCustomStruct c2
QJSValueList args