35 using namespace QQmlJS::Dom;
36 QList<QLspSpecification::TextEdit> result{};
39 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
41 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
43 qWarning() << u"Could not find the file"_s << doc.snapshot.doc.toString();
47 if (
auto envPtr = file.environment().ownerAs<DomEnvironment>())
48 envPtr->clearReferenceCache();
50 auto qmlFile = file.ownerAs<QmlFile>();
51 auto code = qmlFile->code();
54 const auto selectedRange = request->m_parameters.range;
55 const auto selectedRangeStartLine = selectedRange.start.line;
56 const auto selectedRangeEndLine = selectedRange.end.line;
58 LineWriterOptions options;
59 options.attributesSequence = LineWriterOptions::AttributesSequence::Preserve;
61 QTextStream in(&code);
62 FormatTextStatus status = FormatTextStatus::initialStatus();
63 FormatPartialStatus partialStatus({}, options.formatOptions, status);
66 unsigned lineNumber = 0;
68 const auto line = in.readLine();
69 partialStatus = formatCodeLine(line, options.formatOptions, partialStatus.currentStatus);
70 if (++lineNumber >= selectedRangeStartLine)
75 QTextStream out(&resultText);
76 IndentingLineWriter lw([&out](QStringView writtenText) { out << writtenText.toUtf8(); },
77 QString(), options, partialStatus.currentStatus);
78 OutWriter ow(qmlFile, lw);
79 ow.indentNextlines =
true;
83 const auto removeSpaces = [](
const QString &line) {
85 QTextStream out(&result);
86 bool previousIsSpace =
false;
89 for (
int i = 0; i < line.length(); ++i) {
92 if (c ==
'\n'_L1 && newLineCount < 2) {
95 }
else if (c ==
'\r'_L1 && (i + 1) < line.length() && line.at(i + 1) ==
'\n'_L1
96 && newLineCount < 2) {
101 if (!previousIsSpace)
104 previousIsSpace =
true;
107 previousIsSpace =
false;
116 const auto startOffset = QQmlLSUtils::textOffsetFrom(code, selectedRangeStartLine, 0);
117 auto endOffset = QQmlLSUtils::textOffsetFrom(code, selectedRangeEndLine + 1, 0);
121 if (endOffset < code.size() && code[endOffset - 1] == u'\r' && code[endOffset] == u'\n')
124 const auto &toFormat = code.mid(startOffset, endOffset - startOffset);
125 ow.write(removeSpaces(toFormat));
129 const auto documentLineCount = QQmlLSUtils::textRowAndColumnFrom(code, code.length()).line;
130 code.replace(startOffset, toFormat.length(), resultText);
132 QLspSpecification::TextEdit add;
133 add.newText = code.toUtf8();
134 add.range = { { 0, 0 }, { documentLineCount + 1u } };
137 request->m_response.sendResponse(result);