36 QList<QLspSpecification::TextEdit> result;
37 ResponseScopeGuard guard(result, request->m_response);
39 using namespace QQmlJS::Dom;
41 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
43 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
45 guard.setError(QQmlLSUtils::ErrorMessage{
46 0, u"Could not find the file %1."_s.arg(doc.snapshot.doc.canonicalFilePath()) });
49 if (!file.field(Fields::isValid).value().toBool(
false)) {
50 guard.setError(QQmlLSUtils::ErrorMessage{ 0, u"Cannot format invalid documents!"_s });
53 if (
auto envPtr = file.environment().ownerAs<DomEnvironment>())
54 envPtr->clearReferenceCache();
56 auto qmlFile = file.ownerAs<QmlFile>();
57 if (!qmlFile || !qmlFile->isValid()) {
59 [](
const DomItem &,
const ErrorMessage &msg) {
64 guard.setError(QQmlLSUtils::ErrorMessage{
65 0, u"Failed to parse %1."_s.arg(file.canonicalFilePath()) });
71 QQmlFormatSettings settings(QLatin1String(
"qmlformat"));
72 settings.search(qmlFile->canonicalFilePath());
74 QQmlFormatOptions currentFormatOptions = m_formatOptions;
75 currentFormatOptions.applySettings(settings);
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);
83 const auto &code = qmlFile->code();
84 const auto [endLine, endColumn] = QQmlLSUtils::textRowAndColumnFrom(code, code.length());
87 formattedText.range = QLspSpecification::Range{ QLspSpecification::Position{ 0, 0 },
88 QLspSpecification::Position{ endLine + 1, 0 } };
90 result.append(formattedText);