Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlrangeformatting.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 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// Qt-Security score:significant reason:default
4
5#include <qqmlrangeformatting_p.h>
6#include <qqmlcodemodel_p.h>
7#include <qqmllsutils_p.h>
8
9#include <QtQmlDom/private/qqmldomitem_p.h>
10#include <QtQmlDom/private/qqmldomindentinglinewriter_p.h>
11#include <QtQmlDom/private/qqmldomcodeformatter_p.h>
12#include <QtQmlDom/private/qqmldomoutwriter_p.h>
13#include <QtQmlDom/private/qqmldommock_p.h>
14#include <QtQmlDom/private/qqmldomcompare_p.h>
15
17
18QQmlRangeFormatting::QQmlRangeFormatting(QmlLsp::QQmlCodeModelManager *codeModelManager)
19 : QQmlBaseModule(codeModelManager)
20{
21}
22
23void QQmlRangeFormatting::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
24{
25 protocol->registerDocumentRangeFormattingRequestHandler(getRequestHandler());
26}
27
28void QQmlRangeFormatting::setupCapabilities(QLspSpecification::ServerCapabilities &caps)
29{
30 caps.documentRangeFormattingProvider = true;
31}
32
33void QQmlRangeFormatting::process(RequestPointerArgument request)
34{
35 using namespace QQmlJS::Dom;
36 QList<QLspSpecification::TextEdit> result{};
37
38 QmlLsp::OpenDocument doc = m_codeModelManager->openDocumentByUrl(
39 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
40
41 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
42 if (!file) {
43 qWarning() << u"Could not find the file"_s << doc.snapshot.doc.toString();
44 return;
45 }
46
47 if (auto envPtr = file.environment().ownerAs<DomEnvironment>())
48 envPtr->clearReferenceCache();
49
50 auto qmlFile = file.ownerAs<QmlFile>();
51 auto code = qmlFile->code();
52
53 // Range requested to be formatted
54 const auto selectedRange = request->m_parameters.range;
55 const auto selectedRangeStartLine = selectedRange.start.line;
56 const auto selectedRangeEndLine = selectedRange.end.line;
57
58 LineWriterOptions options;
59 options.attributesSequence = LineWriterOptions::AttributesSequence::Preserve;
60
61 QTextStream in(&code);
62 FormatTextStatus status = FormatTextStatus::initialStatus();
63 FormatPartialStatus partialStatus({}, options.formatOptions, status);
64
65 // Get the token status of the previous line without performing write operation
66 unsigned lineNumber = 0;
67 while (!in.atEnd()) {
68 const auto line = in.readLine();
69 partialStatus = formatCodeLine(line, options.formatOptions, partialStatus.currentStatus);
70 if (++lineNumber >= selectedRangeStartLine)
71 break;
72 }
73
74 QString resultText;
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;
80
81 // TODO: This is a workaround and will/should be handled by the actual formatter
82 // once we improve the range-formatter design in QTBUG-116139
83 const auto removeSpaces = [](const QString &line) {
84 QString result;
85 QTextStream out(&result);
86 bool previousIsSpace = false;
87
88 int newLineCount = 0;
89 for (int i = 0; i < line.length(); ++i) {
90 QChar c = line.at(i);
91 if (c.isSpace()) {
92 if (c == '\n'_L1 && newLineCount < 2) {
93 out << '\n'_L1;
94 ++newLineCount;
95 } else if (c == '\r'_L1 && (i + 1) < line.length() && line.at(i + 1) == '\n'_L1
96 && newLineCount < 2) {
97 out << "\r\n";
98 ++newLineCount;
99 ++i;
100 } else {
101 if (!previousIsSpace)
102 out << ' '_L1;
103 }
104 previousIsSpace = true;
105 } else {
106 out << c;
107 previousIsSpace = false;
108 newLineCount = 0;
109 }
110 }
111
112 out.flush();
113 return result;
114 };
115
116 const auto startOffset = QQmlLSUtils::textOffsetFrom(code, selectedRangeStartLine, 0);
117 auto endOffset = QQmlLSUtils::textOffsetFrom(code, selectedRangeEndLine + 1, 0);
118
119 // note: the character at endOffset (usually a \n) is ignored. Therefore avoid
120 // formatting \r\n that will ignore \n and format \r as a space (' ').
121 if (endOffset < code.size() && code[endOffset - 1] == u'\r' && code[endOffset] == u'\n')
122 --endOffset;
123
124 const auto &toFormat = code.mid(startOffset, endOffset - startOffset);
125 ow.write(removeSpaces(toFormat));
126 ow.flush();
127 ow.eof();
128
129 const auto documentLineCount = QQmlLSUtils::textRowAndColumnFrom(code, code.length()).line;
130 code.replace(startOffset, toFormat.length(), resultText);
131
132 QLspSpecification::TextEdit add;
133 add.newText = code.toUtf8();
134 add.range = { { 0, 0 }, { documentLineCount + 1u } };
135 result.append(add);
136
137 request->m_response.sendResponse(result);
138}
139
140QT_END_NAMESPACE
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) override
void process(RequestPointerArgument req) override
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
Combined button and popup list for selecting options.