43 QList<QLspSpecification::TextEdit> result;
46 using namespace QQmlJS::Dom;
48 QQmlLSUtils::lspUriToQmlUrl(request->m_parameters.textDocument.uri));
50 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
52 guard.setError(QQmlLSUtils::ErrorMessage{
53 0, u"Could not find the file %1."_s.arg(doc.snapshot.doc.canonicalFilePath()) });
56 if (!file.field(Fields::isValid).value().toBool(
false)) {
57 guard.setError(QQmlLSUtils::ErrorMessage{ 0, u"Cannot format invalid documents!"_s });
60 if (
auto envPtr = file.environment().ownerAs<DomEnvironment>())
61 envPtr->clearReferenceCache();
63 auto qmlFile = file.ownerAs<QmlFile>();
64 if (!qmlFile || !qmlFile->isValid()) {
66 [](
const DomItem &,
const ErrorMessage &msg) {
71 guard.setError(QQmlLSUtils::ErrorMessage{
72 0, u"Failed to parse %1."_s.arg(file.canonicalFilePath()) });
78 QQmlFormatSettings settings(QLatin1String(
"qmlformat"));
79 settings.search(qmlFile->canonicalFilePath());
81 QQmlFormatOptions currentFormatOptions = m_formatOptions;
82 currentFormatOptions.applySettings(settings);
84 QLspSpecification::TextEdit formattedText;
85 LineWriter lw([&formattedText](QStringView s) { formattedText.newText += s.toUtf8(); },
86 QString(), currentFormatOptions.optionsForCode(qmlFile->code()));
87 OutWriter ow(qmlFile, lw);
88 file.writeOutForFile(ow, WriteOutCheck::None);
90 const auto &code = qmlFile->code();
91 const auto [endLine, endColumn] = QQmlLSUtils::textRowAndColumnFrom(code, code.length());
94 formattedText.range = QLspSpecification::Range{ QLspSpecification::Position{ 0, 0 },
95 QLspSpecification::Position{ endLine + 1, 0 } };
97 result.append(formattedText);