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 Q_ASSERT(selectedRangeStartLine >= 0);
58 Q_ASSERT(selectedRangeEndLine >= 0);
59
60 LineWriterOptions options;
61 options.attributesSequence = LineWriterOptions::AttributesSequence::Preserve;
62
63 QTextStream in(&code);
64 FormatTextStatus status = FormatTextStatus::initialStatus();
65 FormatPartialStatus partialStatus({}, options.formatOptions, status);
66
67 // Get the token status of the previous line without performing write operation
68 int lineNumber = 0;
69 while (!in.atEnd()) {
70 const auto line = in.readLine();
71 partialStatus = formatCodeLine(line, options.formatOptions, partialStatus.currentStatus);
72 if (++lineNumber >= selectedRangeStartLine)
73 break;
74 }
75
76 QString resultText;
77 QTextStream out(&resultText);
78 IndentingLineWriter lw([&out](QStringView writtenText) { out << writtenText.toUtf8(); },
79 QString(), options, partialStatus.currentStatus);
80 OutWriter ow(qmlFile, lw);
81 ow.indentNextlines = true;
82
83 // TODO: This is a workaround and will/should be handled by the actual formatter
84 // once we improve the range-formatter design in QTBUG-116139
85 const auto removeSpaces = [](const QString &line) {
86 QString result;
87 QTextStream out(&result);
88 bool previousIsSpace = false;
89
90 int newLineCount = 0;
91 for (int i = 0; i < line.length(); ++i) {
92 QChar c = line.at(i);
93 if (c.isSpace()) {
94 if (c == '\n'_L1 && newLineCount < 2) {
95 out << '\n'_L1;
96 ++newLineCount;
97 } else if (c == '\r'_L1 && (i + 1) < line.length() && line.at(i + 1) == '\n'_L1
98 && newLineCount < 2) {
99 out << "\r\n";
100 ++newLineCount;
101 ++i;
102 } else {
103 if (!previousIsSpace)
104 out << ' '_L1;
105 }
106 previousIsSpace = true;
107 } else {
108 out << c;
109 previousIsSpace = false;
110 newLineCount = 0;
111 }
112 }
113
114 out.flush();
115 return result;
116 };
117
118 const auto startOffset = QQmlLSUtils::textOffsetFrom(code, selectedRangeStartLine, 0);
119 auto endOffset = QQmlLSUtils::textOffsetFrom(code, selectedRangeEndLine + 1, 0);
120
121 // note: the character at endOffset (usually a \n) is ignored. Therefore avoid
122 // formatting \r\n that will ignore \n and format \r as a space (' ').
123 if (endOffset < code.size() && code[endOffset - 1] == u'\r' && code[endOffset] == u'\n')
124 --endOffset;
125
126 const auto &toFormat = code.mid(startOffset, endOffset - startOffset);
127 ow.write(removeSpaces(toFormat));
128 ow.flush();
129 ow.eof();
130
131 const auto documentLineCount = QQmlLSUtils::textRowAndColumnFrom(code, code.length()).line;
132 code.replace(startOffset, toFormat.length(), resultText);
133
134 QLspSpecification::TextEdit add;
135 add.newText = code.toUtf8();
136 add.range = { { 0, 0 }, { documentLineCount + 1 } };
137 result.append(add);
138
139 request->m_response.sendResponse(result);
140}
141
142QT_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.