File indexing completed on 2024-06-09 05:44:30

0001 /*
0002     SPDX-FileCopyrightText: 2022 Waqar Ahmed <waqar.17a@gmail.com>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 #pragma once
0006 
0007 #include "Formatters.h"
0008 #include "FormattersEnum.h"
0009 
0010 #include <QJsonObject>
0011 
0012 static AbstractFormatter *formatterForDoc(KTextEditor::Document *doc, const QJsonObject &config)
0013 {
0014     if (!doc) {
0015         qWarning() << "Unexpected null doc";
0016         return nullptr;
0017     }
0018     const auto mode = doc->highlightingMode().toLower();
0019     auto is = [mode](const char *s) {
0020         return mode == QLatin1String(s);
0021     };
0022     auto is_or_contains = [mode](const char *s) {
0023         return mode == QLatin1String(s) || mode.contains(QLatin1String(s));
0024     };
0025 
0026     if (is_or_contains("c++") || is("c") || is("objective-c") || is("objective-c++") || is("protobuf")) {
0027         return new ClangFormat(config, doc);
0028     } else if (is("dart")) {
0029         return new DartFormat(config, doc);
0030     } else if (is("html")) {
0031         return new PrettierFormat(config, doc);
0032     } else if (is("javascript") || is("typescript") || is("typescript react (tsx)") || is("javascript react (jsx)") || is("css")) {
0033         return new PrettierFormat(config, doc);
0034     } else if (is("json")) {
0035         Formatters f = formatterForName(config.value(QStringLiteral("formatterForJson")).toString(), Formatters::Prettier);
0036         if (f == Formatters::Prettier) {
0037             return new PrettierFormat(config, doc);
0038         } else if (f == Formatters::ClangFormat) {
0039             return new ClangFormat(config, doc);
0040         } else if (f == Formatters::Jq) {
0041             return new JsonJqFormat(config, doc);
0042         }
0043         qWarning() << "Unexpected formatterForJson value: " << (int)f;
0044         return new JsonJqFormat(config, doc);
0045     } else if (is("rust")) {
0046         return new RustFormat(config, doc);
0047     } else if (is("xml")) {
0048         return new XmlLintFormat(config, doc);
0049     } else if (is("go")) {
0050         return new GoFormat(config, doc);
0051     } else if (is("zig")) {
0052         return new ZigFormat(config, doc);
0053     } else if (is("cmake")) {
0054         return new CMakeFormat(config, doc);
0055     } else if (is("python")) {
0056         return new AutoPep8Format(config, doc);
0057     }
0058 
0059     Utils::showMessage(i18n("Failed to run formatter. Unsupported language %1", mode), {}, i18n("Format"), MessageType::Info);
0060 
0061     return nullptr;
0062 }