Commit 411b415a authored by Igor Kushnir's avatar Igor Kushnir
Browse files

Reformat actions' UI: display a style's caption, not name

SourceFormatterStyle::name() is used as a KConfig key and never appears
in the UI. The names of user-defined styles are particularly
uninformative, e.g. "User4". SourceFormatterStyle::caption() is
consistently shown in the UI everywhere else.

Unfortunately, SourceFormatterController::styleForUrl() returns a
SourceFormatterStyle with only the name filled-in for predefined styles.
Retrieving the real caption of a predefined style requires moving
CustomScriptPlugin::predefinedStyle() to the base formatter class -
ISourceFormatter.

Rename AStylePlugin's helper function predefinedStyle() =>
createPredefinedStyle(). The old name coincided with the name of
base class ISourceFormatter's newly added member function. Make the
renamed function static: not used outside of the file it is defined in.
parent d8ab45cc
......@@ -11,6 +11,8 @@
#include <QVariant>
#include <QMimeType>
#include <algorithm>
namespace KDevelop
{
......@@ -157,6 +159,15 @@ void SourceFormatterStyle::copyDataFrom(SourceFormatterStyle *other)
m_overrideSample = other->overrideSample();
}
SourceFormatterStyle ISourceFormatter::predefinedStyle(const QString& name) const
{
const auto styles = predefinedStyles();
const auto it = std::find_if(styles.cbegin(), styles.cend(), [&name](const SourceFormatterStyle& style) {
return style.name() == name;
});
return it == styles.cend() ? SourceFormatterStyle{name} : *it;
}
QString ISourceFormatter::optionMapToString(const QMap<QString, QVariant> &map)
{
QString options;
......
......@@ -173,6 +173,11 @@ class KDEVPLATFORMINTERFACES_EXPORT ISourceFormatter
*/
virtual QVector<SourceFormatterStyle> predefinedStyles() const = 0;
/** \return A predefined style with the specified name or SourceFormatterStyle{name}
* if this formatter has no such predefined style.
*/
SourceFormatterStyle predefinedStyle(const QString& name) const;
/** \return The widget to edit a style.
*/
virtual SettingsWidget* editStyleWidget(const QMimeType &mime) const = 0;
......
......@@ -50,6 +50,7 @@
#include <algorithm>
#include <tuple>
#include <utility>
namespace {
......@@ -515,8 +516,13 @@ void SourceFormatterController::updateFormatTextAction()
}
const auto style = styleForUrl(url, mime);
auto styleCaption = style.caption();
if (styleCaption.isEmpty()) {
// This could be an incomplete predefined style, for which only the name is stored in config.
styleCaption = formatter->predefinedStyle(style.name()).caption();
}
return std::tuple { true, formatter->caption(), style.name() };
return std::tuple { true, formatter->caption(), std::move(styleCaption) };
}();
d->formatTextAction->setEnabled(enabled);
......
......@@ -258,7 +258,7 @@ QString AStylePlugin::formatSource(const QString& text, const QUrl &url, const Q
return formatSourceWithStyle(style, text, url, mime, leftContext, rightContext);
}
SourceFormatterStyle predefinedStyle(const QString& name, const QString& caption = QString())
static SourceFormatterStyle createPredefinedStyle(const QString& name, const QString& caption = QString())
{
SourceFormatterStyle st = SourceFormatterStyle( name );
st.setCaption( caption.isEmpty() ? name : caption );
......@@ -273,18 +273,18 @@ SourceFormatterStyle predefinedStyle(const QString& name, const QString& caption
QVector<SourceFormatterStyle> AStylePlugin::predefinedStyles() const
{
static const QVector<SourceFormatterStyle> list = {
predefinedStyle(QStringLiteral("ANSI")),
predefinedStyle(QStringLiteral("GNU")),
predefinedStyle(QStringLiteral("Java")),
predefinedStyle(QStringLiteral("K&R"), QStringLiteral("Kernighan & Ritchie")),
predefinedStyle(QStringLiteral("Linux")),
predefinedStyle(QStringLiteral("Stroustrup")),
predefinedStyle(QStringLiteral("Horstmann")),
predefinedStyle(QStringLiteral("Whitesmith")),
predefinedStyle(QStringLiteral("Banner")),
predefinedStyle(QStringLiteral("1TBS")),
predefinedStyle(QStringLiteral("KDELibs"), QStringLiteral("KDE Frameworks")),
predefinedStyle(QStringLiteral("Qt")),
createPredefinedStyle(QStringLiteral("ANSI")),
createPredefinedStyle(QStringLiteral("GNU")),
createPredefinedStyle(QStringLiteral("Java")),
createPredefinedStyle(QStringLiteral("K&R"), QStringLiteral("Kernighan & Ritchie")),
createPredefinedStyle(QStringLiteral("Linux")),
createPredefinedStyle(QStringLiteral("Stroustrup")),
createPredefinedStyle(QStringLiteral("Horstmann")),
createPredefinedStyle(QStringLiteral("Whitesmith")),
createPredefinedStyle(QStringLiteral("Banner")),
createPredefinedStyle(QStringLiteral("1TBS")),
createPredefinedStyle(QStringLiteral("KDELibs"), QStringLiteral("KDE Frameworks")),
createPredefinedStyle(QStringLiteral("Qt")),
};
return list;
}
......
......@@ -30,7 +30,6 @@
#include <util/path.h>
#include <debug.h>
#include <algorithm>
#include <memory>
using namespace KDevelop;
......@@ -315,15 +314,6 @@ QVector<SourceFormatterStyle> CustomScriptPlugin::predefinedStyles() const
return styles;
}
SourceFormatterStyle CustomScriptPlugin::predefinedStyle(const QString& name) const
{
const auto styles = predefinedStyles();
const auto it = std::find_if(styles.cbegin(), styles.cend(), [&name](const SourceFormatterStyle& style) {
return style.name() == name;
});
return it == styles.cend() ? SourceFormatterStyle{name} : *it;
}
KDevelop::SettingsWidget* CustomScriptPlugin::editStyleWidget(const QMimeType& mime) const
{
Q_UNUSED(mime);
......
......@@ -60,7 +60,6 @@ public:
private:
QStringList computeIndentationFromSample(const QUrl& url) const;
KDevelop::SourceFormatterStyle predefinedStyle(const QString& name) const;
};
class CustomScriptPreferences
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment