4#ifndef QMLDOMOUTWRITER_P_H
5#define QMLDOMOUTWRITER_P_H
24#include <QtCore/QLoggingCategory>
35 bool indentNextlines =
false;
37 bool skipComments =
false;
38 LineWriter &lineWriter;
40 using RegionToCommentMap = QMap<FileLocationRegion, CommentedElement>;
41 QStack<RegionToCommentMap> pendingComments;
43 bool isFormatterEnabled =
true;
45 using OffsetToDisabledRegionMap = QMap<quint32, SourceLocation>;
46 OffsetToDisabledRegionMap formatDisabledRegions;
48 explicit OutWriter(LineWriter &lw) : lineWriter(lw)
50 lineWriter.addInnerSink([
this](QStringView s) { writtenStr.append(s); });
51 lineWriter.addTextAddCallback([
this](LineWriter &, LineWriter::TextAddType tt) {
52 if (indentNextlines && tt == LineWriter::TextAddType::Normal
53 && QStringView(lineWriter.currentLine()).trimmed().isEmpty())
54 lineWriter.setLineIndent(indent);
59 OutWriter(std::shared_ptr<ExternalOwningItem> ownerFile, LineWriter &lw)
65 code = ownerFile->code();
67 const auto engine = [&ownerFile]() -> std::shared_ptr<QQmlJS::Engine> {
68 if (
auto qmlfilePtr = std::dynamic_pointer_cast<QmlFile>(ownerFile))
69 return qmlfilePtr->engine();
70 else if (
auto jsfilePtr = std::dynamic_pointer_cast<JsFile>(ownerFile))
71 return jsfilePtr->engine();
76 scanFormatDirectives(code, engine->comments());
79 int increaseIndent(
int level = 1)
81 int oldIndent = indent;
82 indent += lineWriter.options().formatOptions.indentSize * level;
85 int decreaseIndent(
int level = 1,
int expectedIndent = -1)
87 indent -= lineWriter.options().formatOptions.indentSize * level;
88 Q_ASSERT(expectedIndent < 0 || expectedIndent == indent);
92 void itemStart(
const DomItem &it);
94 void writePreComment(FileLocationRegion region);
95 void writePostComment(FileLocationRegion regino);
97 quint32 counter()
const {
return lineWriter.counter(); }
98 OutWriter &writeRegion(
const FileLocations::Tree &fLoc, FileLocationRegion region, QStringView toWrite);
99 OutWriter &writeRegion(
const FileLocations::Tree &fLoc, FileLocationRegion region);
100 OutWriter &ensureNewline(
int nNewlines = 1)
102 if (formatterEnabled())
103 lineWriter.ensureNewline(nNewlines);
107 OutWriter &ensureSpace()
109 if (formatterEnabled())
110 lineWriter.ensureSpace();
114 OutWriter &ensureSpace(QStringView space)
116 if (formatterEnabled())
117 lineWriter.ensureSpace(space);
123 if (formatterEnabled())
124 lineWriter.newline();
128 OutWriter &write(QStringView v, LineWriter::TextAddType t = LineWriter::TextAddType::Normal)
130 if (formatterEnabled())
131 lineWriter.write(v, t);
136 if (formatterEnabled())
140 void eof(
bool ensureNewline =
true)
142 if (formatterEnabled())
143 lineWriter.eof(ensureNewline);
146 int addNewlinesAutospacerCallback(
int nLines)
148 return lineWriter.addNewlinesAutospacerCallback(nLines);
150 int addTextAddCallback(std::function<
bool(LineWriter &, LineWriter::TextAddType)> callback)
152 return lineWriter.addTextAddCallback(callback);
154 bool removeTextAddCallback(
int i) {
return lineWriter.removeTextAddCallback(i); }
156 void maybeWriteComment(
const Comment &comment);
157 bool formatterEnabled()
const;
158 bool shouldFormat(
const FileLocations::Tree &fLoc, FileLocationRegion region);
159 void maybeWriteDisabledRegion(
const SourceLocation &loc);
160 void scanFormatDirectives(QStringView code,
const QList<SourceLocation> &comments);
161 void writeDisabledRegion(
const SourceLocation &loc);
162 QStringView attachedDisableCode(quint32 offset)
const;
void addNodeRanges(AST::Node *rootNode)
bool preVisit(Node *n) override
bool visit(Block *block) override
AstRangesVisitor()=default
void addSourceLocations(Node *n, const SourceLocation &sourceLocation)
void addItemRanges(const DomItem &item, const FileLocations::Tree &itemLocations, const Path ¤tP)
void addSourceLocations(Node *n, qsizetype start, qsizetype end, CommentAnchor commentAnchor)
QMap< qsizetype, ElementRef > ends
static const QSet< int > kindsToSkip()
static bool shouldSkipRegion(const DomItem &item, FileLocationRegion region)
returns true if comments should skip attaching to this region
void addSourceLocations(Node *n)
QMap< qsizetype, ElementRef > starts
void throwRecursionDepthError() override
A value type that references any element of the Dom.
DomItem path(const Path &p, const ErrorHandler &h=&defaultErrorHandler) const
DomItem subMapItem(const Map &map) const
InternalKind internalKind() const
Path pathFromOwner() const
ElementRef(const Path &path, FileLocationRegion region, qsizetype size)
ElementRef(AST::Node *node, qsizetype size, CommentAnchor commentAnchor)
std::variant< NodeRef, RegionRef > element
CommentAnchor commentAnchor
Path withPath(const Path &toAdd, bool avoidToAddAsBase=false) const
Returns a copy of this with toAdd appended to it.
FileLocationRegion regionName
A vistor that visits all the AST:Node.
Provides entities to maintain mappings between elements and their location in a file.
std::shared_ptr< Node > Tree
static void writeComments(OutWriter &lw, const QList< Comment > &comments)
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")