[utilities/kate] /: Add yaml formatting support
Waqar Ahmed
null at kde.org
Tue May 21 12:46:33 BST 2024
Git commit aa4c53ae64ea4cfe801386c0a01be3d38e3b09e5 by Waqar Ahmed.
Committed on 21/05/2024 at 11:45.
Pushed by waqar into branch 'master'.
Add yaml formatting support
M +11 -0 addons/format/FormatterFactory.h
M +4 -0 addons/format/FormatterSettings.json
M +2 -0 addons/format/Formatters.cpp
M +1 -0 addons/format/Formatters.h
M +4 -0 addons/format/FormattersEnum.h
M +2 -0 addons/format/autotests/test_formatapply.cpp
M +9 -0 doc/kate/plugins.docbook
https://invent.kde.org/utilities/kate/-/commit/aa4c53ae64ea4cfe801386c0a01be3d38e3b09e5
diff --git a/addons/format/FormatterFactory.h b/addons/format/FormatterFactory.h
index 36241b451d..303a11b136 100644
--- a/addons/format/FormatterFactory.h
+++ b/addons/format/FormatterFactory.h
@@ -23,6 +23,7 @@ static AbstractFormatter *formatterForDoc(KTextEditor::Document *doc, const QJso
return mode == QLatin1String(s) || mode.contains(QLatin1String(s));
};
+ // NOTE: When adding a new formatter ensure that it is documented in plugins.docbook
if (is_or_contains("c++") || is("c") || is("objective-c") || is("objective-c++") || is("protobuf")) {
return new ClangFormat(config, doc);
} else if (is("dart")) {
@@ -73,6 +74,16 @@ static AbstractFormatter *formatterForDoc(KTextEditor::Document *doc, const QJso
return nixfmt(config, doc);
} else if (is("qml")) {
return new QMLFormat(config, doc);
+ } else if (is("yaml")) {
+ const auto configValue = config.value(QStringLiteral("formatterForYaml")).toString();
+ Formatters f = formatterForName(configValue, Formatters::YamlFmt);
+ if (f == Formatters::YamlFmt) {
+ return yamlfmt(config, doc);
+ } else if (f == Formatters::Prettier) {
+ return new PrettierFormat(config, doc);
+ }
+ Utils::showMessage(i18n("Unknown formatterForYaml: %1, falling back to yamlfmt", configValue), {}, i18n("Format"), MessageType::Error);
+ return yamlfmt(config, doc);
}
static QList<QString> alreadyWarned;
diff --git a/addons/format/FormatterSettings.json b/addons/format/FormatterSettings.json
index 4849ee335c..2f80922ccc 100644
--- a/addons/format/FormatterSettings.json
+++ b/addons/format/FormatterSettings.json
@@ -2,6 +2,7 @@
"formatOnSave": true,
"formatterForJson": "jq",
"formatterForPython": "ruff",
+ "formatterForYaml": "yamlfmt",
"clang-format": {
"command": ["clang-format", "--fallback-style=Webkit"],
"formatModifiedLinesOnly": true,
@@ -51,5 +52,8 @@
},
"qmlformat": {
"command": ["qmlformat"]
+ },
+ "yamlfmt": {
+ "command": ["yamlfmt", "-quiet", "-"]
}
}
diff --git a/addons/format/Formatters.cpp b/addons/format/Formatters.cpp
index d1f4ec3852..c146752fbc 100644
--- a/addons/format/Formatters.cpp
+++ b/addons/format/Formatters.cpp
@@ -98,6 +98,8 @@ static QString filenameFromMode(KTextEditor::Document *doc)
return prefix.append(QLatin1String(".css"));
} else if (is("html")) {
return prefix.append(QLatin1String(".html"));
+ } else if (is("yaml")) {
+ return prefix.append(QLatin1String(".yml"));
}
return {};
}
diff --git a/addons/format/Formatters.h b/addons/format/Formatters.h
index 917b357e49..6a77b9de0d 100644
--- a/addons/format/Formatters.h
+++ b/addons/format/Formatters.h
@@ -315,6 +315,7 @@ DEFINE_STDIN_FORMATTER(cMakeFormat, "cmake-format", (QStringList{S("-")}));
DEFINE_STDIN_FORMATTER(dfmt, "dfmt", (QStringList{}));
DEFINE_STDIN_FORMATTER(fishIndent, "fish_indent", (QStringList{}));
DEFINE_STDIN_FORMATTER(nixfmt, "nixfmt", (QStringList{}));
+DEFINE_STDIN_FORMATTER(yamlfmt, "yamlfmt", (QStringList{}));
inline AbstractFormatter *shfmt(const QJsonObject &obj, KTextEditor::Document *parent)
{
diff --git a/addons/format/FormattersEnum.h b/addons/format/FormattersEnum.h
index ccb3a4cc32..6fb864d5b2 100644
--- a/addons/format/FormattersEnum.h
+++ b/addons/format/FormattersEnum.h
@@ -16,6 +16,7 @@ enum class Formatters {
XmlLint,
Autopep8,
Ruff,
+ YamlFmt,
// keep at end
COUNT,
@@ -45,5 +46,8 @@ inline Formatters formatterForName(const QString &name, Formatters defaultValue)
if (eq("ruff")) {
return Formatters::Ruff;
}
+ if (eq("yamlfmt")) {
+ return Formatters::YamlFmt;
+ }
return defaultValue;
}
diff --git a/addons/format/autotests/test_formatapply.cpp b/addons/format/autotests/test_formatapply.cpp
index dc0dea87e8..98cb4024d8 100644
--- a/addons/format/autotests/test_formatapply.cpp
+++ b/addons/format/autotests/test_formatapply.cpp
@@ -67,6 +67,8 @@ void FormatApplyTest::testFormatterForName()
return "autopep8";
case Formatters::Ruff:
return "ruff";
+ case Formatters::YamlFmt:
+ return "yamlfmt";
case Formatters::COUNT:
Q_ASSERT(false);
return "";
diff --git a/doc/kate/plugins.docbook b/doc/kate/plugins.docbook
index 2fcf5f71c1..18df02c36c 100644
--- a/doc/kate/plugins.docbook
+++ b/doc/kate/plugins.docbook
@@ -4465,6 +4465,8 @@ The current list of supported languages and formatters are as follows:
<listitem><para>Fish Shell - <command>fish_indent</command></para></listitem>
<listitem><para>Bash - <command>shfmt</command></para></listitem>
<listitem><para>Nix - <command>nixfmt</command></para></listitem>
+<listitem><para>QML - <command>qmlformat</command></para></listitem>
+<listitem><para>HTML - <command>prettier</command></para></listitem>
<listitem><para>Python
<itemizedlist>
@@ -4473,6 +4475,13 @@ The current list of supported languages and formatters are as follows:
</itemizedlist>
</para></listitem>
+<listitem><para>YAML
+<itemizedlist>
+ <listitem><para><command>yamlfmt</command></para></listitem>
+ <listitem><para><command>prettier</command></para></listitem>
+</itemizedlist>
+</para></listitem>
+
</itemizedlist>
</para>
More information about the kde-doc-english
mailing list