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
qqmlformatting.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 <qqmlformatting_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/qqmldomoutwriter_p.h>
12#include <QtQmlFormat/private/qqmlformatsettings_p.h>
13
15
16Q_LOGGING_CATEGORY(formatLog, "qt.languageserver.formatting")
17
18QQmlDocumentFormatting::QQmlDocumentFormatting(QmlLsp::QQmlCodeModelManager *codeModelManager)
19 : QQmlBaseModule(codeModelManager)
20{
21}
22
23void QQmlDocumentFormatting::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
24{
25 protocol->registerDocumentFormattingRequestHandler(getRequestHandler());
26}
27
28void QQmlDocumentFormatting::setupCapabilities(QLspSpecification::ServerCapabilities &caps)
29{
30 // TODO: Allow customized formatting in future
31 caps.documentFormattingProvider = true;
32}
33
34void QQmlDocumentFormatting::process(RequestPointerArgument request)
35{
36 QList<QLspSpecification::TextEdit> result;
37 ResponseScopeGuard guard(result, request->m_response);
38
39 using namespace QQmlJS::Dom;
40 QmlLsp::OpenDocument doc = m_codeModelManager->openDocumentByUrl(
41 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
42
43 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
44 if (!file) {
45 guard.setError(QQmlLSUtils::ErrorMessage{
46 0, u"Could not find the file %1."_s.arg(doc.snapshot.doc.canonicalFilePath()) });
47 return;
48 }
49 if (!file.field(Fields::isValid).value().toBool(false)) {
50 guard.setError(QQmlLSUtils::ErrorMessage{ 0, u"Cannot format invalid documents!"_s });
51 return;
52 }
53 if (auto envPtr = file.environment().ownerAs<DomEnvironment>())
54 envPtr->clearReferenceCache();
55
56 auto qmlFile = file.ownerAs<QmlFile>();
57 if (!qmlFile || !qmlFile->isValid()) {
58 file.iterateErrors(
59 [](const DomItem &, const ErrorMessage &msg) {
60 errorToQDebug(msg);
61 return true;
62 },
63 true);
64 guard.setError(QQmlLSUtils::ErrorMessage{
65 0, u"Failed to parse %1."_s.arg(file.canonicalFilePath()) });
66 return;
67 }
68
69 // TODO: implement formatting options via LSP
70 // For now, qmlformat's default options via m_formatOptions and read .qmlformat.ini
71 QQmlFormatSettings settings(QLatin1String("qmlformat"));
72 settings.search(qmlFile->canonicalFilePath());
73
74 QQmlFormatOptions currentFormatOptions = m_formatOptions;
75 currentFormatOptions.applySettings(settings);
76
77 QLspSpecification::TextEdit formattedText;
78 LineWriter lw([&formattedText](QStringView s) { formattedText.newText += s.toUtf8(); },
79 QString(), currentFormatOptions.optionsForCode(qmlFile->code()));
80 OutWriter ow(qmlFile, lw);
81 file.writeOutForFile(ow, WriteOutCheck::None);
82 ow.flush();
83 const auto &code = qmlFile->code();
84 const auto [endLine, endColumn] = QQmlLSUtils::textRowAndColumnFrom(code, code.length());
85
86 Q_UNUSED(endColumn);
87 formattedText.range = QLspSpecification::Range{ QLspSpecification::Position{ 0, 0 },
88 QLspSpecification::Position{ endLine + 1, 0 } };
89
90 result.append(formattedText);
91}
92
93QT_END_NAMESPACE
void process(RequestPointerArgument req) override
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) override
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
Combined button and popup list for selecting options.